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