48 #ifndef PACKAGES_XPETRA_SUP_UTILS_XPETRA_IO_HPP_ 49 #define PACKAGES_XPETRA_SUP_UTILS_XPETRA_IO_HPP_ 53 #ifdef HAVE_XPETRA_EPETRA 55 # include "Epetra_MpiComm.h" 59 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 60 #include <EpetraExt_MatrixMatrix.h> 61 #include <EpetraExt_RowMatrixOut.h> 62 #include <EpetraExt_MultiVectorOut.h> 63 #include <EpetraExt_CrsMatrixIn.h> 64 #include <EpetraExt_MultiVectorIn.h> 65 #include <EpetraExt_BlockMapIn.h> 68 #include <EpetraExt_BlockMapOut.h> 71 #ifdef HAVE_XPETRA_TPETRA 72 #include <MatrixMarket_Tpetra.hpp> 73 #include <Tpetra_RowMatrixTransposer.hpp> 74 #include <TpetraExt_MatrixMatrix.hpp> 80 #ifdef HAVE_XPETRA_EPETRA 100 #ifdef HAVE_XPETRA_EPETRA 102 template<
class SC,
class LO,
class GO,
class NO>
103 RCP<Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >
106 "Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap cannot be used with Scalar != double, LocalOrdinal != int, GlobalOrdinal != int");
107 return Teuchos::null;
112 inline RCP<Xpetra::CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> (RCP<Epetra_CrsMatrix> &epAB) {
130 template <
class Scalar,
131 class LocalOrdinal = int,
132 class GlobalOrdinal = LocalOrdinal,
137 #undef XPETRA_IO_SHORT 142 #ifdef HAVE_XPETRA_EPETRA 159 if (xeMap == Teuchos::null)
160 throw Exceptions::BadCast(
"Utils::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
161 return xeMap->getEpetra_Map();
166 #ifdef HAVE_XPETRA_TPETRA 188 if (tmp_TMap == Teuchos::null)
189 throw Exceptions::BadCast(
"Utils::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
190 return tmp_TMap->getTpetra_Map();
199 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > tmp_Map = rcpFromRef(M);
200 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 202 if (tmp_EMap != Teuchos::null) {
203 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
208 #endif // HAVE_XPETRA_EPETRAEXT 210 #ifdef HAVE_XPETRA_TPETRA 211 const RCP<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> > &tmp_TMap =
213 if (tmp_TMap != Teuchos::null) {
214 RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > TMap = tmp_TMap->
getTpetra_Map();
215 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
218 #endif // HAVE_XPETRA_TPETRA 226 std::string mapfile =
"map_" + fileName;
229 RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > tmp_Vec = Teuchos::rcpFromRef(vec);
230 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 232 if (tmp_EVec != Teuchos::null) {
233 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
238 #endif // HAVE_XPETRA_EPETRA 240 #ifdef HAVE_XPETRA_TPETRA 241 const RCP<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > &tmp_TVec =
243 if (tmp_TVec != Teuchos::null) {
244 RCP<const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > TVec = tmp_TVec->
getTpetra_MultiVector();
245 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
248 #endif // HAVE_XPETRA_TPETRA 250 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
265 RCP<const Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > tmp_CrsMtx = crsOp.
getCrsMatrix();
266 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 268 if (tmp_ECrsMtx != Teuchos::null) {
270 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
277 #ifdef HAVE_XPETRA_TPETRA 278 const RCP<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& tmp_TCrsMtx =
280 if (tmp_TCrsMtx != Teuchos::null) {
281 RCP<const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A = tmp_TCrsMtx->
getTpetra_CrsMatrix();
282 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
285 #endif // HAVE_XPETRA_TPETRA 287 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
300 for (
size_t r = 0; r < Op.
Rows(); ++r) {
301 for (
size_t c = 0; c < Op.
Cols(); ++c) {
303 if(m != Teuchos::null) {
305 "Sub block matrix (" << r <<
"," << c <<
") is not of type CrsMatrixWrap.");
315 for(
size_t r = 0; r < rangeMapExtractor->NumMaps(); ++r) {
316 RCP<const XpMap> map = rangeMapExtractor->getMap(r);
317 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", *map);
319 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
321 for(
size_t c = 0; c < domainMapExtractor->NumMaps(); ++c) {
322 RCP<const XpMap> map = domainMapExtractor->getMap(c);
323 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", *map);
325 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
329 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) {
330 if (binary ==
false) {
333 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 334 Epetra_CrsMatrix *eA;
336 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
340 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
342 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A =
343 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
349 #ifdef HAVE_XPETRA_TPETRA 350 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
352 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
355 Teuchos::ParameterList pl = Teuchos::ParameterList();
356 RCP<Node> node = rcp(
new Node(pl));
357 bool callFillComplete =
true;
359 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(fileName, comm, node, callFillComplete);
377 std::ifstream ifs(fileName.c_str(), std::ios::binary);
380 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
381 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
382 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
384 int myRank = comm->getRank();
394 Teuchos::Array<GlobalOrdinal> inds;
395 Teuchos::Array<Scalar> vals;
396 for (
int i = 0; i < m; i++) {
398 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
399 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
402 for (
int j = 0; j < rownnz; j++) {
404 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
405 inds[j] = Teuchos::as<GlobalOrdinal>(index);
407 for (
int j = 0; j < rownnz; j++) {
409 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
410 vals[j] = Teuchos::as<SC>(value);
412 A->insertGlobalValues(row, inds, vals);
416 A->fillComplete(domainMap, rangeMap);
421 return Teuchos::null;
430 static Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
431 Read(
const std::string& filename,
436 const bool callFillComplete =
true,
437 const bool binary =
false,
438 const bool tolerant =
false,
439 const bool debug =
false) {
442 RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > domain = (domainMap.is_null() ? rowMap : domainMap);
443 RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > range = (rangeMap .is_null() ? rowMap : rangeMap);
446 if (binary ==
false) {
448 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 449 Epetra_CrsMatrix *eA;
455 if (colMap.is_null()) {
456 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
460 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
466 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
467 RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A =
468 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
475 #ifdef HAVE_XPETRA_TPETRA 476 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
477 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
478 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
480 const RCP<const map_type> tpetraRowMap =
Map2TpetraMap(*rowMap);
481 RCP<const map_type> tpetraColMap = (colMap.is_null() ? Teuchos::null :
Map2TpetraMap(*colMap));
482 const RCP<const map_type> tpetraRangeMap = (rangeMap.is_null() ? tpetraRowMap :
Map2TpetraMap(*rangeMap));
483 const RCP<const map_type> tpetraDomainMap = (domainMap.is_null() ? tpetraRowMap :
Map2TpetraMap(*domainMap));
485 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
486 callFillComplete, tolerant, debug);
503 std::ifstream ifs(filename.c_str(), std::ios::binary);
506 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
507 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
508 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
514 Teuchos::ArrayView<const GlobalOrdinal> rowElements = rowMap->getNodeElementList();
515 Teuchos::ArrayView<const GlobalOrdinal> colElements = colMap->getNodeElementList();
517 Teuchos::Array<GlobalOrdinal> inds;
518 Teuchos::Array<Scalar> vals;
519 for (
int i = 0; i < m; i++) {
521 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
522 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
525 for (
int j = 0; j < rownnz; j++) {
527 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
528 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
530 for (
int j = 0; j < rownnz; j++) {
532 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
533 vals[j] = Teuchos::as<SC>(value);
535 A->insertGlobalValues(rowElements[row], inds, vals);
537 A->fillComplete(domainMap, rangeMap);
541 return Teuchos::null;
546 static RCP<MultiVector>
ReadMultiVector (
const std::string& fileName,
const RCP<const Map>& map) {
553 #ifdef HAVE_XPETRA_TPETRA 554 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
555 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
556 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
557 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
559 RCP<const map_type> temp =
toTpetra(map);
560 RCP<multivector_type> TMV = reader_type::readDenseFile(fileName,map->getComm(),map->getNode(),temp);
570 return Teuchos::null;
577 #ifdef HAVE_XPETRA_TPETRA 578 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
579 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
581 RCP<Node> node = rcp(
new Node());
583 RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > tMap = reader_type::readMapFile(fileName, comm, node);
595 return Teuchos::null;
609 size_t numBlocks = 2;
611 std::vector<RCP<const XpMap> > rgMapVec;
612 for(
size_t r = 0; r < numBlocks; ++r) {
613 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
614 rgMapVec.push_back(map);
616 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
618 std::vector<RCP<const XpMap> > doMapVec;
619 for(
size_t c = 0; c < numBlocks; ++c) {
620 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
621 doMapVec.push_back(map);
623 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
639 bool bRangeUseThyraStyleNumbering =
false;
646 RCP<const XpMapExtractor> rangeMapExtractor =
647 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
651 bool bDomainUseThyraStyleNumbering =
false;
658 RCP<const XpMapExtractor> domainMapExtractor =
659 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
661 RCP<XpBlockedCrsMat> bOp = Teuchos::rcp(
new XpBlockedCrsMat(rangeMapExtractor,domainMapExtractor,33));
664 for (
size_t r = 0; r < numBlocks; ++r) {
665 for (
size_t c = 0; c < numBlocks; ++c) {
666 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
667 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
668 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
669 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
670 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
672 bOp->setMatrix(r, c, mat);
685 std::ostringstream buf;
692 #ifdef HAVE_XPETRA_EPETRA 702 template <
class Scalar>
709 #ifdef HAVE_XPETRA_EPETRA 714 if (xeMap == Teuchos::null)
715 throw Exceptions::BadCast(
"IO::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
716 return xeMap->getEpetra_Map();
721 #ifdef HAVE_XPETRA_TPETRA 726 if (tmp_TMap == Teuchos::null)
727 throw Exceptions::BadCast(
"IO::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
728 return tmp_TMap->getTpetra_Map();
737 RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > tmp_Map = rcpFromRef(M);
738 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 740 if (tmp_EMap != Teuchos::null) {
741 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
746 #endif // HAVE_XPETRA_EPETRA 748 #ifdef HAVE_XPETRA_TPETRA 749 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 750 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 753 const RCP<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> > &tmp_TMap =
755 if (tmp_TMap != Teuchos::null) {
756 RCP<const Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > TMap = tmp_TMap->
getTpetra_Map();
757 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
761 #endif // HAVE_XPETRA_TPETRA 767 std::string mapfile =
"map_" + fileName;
770 RCP<const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > tmp_Vec = Teuchos::rcpFromRef(vec);
771 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 773 if (tmp_EVec != Teuchos::null) {
774 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
779 #endif // HAVE_XPETRA_EPETRAEXT 781 #ifdef HAVE_XPETRA_TPETRA 782 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 783 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 786 const RCP<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > &tmp_TVec =
788 if (tmp_TVec != Teuchos::null) {
789 RCP<const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > TVec = tmp_TVec->
getTpetra_MultiVector();
790 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
794 #endif // HAVE_XPETRA_TPETRA 796 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
812 RCP<const Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > tmp_CrsMtx = crsOp.
getCrsMatrix();
813 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 815 if (tmp_ECrsMtx != Teuchos::null) {
817 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
822 #endif // endif HAVE_XPETRA_EPETRA 824 #ifdef HAVE_XPETRA_TPETRA 825 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 826 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 829 const RCP<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& tmp_TCrsMtx =
831 if (tmp_TCrsMtx != Teuchos::null) {
832 RCP<const Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A = tmp_TCrsMtx->
getTpetra_CrsMatrix();
833 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
837 #endif // HAVE_XPETRA_TPETRA 839 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
852 for (
size_t r = 0; r < Op.
Rows(); ++r) {
853 for (
size_t c = 0; c < Op.
Cols(); ++c) {
855 if(m != Teuchos::null) {
857 "Sub block matrix (" << r <<
"," << c <<
") is not of type CrsMatrixWrap.");
867 for(
size_t r = 0; r < rangeMapExtractor->NumMaps(); ++r) {
868 RCP<const XpMap> map = rangeMapExtractor->getMap(r);
869 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", *map);
871 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
873 for(
size_t c = 0; c < domainMapExtractor->NumMaps(); ++c) {
874 RCP<const XpMap> map = domainMapExtractor->getMap(c);
875 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", *map);
877 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
881 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) {
882 if (binary ==
false) {
885 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 886 Epetra_CrsMatrix *eA;
888 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
892 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
894 RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A =
895 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
901 #ifdef HAVE_XPETRA_TPETRA 902 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 903 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 906 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
908 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
911 Teuchos::ParameterList pl = Teuchos::ParameterList();
912 RCP<Node> node = rcp(
new Node(pl));
913 bool callFillComplete =
true;
915 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(fileName, comm, node, callFillComplete);
934 std::ifstream ifs(fileName.c_str(), std::ios::binary);
937 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
938 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
939 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
941 int myRank = comm->getRank();
951 Teuchos::Array<GlobalOrdinal> inds;
952 Teuchos::Array<Scalar> vals;
953 for (
int i = 0; i < m; i++) {
955 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
956 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
959 for (
int j = 0; j < rownnz; j++) {
961 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
962 inds[j] = Teuchos::as<GlobalOrdinal>(index);
964 for (
int j = 0; j < rownnz; j++) {
966 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
967 vals[j] = Teuchos::as<Scalar>(value);
969 A->insertGlobalValues(row, inds, vals);
973 A->fillComplete(domainMap, rangeMap);
978 return Teuchos::null;
987 static Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
Read(
const std::string& filename,
992 const bool callFillComplete =
true,
993 const bool binary =
false,
994 const bool tolerant =
false,
995 const bool debug =
false) {
998 RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > domain = (domainMap.is_null() ? rowMap : domainMap);
999 RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > range = (rangeMap .is_null() ? rowMap : rangeMap);
1002 if (binary ==
false) {
1004 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 1005 Epetra_CrsMatrix *eA;
1011 if (colMap.is_null()) {
1012 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
1016 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
1022 RCP<Epetra_CrsMatrix> tmpA = rcp(eA);
1023 RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A =
1024 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1031 #ifdef HAVE_XPETRA_TPETRA 1032 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 1033 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 1036 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1037 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1038 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1040 const RCP<const map_type> tpetraRowMap =
Map2TpetraMap(*rowMap);
1041 RCP<const map_type> tpetraColMap = (colMap.is_null() ? Teuchos::null :
Map2TpetraMap(*colMap));
1042 const RCP<const map_type> tpetraRangeMap = (rangeMap.is_null() ? tpetraRowMap :
Map2TpetraMap(*rangeMap));
1043 const RCP<const map_type> tpetraDomainMap = (domainMap.is_null() ? tpetraRowMap :
Map2TpetraMap(*domainMap));
1045 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
1046 callFillComplete, tolerant, debug);
1064 std::ifstream ifs(filename.c_str(), std::ios::binary);
1067 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
1068 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
1069 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
1075 Teuchos::ArrayView<const GlobalOrdinal> rowElements = rowMap->getNodeElementList();
1076 Teuchos::ArrayView<const GlobalOrdinal> colElements = colMap->getNodeElementList();
1078 Teuchos::Array<GlobalOrdinal> inds;
1079 Teuchos::Array<Scalar> vals;
1080 for (
int i = 0; i < m; i++) {
1082 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
1083 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
1084 inds.resize(rownnz);
1085 vals.resize(rownnz);
1086 for (
int j = 0; j < rownnz; j++) {
1088 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
1089 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
1091 for (
int j = 0; j < rownnz; j++) {
1093 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
1094 vals[j] = Teuchos::as<Scalar>(value);
1096 A->insertGlobalValues(rowElements[row], inds, vals);
1098 A->fillComplete(domainMap, rangeMap);
1102 return Teuchos::null;
1113 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 1114 Epetra_MultiVector * MV;
1115 EpetraExt::MatrixMarketFileToMultiVector(fileName.c_str(),
toEpetra(map), MV);
1116 return Xpetra::toXpetra<int,Node>(rcp(MV));
1121 #ifdef HAVE_XPETRA_TPETRA 1122 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 1123 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 1126 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1127 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1128 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1129 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
1131 RCP<const map_type> temp =
toTpetra(map);
1132 RCP<multivector_type> TMV = reader_type::readDenseFile(fileName,map->getComm(),map->getNode(),temp);
1133 RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > rmv =
Xpetra::toXpetra(TMV);
1143 return Teuchos::null;
1148 static RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> >
ReadMap (
const std::string& fileName,
Xpetra::UnderlyingLib lib,
const RCP<
const Teuchos::Comm<int> >& comm) {
1152 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT) 1154 int rv = EpetraExt::MatrixMarketFileToMap(fileName.c_str(), *(
Xpetra::toEpetra(comm)), eMap);
1158 RCP<Epetra_Map> eMap1 = rcp(
new Epetra_Map(*eMap));
1159 return Xpetra::toXpetra<int,Node>(*eMap1);
1164 #ifdef HAVE_XPETRA_TPETRA 1165 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \ 1166 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT)))) 1169 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1170 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1172 RCP<Node> node = rcp(
new Node());
1174 RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > tMap = reader_type::readMapFile(fileName, comm, node);
1187 return Teuchos::null;
1202 size_t numBlocks = 2;
1204 std::vector<RCP<const XpMap> > rgMapVec;
1205 for(
size_t r = 0; r < numBlocks; ++r) {
1206 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
1207 rgMapVec.push_back(map);
1209 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
1211 std::vector<RCP<const XpMap> > doMapVec;
1212 for(
size_t c = 0; c < numBlocks; ++c) {
1213 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
1214 doMapVec.push_back(map);
1216 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
1232 bool bRangeUseThyraStyleNumbering =
false;
1239 RCP<const XpMapExtractor> rangeMapExtractor =
1240 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
1243 bool bDomainUseThyraStyleNumbering =
false;
1249 RCP<const XpMapExtractor> domainMapExtractor =
1250 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
1252 RCP<XpBlockedCrsMat> bOp = Teuchos::rcp(
new XpBlockedCrsMat(rangeMapExtractor,domainMapExtractor,33));
1255 for (
size_t r = 0; r < numBlocks; ++r) {
1256 for (
size_t c = 0; c < numBlocks; ++c) {
1257 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1258 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1259 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1260 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1261 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
1263 bOp->setMatrix(r, c, mat);
1267 bOp->fillComplete();
1275 std::ostringstream buf;
1280 #endif // HAVE_XPETRA_EPETRA 1285 #define XPETRA_IO_SHORT std::string toString(Xpetra::UnderlyingLib lib)
Convert a Xpetra::UnderlyingLib to a std::string.
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, const Teuchos::RCP< Node > &node=defaultArgNode())
Map constructor with Xpetra-defined contiguous uniform distribution.
static Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Read(const std::string &filename, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > rowMap, RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > colMap=Teuchos::null, 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 file in Matrix Market or binary format.
RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getTpetra_Map() const
Get the underlying Tpetra map.
static Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Read(const std::string &filename, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > rowMap, RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > colMap=Teuchos::null, 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 file in Matrix Market or binary format.
virtual const RCP< const Map > & getRowMap() const
Returns the Map that describes the row distribution in this matrix.
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
Read/Write methods.
static void Write(const std::string &fileName, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Save matrix to file in Matrix Market format.
Exception throws to report errors in the internal logical of the program.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal, Node > > &graph)
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 matrix to file in Matrix Market format.
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.
RCP< const MapExtractor > getRangeMapExtractor() const
Returns map extractor class for range map.
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 const RCP< const Map > & getColMap() const
Returns the Map that describes the column distribution in this matrix. This might be null until fillC...
Exception indicating invalid cast attempted.
static void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Save matrix to file in Matrix Market format.
static RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
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.
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map)
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const=0
The Map describing the parallel distribution of this object.
RCP< CrsMatrix > getCrsMatrix() const
static std::string toString(const T &what)
Little helper function to convert non-string types to strings.
RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_CrsMatrix() const
Get the underlying Tpetra matrix.
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadMultiVector(const std::string &fileName, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)
RCP< const Epetra_CrsMatrix > getEpetra_CrsMatrix() const
RCP< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &epAB)
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.
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.
Concrete implementation of Xpetra::Matrix.
Teuchos::RCP< Matrix > getMatrix(size_t r, size_t c) const
return block (r,c)
virtual Teuchos::RCP< const Map > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y...
RCP< const MapExtractor > getDomainMapExtractor() const
Returns map extractor for domain map.
static RCP< Matrix > Build(const RCP< const Map > &rowMap, size_t maxNumEntriesPerRow, Xpetra::ProfileType pftype=Xpetra::DynamicProfile)
Constructor specifying the number of non-zeros for all rows.
static RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_MultiVector() const
Get the underlying Tpetra multivector.
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.
virtual size_t Rows() const
number of row blocks
virtual size_t Cols() const
number of column blocks
static void Write(const std::string &fileName, const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Save matrix to file in Matrix Market format.
Xpetra utility class containing IO routines to read/write vectors, matrices etc...
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
Read/Write methods.
static void Write(const std::string &fileName, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save vector to file in Matrix Market format.
static void Write(const std::string &fileName, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save vector to file in Matrix Market format.
virtual Teuchos::RCP< const Map > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X...
Xpetra-specific matrix class.
static void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Save matrix to file in Matrix Market format.
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 matrix to file in Matrix Market format.
static std::string toString(const T &what)
Little helper function to convert non-string types to strings.