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_TpetraCrsGraph.hpp>
78#include <Xpetra_TpetraCrsMatrix.hpp>
79#include <Xpetra_TpetraBlockCrsMatrix.hpp>
82#ifdef HAVE_XPETRA_EPETRA
89#include "Xpetra_CrsMatrixWrap.hpp"
92#include "Xpetra_Map.hpp"
93#include "Xpetra_StridedMap.hpp"
94#include "Xpetra_StridedMapFactory.hpp"
95#include "Xpetra_MapExtractor.hpp"
98#include <Teuchos_MatrixMarket_Raw_Writer.hpp>
105#ifdef HAVE_XPETRA_EPETRA
107 template<
class SC,
class LO,
class GO,
class NO>
108 RCP<Xpetra::CrsMatrixWrap<SC,LO,GO,NO> >
111 "Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap cannot be used with Scalar != double, LocalOrdinal != int, GlobalOrdinal != int");
117 inline RCP<Xpetra::CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<double,int,int,Xpetra::EpetraNode> (RCP<Epetra_CrsMatrix> &epAB) {
124 RCP<Xpetra::CrsMatrix<SC,LO,GO,NO> > tmpC2 = Teuchos::rcp_implicit_cast<Xpetra::CrsMatrix<SC,LO,GO,NO> >(tmpC1);
131 template<
class SC,
class LO,
class GO,
class NO>
132 RCP<Xpetra::MultiVector<SC,LO,GO,NO> >
135 "Convert_Epetra_MultiVector_ToXpetra_MultiVector cannot be used with Scalar != double, LocalOrdinal != int, GlobalOrdinal != int");
141 inline RCP<Xpetra::MultiVector<double,int,int,Xpetra::EpetraNode> > Convert_Epetra_MultiVector_ToXpetra_MultiVector<double,int,int,Xpetra::EpetraNode> (RCP<Epetra_MultiVector> &epX) {
147 RCP<Xpetra::MultiVector<SC,LO,GO,NO >> tmp = Xpetra::toXpetra<GO,NO>(epX);
157 template <
class Scalar,
158 class LocalOrdinal = int,
159 class GlobalOrdinal = LocalOrdinal,
164#undef XPETRA_IO_SHORT
169#ifdef HAVE_XPETRA_EPETRA
186 if (xeMap == Teuchos::null)
187 throw Exceptions::BadCast(
"Utils::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
188 return xeMap->getEpetra_Map();
193#ifdef HAVE_XPETRA_TPETRA
215 if (tmp_TMap == Teuchos::null)
216 throw Exceptions::BadCast(
"Utils::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
217 return tmp_TMap->getTpetra_Map();
227#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
229 if (tmp_EMap != Teuchos::null) {
230 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
237#ifdef HAVE_XPETRA_TPETRA
239 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> >(tmp_Map);
240 if (tmp_TMap != Teuchos::null) {
242 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
253 std::string mapfile =
"map_" + fileName;
257#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
259 if (tmp_EVec != Teuchos::null) {
260 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
267#ifdef HAVE_XPETRA_TPETRA
269 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_Vec);
270 if (tmp_TVec != Teuchos::null) {
272 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
277 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
296#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
298 if (tmp_ECrsMtx != Teuchos::null) {
300 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
307#ifdef HAVE_XPETRA_TPETRA
309 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_CrsMtx);
310 if (tmp_TCrsMtx != Teuchos::null) {
312 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
317 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
331 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
339 for (
size_t j = 0; j<rowptr.
size(); j++)
340 rowptr2[j] = rowptr[j];
343 writer.
writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
367 for (
size_t row = 0; row < Op.
Rows(); ++row) {
368 for (
size_t col = 0; col < Op.
Cols(); ++col) {
370 if(m != Teuchos::null) {
372 "Sub block matrix (" << row <<
"," << col <<
") is not of type CrsMatrixWrap.");
373 XpIO::Write(fileName +
toString(row) +
toString(col) +
".m", *m, writeAllMaps);
382 for(
size_t row = 0; row < rangeMapExtractor->NumMaps(); ++row) {
384 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(row) +
".m", *map);
386 XpIO::Write(
"fullRangeMap_" + fileName +
".m",*(rangeMapExtractor->getFullMap()));
388 for(
size_t col = 0; col < domainMapExtractor->NumMaps(); ++col) {
390 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(col) +
".m", *map);
392 XpIO::Write(
"fullDomainMap_" + fileName+
".m",*(domainMapExtractor->getFullMap()));
397 if (binary ==
false) {
400#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
403 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
410 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
416#ifdef HAVE_XPETRA_TPETRA
417 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
419 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
421 bool callFillComplete =
true;
441 std::ifstream ifs(fileName.c_str(), std::ios::binary);
444 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
445 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
446 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
448 int myRank = comm->getRank();
460 for (
int i = 0; i < m; i++) {
462 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
463 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
464 numEntriesPerRow[i] = rownnz;
465 for (
int j = 0; j < rownnz; j++) {
467 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
469 for (
int j = 0; j < rownnz; j++) {
471 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
478 ifs.seekg(0, ifs.beg);
480 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(junk));
481 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(junk));
482 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(junk));
483 for (
int i = 0; i < m; i++) {
485 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
486 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
489 for (
int j = 0; j < rownnz; j++) {
491 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
492 inds[j] = Teuchos::as<GlobalOrdinal>(index);
494 for (
int j = 0; j < rownnz; j++) {
496 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
497 vals[j] = Teuchos::as<Scalar>(value);
499 A->insertGlobalValues(row, inds, vals);
503 A->fillComplete(domainMap, rangeMap);
518 Read(
const std::string& filename,
523 const bool callFillComplete =
true,
524 const bool binary =
false,
525 const bool tolerant =
false,
526 const bool debug =
false) {
533 if (binary ==
false) {
535#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
542 if (colMap.is_null()) {
543 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
547 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
555 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
562#ifdef HAVE_XPETRA_TPETRA
563 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
564 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
565 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
572 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
573 callFillComplete, tolerant, debug);
590 std::ifstream ifs(filename.c_str(), std::ios::binary);
593 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
594 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
595 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
608 for (
int i = 0; i < m; i++) {
610 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
611 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
614 for (
int j = 0; j < rownnz; j++) {
616 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
617 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
619 for (
int j = 0; j < rownnz; j++) {
621 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
622 vals[j] = Teuchos::as<SC>(value);
625 A->insertGlobalValues(rowElements[row], inds, vals);
627 A->fillComplete(domainMap, rangeMap);
643#ifdef HAVE_XPETRA_TPETRA
644 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
645 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
646 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
647 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
667#ifdef HAVE_XPETRA_TPETRA
668 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
669 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
703 size_t numBlocks = 2;
705 std::vector<RCP<const Map> > rangeMapVec;
706 for(
size_t row = 0; row < numBlocks; ++row) {
707 RCP<const Map> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(row) +
".m", lib, comm);
708 rangeMapVec.push_back(map);
710 RCP<const Map> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
712 std::vector<RCP<const Map> > domainMapVec;
713 for(
size_t col = 0; col < numBlocks; ++col) {
714 RCP<const Map> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(col) +
".m", lib, comm);
715 domainMapVec.push_back(map);
717 RCP<const Map> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
733 bool bRangeUseThyraStyleNumbering =
false;
745 bool bDomainUseThyraStyleNumbering =
false;
758 for (
size_t row = 0; row < numBlocks; ++row) {
759 for (
size_t col = 0; col < numBlocks; ++col) {
760 RCP<const Map> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
761 RCP<const Map> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
762 RCP<const Map> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
763 RCP<const Map> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
764 RCP<Matrix> mat = XpIO::Read(fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
765 bOp->setMatrix(row, col, mat);
778 std::ostringstream buf;
785#ifdef HAVE_XPETRA_EPETRA
795 template <
class Scalar>
802#ifdef HAVE_XPETRA_EPETRA
807 if (xeMap == Teuchos::null)
808 throw Exceptions::BadCast(
"IO::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
809 return xeMap->getEpetra_Map();
814#ifdef HAVE_XPETRA_TPETRA
819 if (tmp_TMap == Teuchos::null)
820 throw Exceptions::BadCast(
"IO::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
821 return tmp_TMap->getTpetra_Map();
831#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
833 if (tmp_EMap != Teuchos::null) {
834 int rv = EpetraExt::BlockMapToMatrixMarketFile(fileName.c_str(), tmp_EMap->getEpetra_Map());
841#ifdef HAVE_XPETRA_TPETRA
842# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
843 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
847 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMap<LocalOrdinal, GlobalOrdinal, Node> >(tmp_Map);
848 if (tmp_TMap != Teuchos::null) {
850 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeMapFile(fileName, *TMap);
860 std::string mapfile =
"map_" + fileName;
864#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
866 if (tmp_EVec != Teuchos::null) {
867 int rv = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), *(tmp_EVec->getEpetra_MultiVector()));
874#ifdef HAVE_XPETRA_TPETRA
875# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
876 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
880 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_Vec);
881 if (tmp_TVec != Teuchos::null) {
883 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeDenseFile(fileName, TVec);
889 throw Exceptions::BadCast(
"Could not cast to EpetraMultiVector or TpetraMultiVector in multivector writing");
909#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
911 if (tmp_ECrsMtx != Teuchos::null) {
913 int rv = EpetraExt::RowMatrixToMatrixMarketFile(fileName.c_str(), *A);
920#ifdef HAVE_XPETRA_TPETRA
921# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
922 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
926 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(tmp_CrsMtx);
927 if (tmp_TCrsMtx != Teuchos::null) {
929 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseFile(fileName, A);
935 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
952#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
954 if (tmp_ECrsGraph != Teuchos::null) {
959#ifdef HAVE_XPETRA_TPETRA
960# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
961 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
965 Teuchos::rcp_dynamic_cast<const Xpetra::TpetraCrsGraph<LocalOrdinal, GlobalOrdinal, Node> >(tmp_Graph);
966 if (tmp_TCrsGraph != Teuchos::null) {
968 Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> >::writeSparseGraphFile(fileName, G);
974 throw Exceptions::BadCast(
"Could not cast to EpetraCrsMatrix or TpetraCrsMatrix in matrix writing");
988 tmp_CrsMtx->getAllValues(rowptr_RCP, colind_RCP, vals_RCP);
996 for (
size_t j = 0; j<Teuchos::as<size_t>(rowptr.
size()); j++)
997 rowptr2[j] = rowptr[j];
1000 writer.
writeFile(fileName +
"." + std::to_string(Op.
getRowMap()->getComm()->getSize()) +
"." + std::to_string(Op.
getRowMap()->getComm()->getRank()),
1001 rowptr2,colind,vals,
1026 for (
size_t row = 0; row < Op.
Rows(); ++row) {
1027 for (
size_t col = 0; col < Op.
Cols(); ++col) {
1029 if(m != Teuchos::null) {
1031 "Sub block matrix (" << row <<
"," << col <<
") is not of type CrsMatrixWrap.");
1032 XpIO::Write(fileName +
toString(row) +
toString(col) +
".m", *m, writeAllMaps);
1041 for(
size_t row = 0; row < rangeMapExtractor->NumMaps(); ++row) {
1043 XpIO::Write(
"subRangeMap_" + fileName + XpIO::toString<size_t>(row) +
".m", *map);
1045 XpIO::Write(
"fullRangeMap_" + fileName +
".m", *(rangeMapExtractor->getFullMap()));
1047 for(
size_t col = 0; col < domainMapExtractor->NumMaps(); ++col) {
1049 XpIO::Write(
"subDomainMap_" + fileName + XpIO::toString<size_t>(col) +
".m", *map);
1051 XpIO::Write(
"fullDomainMap_" + fileName+
".m", *(domainMapExtractor->getFullMap()));
1056 if (binary ==
false) {
1059#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1062 int rv = EpetraExt::MatrixMarketFileToCrsMatrix(fileName.c_str(), *epcomm, eA);
1069 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1075#ifdef HAVE_XPETRA_TPETRA
1076# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1077 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1080 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1082 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1084 bool callFillComplete =
true;
1105 std::ifstream ifs(fileName.c_str(), std::ios::binary);
1108 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
1109 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
1110 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
1112 int myRank = comm->getRank();
1125 for (
int i = 0; i < m; i++) {
1127 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1128 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1129 numEntriesPerRow[i] = rownnz;
1130 for (
int j = 0; j < rownnz; j++) {
1132 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1134 for (
int j = 0; j < rownnz; j++) {
1136 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1143 ifs.seekg(0, ifs.beg);
1145 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(junk));
1146 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(junk));
1147 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(junk));
1148 for (
int i = 0; i < m; i++) {
1150 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1151 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1154 for (
int j = 0; j < rownnz; j++) {
1156 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1157 inds[j] = Teuchos::as<GlobalOrdinal>(index);
1159 for (
int j = 0; j < rownnz; j++) {
1161 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1162 vals[j] = Teuchos::as<Scalar>(value);
1164 A->insertGlobalValues(row, inds, vals);
1168 A->fillComplete(domainMap, rangeMap);
1187 const bool callFillComplete =
true,
1188 const bool binary =
false,
1189 const bool tolerant =
false,
1190 const bool debug =
false) {
1197 if (binary ==
false) {
1199#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1206 if (colMap.is_null()) {
1207 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraRangeMap, epetraDomainMap, eA);
1211 rv = EpetraExt::MatrixMarketFileToCrsMatrix(filename.c_str(), epetraRowMap, epetraColMap, epetraRangeMap, epetraDomainMap, eA);
1219 Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tmpA);
1226#ifdef HAVE_XPETRA_TPETRA
1227# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1228 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1231 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1232 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1233 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1240 RCP<sparse_matrix_type> tA = reader_type::readSparseFile(filename, tpetraRowMap, tpetraColMap, tpetraDomainMap, tpetraRangeMap,
1241 callFillComplete, tolerant, debug);
1259 std::ifstream ifs(filename.c_str(), std::ios::binary);
1262 ifs.read(
reinterpret_cast<char*
>(&m),
sizeof(m));
1263 ifs.read(
reinterpret_cast<char*
>(&n),
sizeof(n));
1264 ifs.read(
reinterpret_cast<char*
>(&nnz),
sizeof(nnz));
1277 for (
int i = 0; i < m; i++) {
1279 ifs.read(
reinterpret_cast<char*
>(&row),
sizeof(row));
1280 ifs.read(
reinterpret_cast<char*
>(&rownnz),
sizeof(rownnz));
1283 for (
int j = 0; j < rownnz; j++) {
1285 ifs.read(
reinterpret_cast<char*
>(&index),
sizeof(index));
1286 inds[j] = colElements[Teuchos::as<LocalOrdinal>(index)];
1288 for (
int j = 0; j < rownnz; j++) {
1290 ifs.read(
reinterpret_cast<char*
>(&value),
sizeof(value));
1291 vals[j] = Teuchos::as<Scalar>(value);
1294 A->insertGlobalValues(rowElements[row], inds, vals);
1296 A->fillComplete(domainMap, rangeMap);
1311#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1313 int rv = EpetraExt::MatrixMarketFileToMultiVector(fileName.c_str(),
toEpetra(map), MV);
1316 return Convert_Epetra_MultiVector_ToXpetra_MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(MVrcp);
1321#ifdef HAVE_XPETRA_TPETRA
1322# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1323 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1326 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1327 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1328 typedef Tpetra::Map<LocalOrdinal, GlobalOrdinal, Node> map_type;
1329 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> multivector_type;
1352#if defined(HAVE_XPETRA_EPETRA) && defined(HAVE_XPETRA_EPETRAEXT)
1354 int rv = EpetraExt::MatrixMarketFileToMap(fileName.c_str(), *(
Xpetra::toEpetra(comm)), eMap);
1359 return Xpetra::toXpetra<int,Node>(*eMap1);
1364#ifdef HAVE_XPETRA_TPETRA
1365# if ((defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_OPENMP) || !defined(HAVE_TPETRA_INST_INT_INT))) || \
1366 (!defined(EPETRA_HAVE_OMP) && (!defined(HAVE_TPETRA_INST_SERIAL) || !defined(HAVE_TPETRA_INST_INT_INT))))
1369 typedef Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> sparse_matrix_type;
1370 typedef Tpetra::MatrixMarket::Reader<sparse_matrix_type> reader_type;
1408 size_t numBlocks = 2;
1410 std::vector<RCP<const Map> > rangeMapVec;
1411 for(
size_t row = 0; row < numBlocks; ++row) {
1412 RCP<const Map> map = XpIO::ReadMap(
"subRangeMap_" + fileName + XpIO::toString<size_t>(row) +
".m", lib, comm);
1413 rangeMapVec.push_back(map);
1415 RCP<const Map> fullRangeMap = XpIO::ReadMap(
"fullRangeMap_" + fileName +
".m", lib, comm);
1417 std::vector<RCP<const Map> > domainMapVec;
1418 for(
size_t col = 0; col < numBlocks; ++col) {
1419 RCP<const Map> map = XpIO::ReadMap(
"subDomainMap_" + fileName + XpIO::toString<size_t>(col) +
".m", lib, comm);
1420 domainMapVec.push_back(map);
1422 RCP<const Map> fullDomainMap = XpIO::ReadMap(
"fullDomainMap_" + fileName +
".m", lib, comm);
1438 bool bRangeUseThyraStyleNumbering =
false;
1446 rcp(
new MapExtractor(fullRangeMap, rangeMapVec, bRangeUseThyraStyleNumbering));
1449 bool bDomainUseThyraStyleNumbering =
false;
1456 rcp(
new MapExtractor(fullDomainMap, domainMapVec, bDomainUseThyraStyleNumbering));
1461 for (
size_t row = 0; row < numBlocks; ++row) {
1462 for (
size_t col = 0; col < numBlocks; ++col) {
1463 RCP<const Map> rowSubMap = XpIO::ReadMap(
"rowmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
1464 RCP<const Map> colSubMap = XpIO::ReadMap(
"colmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
1465 RCP<const Map> domSubMap = XpIO::ReadMap(
"domainmap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
1466 RCP<const Map> ranSubMap = XpIO::ReadMap(
"rangemap_" + fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", lib, comm);
1467 RCP<Matrix> mat = XpIO::Read(fileName + XpIO::toString<size_t>(row) + XpIO::toString<size_t>(col) +
".m", rowSubMap, colSubMap, domSubMap, ranSubMap);
1468 bOp->setMatrix(row, col, mat);
1472 bOp->fillComplete();
1480 std::ostringstream buf;
1490#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)
Teuchos::RCP< Matrix > getMatrix(size_t r, size_t c) const
return block (r,c)
virtual size_t Rows() const
number of row blocks
RCP< const MapExtractor > getRangeMapExtractor() const
Returns map extractor class for range map.
RCP< const MapExtractor > getDomainMapExtractor() const
Returns map extractor for domain map.
virtual size_t Cols() const
number of column blocks
virtual RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const =0
Returns the Map associated with the domain of this graph.
virtual RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const =0
Returns the Map that describes the row distribution in this graph.
virtual RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const =0
Returns the Map associated with the domain of this graph.
virtual RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const =0
Returns the Map that describes the column distribution in this graph.
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 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 void WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save block matrix to one file per block in Matrix Market format.
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 RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadMultiVector(const std::string &fileName, const RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
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 Write(const std::string &fileName, const Xpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > &graph, const bool &writeAllMaps=false)
Save CrsGraph to file 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 RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadBlockedCrsMatrix(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
Read block matrix from one file per block in Matrix Market format.
static std::string toString(const T &what)
Little helper function to convert non-string types to strings.
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 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.
Xpetra utility class containing IO routines to read/write vectors, matrices etc...
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 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 RCP< const Map > ReadMap(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
static RCP< MultiVector > ReadMultiVector(const std::string &fileName, const RCP< const Map > &map)
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 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 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 WriteBlockedCrsMatrix(const std::string &fileName, const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const bool &writeAllMaps=false)
Save block matrix to one file per block in Matrix Market format.
static RCP< const Xpetra::BlockedCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > ReadBlockedCrsMatrix(const std::string &fileName, Xpetra::UnderlyingLib lib, const RCP< const Teuchos::Comm< int > > &comm)
Read block matrix from one file per block in Matrix Market format.
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 > & getColMap() const
Returns the Map that describes the column distribution in this matrix. This might be null until fillC...
virtual const RCP< const Map > & getRowMap() const
Returns the Map that describes the row distribution in this 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 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)
RCP< Xpetra::MultiVector< SC, LO, GO, NO > > Convert_Epetra_MultiVector_ToXpetra_MultiVector(RCP< Epetra_MultiVector > &epX)
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal, Node > > &graph)
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
RCP< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &)