Zoltan2 Version of the Day
Zoltan2_XpetraRowMatrixInput.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_XPETRACRSMATRIXINPUT_HPP_
00051 #define _ZOLTAN2_XPETRACRSMATRIXINPUT_HPP_
00052 
00053 #include <Zoltan2_MatrixInput.hpp>
00054 #include <Zoltan2_StridedData.hpp>
00055 #include <Zoltan2_XpetraTraits.hpp>
00056 #include <Zoltan2_PartitioningSolution.hpp>
00057 
00058 #include <Xpetra_RowMatrix.hpp>
00059 
00060 namespace Zoltan2 {
00061 
00063 
00084 template <typename User>
00085   class XpetraRowMatrixInput : public MatrixInput<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::RowMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
00095   typedef MatrixInput<User>       base_adapter_t;
00096   typedef User user_t;
00097 #endif
00098 
00101   ~XpetraRowMatrixInput() { }
00102 
00112   XpetraRowMatrixInput(const RCP<const User> &inmatrix, int coordDim=0, 
00113     int weightDim=0);
00114 
00129   void setRowCoordinates(int dim, const scalar_t *coordVal, int stride);
00130 
00146   void setRowWeights(int dim, const scalar_t *weightVal, int stride);
00147 
00153   void setRowWeightIsNumberOfNonZeros(int dim);
00154 
00158   const RCP<const xmatrix_t> &getMatrix() const
00159   {
00160     return matrix_;
00161   }
00162 
00164   // The InputAdapter interface.
00166 
00167   string inputAdapterName()const { return string("XpetraRowMatrix");}
00168 
00169   size_t getLocalNumberOfObjects() const { return getLocalNumRows();}
00170 
00171   int getNumberOfWeightsPerObject() const { return 0;}
00172 
00173   size_t getObjectWeights(int dim, const scalar_t *&wgt, int &stride) const
00174   {
00175     return getRowWeights(dim, wgt, stride);
00176   }
00177 
00179   // The MatrixInput interface.
00181 
00182   size_t getLocalNumRows() const { 
00183     return matrix_->getNodeNumRows();
00184   }
00185 
00186   global_size_t getGlobalNumRows() const { 
00187     return matrix_->getGlobalNumRows();
00188   }
00189 
00190   size_t getLocalNumColumns() const { 
00191     return matrix_->getNodeNumCols();
00192   }
00193 
00194   global_size_t getGlobalNumColumns() const { 
00195     return matrix_->getGlobalNumCols();
00196   }
00197 
00198   size_t getRowListView(const gid_t *&rowIds,
00199     const lno_t *&offsets, const gid_t *& colIds) const
00200   {
00201     size_t nrows = getLocalNumRows();
00202 
00203     ArrayView<const gid_t> rowView = rowMap_->getNodeElementList();
00204     rowIds = rowView.getRawPtr();
00205     offsets = offset_.getRawPtr();
00206     colIds = columnIds_.getRawPtr();
00207     return nrows;
00208   }
00209 
00210   int getRowWeightDimension() const
00211   {
00212     return weightDim_;
00213   }
00214 
00215   size_t getRowWeights(int dim,
00216      const scalar_t *&weights, int &stride) const
00217   {
00218     env_->localInputAssertion(__FILE__, __LINE__,
00219       "invalid weight dimension",
00220       dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
00221 
00222     size_t length;
00223     rowWeights_[dim].getStridedList(length, weights, stride);
00224     return length;
00225   }
00226 
00227   bool getRowWeightIsNumberOfNonZeros(int dim) const { return numNzWeight_[dim];}
00228 
00229   int getCoordinateDimension() const {return coordinateDim_;}
00230 
00231   size_t getRowCoordinates(int dim,
00232     const scalar_t *&coords, int &stride) const
00233   {
00234     env_->localInputAssertion(__FILE__, __LINE__,
00235       "invalid coordinate dimension",
00236       dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00237 
00238     size_t length;
00239     rowCoords_[dim].getStridedList(length, coords, stride);
00240     return length;
00241   }
00242 
00244   // End of MatrixInput interface.
00246 
00247   template <typename Adapter>
00248     size_t applyPartitioningSolution(const User &in, User *&out,
00249          const PartitioningSolution<Adapter> &solution) const;
00250 
00251 private:
00252 
00253   RCP<Environment> env_;    // for error messages, etc.
00254 
00255   RCP<const User> inmatrix_;
00256   RCP<const xmatrix_t> matrix_;
00257   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > rowMap_;
00258   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > colMap_;
00259   lno_t base_;
00260   ArrayRCP<lno_t> offset_;
00261   ArrayRCP<gno_t> columnIds_;
00262 
00263   int coordinateDim_;
00264   ArrayRCP<StridedData<lno_t, scalar_t> > rowCoords_;
00265 
00266   int weightDim_;
00267   ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
00268   ArrayRCP<bool> numNzWeight_;
00269 
00270   bool mayHaveDiagonalEntries;
00271 };
00272 
00274 // Definitions
00276 
00277 template <typename User>
00278   XpetraRowMatrixInput<User>::XpetraRowMatrixInput(
00279     const RCP<const User> &inmatrix, int coordDim, int weightDim):
00280       env_(rcp(new Environment)),
00281       inmatrix_(inmatrix), matrix_(), rowMap_(), colMap_(), base_(),
00282       offset_(), columnIds_(),
00283       coordinateDim_(coordDim), rowCoords_(),
00284       weightDim_(weightDim), rowWeights_(), numNzWeight_(),
00285       mayHaveDiagonalEntries(true)
00286 {
00287   typedef StridedData<lno_t,scalar_t> input_t;
00288   matrix_ = XpetraTraits<User>::convertToXpetra(inmatrix);
00289 
00290   rowMap_ = matrix_->getRowMap();
00291   colMap_ = matrix_->getColMap();
00292   base_ = rowMap_->getIndexBase();
00293 
00294   size_t nrows = matrix_->getNodeNumRows();
00295   size_t nnz = matrix_->getNodeNumEntries();
00296   size_t maxnumentries = matrix_->getNodeMaxNumRowEntries();
00297  
00298   offset_.resize(nrows+1, 0);
00299   columnIds_.resize(nnz);
00300   ArrayRCP<lno_t> indices(maxnumentries);
00301   ArrayRCP<scalar_t> nzs(maxnumentries);
00302 
00303   lno_t next = 0;
00304   for (size_t i=0; i < nrows; i++){
00305     lno_t row = i + base_;
00306     matrix_->getLocalRowCopy(row,indices(),nzs(),nnz);
00307     for (size_t j=0; j < nnz; j++){
00308       // TODO - this will be slow
00309       //   Is it possible that global columns ids might be stored in order?
00310       columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
00311     }
00312     offset_[i+1] = offset_[i] + nnz;
00313   } 
00314 
00315   if (coordinateDim_ > 0)
00316     rowCoords_ = arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
00317 
00318   if (weightDim_ > 0){
00319     rowWeights_ = arcp(new input_t [weightDim_], 0, weightDim_, true);
00320     numNzWeight_ = arcp(new bool [weightDim_], 0, weightDim_, true);
00321     for (int i=0; i < weightDim_; i++)
00322       numNzWeight_[i] = false;
00323   }
00324 }
00325 
00326 // TODO (from 3/21/12 mtg):  Consider changing interface to take an XpetraMultivector
00327 template <typename User>
00328   void XpetraRowMatrixInput<User>::setRowCoordinates(int dim,
00329     const scalar_t *coordVal, int stride)
00330 {
00331   typedef StridedData<lno_t,scalar_t> input_t;
00332 
00333   env_->localInputAssertion(__FILE__, __LINE__, 
00334     "invalid row coordinate dimension",
00335     dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00336 
00337   size_t nvtx = getLocalNumRows();
00338 
00339   ArrayRCP<const scalar_t> coordV(coordVal, 0, nvtx, false);
00340   rowCoords_[dim] = input_t(coordV, stride);
00341 }
00342 
00343 template <typename User>
00344   void XpetraRowMatrixInput<User>::setRowWeights(int dim,
00345     const scalar_t *weightVal, int stride)
00346 {
00347   typedef StridedData<lno_t,scalar_t> input_t;
00348 
00349   env_->localInputAssertion(__FILE__, __LINE__,
00350     "invalid row weight dimension",
00351     dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
00352 
00353   size_t nvtx = getLocalNumRows();
00354 
00355   ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx, false);
00356   rowWeights_[dim] = input_t(weightV, stride);
00357 }
00358 
00359 template <typename User>
00360   void XpetraRowMatrixInput<User>::setRowWeightIsNumberOfNonZeros(int dim)
00361 {
00362   env_->localInputAssertion(__FILE__, __LINE__,
00363     "invalid row weight dimension",
00364     dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
00365 
00366   numNzWeight_[dim] = true;
00367 }
00368 
00369 template <typename User>
00370   template <typename Adapter>
00371     size_t XpetraRowMatrixInput<User>::applyPartitioningSolution(
00372       const User &in, User *&out, 
00373       const PartitioningSolution<Adapter> &solution) const
00374 { 
00375   // Get an import list
00376 
00377   size_t len = solution.getLocalNumberOfIds();
00378   const gid_t *gids = solution.getIdList();
00379   const partId_t *parts = solution.getPartList();
00380   ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false); 
00381   ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len, 
00382     false); 
00383   ArrayRCP<lno_t> dummyIn;
00384   ArrayRCP<gid_t> importList;
00385   ArrayRCP<lno_t> dummyOut;
00386   size_t numNewRows;
00387   const RCP<const Comm<int> > comm = matrix_->getRowMap()->getComm();
00388 
00389   try{
00390     numNewRows = solution.convertSolutionToImportList(
00391       0, dummyIn, importList, dummyOut);
00392   }
00393   Z2_FORWARD_EXCEPTIONS;
00394 
00395   gno_t lsum = numNewRows;
00396   gno_t gsum = 0;
00397   reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
00398 
00399   RCP<const User> inPtr = rcp(&in, false);
00400 
00401   RCP<const User> outPtr = XpetraTraits<User>::doMigration(
00402    inPtr, lsum, importList.getRawPtr());
00403 
00404   out = const_cast<User *>(outPtr.get());
00405   outPtr.release();
00406   return numNewRows;
00407 }
00408 
00409 }  //namespace Zoltan2
00410   
00411 #endif