Zoltan2 Version of the Day
Zoltan2_XpetraCrsMatrixInput.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //   Zoltan2: A package of combinatorial algorithms for scientific computing
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Karen Devine      (kddevin@sandia.gov)
00039 //                    Erik Boman        (egboman@sandia.gov)
00040 //                    Siva Rajamanickam (srajama@sandia.gov)
00041 //
00042 // ***********************************************************************
00043 //
00044 // @HEADER
00045 
00050 #ifndef _ZOLTAN2_XPETRACRSMATRIXADAPTER_HPP_
00051 #define _ZOLTAN2_XPETRACRSMATRIXADAPTER_HPP_
00052 
00053 #include <Zoltan2_MatrixInput.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 #include <Zoltan2_XpetraTraits.hpp>
00056 
00057 #include <Xpetra_CrsMatrix.hpp>
00058 
00059 namespace Zoltan2 {
00060 
00062 
00084 template <typename User>
00085   class XpetraCrsMatrixAdapter : public MatrixAdapter<User> {
00086 public:
00087 
00088 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00089   typedef typename InputTraits<User>::scalar_t    scalar_t;
00090   typedef typename InputTraits<User>::lno_t    lno_t;
00091   typedef typename InputTraits<User>::gno_t    gno_t;
00092   typedef typename InputTraits<User>::gid_t    gid_t;
00093   typedef typename InputTraits<User>::node_t   node_t;
00094   typedef Xpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
00095   typedef MatrixAdapter<User> base_adapter_t;
00096   typedef User user_t;
00097 #endif
00098 
00101   ~XpetraCrsMatrixAdapter() { }
00102 
00112   XpetraCrsMatrixAdapter(const RCP<const User> &inmatrix,
00113                          int numWeightsPerRow=0, int coordDim=0);
00114 
00129   void setRowCoordinates(const scalar_t *coordVal, int stride, int dim);
00130 
00146   void setRowWeights(const scalar_t *weightVal, int stride, int idx = 0);
00147 
00153   void setRowWeightIsNumberOfNonZeros(int idx);
00154 
00156   // The MatrixAdapter interface.
00158 
00159   size_t getLocalNumRows() const { 
00160     return matrix_->getNodeNumRows();
00161   }
00162 
00163   size_t getLocalNumColumns() const { 
00164     return matrix_->getNodeNumCols();
00165   }
00166 
00167   size_t getLocalNumEntries() const {
00168     return matrix_->getNodeNumEntries();
00169   }
00170 
00171   bool CRSViewAvailable() const { return true; }
00172 
00173   void getRowIDsView(const gid_t *&rowIds) const 
00174   {
00175     ArrayView<const gid_t> rowView = rowMap_->getNodeElementList();
00176     rowIds = rowView.getRawPtr();
00177   }
00178 
00179   void getCRSView(const lno_t *&offsets, const gid_t *&colIds) const
00180   {
00181     offsets = offset_.getRawPtr();
00182     colIds = columnIds_.getRawPtr();
00183   }
00184 
00185   void getCRSView(const lno_t *&offsets, const gid_t *&colIds,
00186                     const scalar_t *&values) const
00187   {
00188     offsets = offset_.getRawPtr();
00189     colIds = columnIds_.getRawPtr();
00190     values = values_.getRawPtr();
00191   }
00192 
00193 
00194   int getNumWeightsPerRow() const
00195   {
00196     return weightDim_;
00197   }
00198 
00199   void getRowWeightsView(const scalar_t *&weights, int &stride,
00200                            int idx = 0) const
00201   {
00202     env_->localInputAssertion(__FILE__, __LINE__,
00203       "invalid weight index",
00204       idx >= 0 && idx < weightDim_, BASIC_ASSERTION);
00205     size_t length;
00206     rowWeights_[idx].getStridedList(length, weights, stride);
00207   }
00208 
00209   bool useNumNonzerosAsRowWeight(int idx) const { return numNzWeight_[idx];}
00210 
00211   int getDimension() const {return coordinateDim_;}
00212 
00213   void getRowCoordinatesView(const scalar_t *&coords, int &stride,
00214                              int dim) const
00215   {
00216     env_->localInputAssertion(__FILE__, __LINE__,
00217       "invalid coordinate dimension",
00218       dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00219     size_t length;
00220     rowCoords_[dim].getStridedList(length, coords, stride);
00221   }
00222 
00223   template <typename Adapter>
00224     size_t applyPartitioningSolution(const User &in, User *&out,
00225          const PartitioningSolution<Adapter> &solution) const;
00226 
00227 private:
00228 
00229   RCP<Environment> env_;    // for error messages, etc.
00230 
00231   RCP<const User> inmatrix_;
00232   RCP<const xmatrix_t> matrix_;
00233   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > rowMap_;
00234   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > colMap_;
00235   lno_t base_;
00236   ArrayRCP<lno_t> offset_;
00237   ArrayRCP<gno_t> columnIds_;  // TODO:  KDD Is it necessary to copy and store
00238   ArrayRCP<scalar_t> values_;  // TODO:  the matrix here?  Would prefer views.
00239 
00240   int coordinateDim_;
00241   ArrayRCP<StridedData<lno_t, scalar_t> > rowCoords_;
00242 
00243   int weightDim_;
00244   ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
00245   ArrayRCP<bool> numNzWeight_;
00246 
00247   bool mayHaveDiagonalEntries;
00248 };
00249 
00251 // Definitions
00253 
00254 template <typename User>
00255   XpetraCrsMatrixAdapter<User>::XpetraCrsMatrixAdapter(
00256     const RCP<const User> &inmatrix, int weightDim, int coordDim):
00257       env_(rcp(new Environment)),
00258       inmatrix_(inmatrix), matrix_(), rowMap_(), colMap_(), base_(),
00259       offset_(), columnIds_(),
00260       coordinateDim_(coordDim), rowCoords_(),
00261       weightDim_(weightDim), rowWeights_(), numNzWeight_(),
00262       mayHaveDiagonalEntries(true)
00263 {
00264   typedef StridedData<lno_t,scalar_t> input_t;
00265   matrix_ = XpetraTraits<User>::convertToXpetra(inmatrix);
00266   rowMap_ = matrix_->getRowMap();
00267   colMap_ = matrix_->getColMap();
00268   base_ = rowMap_->getIndexBase();
00269 
00270   size_t nrows = matrix_->getNodeNumRows();
00271   size_t nnz = matrix_->getNodeNumEntries();
00272  
00273   offset_.resize(nrows+1, 0);
00274   columnIds_.resize(nnz);
00275   values_.resize(nnz);
00276   ArrayView<const lno_t> indices;
00277   ArrayView<const scalar_t> nzs;
00278   lno_t next = 0;
00279 //TODO WE ARE COPYING THE MATRIX HERE.  IS THERE A WAY TO USE VIEWS?
00280 //TODO THEY ARE AVAILABLE IN EPETRA; ARE THEY AVAIL IN TPETRA AND XPETRA?
00281   for (size_t i=0; i < nrows; i++){
00282     lno_t row = i + base_;
00283     nnz = matrix_->getNumEntriesInLocalRow(row);
00284     matrix_->getLocalRowView(row, indices, nzs);
00285     for (size_t j=0; j < nnz; j++){
00286       values_[next] = nzs[j];
00287       // TODO - this will be slow
00288       //   Is it possible that global columns ids might be stored in order?
00289       columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
00290     }
00291     offset_[i+1] = offset_[i] + nnz;
00292   } 
00293 
00294   if (coordinateDim_ > 0)
00295     rowCoords_ = arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
00296 
00297   if (weightDim_ > 0){
00298     rowWeights_ = arcp(new input_t [weightDim_], 0, weightDim_, true);
00299     numNzWeight_ = arcp(new bool [weightDim_], 0, weightDim_, true);
00300     for (int i=0; i < weightDim_; i++)
00301       numNzWeight_[i] = false;
00302   }
00303 }
00304 
00305 // TODO (from 3/21/12 mtg):  Consider changing interface to take an XpetraMultivector
00306 template <typename User>
00307   void XpetraCrsMatrixAdapter<User>::setRowCoordinates(
00308     const scalar_t *coordVal, int stride, int dim)
00309 {
00310   typedef StridedData<lno_t,scalar_t> input_t;
00311   env_->localInputAssertion(__FILE__, __LINE__, 
00312     "invalid row coordinate dimension",
00313     dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00314   size_t nvtx = getLocalNumRows();
00315   ArrayRCP<const scalar_t> coordV(coordVal, 0, nvtx*stride, false);
00316   rowCoords_[dim] = input_t(coordV, stride);
00317 }
00318 
00319 template <typename User>
00320   void XpetraCrsMatrixAdapter<User>::setRowWeights(
00321     const scalar_t *weightVal, int stride, int idx)
00322 {
00323   typedef StridedData<lno_t,scalar_t> input_t;
00324   env_->localInputAssertion(__FILE__, __LINE__,
00325     "invalid row weight index",
00326     idx >= 0 && idx < weightDim_, BASIC_ASSERTION);
00327   size_t nvtx = getLocalNumRows();
00328   ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx*stride, false);
00329   rowWeights_[idx] = input_t(weightV, stride);
00330 }
00331 
00332 template <typename User>
00333   void XpetraCrsMatrixAdapter<User>::setRowWeightIsNumberOfNonZeros(int dim)
00334 {
00335   env_->localInputAssertion(__FILE__, __LINE__,
00336     "invalid row weight dimension",
00337     dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
00338 
00339   numNzWeight_[dim] = true;
00340 }
00341 
00342 template <typename User>
00343   template <typename Adapter>
00344     size_t XpetraCrsMatrixAdapter<User>::applyPartitioningSolution(
00345       const User &in, User *&out, 
00346       const PartitioningSolution<Adapter> &solution) const
00347 { 
00348   // Get an import list
00349 
00350   size_t len = solution.getLocalNumberOfIds();
00351   const gid_t *gids = solution.getIdList();
00352   const partId_t *parts = solution.getPartList();
00353   ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false); 
00354   ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len, 
00355     false); 
00356   ArrayRCP<lno_t> dummyIn;
00357   ArrayRCP<gid_t> importList;
00358   ArrayRCP<lno_t> dummyOut;
00359   size_t numNewRows;
00360   const RCP<const Comm<int> > comm = matrix_->getRowMap()->getComm();
00361 
00362   try{
00363     numNewRows = solution.convertSolutionToImportList(
00364       0, dummyIn, importList, dummyOut);
00365   }
00366   Z2_FORWARD_EXCEPTIONS;
00367 
00368   gno_t lsum = numNewRows;
00369   gno_t gsum = 0;
00370   reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
00371 
00372   RCP<const User> inPtr = rcp(&in, false);
00373 
00374   RCP<const User> outPtr = XpetraTraits<User>::doMigration(
00375    inPtr, lsum, importList.getRawPtr());
00376 
00377   out = const_cast<User *>(outPtr.get());
00378   outPtr.release();
00379   return numNewRows;
00380 }
00381 
00382 }  //namespace Zoltan2
00383   
00384 #endif