50 #ifndef _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_ 51 #define _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_ 57 #include <Tpetra_RowMatrix.hpp> 75 template <
typename User,
typename UserCoord=User>
79 #ifndef DOXYGEN_SHOULD_SKIP_THIS 86 typedef UserCoord userCoord_t;
99 int nWeightsPerRow=0);
151 return matrix_->getNodeNumRows();
155 return matrix_->getNodeNumCols();
159 return matrix_->getNodeNumEntries();
166 ArrayView<const gno_t> rowView = rowMap_->getNodeElementList();
167 rowIds = rowView.getRawPtr();
172 offsets = offset_.getRawPtr();
173 colIds = columnIds_.getRawPtr();
179 offsets = offset_.getRawPtr();
180 colIds = columnIds_.getRawPtr();
181 values = values_.getRawPtr();
190 env_->localInputAssertion(__FILE__, __LINE__,
191 "invalid weight index",
194 rowWeights_[idx].getStridedList(length,
weights, stride);
199 template <
typename Adapter>
203 template <
typename Adapter>
209 RCP<Environment> env_;
211 RCP<const User> matrix_;
212 RCP<const Tpetra::Map<lno_t, gno_t, node_t> > rowMap_;
213 RCP<const Tpetra::Map<lno_t, gno_t, node_t> > colMap_;
215 ArrayRCP<lno_t> offset_;
216 ArrayRCP<gno_t> columnIds_;
217 ArrayRCP<scalar_t> values_;
220 ArrayRCP<StridedData<lno_t, scalar_t> > rowWeights_;
221 ArrayRCP<bool> numNzWeight_;
223 bool mayHaveDiagonalEntries;
225 RCP<User> doMigration(
const User &from,
size_t numLocalRows,
226 const gno_t *myNewRows)
const;
233 template <
typename User,
typename UserCoord>
235 const RCP<const User> &inmatrix,
int nWeightsPerRow):
237 matrix_(inmatrix), rowMap_(), colMap_(), base_(),
238 offset_(), columnIds_(),
239 nWeightsPerRow_(nWeightsPerRow), rowWeights_(), numNzWeight_(),
240 mayHaveDiagonalEntries(true)
244 rowMap_ = matrix_->getRowMap();
245 colMap_ = matrix_->getColMap();
246 base_ = rowMap_->getIndexBase();
248 size_t nrows = matrix_->getNodeNumRows();
249 size_t nnz = matrix_->getNodeNumEntries();
250 size_t maxnumentries =
251 matrix_->getNodeMaxNumRowEntries();
253 offset_.resize(nrows+1, 0);
254 columnIds_.resize(nnz);
256 ArrayRCP<lno_t> indices(maxnumentries);
257 ArrayRCP<scalar_t> nzs(maxnumentries);
259 for (
size_t i=0; i < nrows; i++){
260 lno_t row = i + base_;
261 matrix_->getLocalRowCopy(row, indices(), nzs(), nnz);
262 for (
size_t j=0; j < nnz; j++){
263 values_[next] = nzs[j];
266 columnIds_[next++] = colMap_->getGlobalElement(indices[j]);
268 offset_[i+1] = offset_[i] + nnz;
271 if (nWeightsPerRow_ > 0){
272 rowWeights_ = arcp(
new input_t [nWeightsPerRow_], 0, nWeightsPerRow_,
true);
273 numNzWeight_ = arcp(
new bool [nWeightsPerRow_], 0, nWeightsPerRow_,
true);
274 for (
int i=0; i < nWeightsPerRow_; i++)
275 numNzWeight_[i] =
false;
280 template <
typename User,
typename UserCoord>
282 const scalar_t *weightVal,
int stride,
int idx)
284 if (this->getPrimaryEntityType() ==
MATRIX_ROW)
285 setRowWeights(weightVal, stride, idx);
288 std::ostringstream emsg;
289 emsg << __FILE__ <<
"," << __LINE__
290 <<
" error: setWeights not yet supported for" 291 <<
" columns or nonzeros." 293 throw std::runtime_error(emsg.str());
298 template <
typename User,
typename UserCoord>
300 const scalar_t *weightVal,
int stride,
int idx)
303 env_->localInputAssertion(__FILE__, __LINE__,
304 "invalid row weight index",
306 size_t nvtx = getLocalNumRows();
307 ArrayRCP<const scalar_t> weightV(weightVal, 0, nvtx*stride,
false);
308 rowWeights_[idx] = input_t(weightV, stride);
312 template <
typename User,
typename UserCoord>
316 if (this->getPrimaryEntityType() ==
MATRIX_ROW)
317 setRowWeightIsNumberOfNonZeros(idx);
320 std::ostringstream emsg;
321 emsg << __FILE__ <<
"," << __LINE__
322 <<
" error: setWeightIsNumberOfNonZeros not yet supported for" 323 <<
" columns" << std::endl;
324 throw std::runtime_error(emsg.str());
329 template <
typename User,
typename UserCoord>
333 env_->localInputAssertion(__FILE__, __LINE__,
334 "invalid row weight index",
337 numNzWeight_[idx] =
true;
341 template <
typename User,
typename UserCoord>
342 template <
typename Adapter>
344 const User &in, User *&out,
349 ArrayRCP<gno_t> importList;
353 (solution,
this, importList);
358 RCP<User> outPtr = doMigration(in, numNewRows, importList.getRawPtr());
364 template <
typename User,
typename UserCoord>
365 template <
typename Adapter>
367 const User &in, RCP<User> &out,
372 ArrayRCP<gno_t> importList;
376 (solution,
this, importList);
381 out = doMigration(in, numNewRows, importList.getRawPtr());
386 template <
typename User,
typename UserCoord>
390 const gno_t *myNewRows
393 typedef Tpetra::Map<lno_t, gno_t, node_t> map_t;
394 typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> tcrsmatrix_t;
405 const tcrsmatrix_t *pCrsMatrix =
dynamic_cast<const tcrsmatrix_t *
>(&from);
408 throw std::logic_error(
"TpetraRowMatrixAdapter cannot migrate data for " 409 "your RowMatrix; it can migrate data only for " 410 "Tpetra::CrsMatrix. " 411 "You can inherit from TpetraRowMatrixAdapter and " 412 "implement migration for your RowMatrix.");
418 const RCP<const map_t> &smap = from.getRowMap();
419 gno_t numGlobalRows = smap->getGlobalNumElements();
422 ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
423 const RCP<const Teuchos::Comm<int> > &comm = from.getComm();
424 RCP<const map_t> tmap = rcp(
new map_t(numGlobalRows, rowList, base, comm));
427 Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
447 int oldNumElts = smap->getNodeNumElements();
448 int newNumElts = numLocalRows;
451 typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> vector_t;
452 vector_t numOld(smap);
453 vector_t numNew(tmap);
454 for (
int lid=0; lid < oldNumElts; lid++){
455 numOld.replaceGlobalValue(smap->getGlobalElement(lid),
456 scalar_t(from.getNumEntriesInLocalRow(lid)));
458 numNew.doImport(numOld, importer, Tpetra::INSERT);
461 ArrayRCP<size_t> nnz(newNumElts);
463 ArrayRCP<scalar_t> ptr = numNew.getDataNonConst(0);
464 for (
int lid=0; lid < newNumElts; lid++){
465 nnz[lid] =
static_cast<size_t>(ptr[lid]);
469 RCP<tcrsmatrix_t> M = rcp(
new tcrsmatrix_t(tmap, nnz,
470 Tpetra::StaticProfile));
471 M->doImport(from, importer, Tpetra::INSERT);
475 return Teuchos::rcp_dynamic_cast<User>(M);
InputTraits< User >::scalar_t scalar_t
size_t getLocalNumColumns() const
Returns the number of columns on this process.
Helper functions for Partitioning Problems.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
MatrixAdapter defines the adapter interface for matrices.
InputTraits< User >::gno_t gno_t
bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row...
InputTraits< User >::lno_t lno_t
int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
size_t getImportList(const PartitioningSolution< SolutionAdapter > &solution, const DataAdapter *const data, ArrayRCP< typename DataAdapter::gno_t > &imports)
From a PartitioningSolution, get a list of IDs to be imported. Assumes part numbers in PartitioningSo...
~TpetraRowMatrixAdapter()
Destructor.
Provides access for Zoltan2 to Tpetra::RowMatrix data.
size_t getLocalNumRows() const
Returns the number of rows on this process.
void getCRSView(const lno_t *&offsets, const gno_t *&colIds, const scalar_t *&values) const
A PartitioningSolution is a solution to a partitioning problem.
size_t getLocalNumEntries() const
Returns the number of nonzeros on this process.
The StridedData class manages lists of weights or coordinates.
void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
InputTraits< User >::part_t part_t
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void setRowWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each row.
void getCRSView(const lno_t *&offsets, const gno_t *&colIds) const
Sets pointers to this process' matrix entries using compressed sparse row (CRS) format. All matrix adapters must implement either getCRSView or getCCSView, but implementation of both is not required.
bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
void setWeightIsDegree(int idx)
Specify an index for which the weight should be the degree of the entity.
Defines the MatrixAdapter interface.
void setWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each entity of the primaryEntityType.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
void setRowWeightIsNumberOfNonZeros(int idx)
Specify an index for which the row weight should be the global number of nonzeros in the row...
void getRowIDsView(const gno_t *&rowIds) const
Sets pointer to this process' rows' global IDs.
TpetraRowMatrixAdapter(const RCP< const User > &inmatrix, int nWeightsPerRow=0)
Constructor
This file defines the StridedData class.