14 #ifdef HAVE_XPETRA_EPETRA
16 #include "Epetra_MpiComm.h"
20 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
21 #include <EpetraExt_MatrixMatrix.h>
22 #include <EpetraExt_RowMatrixOut.h>
23 #include <EpetraExt_MultiVectorOut.h>
24 #include <EpetraExt_CrsMatrixIn.h>
25 #include <EpetraExt_MultiVectorIn.h>
26 #include <EpetraExt_BlockMapIn.h>
29 #include <EpetraExt_BlockMapOut.h>
32 #ifdef HAVE_XPETRA_TPETRA
33 #include <MatrixMarket_Tpetra.hpp>
34 #include <Tpetra_RowMatrixTransposer.hpp>
35 #include <TpetraExt_MatrixMatrix.hpp>
36 #include <Xpetra_TpetraMultiVector.hpp>
37 #include <Xpetra_TpetraCrsGraph.hpp>
38 #include <Xpetra_TpetraCrsMatrix.hpp>
39 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
40 #include "Tpetra_Util.hpp"
43 #ifdef HAVE_XPETRA_EPETRA
47 #include "Xpetra_Matrix.hpp"
48 #include "Xpetra_MatrixMatrix.hpp"
50 #include "Xpetra_CrsMatrixWrap.hpp"
51 #include "Xpetra_BlockedCrsMatrix.hpp"
53 #include "Xpetra_Map.hpp"
54 #include "Xpetra_StridedMap.hpp"
55 #include "Xpetra_StridedMapFactory.hpp"
56 #include "Xpetra_MapExtractor.hpp"
57 #include "Xpetra_MatrixFactory.hpp"
59 #include <Teuchos_MatrixMarket_Raw_Writer.hpp>
60 #include <Teuchos_MatrixMarket_Raw_Reader.hpp>
65 #ifdef HAVE_XPETRA_EPETRA
66 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
69 if (xeMap == Teuchos::null)
70 throw Exceptions::BadCast(
"Utils::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
71 return xeMap->getEpetra_Map();
75 #ifdef HAVE_XPETRA_TPETRA
76 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
79 if (tmp_TMap == Teuchos::null)
80 throw Exceptions::BadCast(
"Utils::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
81 return tmp_TMap->getTpetra_Map();
85 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
87 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> tmp_Map = rcpFromRef(M);
88 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
90 if (tmp_EMap != Teuchos::null) {
91 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
96 #endif // HAVE_XPETRA_EPETRAEXT
98 #ifdef HAVE_XPETRA_TPETRA
99 const RCP<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node>>& tmp_TMap =
101 if (tmp_TMap != Teuchos::null) {
102 RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> TMap = tmp_TMap->
getTpetra_Map();
103 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>::writeMapFile(fileName, *TMap);
106 #endif // HAVE_XPETRA_TPETRA
111 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
113 std::string mapfile =
"map_" + fileName;
114 Write(mapfile, *(vec.
getMap()));
116 RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> tmp_Vec = Teuchos::rcpFromRef(vec);
117 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
119 if (tmp_EVec != Teuchos::null) {
120 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
125 #endif // HAVE_XPETRA_EPETRA
127 #ifdef HAVE_XPETRA_TPETRA
128 const RCP<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& tmp_TVec =
130 if (tmp_TVec != Teuchos::null) {
131 RCP<const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> TVec = tmp_TVec->
getTpetra_MultiVector();
132 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>::writeDenseFile(fileName, TVec);
135 #endif // HAVE_XPETRA_TPETRA
137 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
140 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
142 std::string mapfile =
"map_" + fileName;
143 Write(mapfile, *(vec.
getMap()));
145 RCP<const Xpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> tmp_Vec = Teuchos::rcpFromRef(vec);
146 #ifdef HAVE_XPETRA_TPETRA
147 const RCP<const Xpetra::TpetraMultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>& tmp_TVec =
149 if (tmp_TVec != Teuchos::null) {
150 RCP<const Tpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> TVec = tmp_TVec->
getTpetra_MultiVector();
151 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>::writeDenseFile(fileName, TVec);
154 #endif // HAVE_XPETRA_TPETRA
156 throw Exceptions::RuntimeError(
"Xpetra cannot write MV<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node> when the underlying library is Epetra.");
159 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
162 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
164 std::string mapfile =
"map_" + fileName;
165 Write(mapfile, *(vec.
getMap()));
167 RCP<const Xpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> tmp_Vec = Teuchos::rcpFromRef(vec);
168 #ifdef HAVE_XPETRA_TPETRA
169 const RCP<const Xpetra::TpetraMultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>& tmp_TVec =
171 if (tmp_TVec != Teuchos::null) {
172 RCP<const Tpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> TVec = tmp_TVec->
getTpetra_MultiVector();
173 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>::writeDenseFile(fileName, TVec);
176 #endif // HAVE_XPETRA_TPETRA
178 throw Exceptions::RuntimeError(
"Xpetra cannot write MV<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node> when the underlying library is Epetra.");
181 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
184 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
186 Write(
"rowmap_" + fileName, *(Op.
getRowMap()));
192 Write(
"colmap_" + fileName, *(Op.
getColMap()));
196 RCP<const Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> tmp_CrsMtx = crsOp.
getCrsMatrix();
197 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
199 if (tmp_ECrsMtx != Teuchos::null) {
201 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
208 #ifdef HAVE_XPETRA_TPETRA
209 const RCP<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& tmp_TCrsMtx =
211 if (tmp_TCrsMtx != Teuchos::null) {
212 RCP<const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A = tmp_TCrsMtx->
getTpetra_CrsMatrix();
213 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>::writeSparseFile(fileName, A);
216 const RCP<const Xpetra::TpetraBlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& tmp_BlockCrs =
218 if (tmp_BlockCrs != Teuchos::null) {
219 std::ofstream outstream(fileName, std::ofstream::out);
220 Teuchos::FancyOStream ofs(Teuchos::rcpFromRef(outstream));
221 tmp_BlockCrs->getTpetra_BlockCrsMatrix()->describe(ofs, Teuchos::VERB_EXTREME);
225 #endif // HAVE_XPETRA_TPETRA
227 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
230 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
234 RCP<const Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> tmp_CrsMtx = crsOp.
getCrsMatrix();
236 ArrayRCP<const size_t> rowptr_RCP;
237 ArrayRCP<LocalOrdinal> rowptr2_RCP;
238 ArrayRCP<const LocalOrdinal> colind_RCP;
239 ArrayRCP<const Scalar> vals_RCP;
240 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
242 ArrayView<const size_t> rowptr = rowptr_RCP();
243 ArrayView<const LocalOrdinal> colind = colind_RCP();
244 ArrayView<const Scalar> vals = vals_RCP();
246 rowptr2_RCP.resize(rowptr.size());
247 ArrayView<LocalOrdinal> rowptr2 = rowptr2_RCP();
248 for (LocalOrdinal j = 0; j < rowptr.size(); j++)
249 rowptr2[j] = rowptr[j];
251 Teuchos::MatrixMarket::Raw::Writer<Scalar, LocalOrdinal> writer;
252 writer.writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
253 rowptr2, colind, vals,
254 rowptr.size() - 1, Op.
getColMap()->getLocalNumElements());
257 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
260 for (
size_t row = 0; row < Op.
Rows(); ++row) {
261 for (
size_t col = 0; col < Op.
Cols(); ++col) {
262 RCP<const Matrix> m = Op.
getMatrix(row, col);
263 if (m != Teuchos::null) {
265 "Sub block matrix (" << row <<
"," << col <<
") is not of type CrsMatrixWrap.");
275 for (
size_t row = 0; row < rangeMapExtractor->NumMaps(); ++row) {
276 RCP<const Map> map = rangeMapExtractor->getMap(row);
277 Write(
"subRangeMap_" + fileName +
toString(row) +
".m", *map);
279 Write(
"fullRangeMap_" + fileName +
".m", *(rangeMapExtractor->getFullMap()));
281 for (
size_t col = 0; col < domainMapExtractor->NumMaps(); ++col) {
282 RCP<const Map> map = domainMapExtractor->getMap(col);
283 Write(
"subDomainMap_" + fileName +
toString(col) +
".m", *map);
285 Write(
"fullDomainMap_" + fileName +
".m", *(domainMapExtractor->getFullMap()));
288 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
290 if (binary ==
false) {
293 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
294 Epetra_CrsMatrix* eA;
296 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
300 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
302 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A =
303 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
309 #ifdef HAVE_XPETRA_TPETRA
310 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
312 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
314 bool callFillComplete =
true;
316 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(fileName, comm, callFillComplete);
334 std::ifstream ifs(fileName.c_str(), std::ios::binary);
337 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
338 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
339 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
341 int myRank = comm->getRank();
343 GlobalOrdinal indexBase = 0;
346 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A;
349 Teuchos::Array<GlobalOrdinal> inds;
350 Teuchos::Array<Scalar> vals;
352 Teuchos::ArrayRCP<size_t> numEntriesPerRow(m, (
size_t)(0));
353 for (
int i = 0; i < m; i++) {
355 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
356 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
357 numEntriesPerRow[row] = rownnz;
358 for (
int j = 0; j < rownnz; j++) {
360 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
362 for (
int j = 0; j < rownnz; j++) {
364 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
371 ifs.seekg(0, ifs.beg);
373 ifs.read(reinterpret_cast<char*>(&junk),
sizeof(junk));
374 ifs.read(reinterpret_cast<char*>(&junk),
sizeof(junk));
375 ifs.read(reinterpret_cast<char*>(&junk),
sizeof(junk));
376 for (
int i = 0; i < m; i++) {
378 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
379 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
382 for (
int j = 0; j < rownnz; j++) {
384 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
385 inds[j] = Teuchos::as<GlobalOrdinal>(index);
387 for (
int j = 0; j < rownnz; j++) {
389 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
390 vals[j] = Teuchos::as<Scalar>(value);
392 A->insertGlobalValues(row, inds, vals);
396 Teuchos::ArrayRCP<size_t> numEntriesPerRow(0, (
size_t)(0));
400 A->fillComplete(domainMap, rangeMap);
405 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
408 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
409 Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>
415 const bool callFillComplete,
421 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> domain = (domainMap.is_null() ? rowMap : domainMap);
422 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> range = (rangeMap.is_null() ? rowMap : rangeMap);
425 if (binary ==
false) {
427 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
428 Epetra_CrsMatrix* eA;
434 if (colMap.is_null()) {
435 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
438 const Epetra_Map& epetraColMap = Map2EpetraMap(*colMap);
439 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
445 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
446 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> A =
447 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
454 #ifdef HAVE_XPETRA_TPETRA
455 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
456 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
457 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
459 const RCP<const map_type> tpetraRowMap = Map2TpetraMap(*rowMap);
460 RCP<const map_type> tpetraColMap = (colMap.is_null() ? Teuchos::null : Map2TpetraMap(*colMap));
461 const RCP<const map_type> tpetraRangeMap = (rangeMap.is_null() ? tpetraRowMap : Map2TpetraMap(*rangeMap));
462 const RCP<const map_type> tpetraDomainMap = (domainMap.is_null() ? tpetraRowMap : Map2TpetraMap(*domainMap));
464 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
465 callFillComplete, tolerant, debug);
482 auto tempA = Read(filename, lib, rowMap->getComm(), binary);
487 if (callFillComplete)
488 A->fillComplete(domainMap, rangeMap);
493 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
496 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
502 const bool callFillComplete,
506 TEUCHOS_TEST_FOR_EXCEPTION(rowMap.is_null(),
Exceptions::RuntimeError,
"Utils::ReadLocal() : rowMap cannot be null");
507 TEUCHOS_TEST_FOR_EXCEPTION(colMap.is_null(),
Exceptions::RuntimeError,
"Utils::ReadLocal() : colMap cannot be null");
513 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> domain = (domainMap.is_null() ? rowMap : domainMap);
514 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> range = (rangeMap.is_null() ? rowMap : rangeMap);
516 std::string rankFilename = filename +
"." + std::to_string(rowMap->getComm()->getSize()) +
"." + std::to_string(rowMap->getComm()->getRank());
517 RCP<matrix_type> A = rcp(
new crs_wrap_type(rowMap, colMap, 0));
519 if (binary ==
false) {
520 RCP<Teuchos::ParameterList> params = rcp(
new Teuchos::ParameterList());
521 params->set(
"Parse tolerantly", tolerant);
522 params->set(
"Debug mode", debug);
524 LocalOrdinal numRows = rowMap->getLocalNumElements();
525 LocalOrdinal numCols = colMap->getLocalNumElements();
527 ArrayRCP<LocalOrdinal> rowptr2_RCP;
528 ArrayRCP<LocalOrdinal> colind2_RCP;
529 ArrayRCP<Scalar> vals2_RCP;
531 Teuchos::MatrixMarket::Raw::Reader<Scalar, LocalOrdinal> reader;
532 reader.readFile(rowptr2_RCP, colind2_RCP, vals2_RCP,
536 RCP<crs_type> ACrs = Teuchos::rcp_dynamic_cast<crs_wrap_type>(A)->getCrsMatrix();
538 ArrayRCP<size_t> rowptr_RCP;
539 ArrayRCP<LocalOrdinal> colind_RCP;
540 ArrayRCP<Scalar> vals_RCP;
541 ACrs->allocateAllValues(colind2_RCP.size(), rowptr_RCP, colind_RCP, vals_RCP);
543 rowptr_RCP.assign(rowptr2_RCP.begin(), rowptr2_RCP.end());
544 colind_RCP = colind2_RCP;
545 vals_RCP = vals2_RCP;
547 ACrs->setAllValues(rowptr_RCP, colind_RCP, vals_RCP);
550 std::ifstream ifs = std::ifstream(rankFilename.c_str(), std::ios::binary);
554 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
555 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
556 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
558 TEUCHOS_ASSERT_EQUALITY(Teuchos::as<int>(rowMap->getLocalNumElements()), m);
560 Teuchos::ArrayRCP<size_t> rowptrRCP;
561 Teuchos::ArrayRCP<LocalOrdinal> indicesRCP;
562 Teuchos::ArrayRCP<Scalar> valuesRCP;
564 RCP<crs_type> ACrs = Teuchos::rcp_dynamic_cast<crs_wrap_type>(A)->getCrsMatrix();
566 ACrs->allocateAllValues(nnz, rowptrRCP, indicesRCP, valuesRCP);
568 Teuchos::ArrayView<size_t> rowptr = rowptrRCP();
569 Teuchos::ArrayView<LocalOrdinal> indices = indicesRCP();
570 Teuchos::ArrayView<Scalar> values = valuesRCP();
575 for (
int i = 0; i < m; i++) {
577 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
578 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
580 rowptr[row + 1] += rownnz;
581 ifs.seekg(
sizeof(
int) * rownnz +
sizeof(
double) * rownnz, ifs.cur);
583 for (
int i = 0; i < m; i++)
584 rowptr[i + 1] += rowptr[i];
585 TEUCHOS_ASSERT(Teuchos::as<int>(rowptr[m]) == nnz);
588 ifs.seekg(
sizeof(
int) * 3, ifs.beg);
591 for (
int i = 0; i < m; i++) {
593 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
594 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
595 size_t ptr = rowptr[row];
596 for (
int j = 0; j < rownnz; j++) {
598 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
599 indices[ptr] = Teuchos::as<LocalOrdinal>(index);
601 sorted = sorted & (indices[ptr - 1] < indices[ptr]);
605 for (
int j = 0; j < rownnz; j++) {
607 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
608 values[ptr] = Teuchos::as<Scalar>(value);
611 rowptr[row] += rownnz;
613 for (
int i = m; i > 0; i--)
614 rowptr[i] = rowptr[i - 1];
617 #ifdef HAVE_XPETRA_TPETRA
619 for (LocalOrdinal lclRow = 0; lclRow < m; lclRow++) {
620 size_t rowBegin = rowptr[lclRow];
621 size_t rowEnd = rowptr[lclRow + 1];
622 Tpetra::sort2(&indices[rowBegin], &indices[rowEnd], &values[rowBegin]);
626 TEUCHOS_ASSERT(sorted);
629 ACrs->setAllValues(rowptrRCP, indicesRCP, valuesRCP);
632 if (callFillComplete)
633 A->fillComplete(domainMap, rangeMap);
637 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
647 #ifdef HAVE_XPETRA_TPETRA
648 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
649 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
650 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
651 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
653 RCP<const map_type> temp =
toTpetra(map);
654 RCP<multivector_type> TMV = reader_type::readDenseFile(fileName, map->getComm(), temp,
false,
false, binary);
664 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
667 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
674 #ifdef HAVE_XPETRA_TPETRA
675 typedef Tpetra::CrsMatrix<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
676 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
677 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
678 typedef Tpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
680 RCP<const map_type> temp =
toTpetra(map);
681 RCP<multivector_type> TMV = reader_type::readDenseFile(fileName, map->getComm(), temp,
false,
false, binary);
682 RCP<Xpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> rmv =
Xpetra::toXpetra(TMV);
691 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
694 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
697 const RCP<
const Teuchos::Comm<int>>& comm,
702 #ifdef HAVE_XPETRA_TPETRA
703 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
704 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
706 RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> tMap = reader_type::readMapFile(fileName, comm,
false,
false, binary);
718 TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
721 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
723 size_t numBlocks = 2;
725 std::vector<RCP<const Map>> rangeMapVec;
726 for (
size_t row = 0; row < numBlocks; ++row) {
727 RCP<const Map> map = ReadMap(
"subRangeMap_" + fileName +
toString(row) +
".m", lib, comm);
728 rangeMapVec.push_back(map);
730 RCP<const Map> fullRangeMap = ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
732 std::vector<RCP<const Map>> domainMapVec;
733 for (
size_t col = 0; col < numBlocks; ++col) {
734 RCP<const Map> map = ReadMap(
"subDomainMap_" + fileName +
toString(col) +
".m", lib, comm);
735 domainMapVec.push_back(map);
737 RCP<const Map> fullDomainMap = ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
753 bool bRangeUseThyraStyleNumbering =
false;
760 RCP<const MapExtractor> rangeMapExtractor =
761 Teuchos::rcp(
new MapExtractor(fullRangeMap, rangeMapVec, bRangeUseThyraStyleNumbering));
764 bool bDomainUseThyraStyleNumbering =
false;
771 RCP<const MapExtractor> domainMapExtractor =
772 Teuchos::rcp(
new MapExtractor(fullDomainMap, domainMapVec, bDomainUseThyraStyleNumbering));
774 RCP<BlockedCrsMatrix> bOp = Teuchos::rcp(
new BlockedCrsMatrix(rangeMapExtractor, domainMapExtractor, 33));
777 for (
size_t row = 0; row < numBlocks; ++row) {
778 for (
size_t col = 0; col < numBlocks; ++col) {
779 RCP<const Map> rowSubMap = ReadMap(
"rowmap_" + fileName +
toString(row) +
toString(col) +
".m", lib, comm);
780 RCP<const Map> colSubMap = ReadMap(
"colmap_" + fileName +
toString(row) +
toString(col) +
".m", lib, comm);
781 RCP<const Map> domSubMap = ReadMap(
"domainmap_" + fileName +
toString(row) +
toString(col) +
".m", lib, comm);
782 RCP<const Map> ranSubMap = ReadMap(
"rangemap_" + fileName +
toString(row) +
toString(col) +
".m", lib, comm);
783 RCP<Matrix> mat = Read(fileName +
toString(row) +
toString(col) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
784 bOp->setMatrix(row, col, mat);
793 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
796 std::ostringstream buf;
static void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save block matrix to one file per block in Matrix Market format.
RCP< CrsMatrix > getCrsMatrix() const
std::string toString(Xpetra::UnderlyingLib lib)
Convert a Xpetra::UnderlyingLib to a std::string.
virtual const RCP< const Map > & getColMap() const
Returns the Map that describes the column distribution in this matrix. This might be null until fillC...
RCP< const MapExtractor > getDomainMapExtractor() const
Returns map extractor for domain map.
static RCP< Xpetra::MultiVector< LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node > > ReadMultiVectorLO(const std::string &fileName, const RCP< const Map > &map, const bool binary=false)
Read a MultiVector with Scalar=LocalOrdinal from file in Matrix Matrix or binary format.
static void WriteLOMV(const std::string &fileName, const Xpetra::MultiVector< LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save multivector with Scalar=LocalOrdinal to file in Matrix Market format.
virtual const RCP< const Map > & getRowMap() const
Returns the Map that describes the row distribution in this matrix.
Exception throws to report errors in the internal logical of the program.
static RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadBlockedCrsMatrix(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int >> &comm)
Read block matrix from one file per block in Matrix Market format.
virtual size_t Cols() const
number of column blocks
static void WriteGOMV(const std::string &fileName, const Xpetra::MultiVector< GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save multivector with Scalar=GlobalOrdinal to file in Matrix Market format.
Exception indicating invalid cast attempted.
static const RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > Map2TpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const =0
The Map describing the parallel distribution of this object.
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int >> &comm, LocalGlobal lg=Xpetra::GloballyDistributed)
Map constructor with Xpetra-defined contiguous uniform distribution.
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
RCP< const Epetra_CrsMatrix > getEpetra_CrsMatrix() const
virtual size_t Rows() const
number of row blocks
virtual const Teuchos::RCP< const map_type > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y...
RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getTpetra_Map() const
Get the underlying Tpetra map.
RCP< const MapExtractor > getRangeMapExtractor() const
Returns map extractor class for range map.
static const Epetra_Map & Map2EpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
Teuchos::RCP< Matrix > getMatrix(size_t r, size_t c) const
return block (r,c)
static RCP< Matrix > Build(const RCP< const Map > &rowMap)
RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_CrsMatrix() const
Get the underlying Tpetra matrix.
Concrete implementation of Xpetra::Matrix.
static void WriteLocal(const std::string &fileName, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Save local parts of matrix to files in Matrix Market format.
virtual const Teuchos::RCP< const map_type > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X...
RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_MultiVector() const
Get the underlying Tpetra multivector.
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map, const bool binary=false)
Read a MultiVector from file in Matrix Matrix or binary format.
static std::string toString(const T &what)
Little helper function to convert non-string types to strings.
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
Read/Write methods.
static RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int >> &comm, const bool binary=false)
static void Write(const std::string &fileName, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save matrix to file in Matrix Market format.
static RCP< Import< LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &source, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &target, const Teuchos::RCP< Teuchos::ParameterList > &plist=Teuchos::null)
Constructor specifying the number of non-zeros for all rows.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal, Node > > &graph)
Xpetra-specific matrix class.
static Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadLocal(const std::string &filename, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node >> rowMap, RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node >> colMap, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node >> domainMap=Teuchos::null, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node >> rangeMap=Teuchos::null, const bool callFillComplete=true, const bool binary=false, const bool tolerant=false, const bool debug=false)
Read matrix from local files in Matrix Market or binary format.
static Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Read(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int >> &comm, bool binary=false)
Read matrix from file in Matrix Market or binary format.
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)