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_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_RowMatrix.hpp>
00058 
00059 namespace Zoltan2 {
00060 
00062 
00079 template <typename User>
00080   class XpetraRowMatrixAdapter : public MatrixAdapter<User> {
00081 public:
00082 
00083 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00084   typedef typename InputTraits<User>::scalar_t    scalar_t;
00085   typedef typename InputTraits<User>::lno_t    lno_t;
00086   typedef typename InputTraits<User>::gno_t    gno_t;
00087   typedef typename InputTraits<User>::gid_t    gid_t;
00088   typedef typename InputTraits<User>::node_t   node_t;
00089   typedef Xpetra::RowMatrix<scalar_t, lno_t, gno_t, node_t> xmatrix_t;
00090   typedef MatrixAdapter<User> base_adapter_t;
00091   typedef User user_t;
00092 #endif
00093 
00096   ~XpetraRowMatrixAdapter() { }
00097 
00107   XpetraRowMatrixAdapter(const RCP<const User> &inmatrix,
00108                          int numWeightsPerRow=0, int coordDim=0);
00109 
00124   void setRowCoordinates(const scalar_t *coordVal, int stride, int dim);
00125 
00141   void setRowWeights(const scalar_t *weightVal, int stride, int idx = 0);
00142 
00148   void setRowWeightIsNumberOfNonZeros(int idx);
00149 
00151   // The MatrixAdapter interface.
00153 
00154   size_t getLocalNumRows() const { 
00155     return matrix_->getNodeNumRows();
00156   }
00157 
00158   size_t getLocalNumColumns() const { 
00159     return matrix_->getNodeNumCols();
00160   }
00161 
00162   size_t getLocalNumEntries() const {
00163     return matrix_->getNodeNumEntries();
00164   }
00165 
00166   bool CRSViewAvailable() const { return true; }
00167 
00168   void getRowIDsView(const gid_t *&rowIds) const 
00169   {
00170     ArrayView<const gid_t> rowView = rowMap_->getNodeElementList();
00171     rowIds = rowView.getRawPtr();
00172   }
00173 
00174   void getCRSView(const lno_t *&offsets, const gid_t *& colIds) const
00175   {
00176     offsets = offset_.getRawPtr();
00177     colIds = columnIds_.getRawPtr();
00178   }
00179 
00180   void getCRSView(const lno_t *&offsets, const gid_t *& colIds,
00181                     const scalar_t *&values) const
00182   {
00183     offsets = offset_.getRawPtr();
00184     colIds = columnIds_.getRawPtr();
00185     values = values_.getRawPtr();
00186   }
00187 
00188   int getNumWeightsPerRow() const
00189   {
00190     return weightDim_;
00191   }
00192 
00193   void getRowWeightsView(const scalar_t *&weights, int &stride,
00194                          int idx = 0) const
00195   {
00196     env_->localInputAssertion(__FILE__, __LINE__,
00197       "invalid weight index",
00198       idx >= 0 && idx < weightDim_, BASIC_ASSERTION);
00199     size_t length;
00200     rowWeights_[idx].getStridedList(length, weights, stride);
00201   }
00202 
00203   bool useNumNonzerosAsRowWeight(int idx) const { return numNzWeight_[idx];}
00204 
00205   int getDimension() const {return coordinateDim_;}
00206 
00207   void getRowCoordinatesView(const scalar_t *&coords, int &stride,
00208                              int dim) const
00209   {
00210     env_->localInputAssertion(__FILE__, __LINE__,
00211       "invalid coordinate dimension",
00212       dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00213     size_t length;
00214     rowCoords_[dim].getStridedList(length, coords, stride);
00215   }
00216 
00217   template <typename Adapter>
00218     size_t applyPartitioningSolution(const User &in, User *&out,
00219          const PartitioningSolution<Adapter> &solution) const;
00220 
00221 private:
00222 
00223   RCP<Environment> env_;    // for error messages, etc.
00224 
00225   RCP<const User> inmatrix_;
00226   RCP<const xmatrix_t> matrix_;
00227   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > rowMap_;
00228   RCP<const Xpetra::Map<lno_t, gno_t, node_t> > colMap_;
00229   lno_t base_;
00230   ArrayRCP<lno_t> offset_;
00231   ArrayRCP<gno_t> columnIds_;
00232   ArrayRCP<scalar_t> values_;
00233 
00234   int coordinateDim_;
00235   ArrayRCP<StridedData<lno_t, scalar_t> > rowCoords_;
00236 
00237   int weightDim_;
00238   ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
00239   ArrayRCP<bool> numNzWeight_;
00240 
00241   bool mayHaveDiagonalEntries;
00242 };
00243 
00245 // Definitions
00247 
00248 template <typename User>
00249   XpetraRowMatrixAdapter<User>::XpetraRowMatrixAdapter(
00250     const RCP<const User> &inmatrix, int weightDim, int coordDim):
00251       env_(rcp(new Environment)),
00252       inmatrix_(inmatrix), matrix_(), rowMap_(), colMap_(), base_(),
00253       offset_(), columnIds_(),
00254       coordinateDim_(coordDim), rowCoords_(),
00255       weightDim_(weightDim), rowWeights_(), numNzWeight_(),
00256       mayHaveDiagonalEntries(true)
00257 {
00258   typedef StridedData<lno_t,scalar_t> input_t;
00259   matrix_ = XpetraTraits<User>::convertToXpetra(inmatrix);
00260   rowMap_ = matrix_->getRowMap();
00261   colMap_ = matrix_->getColMap();
00262   base_ = rowMap_->getIndexBase();
00263 
00264   size_t nrows = matrix_->getNodeNumRows();
00265   size_t nnz = matrix_->getNodeNumEntries();
00266   size_t maxnumentries = matrix_->getNodeMaxNumRowEntries();
00267  
00268   offset_.resize(nrows+1, 0);
00269   columnIds_.resize(nnz);
00270   values_.resize(nnz);
00271   ArrayRCP<lno_t> indices(maxnumentries);
00272   ArrayRCP<scalar_t> nzs(maxnumentries);
00273 
00274   lno_t next = 0;
00275   for (size_t i=0; i < nrows; i++){
00276     lno_t row = i + base_;
00277     matrix_->getLocalRowCopy(row, indices(), nzs(), nnz);
00278     for (size_t j=0; j < nnz; j++){
00279       values_[next] = nzs[j];
00280       // TODO - this will be slow
00281       //   Is it possible that global columns ids might be stored in order?
00282       columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
00283     }
00284     offset_[i+1] = offset_[i] + nnz;
00285   } 
00286 
00287   if (coordinateDim_ > 0)
00288     rowCoords_ = arcp(new input_t [coordinateDim_], 0, coordinateDim_, true);
00289 
00290   if (weightDim_ > 0){
00291     rowWeights_ = arcp(new input_t [weightDim_], 0, weightDim_, true);
00292     numNzWeight_ = arcp(new bool [weightDim_], 0, weightDim_, true);
00293     for (int i=0; i < weightDim_; i++)
00294       numNzWeight_[i] = false;
00295   }
00296 }
00297 
00298 // TODO (from 3/21/12 mtg):  Consider changing interface to take an XpetraMultivector
00299 template <typename User>
00300   void XpetraRowMatrixAdapter<User>::setRowCoordinates(
00301     const scalar_t *coordVal, int stride, int dim)
00302 {
00303   typedef StridedData<lno_t,scalar_t> input_t;
00304   env_->localInputAssertion(__FILE__, __LINE__, 
00305     "invalid row coordinate dimension",
00306     dim >= 0 && dim < coordinateDim_, BASIC_ASSERTION);
00307   size_t nvtx = getLocalNumRows();
00308   ArrayRCP<const scalar_t> coordV(coordVal, 0, nvtx*stride, false);
00309   rowCoords_[dim] = input_t(coordV, stride);
00310 }
00311 
00312 template <typename User>
00313   void XpetraRowMatrixAdapter<User>::setRowWeights(
00314     const scalar_t *weightVal, int stride, int idx)
00315 {
00316   typedef StridedData<lno_t,scalar_t> input_t;
00317   env_->localInputAssertion(__FILE__, __LINE__,
00318     "invalid row weight index",
00319     idx >= 0 && idx < weightDim_, BASIC_ASSERTION);
00320   size_t nvtx = getLocalNumRows();
00321   ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx*stride, false);
00322   rowWeights_[idx] = input_t(weightV, stride);
00323 }
00324 
00325 template <typename User>
00326   void XpetraRowMatrixAdapter<User>::setRowWeightIsNumberOfNonZeros(int dim)
00327 {
00328   env_->localInputAssertion(__FILE__, __LINE__,
00329     "invalid row weight dimension",
00330     dim >= 0 && dim < weightDim_, BASIC_ASSERTION);
00331   numNzWeight_[dim] = true;
00332 }
00333 
00334 template <typename User>
00335   template <typename Adapter>
00336     size_t XpetraRowMatrixAdapter<User>::applyPartitioningSolution(
00337       const User &in, User *&out, 
00338       const PartitioningSolution<Adapter> &solution) const
00339 { 
00340   // Get an import list
00341 
00342   size_t len = solution.getLocalNumberOfIds();
00343   const gid_t *gids = solution.getIdList();
00344   const partId_t *parts = solution.getPartList();
00345   ArrayRCP<gid_t> gidList = arcp(const_cast<gid_t *>(gids), 0, len, false); 
00346   ArrayRCP<partId_t> partList = arcp(const_cast<partId_t *>(parts), 0, len, 
00347     false); 
00348   ArrayRCP<lno_t> dummyIn;
00349   ArrayRCP<gid_t> importList;
00350   ArrayRCP<lno_t> dummyOut;
00351   size_t numNewRows;
00352   const RCP<const Comm<int> > comm = matrix_->getRowMap()->getComm();
00353 
00354   try{
00355     numNewRows = solution.convertSolutionToImportList(
00356       0, dummyIn, importList, dummyOut);
00357   }
00358   Z2_FORWARD_EXCEPTIONS;
00359 
00360   gno_t lsum = numNewRows;
00361   gno_t gsum = 0;
00362   reduceAll<int, gno_t>(*comm, Teuchos::REDUCE_SUM, 1, &lsum, &gsum);
00363 
00364   RCP<const User> inPtr = rcp(&in, false);
00365 
00366   RCP<const User> outPtr = XpetraTraits<User>::doMigration(
00367    inPtr, lsum, importList.getRawPtr());
00368 
00369   out = const_cast<User *>(outPtr.get());
00370   outPtr.release();
00371   return numNewRows;
00372 }
00373 
00374 }  //namespace Zoltan2
00375   
00376 #endif