49 #ifndef XPETRA_MATRIXFACTORY_HPP
50 #define XPETRA_MATRIXFACTORY_HPP
55 #include "Xpetra_CrsMatrixWrap.hpp"
57 #include "Xpetra_Map.hpp"
63 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node = KokkosClassic::DefaultNode::DefaultNodeType>
64 class MatrixFactory2 {
65 #undef XPETRA_MATRIXFACTORY2_SHORT
70 RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(A);
71 if (oldOp == Teuchos::null)
74 RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
79 "Not Epetra or Tpetra matrix");
81 #ifdef HAVE_XPETRA_EPETRA
88 #ifdef HAVE_XPETRA_TPETRA
91 RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<
const TpetraCrsMatrix>(oldCrsOp);
93 if (oldTCrsOp != Teuchos::null) {
95 RCP<CrsMatrixWrap> newOp (
new CrsMatrixWrap(Teuchos::as<RCP<CrsMatrix> >(newTCrsOp)));
96 if (setFixedBlockSize)
97 newOp->SetFixedBlockSize(A->GetFixedBlockSize());
106 return Teuchos::null;
109 #define XPETRA_MATRIXFACTORY2_SHORT
119 #undef XPETRA_MATRIXFACTORY2_SHORT
123 RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(A);
124 if (oldOp == Teuchos::null)
127 RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
129 #ifdef HAVE_XPETRA_EPETRA
130 #ifndef XPETRA_EPETRA_NO_32BIT_GLOBAL_INDICES
132 if (oldECrsOp != Teuchos::null) {
136 if (setFixedBlockSize)
137 newOp->SetFixedBlockSize(A->GetFixedBlockSize());
143 #ifdef HAVE_XPETRA_TPETRA
145 RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<
const TpetraCrsMatrix>(oldCrsOp);
146 if (oldTCrsOp != Teuchos::null) {
149 if (setFixedBlockSize)
150 newOp->SetFixedBlockSize(A->GetFixedBlockSize());
153 return Teuchos::null;
155 throw Exceptions::BadCast(
"Cast from Xpetra::Matrix to Xpetra::EpetraCrsMatrix or Xpetra::TpetraCrsMatrix failed");
156 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
162 #define XPETRA_MATRIXFACTORY2_SHORT
164 #ifdef HAVE_XPETRA_INT_LONG_LONG
168 class MatrixFactory2<double, int, long long, Node> {
169 typedef double Scalar;
170 typedef int LocalOrdinal;
171 typedef long long GlobalOrdinal;
173 #undef XPETRA_MATRIXFACTORY2_SHORT
177 RCP<const CrsMatrixWrap> oldOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(A);
178 if (oldOp == Teuchos::null)
179 throw Exceptions::BadCast(
"Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
181 RCP<const CrsMatrix> oldCrsOp = oldOp->getCrsMatrix();
183 #ifdef HAVE_XPETRA_EPETRA
184 #ifndef XPETRA_EPETRA_NO_64BIT_GLOBAL_INDICES
185 RCP<const EpetraCrsMatrixT<GlobalOrdinal,Node> > oldECrsOp = Teuchos::rcp_dynamic_cast<
const EpetraCrsMatrixT<GlobalOrdinal,Node> >(oldCrsOp);
186 if (oldECrsOp != Teuchos::null) {
188 RCP<CrsMatrix> newECrsOp(
new EpetraCrsMatrixT<GlobalOrdinal,Node>(*oldECrsOp));
189 RCP<CrsMatrixWrap> newOp (
new CrsMatrixWrap (newECrsOp));
190 if (setFixedBlockSize)
191 newOp->SetFixedBlockSize(A->GetFixedBlockSize());
197 #ifdef HAVE_XPETRA_TPETRA
199 RCP<const TpetraCrsMatrix> oldTCrsOp = Teuchos::rcp_dynamic_cast<
const TpetraCrsMatrix>(oldCrsOp);
200 if (oldTCrsOp != Teuchos::null) {
201 RCP<CrsMatrix> newTCrsOp(
new TpetraCrsMatrix(*oldTCrsOp));
202 RCP<CrsMatrixWrap> newOp (
new CrsMatrixWrap(newTCrsOp));
203 if (setFixedBlockSize)
204 newOp->SetFixedBlockSize(A->GetFixedBlockSize());
208 throw Exceptions::BadCast(
"Cast from Xpetra::Matrix to Xpetra::EpetraCrsMatrix or Xpetra::TpetraCrsMatrix failed");
211 return Teuchos::null;
214 #endif // HAVE_XPETRA_INT_LONG_LONG
216 #define XPETRA_MATRIXFACTORY2_SHORT
219 template <
class Scalar,
223 class MatrixFactory {
224 #undef XPETRA_MATRIXFACTORY_SHORT
234 static RCP<Matrix>
Build(
const RCP<const Map>& rowMap) {
239 static RCP<Matrix>
Build(
const RCP<const Map>& rowMap,
size_t maxNumEntriesPerRow) {
244 static RCP<Matrix>
Build(
const RCP<const Map>& rowMap,
const RCP<const Map>& colMap,
size_t maxNumEntriesPerRow) {
245 return rcp(
new CrsMatrixWrap(rowMap, colMap, maxNumEntriesPerRow));
249 static RCP<Matrix>
Build(
const RCP<const Map>& rowMap,
const RCP<const Map>& colMap,
const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc) {
250 return rcp(
new CrsMatrixWrap(rowMap, colMap, NumEntriesPerRowToAlloc));
253 #ifdef HAVE_XPETRA_KOKKOS_REFACTOR
254 static RCP<Matrix>
Build (
256 const Teuchos::RCP<const Map>& rowMap,
257 const Teuchos::RCP<const Map>& colMap,
259 const Teuchos::RCP<Teuchos::ParameterList>& params = null) {
261 return rcp(
new CrsMatrixWrap(rowMap, colMap, lclMatrix, params));
264 static RCP<Matrix>
Build (
266 const Teuchos::RCP<const Map>& rowMap,
267 const Teuchos::RCP<const Map>& colMap,
268 const Teuchos::RCP<const Map>& domainMap = Teuchos::null,
269 const Teuchos::RCP<const Map>& rangeMap = Teuchos::null,
270 const Teuchos::RCP<Teuchos::ParameterList>& params = null) {
272 return rcp(
new CrsMatrixWrap(lclMatrix, rowMap, colMap, domainMap, rangeMap, params));
277 static RCP<Matrix>
Build(
const RCP<const Map> &rowMap,
const ArrayRCP<const size_t> &NumEntriesPerRowToAlloc) {
278 return rcp(
new CrsMatrixWrap(rowMap, NumEntriesPerRowToAlloc) );
282 static RCP<Matrix>
Build(
const RCP<const CrsGraph>& graph,
const RCP<ParameterList>& paramList = Teuchos::null) {
287 static RCP<Matrix>
Build(
const RCP<const Vector>& diagonal) {
288 Teuchos::ArrayRCP<const Scalar> vals = diagonal->getData(0);
289 LocalOrdinal NumMyElements = diagonal->getMap()->getNodeNumElements();
290 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = diagonal->getMap()->getNodeElementList();
292 Teuchos::RCP<CrsMatrixWrap> mtx = Teuchos::rcp(
new CrsMatrixWrap(diagonal->getMap(), 1));
294 for (LocalOrdinal i = 0; i < NumMyElements; ++i) {
295 mtx->insertGlobalValues(MyGlobalElements[i],
296 Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
297 Teuchos::tuple<Scalar>(vals[i]) );
304 static RCP<Matrix>
Build(
const RCP<const Matrix>& sourceMatrix,
const Import& importer,
const RCP<const Map>& domainMap = Teuchos::null,
const RCP<const Map>& rangeMap = Teuchos::null,
const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
305 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(sourceMatrix);
306 if (crsOp == Teuchos::null)
309 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
311 if (newCrs->hasMatrix())
314 return Teuchos::null;
318 static RCP<Matrix>
Build(
const RCP<const Matrix> & sourceMatrix,
const Export &exporter,
const RCP<const Map> & domainMap,
const RCP<const Map> & rangeMap,
const Teuchos::RCP<Teuchos::ParameterList>& params) {
319 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(sourceMatrix);
320 if (crsOp == Teuchos::null)
323 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
328 static RCP<Matrix>
Build(
const RCP<const Matrix>& sourceMatrix,
const Import& RowImporter,
const Import& DomainImporter,
const RCP<const Map>& domainMap,
const RCP<const Map>& rangeMap,
const Teuchos::RCP<Teuchos::ParameterList>& params) {
329 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(sourceMatrix);
330 if (crsOp == Teuchos::null)
333 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
334 RCP<CrsMatrix> newCrs =
CrsMatrixFactory::Build(originalCrs, RowImporter, Teuchos::rcpFromRef(DomainImporter), domainMap, rangeMap, params);
335 if (newCrs->hasMatrix())
338 return Teuchos::null;
342 static RCP<Matrix>
Build(
const RCP<const Matrix> & sourceMatrix,
const Export &RowExporter,
const Export &DomainExporter,
const RCP<const Map> & domainMap = Teuchos::null,
const RCP<const Map> & rangeMap = Teuchos::null,
const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) {
343 RCP<const CrsMatrixWrap> crsOp = Teuchos::rcp_dynamic_cast<
const CrsMatrixWrap>(sourceMatrix);
344 if (crsOp == Teuchos::null)
347 RCP<CrsMatrix> originalCrs = crsOp->getCrsMatrix();
348 RCP<CrsMatrix> newCrs =
CrsMatrixFactory::Build(originalCrs, RowExporter, Teuchos::rcpFromRef(DomainExporter), domainMap, rangeMap, params);
349 if (newCrs->hasMatrix())
352 return Teuchos::null;
359 RCP<const BlockedCrsMatrix> input = Teuchos::rcp_dynamic_cast<
const BlockedCrsMatrix>(A);
360 if(input == Teuchos::null)
364 RCP<const MapExtractor> rgMapExt = Teuchos::rcp(
new MapExtractor(*(input->getRangeMapExtractor())));
365 RCP<const MapExtractor> doMapExt = Teuchos::rcp(
new MapExtractor(*(input->getDomainMapExtractor())));
368 RCP<BlockedCrsMatrix> bop = Teuchos::rcp(
new BlockedCrsMatrix(rgMapExt, doMapExt, input->getNodeMaxNumRowEntries()));
370 for (
size_t r = 0; r < input->Rows(); ++r) {
371 for (
size_t c = 0; c < input->Cols(); ++c)
372 if(input->getMatrix(r,c) != Teuchos::null) {
377 bop->setMatrix(r,c,mat);
381 if(input->isFillComplete())
386 #define XPETRA_MATRIXFACTORY_SHORT
390 #define XPETRA_MATRIXFACTORY_SHORT
391 #define XPETRA_MATRIXFACTORY2_SHORT
static RCP< CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap)
Constructor for empty matrix (intended use is an import/export target - can't insert entries directly...
static RCP< Matrix > Build(const RCP< const Vector > &diagonal)
Constructor for creating a diagonal Xpetra::Matrix using the entries of a given vector for the diagon...
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > BuildCopy(const RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > A, bool setFixedBlockSize=true)
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Import &importer, const RCP< const Map > &domainMap=Teuchos::null, const RCP< const Map > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Constructor to create a Matrix using a fusedImport-style construction. The originalMatrix must be a X...
static RCP< Matrix > Build(const RCP< const CrsGraph > &graph, const RCP< ParameterList > ¶mList=Teuchos::null)
Constructor specifying graph.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, size_t maxNumEntriesPerRow)
Constructor specifying the max number of non-zeros per row and providing column map.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, size_t maxNumEntriesPerRow)
Constructor specifying the number of non-zeros for all rows.
static RCP< Matrix > Build(const RCP< const Map > &rowMap)
Exception throws to report errors in the internal logical of the program.
Exception indicating invalid cast attempted.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc)
Constructor specifying (possibly different) number of entries in each row.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, const RCP< const Map > &colMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc)
Constructor specifying the (possibly different) number of entries per row and providing column map...
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > BuildCopy(const RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > A, bool setFixedBlockSize=true)
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Export &exporter, const RCP< const Map > &domainMap, const RCP< const Map > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor to create a Matrix using a fusedExport-style construction. The originalMatrix must be a X...
Concrete implementation of Xpetra::Matrix.
#define XPETRA_MONITOR(funcName)
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Export &RowExporter, const Export &DomainExporter, const RCP< const Map > &domainMap=Teuchos::null, const RCP< const Map > &rangeMap=Teuchos::null, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Constructor to create a Matrix using a fusedExport-style construction. The originalMatrix must be a X...
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > BuildCopy(const RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > A, bool setFixedBlockSize=true)
Xpetra-specific matrix class.
MatrixFactory()
Private constructor. This is a static class.
static RCP< Matrix > Build(const RCP< const Matrix > &sourceMatrix, const Import &RowImporter, const Import &DomainImporter, const RCP< const Map > &domainMap, const RCP< const Map > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor to create a Matrix using a fusedImport-style construction. The originalMatrix must be a X...