10 #ifndef TPETRA_CRSMATRIXMULTIPLYOP_HPP
11 #define TPETRA_CRSMATRIXMULTIPLYOP_HPP
19 #include "Tpetra_CrsMatrix.hpp"
23 #include "Tpetra_LocalCrsMatrixOperator.hpp"
63 template <
class Scalar,
77 using local_matrix_device_type =
91 A->getLocalMatrixDevice())) {}
105 Teuchos::ETransp mode = Teuchos::NO_TRANS,
106 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
107 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero())
const override {
108 TEUCHOS_TEST_FOR_EXCEPTION(!
matrix_->isFillComplete(), std::runtime_error,
109 Teuchos::typeName(*
this) <<
"::apply(): underlying matrix is not fill-complete.");
111 Teuchos::typeName(*
this) <<
"::apply(X,Y): X and Y must have the same number of vectors.");
112 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::ScalarTraits<Scalar>::isComplex && mode == Teuchos::TRANS, std::logic_error,
113 Teuchos::typeName(*
this) <<
"::apply() does not currently support transposed multiplications for complex scalar types.");
114 if (mode == Teuchos::NO_TRANS) {
132 return matrix_->getDomainMap();
146 const Teuchos::RCP<const crs_matrix_type>
matrix_;
163 mutable Teuchos::RCP<MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
importMV_;
177 mutable Teuchos::RCP<MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
exportMV_;
184 Teuchos::ETransp mode,
192 using STS = Teuchos::ScalarTraits<Scalar>;
199 RCP<const import_type> importer =
matrix_->getGraph()->getImporter();
200 RCP<const export_type> exporter =
matrix_->getGraph()->getExporter();
204 const bool Y_is_overwritten = (beta == STS::zero());
205 const int myRank =
matrix_->getComm()->getRank();
206 if (Y_is_replicated && myRank != 0) {
215 X = Teuchos::rcp(
new MV(X_in, Teuchos::Copy));
218 X = Teuchos::rcpFromRef(X_in);
222 if (importer != null) {
230 if (exporter != null) {
240 if (exporter != null) {
245 auto X_lcl = X->getLocalViewDevice(Access::ReadOnly);
249 if (importer != null) {
251 auto Y_lcl =
importMV_->getLocalViewDevice(Access::OverwriteAll);
254 if (Y_is_overwritten) {
266 MV Y(Y_in, Teuchos::Copy);
267 nonconst_view_type Y_lcl;
268 if (Y_is_overwritten)
276 nonconst_view_type Y_lcl;
277 if (Y_is_overwritten)
286 if (Y_is_replicated) {
297 using Teuchos::NO_TRANS;
300 using Teuchos::rcp_const_cast;
301 using Teuchos::rcpFromRef;
305 typedef Teuchos::ScalarTraits<Scalar> STS;
308 if (alpha == STS::zero()) {
309 if (beta == STS::zero()) {
311 }
else if (beta != STS::one()) {
326 RCP<const import_type> importer =
matrix_->getGraph()->getImporter();
327 RCP<const export_type> exporter =
matrix_->getGraph()->getExporter();
333 const bool Y_is_overwritten = (beta == STS::zero());
336 const bool Y_is_replicated =
346 if (Y_is_replicated &&
matrix_->getComm()->getRank() > 0) {
353 RCP<const MV> X_colMap;
354 if (importer.is_null()) {
362 RCP<MV> X_colMapNonConst = getColumnMapMultiVector(X_in,
true);
364 X_colMap = rcp_const_cast<
const MV>(X_colMapNonConst);
368 X_colMap = rcpFromRef(X_in);
371 ProfilingRegion regionImport(
"Tpetra::CrsMatrixMultiplyOp::apply: Import");
377 RCP<MV> X_colMapNonConst = getColumnMapMultiVector(X_in);
380 X_colMapNonConst->doImport(X_in, *importer,
INSERT);
381 X_colMap = rcp_const_cast<
const MV>(X_colMapNonConst);
388 RCP<MV> Y_rowMap = getRowMapMultiVector(Y_in);
390 auto X_lcl = X_colMap->getLocalViewDevice(Access::ReadOnly);
397 if (!exporter.is_null()) {
398 auto Y_lcl = Y_rowMap->getLocalViewDevice(Access::OverwriteAll);
403 ProfilingRegion regionExport(
"Tpetra::CrsMatrixMultiplyOp::apply: Export");
409 if (Y_is_overwritten) {
434 Y_rowMap = getRowMapMultiVector(Y_in,
true);
438 if (beta != STS::zero()) {
441 nonconst_view_type Y_lcl;
442 if (Y_is_overwritten)
443 Y_lcl = Y_rowMap->getLocalViewDevice(Access::OverwriteAll);
445 Y_lcl = Y_rowMap->getLocalViewDevice(Access::ReadWrite);
450 nonconst_view_type Y_lcl;
451 if (Y_is_overwritten)
464 if (Y_is_replicated) {
465 ProfilingRegion regionReduce(
"Tpetra::CrsMatrixMultiplyOp::apply: Reduce Y");
492 const bool force =
false)
const {
499 RCP<const import_type> importer =
matrix_->getGraph()->getImporter();
500 RCP<const map_type> colMap =
matrix_->getColMap();
513 if (!importer.is_null() || force) {
515 X_colMap = rcp(
new MV(colMap, numVecs));
553 getRowMapMultiVector(
const MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Y_rangeMap,
554 const bool force =
false)
const {
558 typedef Export<LocalOrdinal, GlobalOrdinal, Node> export_type;
560 const size_t numVecs = Y_rangeMap.getNumVectors();
561 RCP<const export_type> exporter =
matrix_->getGraph()->getExporter();
562 RCP<const map_type> rowMap =
matrix_->getRowMap();
574 if (!exporter.is_null() || force) {
576 Y_rowMap = rcp(
new MV(rowMap, numVecs));
593 template <
class OpScalar,
599 CrsMatrixMultiplyOp<OpScalar, MatScalar, LocalOrdinal, GlobalOrdinal, Node> >
605 return Teuchos::rcp(
new op_type(A));
610 #endif // TPETRA_CRSMATRIXMULTIPLYOP_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Abstract interface for local operators (e.g., matrices and preconditioners).
Sparse matrix that presents a row-oriented interface that lets users read or modify entries...
Teuchos::RCP< const map_type > getRangeMap() const override
The range Map of this Operator.
Declaration of Tpetra::Details::Profiling, a scope guard for Kokkos Profiling.
void putScalar(const Scalar &value)
Set all values in the multivector with the given value.
size_t getNumVectors() const
Number of columns in the multivector.
bool isConstantStride() const
Whether this multivector has constant stride between columns.
Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > importMV_
Column Map MultiVector used in apply().
bool isDistributed() const
Whether this is a globally distributed object.
Teuchos::RCP< const map_type > getDomainMap() const override
The domain Map of this Operator.
void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const override
Compute Y = beta*Y + alpha*Op(A)*X, where Op(A) is either A, , or .
bool hasTransposeApply() const override
Whether this Operator's apply() method can apply the transpose or conjugate transpose.
KokkosSparse::CrsMatrix< impl_scalar_type, local_ordinal_type, device_type, void, typename local_graph_device_type::size_type > local_matrix_device_type
The specialization of Kokkos::CrsMatrix that represents the part of the sparse matrix on each MPI pro...
~CrsMatrixMultiplyOp() override=default
Destructor (virtual for memory safety of derived classes).
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
CrsMatrixMultiplyOp(const Teuchos::RCP< const crs_matrix_type > &A)
Constructor.
LocalCrsMatrixOperator< Scalar, MatScalar, typename crs_matrix_type::device_type > localMultiply_
Implementation of local sparse matrix-vector multiply.
Insert new values that don't currently exist.
void scale(const Scalar &alpha)
Scale in place: this = alpha*this.
Abstract interface for operators (e.g., matrices and preconditioners).
const Teuchos::RCP< const crs_matrix_type > matrix_
The underlying CrsMatrix object.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
void applyTranspose(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X_in, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y_in, Teuchos::ETransp mode, Scalar alpha, Scalar beta) const
Apply the transpose or conjugate transpose of the matrix to X_in, producing Y_in. ...
Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > exportMV_
Row Map MultiVector used in apply().
dual_view_type::t_dev::const_type getLocalViewDevice(Access::ReadOnlyStruct) const
Return a read-only, up-to-date view of this MultiVector's local data on device. This requires that th...
Forward declaration of Tpetra::CrsMatrixMultiplyOp.
void applyNonTranspose(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X_in, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y_in, Scalar alpha, Scalar beta) const
Apply the matrix (not its transpose) to X_in, producing Y_in.
A parallel distribution of indices over processes.
void doExport(const SrcDistObject &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, const CombineMode CM, const bool restrictedMode=false)
Export data into this object using an Export object ("forward mode").
A class for wrapping a CrsMatrix multiply in a Operator.
Stand-alone utility functions and macros.
void reduce()
Sum values of a locally replicated multivector across all processes.
Teuchos::RCP< CrsMatrixMultiplyOp< OpScalar, MatScalar, LocalOrdinal, GlobalOrdinal, Node > > createCrsMatrixMultiplyOp(const Teuchos::RCP< const CrsMatrix< MatScalar, LocalOrdinal, GlobalOrdinal, Node > > &A)
Non-member function to create a CrsMatrixMultiplyOp.
Accumulate new values into existing values (may not be supported in all classes)
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.