14#ifndef _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_
15#define _ZOLTAN2_TPETRAROWMATRIXADAPTER_HPP_
21#include <Tpetra_RowMatrix.hpp>
41template <
typename User,
typename UserCoord = User>
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
53 using device_t =
typename node_t::device_type;
54 using host_t =
typename Kokkos::HostSpace::memory_space;
56 using userCoord_t = UserCoord;
67 int nWeightsPerRow = 0);
182 typename Base::ConstIdsHostView &rowIds)
const override;
185 typename Base::ConstIdsDeviceView &rowIds)
const override;
188 ArrayRCP<const gno_t> &colIds)
const;
191 typename Base::ConstOffsetsHostView &offsets,
192 typename Base::ConstIdsHostView &colIds)
const override;
195 typename Base::ConstOffsetsDeviceView &offsets,
196 typename Base::ConstIdsDeviceView &colIds)
const override;
199 ArrayRCP<const gno_t> &colIds,
200 ArrayRCP<const scalar_t> &values)
const;
203 typename Base::ConstOffsetsHostView &offsets,
204 typename Base::ConstIdsHostView &colIds,
205 typename Base::ConstScalarsHostView &values)
const override;
208 typename Base::ConstOffsetsDeviceView &offsets,
209 typename Base::ConstIdsDeviceView &colIds,
210 typename Base::ConstScalarsDeviceView &values)
const override;
221 typename Base::WeightsDeviceView &
weights)
const override;
227 typename Base::WeightsHostView &
weights)
const override;
231 template <
typename Adapter>
233 const User &in, User *&out,
236 template <
typename Adapter>
238 const User &in, RCP<User> &out,
244 const RCP<const User> &inmatrix)
268 virtual RCP<User>
doMigration(
const User &from,
size_t numLocalRows,
269 const gno_t *myNewRows)
const;
276template <
typename User,
typename UserCoord>
278 const RCP<const User> &inmatrix,
int nWeightsPerRow):
283 using localInds_t =
typename User::nonconst_local_inds_host_view_type;
284 using localVals_t =
typename User::nonconst_values_host_view_type;
286 const auto nrows =
matrix_->getLocalNumRows();
287 const auto nnz =
matrix_->getLocalNumEntries();
288 auto maxNumEntries =
matrix_->getLocalMaxNumRowEntries();
293 colIdsHost_ =
typename Base::ConstIdsHostView(
"colIdsHost_", nnz);
294 offsHost_ =
typename Base::ConstOffsetsHostView(
"offsHost_", nrows + 1);
295 valuesHost_ =
typename Base::ScalarsHostView(
"valuesHost_", nnz);
297 localInds_t localColInds(
"localColInds", maxNumEntries);
298 localVals_t localVals(
"localVals", maxNumEntries);
300 for (
size_t r = 0; r < nrows; r++) {
301 size_t numEntries = 0;
302 matrix_->getLocalRowCopy(r, localColInds, localVals, numEntries);
308 for (
size_t j = 0; j < nnz; j++) {
336template <
typename User,
typename UserCoord>
338 const scalar_t *weightVal,
int stride,
int idx) {
343 std::ostringstream emsg;
344 emsg << __FILE__ <<
"," << __LINE__
345 <<
" error: setWeights not yet supported for"
346 <<
" columns or nonzeros." << std::endl;
347 throw std::runtime_error(emsg.str());
352template <
typename User,
typename UserCoord>
354 typename Base::ConstWeightsDeviceView1D val,
int idx) {
359 std::ostringstream emsg;
360 emsg << __FILE__ <<
"," << __LINE__
361 <<
" error: setWeights not yet supported for"
362 <<
" columns or nonzeros." << std::endl;
363 throw std::runtime_error(emsg.str());
368template <
typename User,
typename UserCoord>
370 typename Base::ConstWeightsHostView1D val,
int idx) {
375 std::ostringstream emsg;
376 emsg << __FILE__ <<
"," << __LINE__
377 <<
" error: setWeights not yet supported for"
378 <<
" columns or nonzeros." << std::endl;
379 throw std::runtime_error(emsg.str());
384template <
typename User,
typename UserCoord>
386 const scalar_t *weightVal,
int stride,
int idx) {
389 "Invalid row weight index: " + std::to_string(idx));
392 ArrayRCP<const scalar_t> weightV(weightVal, 0, nrows * stride,
false);
397template <
typename User,
typename UserCoord>
399 typename Base::ConstWeightsDeviceView1D
weights,
int idx) {
402 "Invalid row weight index: " + std::to_string(idx));
404 Kokkos::parallel_for(
413template <
typename User,
typename UserCoord>
415 typename Base::ConstWeightsHostView1D weightsHost,
int idx) {
417 "Invalid row weight index: " + std::to_string(idx));
419 auto weightsDevice = Kokkos::create_mirror_view_and_copy(
420 typename Base::device_t(), weightsHost);
426template <
typename User,
typename UserCoord>
432 std::ostringstream emsg;
433 emsg << __FILE__ <<
"," << __LINE__
434 <<
" error: setWeightIsNumberOfNonZeros not yet supported for"
435 <<
" columns" << std::endl;
436 throw std::runtime_error(emsg.str());
441template <
typename User,
typename UserCoord>
445 std::ostringstream emsg;
446 emsg << __FILE__ <<
":" << __LINE__ <<
" Invalid row weight index " << idx
448 throw std::runtime_error(emsg.str());
455template <
typename User,
typename UserCoord>
457 return matrix_->getLocalNumRows();
461template <
typename User,
typename UserCoord>
463 return matrix_->getLocalNumCols();
467template <
typename User,
typename UserCoord>
469 return matrix_->getLocalNumEntries();
473template <
typename User,
typename UserCoord>
477template <
typename User,
typename UserCoord>
479 ArrayView<const gno_t> rowView =
matrix_->getRowMap()->getLocalElementList();
480 rowIds = rowView.getRawPtr();
484template <
typename User,
typename UserCoord>
486 typename Base::ConstIdsHostView &rowIds)
const {
487 auto idsDevice =
matrix_->getRowMap()->getMyGlobalIndices();
488 auto tmpIds =
typename Base::IdsHostView(
"", idsDevice.extent(0));
490 Kokkos::deep_copy(tmpIds, idsDevice);
496template <
typename User,
typename UserCoord>
498 typename Base::ConstIdsDeviceView &rowIds)
const {
500 auto idsDevice =
matrix_->getRowMap()->getMyGlobalIndices();
501 auto tmpIds =
typename Base::IdsDeviceView(
"", idsDevice.extent(0));
503 Kokkos::deep_copy(tmpIds, idsDevice);
509template <
typename User,
typename UserCoord>
511 ArrayRCP<const gno_t> &colIds)
const {
517template <
typename User,
typename UserCoord>
519 typename Base::ConstOffsetsHostView &offsets,
520 typename Base::ConstIdsHostView &colIds)
const {
521 auto hostOffsets = Kokkos::create_mirror_view(
offsDevice_);
523 offsets = hostOffsets;
531template <
typename User,
typename UserCoord>
533 typename Base::ConstOffsetsDeviceView &offsets,
534 typename Base::ConstIdsDeviceView &colIds)
const {
540template <
typename User,
typename UserCoord>
542 ArrayRCP<const gno_t> &colIds,
543 ArrayRCP<const scalar_t> &values)
const {
550template <
typename User,
typename UserCoord>
552 typename Base::ConstOffsetsHostView &offsets,
553 typename Base::ConstIdsHostView &colIds,
554 typename Base::ConstScalarsHostView &values)
const {
555 auto hostOffsets = Kokkos::create_mirror_view(
offsDevice_);
557 offsets = hostOffsets;
569template <
typename User,
typename UserCoord>
571 typename Base::ConstOffsetsDeviceView &offsets,
572 typename Base::ConstIdsDeviceView &colIds,
573 typename Base::ConstScalarsDeviceView &values)
const {
580template <
typename User,
typename UserCoord>
584template <
typename User,
typename UserCoord>
588 std::ostringstream emsg;
589 emsg << __FILE__ <<
":" << __LINE__ <<
" Invalid row weight index "
591 throw std::runtime_error(emsg.str());
599template <
typename User,
typename UserCoord>
601 typename Base::WeightsDeviceView1D &
weights,
int idx)
const {
603 "Invalid row weight index.");
606 weights =
typename Base::WeightsDeviceView1D(
"weights", size);
608 Kokkos::parallel_for(
609 size, KOKKOS_CLASS_LAMBDA(
const int id) {
617template <
typename User,
typename UserCoord>
619 typename Base::WeightsDeviceView &
weights)
const {
625template <
typename User,
typename UserCoord>
627 typename Base::WeightsHostView1D &
weights,
int idx)
const {
629 "Invalid row weight index.");
631 auto weightsDevice =
typename Base::WeightsDeviceView1D(
635 weights = Kokkos::create_mirror_view(weightsDevice);
636 Kokkos::deep_copy(
weights, weightsDevice);
640template <
typename User,
typename UserCoord>
642 typename Base::WeightsHostView &
weights)
const {
649template <
typename User,
typename UserCoord>
653template <
typename User,
typename UserCoord>
654template <
typename Adapter>
656 const User &in, User *&out,
660 ArrayRCP<gno_t> importList;
664 solution,
this, importList);
669 RCP<User> outPtr =
doMigration(in, numNewRows, importList.getRawPtr());
675template <
typename User,
typename UserCoord>
676template <
typename Adapter>
678 const User &in, RCP<User> &out,
682 ArrayRCP<gno_t> importList;
686 solution,
this, importList);
691 out =
doMigration(in, numNewRows, importList.getRawPtr());
695template <
typename User,
typename UserCoord>
697 const User &from,
size_t numLocalRows,
const gno_t *myNewRows)
const {
698 typedef Tpetra::Map<lno_t, gno_t, node_t>
map_t;
699 typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t, node_t> tcrsmatrix_t;
710 const tcrsmatrix_t *pCrsMatrix =
dynamic_cast<const tcrsmatrix_t *
>(&from);
713 throw std::logic_error(
"TpetraRowMatrixAdapter cannot migrate data for "
714 "your RowMatrix; it can migrate data only for "
715 "Tpetra::CrsMatrix. "
716 "You can inherit from TpetraRowMatrixAdapter and "
717 "implement migration for your RowMatrix.");
721 const RCP<const map_t> &smap = from.getRowMap();
722 gno_t numGlobalRows = smap->getGlobalNumElements();
723 gno_t base = smap->getMinAllGlobalIndex();
726 ArrayView<const gno_t> rowList(myNewRows, numLocalRows);
727 const RCP<const Teuchos::Comm<int>> &comm = from.getComm();
728 RCP<const map_t> tmap = rcp(
new map_t(numGlobalRows, rowList, base, comm));
731 Tpetra::Import<lno_t, gno_t, node_t> importer(smap, tmap);
733 int oldNumElts = smap->getLocalNumElements();
734 int newNumElts = numLocalRows;
737 typedef Tpetra::Vector<scalar_t, lno_t, gno_t, node_t> vector_t;
738 vector_t numOld(smap);
739 vector_t numNew(tmap);
740 for (
int lid = 0; lid < oldNumElts; lid++) {
741 numOld.replaceGlobalValue(smap->getGlobalElement(lid),
742 scalar_t(from.getNumEntriesInLocalRow(lid)));
744 numNew.doImport(numOld, importer, Tpetra::INSERT);
747 ArrayRCP<size_t> nnz(newNumElts);
748 if (newNumElts > 0) {
749 ArrayRCP<scalar_t> ptr = numNew.getDataNonConst(0);
750 for (
int lid = 0; lid < newNumElts; lid++) {
751 nnz[lid] =
static_cast<size_t>(ptr[lid]);
755 RCP<tcrsmatrix_t> M = rcp(
new tcrsmatrix_t(tmap, nnz()));
757 M->doImport(from, importer, Tpetra::INSERT);
760 return Teuchos::rcp_dynamic_cast<User>(M);
Zoltan2::BasicUserTypes< zscalar_t, zlno_t, zgno_t > user_t
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
Defines the MatrixAdapter interface.
Helper functions for Partitioning Problems.
This file defines the StridedData class.
typename Kokkos::HostSpace::memory_space host_t
typename node_t::device_type device_t
enum MatrixEntityType getPrimaryEntityType() const
A PartitioningSolution is a solution to a partitioning problem.
The StridedData class manages lists of weights or coordinates.
bool CRSViewAvailable() const
Indicates whether the MatrixAdapter implements a view of the matrix in compressed sparse row (CRS) fo...
Base::WeightsDeviceView rowWeightsDevice_
size_t getLocalNumEntries() const
Returns the number of nonzeros on this process.
size_t getLocalNumColumns() const
Returns the number of columns on this process.
Base::ConstIdsDeviceView colIdsDevice_
void getRowWeightsHostView(typename Base::WeightsHostView &weights) const override
size_t getLocalNumRows() const
Returns the number of rows on this process.
Base::ConstIdsHostView colIdsHost_
bool mayHaveDiagonalEntries
TpetraRowMatrixAdapter(const RCP< const User > &inmatrix, int nWeightsPerRow=0)
Constructor.
void getRowIDsHostView(typename Base::ConstIdsHostView &rowIds) const override
void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds, ArrayRCP< const scalar_t > &values) const
ArrayRCP< StridedData< lno_t, scalar_t > > rowWeights_
void getRowWeightsView(const scalar_t *&weights, int &stride, int idx=0) const
TpetraRowMatrixAdapter(int nWeightsPerRow, const RCP< const User > &inmatrix)
void setRowWeightIsNumberOfNonZeros(int idx)
Specify an index for which the row weight should be the global number of nonzeros in the row.
void getRowWeightsHostView(typename Base::WeightsHostView1D &weights, int idx=0) const
Kokkos::View< bool *, host_t > numNzWeight_
Base::ConstOffsetsDeviceView offsDevice_
void setWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each entity of the primaryEntityType.
void getRowIDsView(const gno_t *&rowIds) const override
void getCRSView(ArrayRCP< const offset_t > &offsets, ArrayRCP< const gno_t > &colIds) const
void setRowWeightsDevice(typename Base::ConstWeightsDeviceView1D val, int idx)
Provide a device view to row weights.
void setWeightIsDegree(int idx)
Specify an index for which the weight should be the degree of the entity.
RCP< const ztrowmatrix_t > matrix_
void getCRSHostView(typename Base::ConstOffsetsHostView &offsets, typename Base::ConstIdsHostView &colIds) const override
ArrayRCP< offset_t > offset_
void getRowWeightsDeviceView(typename Base::WeightsDeviceView &weights) const override
void getCRSDeviceView(typename Base::ConstOffsetsDeviceView &offsets, typename Base::ConstIdsDeviceView &colIds) const override
bool useNumNonzerosAsRowWeight(int idx) const
Indicate whether row weight with index idx should be the global number of nonzeros in the row.
Base::ScalarsHostView valuesHost_
void setRowWeightsHost(typename Base::ConstWeightsHostView1D val, int idx)
Provide a host view to row weights.
void setWeightsHost(typename Base::ConstWeightsHostView1D val, int idx)
Provide a host view of weights for the primary entity type.
void getCRSDeviceView(typename Base::ConstOffsetsDeviceView &offsets, typename Base::ConstIdsDeviceView &colIds, typename Base::ConstScalarsDeviceView &values) const override
ArrayRCP< scalar_t > values_
int getNumWeightsPerRow() const
Returns the number of weights per row (0 or greater). Row weights may be used when partitioning matri...
void getRowWeightsDeviceView(typename Base::WeightsDeviceView1D &weights, int idx=0) const
void getCRSHostView(typename Base::ConstOffsetsHostView &offsets, typename Base::ConstIdsHostView &colIds, typename Base::ConstScalarsHostView &values) const override
virtual RCP< User > doMigration(const User &from, size_t numLocalRows, const gno_t *myNewRows) const
void setRowWeights(const scalar_t *weightVal, int stride, int idx=0)
Specify a weight for each row.
void getRowIDsDeviceView(typename Base::ConstIdsDeviceView &rowIds) const override
void applyPartitioningSolution(const User &in, RCP< User > &out, const PartitioningSolution< Adapter > &solution) const
Base::ConstOffsetsHostView offsHost_
void setWeightsDevice(typename Base::ConstWeightsDeviceView1D val, int idx)
Provide a device view of weights for the primary entity type.
void applyPartitioningSolution(const User &in, User *&out, const PartitioningSolution< Adapter > &solution) const
ArrayRCP< gno_t > columnIds_
Base::ScalarsDeviceView valuesDevice_
Created by mbenlioglu on Aug 31, 2020.
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...
static void AssertCondition(bool condition, const std::string &message, const char *file=__FILE__, int line=__LINE__)