10 #ifndef XPETRA_HELPERS_DEF_HPP
11 #define XPETRA_HELPERS_DEF_HPP
17 #ifdef HAVE_XPETRA_EPETRA
18 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
21 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
23 "Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
25 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
26 const RCP<const EpetraCrsMatrixT<GO, NO>>& tmp_ECrsMtx = Teuchos::rcp_dynamic_cast<
const EpetraCrsMatrixT<GO, NO>>(tmp_CrsMtx);
28 "Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
30 return tmp_ECrsMtx->getEpetra_CrsMatrix();
33 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
35 RCP<Epetra_CrsMatrix> A;
37 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
39 "Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
41 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
42 const RCP<const EpetraCrsMatrixT<GO, NO>>& tmp_ECrsMtx = Teuchos::rcp_dynamic_cast<
const EpetraCrsMatrixT<GO, NO>>(tmp_CrsMtx);
43 TEUCHOS_TEST_FOR_EXCEPTION(tmp_ECrsMtx == Teuchos::null,
Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
45 return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst();
48 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
54 const RCP<const EpetraCrsMatrixT<GO, NO>>& tmp_ECrsMtx = Teuchos::rcp_dynamic_cast<
const EpetraCrsMatrixT<GO, NO>>(tmp_CrsMtx);
56 "Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
58 return *tmp_ECrsMtx->getEpetra_CrsMatrix();
65 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 RCP<Epetra_CrsMatrix> A;
72 const RCP<const EpetraCrsMatrixT<GO, NO>>& tmp_ECrsMtx = Teuchos::rcp_dynamic_cast<
const EpetraCrsMatrixT<GO, NO>>(tmp_CrsMtx);
73 TEUCHOS_TEST_FOR_EXCEPTION(tmp_ECrsMtx == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
75 return *Teuchos::rcp_const_cast<Epetra_CrsMatrix>(tmp_ECrsMtx->getEpetra_CrsMatrix());
81 #endif // HAVE_XPETRA_EPETRA
83 #ifdef HAVE_XPETRA_TPETRA
84 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
87 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
88 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
90 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
92 TEUCHOS_TEST_FOR_EXCEPTION(tmp_ECrsMtx == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
94 return tmp_ECrsMtx->getTpetra_CrsMatrix();
97 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
100 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
101 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
102 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
104 TEUCHOS_TEST_FOR_EXCEPTION(tmp_ECrsMtx == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
106 return tmp_ECrsMtx->getTpetra_CrsMatrixNonConst();
109 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
116 TEUCHOS_TEST_FOR_EXCEPTION(tmp_TCrsMtx == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
118 return *tmp_TCrsMtx->getTpetra_CrsMatrix();
125 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
132 TEUCHOS_TEST_FOR_EXCEPTION(tmp_TCrsMtx == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
134 return *Teuchos::rcp_const_cast<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>(tmp_TCrsMtx->getTpetra_CrsMatrix());
141 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
143 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
144 if (crsOp == Teuchos::null)
return false;
145 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->
getCrsMatrix();
147 if (tmp_ECrsMtx == Teuchos::null)
153 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
159 if (tmp_ECrsMtx == Teuchos::null)
168 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
170 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
171 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
173 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
174 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
175 TEUCHOS_TEST_FOR_EXCEPTION(tmp_BlockCrs == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
176 return tmp_BlockCrs->getTpetra_BlockCrsMatrix();
179 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
181 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
182 TEUCHOS_TEST_FOR_EXCEPTION(crsOp == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
184 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->getCrsMatrix();
185 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
186 TEUCHOS_TEST_FOR_EXCEPTION(tmp_BlockCrs == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
187 return tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
190 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
195 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
196 TEUCHOS_TEST_FOR_EXCEPTION(tmp_BlockCrs == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
197 return *tmp_BlockCrs->getTpetra_BlockCrsMatrix();
203 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
208 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
209 TEUCHOS_TEST_FOR_EXCEPTION(tmp_BlockCrs == Teuchos::null,
Xpetra::Exceptions::BadCast,
"Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
210 return *tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
216 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
218 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(Op);
219 if (crsOp == Teuchos::null)
return false;
220 RCP<const CrsMatrix> tmp_CrsMtx = crsOp->
getCrsMatrix();
221 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
222 if (tmp_BlockCrs == Teuchos::null)
228 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
233 RCP<const TpetraBlockCrsMatrix> tmp_BlockCrs = Teuchos::rcp_dynamic_cast<
const TpetraBlockCrsMatrix>(tmp_CrsMtx);
234 if (tmp_BlockCrs == Teuchos::null)
242 #else // HAVE_XPETRA_TPETRA
243 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
248 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
253 #endif // HAVE_XPETRA_TPETRA
255 #ifdef HAVE_XPETRA_TPETRA
256 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
258 const tcrs_matrix_type& A,
bool transposeA,
const typename tcrs_matrix_type::scalar_type alpha,
259 const tcrs_matrix_type& B,
bool transposeB,
const typename tcrs_matrix_type::scalar_type beta) {
260 using Teuchos::Array;
261 using Teuchos::ParameterList;
264 using Teuchos::rcp_implicit_cast;
265 using Teuchos::rcpFromRef;
269 using transposer_type = Tpetra::RowMatrixTransposer<SC, LO, GO, NO>;
270 using import_type = Tpetra::Import<LO, GO, NO>;
271 RCP<const tcrs_matrix_type> Aprime = rcpFromRef(A);
273 Aprime = transposer_type(Aprime).createTranspose();
275 if (A.isFillComplete() && B.isFillComplete()) {
276 RCP<tcrs_matrix_type> C = rcp(
new tcrs_matrix_type(Aprime->getRowMap(), 0));
277 RCP<ParameterList> addParams = rcp(
new ParameterList);
278 addParams->set(
"Call fillComplete",
false);
280 Tpetra::MatrixMatrix::add<SC, LO, GO, NO>(beta, transposeB, B, alpha,
false, *Aprime, *C, Teuchos::null, Teuchos::null, addParams);
281 return rcp_implicit_cast<
Matrix>(rcp(
new CrsWrap(rcp_implicit_cast<CrsType>(rcp(
new XTCrsType(C))))));
287 RCP<const tcrs_matrix_type> Bprime = rcpFromRef(B);
289 Bprime = transposer_type(Bprime).createTranspose();
291 if (!(Aprime->getRowMap()->isSameAs(*(Bprime->getRowMap())))) {
292 auto import = rcp(
new import_type(Bprime->getRowMap(), Aprime->getRowMap()));
293 Bprime = Tpetra::importAndFillCompleteCrsMatrix<tcrs_matrix_type>(Bprime, *
import, Aprime->getDomainMap(), Aprime->getRangeMap());
296 LO numLocalRows = Aprime->getLocalNumRows();
297 Array<size_t> allocPerRow(numLocalRows);
299 for (LO i = 0; i < numLocalRows; i++) {
300 allocPerRow[i] = Aprime->getNumEntriesInLocalRow(i) + Bprime->getNumEntriesInLocalRow(i);
303 RCP<tcrs_matrix_type> C = rcp(
new tcrs_matrix_type(Aprime->getRowMap(), allocPerRow()));
305 Tpetra::MatrixMatrix::Add<SC, LO, GO, NO>(
306 *Aprime,
false, alpha,
307 *Bprime,
false, beta,
309 return rcp(
new CrsWrap(rcp_implicit_cast<CrsType>(rcp(
new XTCrsType(C)))));
314 #ifdef HAVE_XPETRA_EPETRAEXT
315 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
317 Epetra_CrsMatrix& epA = Op2NonConstEpetraCrs(A);
318 Epetra_CrsMatrix& epB = Op2NonConstEpetraCrs(B);
319 Epetra_CrsMatrix& epC = Op2NonConstEpetraCrs(C);
322 const Epetra_Map& Crowmap = epC.RowMap();
324 Epetra_CrsMatrix Ctemp(::Copy, Crowmap, 0,
false);
325 if (fillCompleteResult) {
326 errCode = EpetraExt::MatrixMatrix::Multiply(epA, transposeA, epB, transposeB, Ctemp,
true);
332 errCode = EpetraExt::MatrixMatrix::Multiply(epA, transposeA, epB, transposeB, Ctemp,
false);
334 int numLocalRows = Crowmap.NumMyElements();
335 long long* globalElementList =
nullptr;
336 Crowmap.MyGlobalElementsPtr(globalElementList);
337 Teuchos::Array<int> entriesPerRow(numLocalRows, 0);
338 for (
int i = 0; i < numLocalRows; i++) {
339 entriesPerRow[i] = Ctemp.NumGlobalEntries(globalElementList[i]);
342 epC = Epetra_CrsMatrix(::Copy, Crowmap, entriesPerRow.data(),
true);
343 for (
int i = 0; i < numLocalRows; i++) {
344 int gid = globalElementList[i];
348 Ctemp.ExtractGlobalRowView(gid, numEntries, values, inds);
349 epC.InsertGlobalValues(gid, numEntries, values, inds);
354 std::ostringstream buf;
356 std::string msg =
"EpetraExt::MatrixMatrix::Multiply returned nonzero error code " + buf.str();
RCP< CrsMatrix > getCrsMatrix() const
static bool isTpetraBlockCrs(RCP< Matrix > Op)
Exception throws to report errors in the internal logical of the program.
static RCP< const Tpetra::CrsMatrix< SC, LO, GO, NO > > Op2TpetraCrs(RCP< Matrix > Op)
static bool isTpetraCrs(RCP< Matrix > Op)
Exception indicating invalid cast attempted.
virtual void fillComplete(const RCP< const Map > &domainMap, const RCP< const Map > &rangeMap, const RCP< ParameterList > ¶ms=null)=0
Signal that data entry is complete, specifying domain and range maps.
static RCP< Tpetra::CrsMatrix< SC, LO, GO, NO > > Op2NonConstTpetraCrs(RCP< Matrix > Op)
Tpetra::CrsMatrix< SC, LO, GO, NO > tcrs_matrix_type
static RCP< Tpetra::BlockCrsMatrix< SC, LO, GO, NO > > Op2NonConstTpetraBlockCrs(RCP< Matrix > Op)
static Teuchos::RCP< Matrix > tpetraAdd(const tcrs_matrix_type &A, bool transposeA, const typename tcrs_matrix_type::scalar_type alpha, const tcrs_matrix_type &B, bool transposeB, const typename tcrs_matrix_type::scalar_type beta)
static RCP< const Epetra_CrsMatrix > Op2EpetraCrs(RCP< Matrix > Op)
static void epetraExtMult(const Matrix &A, bool transposeA, const Matrix &B, bool transposeB, Matrix &C, bool fillCompleteResult)
Concrete implementation of Xpetra::Matrix.
static RCP< const Tpetra::BlockCrsMatrix< SC, LO, GO, NO > > Op2TpetraBlockCrs(RCP< Matrix > Op)
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Matrix > Op)
Xpetra-specific matrix class.