48 #ifndef PACKAGES_XPETRA_SUP_UTILS_XPETRA_IO_HPP_
49 #define PACKAGES_XPETRA_SUP_UTILS_XPETRA_IO_HPP_
54 #ifdef HAVE_XPETRA_EPETRA
56 # include "Epetra_MpiComm.h"
60 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
61 #include <EpetraExt_MatrixMatrix.h>
62 #include <EpetraExt_RowMatrixOut.h>
63 #include <EpetraExt_MultiVectorOut.h>
64 #include <EpetraExt_CrsMatrixIn.h>
65 #include <EpetraExt_MultiVectorIn.h>
66 #include <EpetraExt_BlockMapIn.h>
69 #include <EpetraExt_BlockMapOut.h>
72 #ifdef HAVE_XPETRA_TPETRA
73 #include <MatrixMarket_Tpetra.hpp>
74 #include <Tpetra_RowMatrixTransposer.hpp>
75 #include <TpetraExt_MatrixMatrix.hpp>
76 #include <Xpetra_TpetraMultiVector.hpp>
77 #include <Xpetra_TpetraCrsMatrix.hpp>
78 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
81 #ifdef HAVE_XPETRA_EPETRA
87 #include "Xpetra_CrsMatrixWrap.hpp"
90 #include "Xpetra_Map.hpp"
91 #include "Xpetra_StridedMap.hpp"
92 #include "Xpetra_StridedMapFactory.hpp"
93 #include "Xpetra_MapExtractor.hpp"
96 #include <Teuchos_MatrixMarket_Raw_Writer.hpp>
103 #ifdef HAVE_XPETRA_EPETRA
105 template<
class SC,
class LO,
class GO,
class NO>
106 RCP<Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >
109 "Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap cannot be used with Scalar != double, LocalOrdinal != int, GlobalOrdinal != int");
115 inline RCP<Xpetra::CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> (RCP<Epetra_CrsMatrix> &epAB) {
122 RCP<Xpetra::CrsMatrix<SC,LO,GO,NO> > tmpC2 = Teuchos::rcp_implicit_cast<Xpetra::CrsMatrix<SC,LO,GO,NO> >(tmpC1);
129 template<
class SC,
class LO,
class GO,
class NO>
130 RCP<Xpetra::MultiVector<SC,LO,GO,NO> >
133 "Convert_Epetra_MultiVector_ToXpetra_MultiVector cannot be used with Scalar != double, LocalOrdinal != int, GlobalOrdinal != int");
139 inline RCP<Xpetra::MultiVector<double,int,int,Xpetra::EpetraNode> > Convert_Epetra_MultiVector_ToXpetra_MultiVector<double,int,int,Xpetra::EpetraNode> (RCP<Epetra_MultiVector> &epX) {
145 RCP<Xpetra::MultiVector<SC,LO,GO,NO >> tmp = Xpetra::toXpetra<GO,NO>(epX);
155 template <
class Scalar,
156 class LocalOrdinal = int,
157 class GlobalOrdinal = LocalOrdinal,
162 #undef XPETRA_IO_SHORT
167 #ifdef HAVE_XPETRA_EPETRA
184 if (xeMap == Teuchos::null)
185 throw Exceptions::BadCast(
"Utils::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
186 return xeMap->getEpetra_Map();
191 #ifdef HAVE_XPETRA_TPETRA
213 if (tmp_TMap == Teuchos::null)
214 throw Exceptions::BadCast(
"Utils::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
215 return tmp_TMap->getTpetra_Map();
225 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
227 if (tmp_EMap != Teuchos::null) {
228 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
235 #ifdef HAVE_XPETRA_TPETRA
237 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> >(tmp_Map);
238 if (tmp_TMap != Teuchos::null) {
240 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
251 std::string mapfile =
"map_" + fileName;
255 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
257 if (tmp_EVec != Teuchos::null) {
258 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
265 #ifdef HAVE_XPETRA_TPETRA
267 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_Vec);
268 if (tmp_TVec != Teuchos::null) {
270 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
275 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
294 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
296 if (tmp_ECrsMtx != Teuchos::null) {
298 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
305 #ifdef HAVE_XPETRA_TPETRA
307 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_CrsMtx);
308 if (tmp_TCrsMtx != Teuchos::null) {
310 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
315 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
329 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
337 for (
size_t j = 0; j<rowptr.
size(); j++)
338 rowptr2[j] = rowptr[j];
341 writer.
writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
357 for (
size_t r = 0; r < Op.
Rows(); ++r) {
358 for (
size_t c = 0; c < Op.
Cols(); ++c) {
360 if(m != Teuchos::null) {
362 "Sub block matrix (" << r <<
"," << c <<
") is not of type CrsMatrixWrap.");
372 for(
size_t r = 0; r < rangeMapExtractor->NumMaps(); ++r) {
374 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", *map);
376 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
378 for(
size_t c = 0; c < domainMapExtractor->NumMaps(); ++c) {
380 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", *map);
382 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
387 if (binary ==
false) {
390 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
393 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
400 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
406 #ifdef HAVE_XPETRA_TPETRA
407 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
409 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
411 bool callFillComplete =
true;
431 std::ifstream ifs(fileName.c_str(), std::ios::binary);
434 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
435 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
436 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
438 int myRank = comm->getRank();
450 for (
int i = 0; i < m; i++) {
452 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
453 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
454 numEntriesPerRow[i] = rownnz;
455 for (
int j = 0; j < rownnz; j++) {
457 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
459 for (
int j = 0; j < rownnz; j++) {
461 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
468 ifs.seekg(0, ifs.beg);
470 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(junk));
471 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(junk));
472 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(junk));
473 for (
int i = 0; i < m; i++) {
475 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
476 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
479 for (
int j = 0; j < rownnz; j++) {
481 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
482 inds[j] = Teuchos::as<GlobalOrdinal>(index);
484 for (
int j = 0; j < rownnz; j++) {
486 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
487 vals[j] = Teuchos::as<Scalar>(value);
489 A->insertGlobalValues(row, inds, vals);
493 A->fillComplete(domainMap, rangeMap);
508 Read(
const std::string& filename,
513 const bool callFillComplete =
true,
514 const bool binary =
false,
515 const bool tolerant =
false,
516 const bool debug =
false) {
523 if (binary ==
false) {
525 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
532 if (colMap.is_null()) {
533 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
537 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
545 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
552 #ifdef HAVE_XPETRA_TPETRA
553 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
554 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
555 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
562 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
563 callFillComplete, tolerant, debug);
580 std::ifstream ifs(filename.c_str(), std::ios::binary);
583 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
584 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
585 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
598 for (
int i = 0; i < m; i++) {
600 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
601 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
604 for (
int j = 0; j < rownnz; j++) {
606 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
607 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
609 for (
int j = 0; j < rownnz; j++) {
611 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
612 vals[j] = Teuchos::as<SC>(value);
615 A->insertGlobalValues(rowElements[row], inds, vals);
617 A->fillComplete(domainMap, rangeMap);
633 #ifdef HAVE_XPETRA_TPETRA
634 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
635 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
636 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
637 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
657 #ifdef HAVE_XPETRA_TPETRA
658 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
659 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
687 size_t numBlocks = 2;
689 std::vector<RCP<const XpMap> > rgMapVec;
690 for(
size_t r = 0; r < numBlocks; ++r) {
691 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
692 rgMapVec.push_back(map);
694 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
696 std::vector<RCP<const XpMap> > doMapVec;
697 for(
size_t c = 0; c < numBlocks; ++c) {
698 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
699 doMapVec.push_back(map);
701 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
717 bool bRangeUseThyraStyleNumbering =
false;
725 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
729 bool bDomainUseThyraStyleNumbering =
false;
737 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
742 for (
size_t r = 0; r < numBlocks; ++r) {
743 for (
size_t c = 0; c < numBlocks; ++c) {
744 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
745 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
746 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
747 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
748 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
750 bOp->setMatrix(r, c, mat);
763 std::ostringstream buf;
770 #ifdef HAVE_XPETRA_EPETRA
780 template <
class Scalar>
787 #ifdef HAVE_XPETRA_EPETRA
792 if (xeMap == Teuchos::null)
793 throw Exceptions::BadCast(
"IO::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
794 return xeMap->getEpetra_Map();
799 #ifdef HAVE_XPETRA_TPETRA
804 if (tmp_TMap == Teuchos::null)
805 throw Exceptions::BadCast(
"IO::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
806 return tmp_TMap->getTpetra_Map();
816 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
818 if (tmp_EMap != Teuchos::null) {
819 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
826 #ifdef HAVE_XPETRA_TPETRA
827 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
828 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
832 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> >(tmp_Map);
833 if (tmp_TMap != Teuchos::null) {
835 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
845 std::string mapfile =
"map_" + fileName;
849 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
851 if (tmp_EVec != Teuchos::null) {
852 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
859 #ifdef HAVE_XPETRA_TPETRA
860 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
861 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
865 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_Vec);
866 if (tmp_TVec != Teuchos::null) {
868 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
874 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
894 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
896 if (tmp_ECrsMtx != Teuchos::null) {
898 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
905 #ifdef HAVE_XPETRA_TPETRA
906 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
907 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
911 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_CrsMtx);
912 if (tmp_TCrsMtx != Teuchos::null) {
914 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
920 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
934 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
942 for (
size_t j = 0; j<rowptr.
size(); j++)
943 rowptr2[j] = rowptr[j];
946 writer.
writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
961 for (
size_t r = 0; r < Op.
Rows(); ++r) {
962 for (
size_t c = 0; c < Op.
Cols(); ++c) {
964 if(m != Teuchos::null) {
966 "Sub block matrix (" << r <<
"," << c <<
") is not of type CrsMatrixWrap.");
976 for(
size_t r = 0; r < rangeMapExtractor->NumMaps(); ++r) {
978 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", *map);
980 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
982 for(
size_t c = 0; c < domainMapExtractor->NumMaps(); ++c) {
984 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", *map);
986 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
991 if (binary ==
false) {
994 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
997 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
1004 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1010 #ifdef HAVE_XPETRA_TPETRA
1011 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1012 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1015 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1017 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1019 bool callFillComplete =
true;
1040 std::ifstream ifs(fileName.c_str(), std::ios::binary);
1043 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
1044 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
1045 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
1047 int myRank = comm->getRank();
1060 for (
int i = 0; i < m; i++) {
1062 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1063 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1064 numEntriesPerRow[i] = rownnz;
1065 for (
int j = 0; j < rownnz; j++) {
1067 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1069 for (
int j = 0; j < rownnz; j++) {
1071 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1078 ifs.seekg(0, ifs.beg);
1080 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(junk));
1081 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(junk));
1082 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(junk));
1083 for (
int i = 0; i < m; i++) {
1085 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1086 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1089 for (
int j = 0; j < rownnz; j++) {
1091 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1092 inds[j] = Teuchos::as<GlobalOrdinal>(index);
1094 for (
int j = 0; j < rownnz; j++) {
1096 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1097 vals[j] = Teuchos::as<Scalar>(value);
1099 A->insertGlobalValues(row, inds, vals);
1103 A->fillComplete(domainMap, rangeMap);
1122 const bool callFillComplete =
true,
1123 const bool binary =
false,
1124 const bool tolerant =
false,
1125 const bool debug =
false) {
1132 if (binary ==
false) {
1134 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1141 if (colMap.is_null()) {
1142 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
1146 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
1154 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1161 #ifdef HAVE_XPETRA_TPETRA
1162 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1163 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1166 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1167 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1168 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1175 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
1176 callFillComplete, tolerant, debug);
1194 std::ifstream ifs(filename.c_str(), std::ios::binary);
1197 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
1198 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
1199 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
1212 for (
int i = 0; i < m; i++) {
1214 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1215 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1218 for (
int j = 0; j < rownnz; j++) {
1220 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1221 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
1223 for (
int j = 0; j < rownnz; j++) {
1225 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1226 vals[j] = Teuchos::as<Scalar>(value);
1229 A->insertGlobalValues(rowElements[row], inds, vals);
1231 A->fillComplete(domainMap, rangeMap);
1246 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1248 EpetraExt::MatrixMarketFileToMultiVector(fileName.c_str(),
toEpetra(map), MV);
1250 return Convert_Epetra_MultiVector_ToXpetra_MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(MVrcp);
1255 #ifdef HAVE_XPETRA_TPETRA
1256 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1257 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1260 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1261 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1262 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1263 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
1286 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1288 int rv = EpetraExt::MatrixMarketFileToMap(fileName.c_str(), *(
Xpetra::toEpetra(comm)), eMap);
1293 return Xpetra::toXpetra<int,Node>(*eMap1);
1298 #ifdef HAVE_XPETRA_TPETRA
1299 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1300 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1303 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1304 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1334 size_t numBlocks = 2;
1336 std::vector<RCP<const XpMap> > rgMapVec;
1337 for(
size_t r = 0; r < numBlocks; ++r) {
1338 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
1339 rgMapVec.push_back(map);
1341 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
1343 std::vector<RCP<const XpMap> > doMapVec;
1344 for(
size_t c = 0; c < numBlocks; ++c) {
1345 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
1346 doMapVec.push_back(map);
1348 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
1364 bool bRangeUseThyraStyleNumbering =
false;
1372 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
1375 bool bDomainUseThyraStyleNumbering =
false;
1382 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
1387 for (
size_t r = 0; r < numBlocks; ++r) {
1388 for (
size_t c = 0; c < numBlocks; ++c) {
1389 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1390 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1391 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1392 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1393 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
1395 bOp->setMatrix(r, c, mat);
1399 bOp->fillComplete();
1407 std::ostringstream buf;
1417 #define XPETRA_IO_SHORT
void resize(const size_type n, const T &val=T())
void resize(size_type new_size, const value_type &x=value_type())
void writeFile(const std::string &filename, const ArrayView< const OrdinalType > &rowptr, const ArrayView< const OrdinalType > &colind, const ArrayView< const ScalarType > &values, const OrdinalType numRows, const OrdinalType numCols)
virtual size_t Rows() const
number of row blocks
RCP< const MapExtractor > getDomainMapExtractor() const
Returns map extractor for domain map.
RCP< const MapExtractor > getRangeMapExtractor() const
Returns map extractor class for range map.
virtual size_t Cols() const
number of column blocks
Teuchos::RCP< Matrix > getMatrix(size_t r, size_t c) const
return block (r,c)
Concrete implementation of Xpetra::Matrix.
RCP< CrsMatrix > getCrsMatrix() const
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const =0
The Map describing the parallel distribution of this object.
Exception indicating invalid cast attempted.
Exception throws to report errors in the internal logical of the program.
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.
static void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save 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.
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.
static RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
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 const Epetra_Map & Map2EpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
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 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 void Write(const std::string &fileName, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save vector to file in Matrix Market 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.
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadMultiVector(const std::string &fileName, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
Xpetra utility class containing IO routines to read/write vectors, matrices etc...
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, 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.
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.
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< 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.
static void Write(const std::string &fileName, const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
Save vector to file in Matrix Market 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.
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, const bool &writeAllMaps=false)
Save matrix to file in Matrix Market format.
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.
static void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save matrix to file in Matrix Market format.
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map)
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.
static RCP< Matrix > Build(const RCP< const Map > &rowMap)
Xpetra-specific matrix class.
virtual const RCP< const Map > & getRowMap() const
Returns the Map that describes the row distribution in this matrix.
virtual const RCP< const Map > & getColMap() const
Returns the Map that describes the column distribution in this matrix. This might be null until fillC...
virtual Teuchos::RCP< const Map > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y....
virtual Teuchos::RCP< const Map > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X....
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
std::string toString(const T &t)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal, Node > > &graph)
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)
RCP< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &)
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
RCP< Xpetra::MultiVector< SC, LO, GO, NO > > Convert_Epetra_MultiVector_ToXpetra_MultiVector(RCP< Epetra_MultiVector > &epX)