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");
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");
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());
233 #endif // HAVE_XPETRA_EPETRAEXT
235 #ifdef HAVE_XPETRA_TPETRA
238 if (tmp_TMap != Teuchos::null) {
240 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
243 #endif // HAVE_XPETRA_TPETRA
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()));
263 #endif // HAVE_XPETRA_EPETRA
265 #ifdef HAVE_XPETRA_TPETRA
268 if (tmp_TVec != Teuchos::null) {
270 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
273 #endif // HAVE_XPETRA_TPETRA
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
308 if (tmp_TCrsMtx != Teuchos::null) {
310 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
313 #endif // HAVE_XPETRA_TPETRA
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();
451 for (
int i = 0; i < m; i++) {
453 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
454 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
457 for (
int j = 0; j < rownnz; j++) {
459 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
460 inds[j] = Teuchos::as<GlobalOrdinal>(index);
462 for (
int j = 0; j < rownnz; j++) {
464 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
465 vals[j] = Teuchos::as<SC>(value);
467 A->insertGlobalValues(row, inds, vals);
471 A->fillComplete(domainMap, rangeMap);
486 Read(
const std::string& filename,
491 const bool callFillComplete =
true,
492 const bool binary =
false,
493 const bool tolerant =
false,
494 const bool debug =
false) {
501 if (binary ==
false) {
503 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
510 if (colMap.is_null()) {
511 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
515 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
523 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
530 #ifdef HAVE_XPETRA_TPETRA
531 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
532 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
533 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
540 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
541 callFillComplete, tolerant, debug);
558 std::ifstream ifs(filename.c_str(), std::ios::binary);
561 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
562 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
563 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
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));
581 for (
int j = 0; j < rownnz; j++) {
583 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
584 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
586 for (
int j = 0; j < rownnz; j++) {
588 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
589 vals[j] = Teuchos::as<SC>(value);
591 A->insertGlobalValues(rowElements[row], inds, vals);
593 A->fillComplete(domainMap, rangeMap);
609 #ifdef HAVE_XPETRA_TPETRA
610 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
611 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
612 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
613 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
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;
663 size_t numBlocks = 2;
665 std::vector<RCP<const XpMap> > rgMapVec;
666 for(
size_t r = 0; r < numBlocks; ++r) {
667 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
668 rgMapVec.push_back(map);
670 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
672 std::vector<RCP<const XpMap> > doMapVec;
673 for(
size_t c = 0; c < numBlocks; ++c) {
674 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
675 doMapVec.push_back(map);
677 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
693 bool bRangeUseThyraStyleNumbering =
false;
701 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
705 bool bDomainUseThyraStyleNumbering =
false;
713 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
718 for (
size_t r = 0; r < numBlocks; ++r) {
719 for (
size_t c = 0; c < numBlocks; ++c) {
720 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
721 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
722 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
723 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
724 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
726 bOp->setMatrix(r, c, mat);
739 std::ostringstream buf;
746 #ifdef HAVE_XPETRA_EPETRA
756 template <
class Scalar>
763 #ifdef HAVE_XPETRA_EPETRA
768 if (xeMap == Teuchos::null)
769 throw Exceptions::BadCast(
"IO::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
770 return xeMap->getEpetra_Map();
775 #ifdef HAVE_XPETRA_TPETRA
780 if (tmp_TMap == Teuchos::null)
781 throw Exceptions::BadCast(
"IO::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
782 return tmp_TMap->getTpetra_Map();
792 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
794 if (tmp_EMap != Teuchos::null) {
795 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
800 #endif // HAVE_XPETRA_EPETRA
802 #ifdef HAVE_XPETRA_TPETRA
803 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
804 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
809 if (tmp_TMap != Teuchos::null) {
811 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
815 #endif // HAVE_XPETRA_TPETRA
821 std::string mapfile =
"map_" + fileName;
825 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
827 if (tmp_EVec != Teuchos::null) {
828 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
833 #endif // HAVE_XPETRA_EPETRAEXT
835 #ifdef HAVE_XPETRA_TPETRA
836 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
837 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
842 if (tmp_TVec != Teuchos::null) {
844 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
848 #endif // HAVE_XPETRA_TPETRA
850 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
870 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
872 if (tmp_ECrsMtx != Teuchos::null) {
874 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
879 #endif // endif HAVE_XPETRA_EPETRA
881 #ifdef HAVE_XPETRA_TPETRA
882 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
883 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
888 if (tmp_TCrsMtx != Teuchos::null) {
890 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
894 #endif // HAVE_XPETRA_TPETRA
896 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
910 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
918 for (
size_t j = 0; j<rowptr.
size(); j++)
919 rowptr2[j] = rowptr[j];
922 writer.
writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
937 for (
size_t r = 0; r < Op.
Rows(); ++r) {
938 for (
size_t c = 0; c < Op.
Cols(); ++c) {
940 if(m != Teuchos::null) {
942 "Sub block matrix (" << r <<
"," << c <<
") is not of type CrsMatrixWrap.");
952 for(
size_t r = 0; r < rangeMapExtractor->NumMaps(); ++r) {
954 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", *map);
956 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
958 for(
size_t c = 0; c < domainMapExtractor->NumMaps(); ++c) {
960 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", *map);
962 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
967 if (binary ==
false) {
970 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
973 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
980 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
986 #ifdef HAVE_XPETRA_TPETRA
987 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
988 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
991 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
993 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
995 bool callFillComplete =
true;
1016 std::ifstream ifs(fileName.c_str(), std::ios::binary);
1019 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
1020 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
1021 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
1023 int myRank = comm->getRank();
1036 for (
int i = 0; i < m; i++) {
1038 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
1039 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
1042 for (
int j = 0; j < rownnz; j++) {
1044 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
1045 inds[j] = Teuchos::as<GlobalOrdinal>(index);
1047 for (
int j = 0; j < rownnz; j++) {
1049 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
1050 vals[j] = Teuchos::as<Scalar>(value);
1052 A->insertGlobalValues(row, inds, vals);
1056 A->fillComplete(domainMap, rangeMap);
1075 const bool callFillComplete =
true,
1076 const bool binary =
false,
1077 const bool tolerant =
false,
1078 const bool debug =
false) {
1085 if (binary ==
false) {
1087 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1094 if (colMap.is_null()) {
1095 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
1099 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
1107 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1114 #ifdef HAVE_XPETRA_TPETRA
1115 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1116 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1119 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1120 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1121 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1128 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
1129 callFillComplete, tolerant, debug);
1147 std::ifstream ifs(filename.c_str(), std::ios::binary);
1150 ifs.read(reinterpret_cast<char*>(&m),
sizeof(m));
1151 ifs.read(reinterpret_cast<char*>(&n),
sizeof(n));
1152 ifs.read(reinterpret_cast<char*>(&nnz),
sizeof(nnz));
1164 for (
int i = 0; i < m; i++) {
1166 ifs.read(reinterpret_cast<char*>(&row),
sizeof(row));
1167 ifs.read(reinterpret_cast<char*>(&rownnz),
sizeof(rownnz));
1170 for (
int j = 0; j < rownnz; j++) {
1172 ifs.read(reinterpret_cast<char*>(&index),
sizeof(index));
1173 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
1175 for (
int j = 0; j < rownnz; j++) {
1177 ifs.read(reinterpret_cast<char*>(&value),
sizeof(value));
1178 vals[j] = Teuchos::as<Scalar>(value);
1180 A->insertGlobalValues(rowElements[row], inds, vals);
1182 A->fillComplete(domainMap, rangeMap);
1197 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1199 EpetraExt::MatrixMarketFileToMultiVector(fileName.c_str(),
toEpetra(map), MV);
1201 return Convert_Epetra_MultiVector_ToXpetra_MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(MVrcp);
1206 #ifdef HAVE_XPETRA_TPETRA
1207 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1208 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1211 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1212 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1213 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1214 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
1237 #if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1239 int rv = EpetraExt::MatrixMarketFileToMap(fileName.c_str(), *(
Xpetra::toEpetra(comm)), eMap);
1244 return Xpetra::toXpetra<int,Node>(*eMap1);
1249 #ifdef HAVE_XPETRA_TPETRA
1250 # if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1251 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1254 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1255 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1285 size_t numBlocks = 2;
1287 std::vector<RCP<const XpMap> > rgMapVec;
1288 for(
size_t r = 0; r < numBlocks; ++r) {
1289 RCP<const XpMap> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(r) +
".m", lib, comm);
1290 rgMapVec.push_back(map);
1292 RCP<const XpMap> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
1294 std::vector<RCP<const XpMap> > doMapVec;
1295 for(
size_t c = 0; c < numBlocks; ++c) {
1296 RCP<const XpMap> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(c) +
".m", lib, comm);
1297 doMapVec.push_back(map);
1299 RCP<const XpMap> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
1315 bool bRangeUseThyraStyleNumbering =
false;
1323 Teuchos::rcp(
new XpMapExtractor(fullRangeMap, rgMapVec, bRangeUseThyraStyleNumbering));
1326 bool bDomainUseThyraStyleNumbering =
false;
1333 Teuchos::rcp(
new XpMapExtractor(fullDomainMap, doMapVec, bDomainUseThyraStyleNumbering));
1338 for (
size_t r = 0; r < numBlocks; ++r) {
1339 for (
size_t c = 0; c < numBlocks; ++c) {
1340 RCP<const XpMap> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1341 RCP<const XpMap> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1342 RCP<const XpMap> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1343 RCP<const XpMap> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", lib, comm);
1344 RCP<XpMat> mat = XpIO::Read(fileName + XpIO::toString<size_t>(r) + XpIO::toString<size_t>(c) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
1346 bOp->setMatrix(r, c, mat);
1350 bOp->fillComplete();
1358 std::ostringstream buf;
1363 #endif // HAVE_XPETRA_EPETRA
1368 #define XPETRA_IO_SHORT
RCP< CrsMatrix > getCrsMatrix() const
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< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &)
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
static void Write(const std::string &fileName, const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &M)
Read/Write methods.
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.
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.
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.
void resize(const size_type n, const T &val=T())
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
Teuchos::RCP< Matrix > getMatrix(size_t r, size_t c) const
return block (r,c)
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 size_t Cols() const
number of column blocks
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getMap() const =0
The Map describing the parallel distribution of this object.
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.
RCP< Xpetra::MultiVector< SC, LO, GO, NO > > Convert_Epetra_MultiVector_ToXpetra_MultiVector(RCP< Epetra_MultiVector > &epX)
void resize(size_type new_size, const value_type &x=value_type())
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 std::string toString(const T &what)
Little helper function to convert non-string types to strings.
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)
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)
RCP< const MapExtractor > getDomainMapExtractor() const
Returns map extractor for domain map.
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)
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.
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
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 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.
virtual Teuchos::RCP< const Map > getRangeMap() const =0
The Map associated with the range of this operator, which must be compatible with Y...
virtual size_t Rows() const
number of row blocks
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.
virtual const RCP< const Map > & getRowMap() const
Returns the Map that describes the row distribution in this matrix.
static RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
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.
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::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save 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.
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.
std::string toString(const T &t)
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.
RCP< const MapExtractor > getRangeMapExtractor() const
Returns map extractor class for range map.
static std::string toString(const T &what)
Little helper function to convert non-string types to strings.