42#ifndef __MatrixMarket_Tpetra_hpp
43#define __MatrixMarket_Tpetra_hpp
57#include "Tpetra_CrsMatrix.hpp"
58#include "Tpetra_Operator.hpp"
59#include "Tpetra_Vector.hpp"
61#include "Teuchos_MatrixMarket_Raw_Adder.hpp"
62#include "Teuchos_MatrixMarket_Raw_Graph_Adder.hpp"
63#include "Teuchos_MatrixMarket_SymmetrizingAdder.hpp"
64#include "Teuchos_MatrixMarket_SymmetrizingGraphAdder.hpp"
65#include "Teuchos_MatrixMarket_assignScalar.hpp"
66#include "Teuchos_MatrixMarket_Banner.hpp"
67#include "Teuchos_MatrixMarket_CoordDataReader.hpp"
68#include "Teuchos_SetScientific.hpp"
69#include "Teuchos_TimeMonitor.hpp"
72#include "mmio_Tpetra.h"
74#include "Tpetra_Distribution.hpp"
171 template<
class SparseMatrixType>
176 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
191 typedef typename SparseMatrixType::global_ordinal_type
213 typedef Teuchos::Comm<int> comm_type;
223 typedef Teuchos::ArrayRCP<int>::size_type size_type;
235 static Teuchos::RCP<const map_type>
236 makeRangeMap (
const Teuchos::RCP<const comm_type>& pComm,
242 pComm, GloballyDistributed));
272 static Teuchos::RCP<const map_type>
273 makeRowMap (
const Teuchos::RCP<const map_type>& pRowMap,
274 const Teuchos::RCP<const comm_type>& pComm,
279 if (pRowMap.is_null ()) {
280 return rcp (
new map_type (
static_cast<global_size_t> (numRows),
282 pComm, GloballyDistributed));
285 TEUCHOS_TEST_FOR_EXCEPTION
286 (! pRowMap->isDistributed () && pComm->getSize () > 1,
287 std::invalid_argument,
"The specified row map is not distributed, "
288 "but the given communicator includes more than one process (in "
289 "fact, there are " << pComm->getSize () <<
" processes).");
290 TEUCHOS_TEST_FOR_EXCEPTION
291 (pRowMap->getComm () != pComm, std::invalid_argument,
292 "The specified row Map's communicator (pRowMap->getComm()) "
293 "differs from the given separately supplied communicator pComm.");
312 static Teuchos::RCP<const map_type>
313 makeDomainMap (
const Teuchos::RCP<const map_type>& pRangeMap,
322 if (numRows == numCols) {
325 return createUniformContigMapWithNode<LO,GO,NT> (numCols,
326 pRangeMap->getComm ());
403 distribute (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
404 Teuchos::ArrayRCP<size_t>& myRowPtr,
405 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
406 Teuchos::ArrayRCP<scalar_type>& myValues,
407 const Teuchos::RCP<const map_type>& pRowMap,
408 Teuchos::ArrayRCP<size_t>& numEntriesPerRow,
409 Teuchos::ArrayRCP<size_t>& rowPtr,
410 Teuchos::ArrayRCP<global_ordinal_type>& colInd,
411 Teuchos::ArrayRCP<scalar_type>& values,
412 const bool debug=
false)
415 using Teuchos::ArrayRCP;
416 using Teuchos::ArrayView;
419 using Teuchos::CommRequest;
422 using Teuchos::receive;
427 const bool extraDebug =
false;
428 RCP<const comm_type> pComm = pRowMap->getComm ();
429 const int numProcs = pComm->getSize ();
430 const int myRank = pComm->getRank ();
431 const int rootRank = 0;
438 ArrayView<const GO> myRows = pRowMap->getNodeElementList();
439 const size_type myNumRows = myRows.size();
440 TEUCHOS_TEST_FOR_EXCEPTION(
static_cast<size_t>(myNumRows) !=
441 pRowMap->getNodeNumElements(),
443 "pRowMap->getNodeElementList().size() = "
445 <<
" != pRowMap->getNodeNumElements() = "
446 << pRowMap->getNodeNumElements() <<
". "
447 "Please report this bug to the Tpetra developers.");
448 TEUCHOS_TEST_FOR_EXCEPTION(myRank == 0 && numEntriesPerRow.size() < myNumRows,
450 "On Proc 0: numEntriesPerRow.size() = "
451 << numEntriesPerRow.size()
452 <<
" != pRowMap->getNodeElementList().size() = "
453 << myNumRows <<
". Please report this bug to the "
454 "Tpetra developers.");
458 myNumEntriesPerRow = arcp<size_t> (myNumRows);
460 if (myRank != rootRank) {
464 send (*pComm, myNumRows, rootRank);
465 if (myNumRows != 0) {
469 send (*pComm,
static_cast<int> (myNumRows),
470 myRows.getRawPtr(), rootRank);
480 receive (*pComm, rootRank,
481 static_cast<int> (myNumRows),
482 myNumEntriesPerRow.getRawPtr());
487 std::accumulate (myNumEntriesPerRow.begin(),
488 myNumEntriesPerRow.end(), 0);
494 myColInd = arcp<GO> (myNumEntries);
495 myValues = arcp<scalar_type> (myNumEntries);
496 if (myNumEntries > 0) {
499 receive (*pComm, rootRank,
500 static_cast<int> (myNumEntries),
501 myColInd.getRawPtr());
502 receive (*pComm, rootRank,
503 static_cast<int> (myNumEntries),
504 myValues.getRawPtr());
510 cerr <<
"-- Proc 0: Copying my data from global arrays" << endl;
514 for (size_type k = 0; k < myNumRows; ++k) {
515 const GO myCurRow = myRows[k];
517 myNumEntriesPerRow[k] = numEntriesInThisRow;
519 if (extraDebug && debug) {
520 cerr <<
"Proc " << pRowMap->getComm ()->getRank ()
521 <<
": myNumEntriesPerRow[0.." << (myNumRows-1) <<
"] = [";
522 for (size_type k = 0; k < myNumRows; ++k) {
523 cerr << myNumEntriesPerRow[k];
524 if (k < myNumRows-1) {
532 std::accumulate (myNumEntriesPerRow.begin(),
533 myNumEntriesPerRow.end(), 0);
535 cerr <<
"-- Proc 0: I own " << myNumRows <<
" rows and "
536 << myNumEntries <<
" entries" << endl;
538 myColInd = arcp<GO> (myNumEntries);
539 myValues = arcp<scalar_type> (myNumEntries);
547 for (size_type k = 0; k < myNumRows;
548 myCurPos += myNumEntriesPerRow[k], ++k) {
550 const GO myRow = myRows[k];
551 const size_t curPos = rowPtr[myRow];
554 if (curNumEntries > 0) {
555 ArrayView<GO> colIndView = colInd (curPos, curNumEntries);
556 ArrayView<GO> myColIndView = myColInd (myCurPos, curNumEntries);
557 std::copy (colIndView.begin(), colIndView.end(),
558 myColIndView.begin());
560 ArrayView<scalar_type> valuesView =
561 values (curPos, curNumEntries);
562 ArrayView<scalar_type> myValuesView =
563 myValues (myCurPos, curNumEntries);
564 std::copy (valuesView.begin(), valuesView.end(),
565 myValuesView.begin());
570 for (
int p = 1; p < numProcs; ++p) {
572 cerr <<
"-- Proc 0: Processing proc " << p << endl;
575 size_type theirNumRows = 0;
580 receive (*pComm, p, &theirNumRows);
582 cerr <<
"-- Proc 0: Proc " << p <<
" owns "
583 << theirNumRows <<
" rows" << endl;
585 if (theirNumRows != 0) {
590 ArrayRCP<GO> theirRows = arcp<GO> (theirNumRows);
591 receive (*pComm, p, as<int> (theirNumRows),
592 theirRows.getRawPtr ());
601 const global_size_t numRows = pRowMap->getGlobalNumElements ();
602 const GO indexBase = pRowMap->getIndexBase ();
603 bool theirRowsValid =
true;
604 for (size_type k = 0; k < theirNumRows; ++k) {
605 if (theirRows[k] < indexBase ||
606 as<global_size_t> (theirRows[k] - indexBase) >= numRows) {
607 theirRowsValid =
false;
610 if (! theirRowsValid) {
611 TEUCHOS_TEST_FOR_EXCEPTION(
612 ! theirRowsValid, std::logic_error,
613 "Proc " << p <<
" has at least one invalid row index. "
614 "Here are all of them: " <<
615 Teuchos::toString (theirRows ()) <<
". Valid row index "
616 "range (zero-based): [0, " << (numRows - 1) <<
"].");
631 ArrayRCP<size_t> theirNumEntriesPerRow;
632 theirNumEntriesPerRow = arcp<size_t> (theirNumRows);
633 for (size_type k = 0; k < theirNumRows; ++k) {
634 theirNumEntriesPerRow[k] = numEntriesPerRow[theirRows[k]];
641 send (*pComm,
static_cast<int> (theirNumRows),
642 theirNumEntriesPerRow.getRawPtr(), p);
646 std::accumulate (theirNumEntriesPerRow.begin(),
647 theirNumEntriesPerRow.end(), 0);
650 cerr <<
"-- Proc 0: Proc " << p <<
" owns "
651 << theirNumEntries <<
" entries" << endl;
656 if (theirNumEntries == 0) {
665 ArrayRCP<GO> theirColInd (theirNumEntries);
666 ArrayRCP<scalar_type> theirValues (theirNumEntries);
674 for (size_type k = 0; k < theirNumRows;
675 theirCurPos += theirNumEntriesPerRow[k], k++) {
677 const GO theirRow = theirRows[k];
683 if (curNumEntries > 0) {
684 ArrayView<GO> colIndView =
685 colInd (curPos, curNumEntries);
686 ArrayView<GO> theirColIndView =
687 theirColInd (theirCurPos, curNumEntries);
688 std::copy (colIndView.begin(), colIndView.end(),
689 theirColIndView.begin());
691 ArrayView<scalar_type> valuesView =
692 values (curPos, curNumEntries);
693 ArrayView<scalar_type> theirValuesView =
694 theirValues (theirCurPos, curNumEntries);
695 std::copy (valuesView.begin(), valuesView.end(),
696 theirValuesView.begin());
703 send (*pComm,
static_cast<int> (theirNumEntries),
704 theirColInd.getRawPtr(), p);
705 send (*pComm,
static_cast<int> (theirNumEntries),
706 theirValues.getRawPtr(), p);
709 cerr <<
"-- Proc 0: Finished with proc " << p << endl;
717 numEntriesPerRow = null;
722 if (debug && myRank == 0) {
723 cerr <<
"-- Proc 0: About to fill in myRowPtr" << endl;
731 myRowPtr = arcp<size_t> (myNumRows+1);
733 for (size_type k = 1; k < myNumRows+1; ++k) {
734 myRowPtr[k] = myRowPtr[k-1] + myNumEntriesPerRow[k-1];
736 if (extraDebug && debug) {
737 cerr <<
"Proc " << Teuchos::rank (*(pRowMap->getComm()))
738 <<
": myRowPtr[0.." << myNumRows <<
"] = [";
739 for (size_type k = 0; k < myNumRows+1; ++k) {
745 cerr <<
"]" << endl << endl;
748 if (debug && myRank == 0) {
749 cerr <<
"-- Proc 0: Done with distribute" << endl;
766 static Teuchos::RCP<sparse_matrix_type>
767 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
768 Teuchos::ArrayRCP<size_t>& myRowPtr,
769 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
770 Teuchos::ArrayRCP<scalar_type>& myValues,
771 const Teuchos::RCP<const map_type>& pRowMap,
772 const Teuchos::RCP<const map_type>& pRangeMap,
773 const Teuchos::RCP<const map_type>& pDomainMap,
774 const bool callFillComplete =
true)
776 using Teuchos::ArrayView;
790 TEUCHOS_TEST_FOR_EXCEPTION(myRowPtr.is_null(), std::logic_error,
791 "makeMatrix: myRowPtr array is null. "
792 "Please report this bug to the Tpetra developers.");
793 TEUCHOS_TEST_FOR_EXCEPTION(pDomainMap.is_null(), std::logic_error,
794 "makeMatrix: domain map is null. "
795 "Please report this bug to the Tpetra developers.");
796 TEUCHOS_TEST_FOR_EXCEPTION(pRangeMap.is_null(), std::logic_error,
797 "makeMatrix: range map is null. "
798 "Please report this bug to the Tpetra developers.");
799 TEUCHOS_TEST_FOR_EXCEPTION(pRowMap.is_null(), std::logic_error,
800 "makeMatrix: row map is null. "
801 "Please report this bug to the Tpetra developers.");
805 RCP<sparse_matrix_type> A =
811 ArrayView<const GO> myRows = pRowMap->getNodeElementList ();
812 const size_type myNumRows = myRows.size ();
815 const GO indexBase = pRowMap->getIndexBase ();
816 for (size_type i = 0; i < myNumRows; ++i) {
817 const size_type myCurPos = myRowPtr[i];
819 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
820 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
823 for (size_type k = 0; k < curNumEntries; ++k) {
824 curColInd[k] += indexBase;
827 if (curNumEntries > 0) {
828 A->insertGlobalValues (myRows[i], curColInd, curValues);
835 myNumEntriesPerRow = null;
840 if (callFillComplete) {
841 A->fillComplete (pDomainMap, pRangeMap);
851 static Teuchos::RCP<sparse_matrix_type>
852 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
853 Teuchos::ArrayRCP<size_t>& myRowPtr,
854 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
855 Teuchos::ArrayRCP<scalar_type>& myValues,
856 const Teuchos::RCP<const map_type>& pRowMap,
857 const Teuchos::RCP<const map_type>& pRangeMap,
858 const Teuchos::RCP<const map_type>& pDomainMap,
859 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
860 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams)
862 using Teuchos::ArrayView;
876 TEUCHOS_TEST_FOR_EXCEPTION(
877 myRowPtr.is_null(), std::logic_error,
878 "makeMatrix: myRowPtr array is null. "
879 "Please report this bug to the Tpetra developers.");
880 TEUCHOS_TEST_FOR_EXCEPTION(
881 pDomainMap.is_null(), std::logic_error,
882 "makeMatrix: domain map is null. "
883 "Please report this bug to the Tpetra developers.");
884 TEUCHOS_TEST_FOR_EXCEPTION(
885 pRangeMap.is_null(), std::logic_error,
886 "makeMatrix: range map is null. "
887 "Please report this bug to the Tpetra developers.");
888 TEUCHOS_TEST_FOR_EXCEPTION(
889 pRowMap.is_null(), std::logic_error,
890 "makeMatrix: row map is null. "
891 "Please report this bug to the Tpetra developers.");
895 RCP<sparse_matrix_type> A =
897 StaticProfile, constructorParams));
901 ArrayView<const GO> myRows = pRowMap->getNodeElementList();
902 const size_type myNumRows = myRows.size();
905 const GO indexBase = pRowMap->getIndexBase ();
906 for (size_type i = 0; i < myNumRows; ++i) {
907 const size_type myCurPos = myRowPtr[i];
909 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
910 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
913 for (size_type k = 0; k < curNumEntries; ++k) {
914 curColInd[k] += indexBase;
916 if (curNumEntries > 0) {
917 A->insertGlobalValues (myRows[i], curColInd, curValues);
924 myNumEntriesPerRow = null;
929 A->fillComplete (pDomainMap, pRangeMap, fillCompleteParams);
937 static Teuchos::RCP<sparse_matrix_type>
938 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
939 Teuchos::ArrayRCP<size_t>& myRowPtr,
940 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
941 Teuchos::ArrayRCP<scalar_type>& myValues,
942 const Teuchos::RCP<const map_type>& rowMap,
943 Teuchos::RCP<const map_type>& colMap,
944 const Teuchos::RCP<const map_type>& domainMap,
945 const Teuchos::RCP<const map_type>& rangeMap,
946 const bool callFillComplete =
true)
948 using Teuchos::ArrayView;
957 RCP<sparse_matrix_type> A;
958 if (colMap.is_null ()) {
966 ArrayView<const GO> myRows = rowMap->getNodeElementList ();
967 const size_type myNumRows = myRows.size ();
970 const GO indexBase = rowMap->getIndexBase ();
971 for (size_type i = 0; i < myNumRows; ++i) {
972 const size_type myCurPos = myRowPtr[i];
973 const size_type curNumEntries = as<size_type> (myNumEntriesPerRow[i]);
974 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
975 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
978 for (size_type k = 0; k < curNumEntries; ++k) {
979 curColInd[k] += indexBase;
981 if (curNumEntries > 0) {
982 A->insertGlobalValues (myRows[i], curColInd, curValues);
989 myNumEntriesPerRow = null;
994 if (callFillComplete) {
995 A->fillComplete (domainMap, rangeMap);
996 if (colMap.is_null ()) {
997 colMap = A->getColMap ();
1021 static Teuchos::RCP<const Teuchos::MatrixMarket::Banner>
1022 readBanner (std::istream& in,
1024 const bool tolerant=
false,
1026 const bool isGraph=
false)
1028 using Teuchos::MatrixMarket::Banner;
1033 typedef Teuchos::ScalarTraits<scalar_type> STS;
1035 RCP<Banner> pBanner;
1039 const bool readFailed = ! getline(in, line);
1040 TEUCHOS_TEST_FOR_EXCEPTION(readFailed, std::invalid_argument,
1041 "Failed to get Matrix Market banner line from input.");
1048 pBanner = rcp (
new Banner (line, tolerant));
1049 }
catch (std::exception& e) {
1050 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1051 "Matrix Market banner line contains syntax error(s): "
1054 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->objectType() !=
"matrix",
1055 std::invalid_argument,
"The Matrix Market file does not contain "
1056 "matrix data. Its Banner (first) line says that its object type is \""
1057 << pBanner->matrixType() <<
"\", rather than the required \"matrix\".");
1061 TEUCHOS_TEST_FOR_EXCEPTION(
1062 ! STS::isComplex && pBanner->dataType() ==
"complex",
1063 std::invalid_argument,
1064 "The Matrix Market file contains complex-valued data, but you are "
1065 "trying to read it into a matrix containing entries of the real-"
1066 "valued Scalar type \""
1067 << Teuchos::TypeNameTraits<scalar_type>::name() <<
"\".");
1068 TEUCHOS_TEST_FOR_EXCEPTION(
1070 pBanner->dataType() !=
"real" &&
1071 pBanner->dataType() !=
"complex" &&
1072 pBanner->dataType() !=
"integer",
1073 std::invalid_argument,
1074 "When reading Matrix Market data into a Tpetra::CrsMatrix, the "
1075 "Matrix Market file may not contain a \"pattern\" matrix. A "
1076 "pattern matrix is really just a graph with no weights. It "
1077 "should be stored in a CrsGraph, not a CrsMatrix.");
1079 TEUCHOS_TEST_FOR_EXCEPTION(
1081 pBanner->dataType() !=
"pattern",
1082 std::invalid_argument,
1083 "When reading Matrix Market data into a Tpetra::CrsGraph, the "
1084 "Matrix Market file must contain a \"pattern\" matrix.");
1111 static Teuchos::Tuple<global_ordinal_type, 3>
1112 readCoordDims (std::istream& in,
1114 const Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1115 const Teuchos::RCP<const comm_type>& pComm,
1116 const bool tolerant =
false,
1119 using Teuchos::MatrixMarket::readCoordinateDimensions;
1120 using Teuchos::Tuple;
1125 Tuple<global_ordinal_type, 3> dims;
1131 bool success =
false;
1132 if (pComm->getRank() == 0) {
1133 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->matrixType() !=
"coordinate",
1134 std::invalid_argument,
"The Tpetra::CrsMatrix Matrix Market reader "
1135 "only accepts \"coordinate\" (sparse) matrix data.");
1139 success = readCoordinateDimensions (in, numRows, numCols,
1140 numNonzeros, lineNumber,
1146 dims[2] = numNonzeros;
1154 int the_success = success ? 1 : 0;
1155 Teuchos::broadcast (*pComm, 0, &the_success);
1156 success = (the_success == 1);
1161 Teuchos::broadcast (*pComm, 0, dims);
1169 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1170 "Error reading Matrix Market sparse matrix: failed to read "
1171 "coordinate matrix dimensions.");
1186 typedef Teuchos::MatrixMarket::SymmetrizingAdder<Teuchos::MatrixMarket::Raw::Adder<scalar_type, global_ordinal_type> > adder_type;
1188 typedef Teuchos::MatrixMarket::SymmetrizingGraphAdder<Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> > graph_adder_type;
1215 static Teuchos::RCP<adder_type>
1216 makeAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1217 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1218 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1219 const bool tolerant=
false,
1220 const bool debug=
false)
1222 if (pComm->getRank () == 0) {
1223 typedef Teuchos::MatrixMarket::Raw::Adder<
scalar_type,
1226 Teuchos::RCP<raw_adder_type> pRaw =
1227 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1229 return Teuchos::rcp (
new adder_type (pRaw, pBanner->symmType ()));
1232 return Teuchos::null;
1261 static Teuchos::RCP<graph_adder_type>
1262 makeGraphAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1263 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1264 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1265 const bool tolerant=
false,
1266 const bool debug=
false)
1268 if (pComm->getRank () == 0) {
1269 typedef Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> raw_adder_type;
1270 Teuchos::RCP<raw_adder_type> pRaw =
1271 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1273 return Teuchos::rcp (
new graph_adder_type (pRaw, pBanner->symmType ()));
1276 return Teuchos::null;
1281 static Teuchos::RCP<sparse_graph_type>
1282 readSparseGraphHelper (std::istream& in,
1283 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1284 const Teuchos::RCP<const map_type>& rowMap,
1285 Teuchos::RCP<const map_type>& colMap,
1286 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1287 const bool tolerant,
1290 using Teuchos::MatrixMarket::Banner;
1293 using Teuchos::Tuple;
1297 const int myRank = pComm->getRank ();
1298 const int rootRank = 0;
1303 size_t lineNumber = 1;
1305 if (debug && myRank == rootRank) {
1306 cerr <<
"Matrix Market reader: readGraph:" << endl
1307 <<
"-- Reading banner line" << endl;
1316 RCP<const Banner> pBanner;
1322 int bannerIsCorrect = 1;
1323 std::ostringstream errMsg;
1325 if (myRank == rootRank) {
1328 pBanner = readBanner (in, lineNumber, tolerant, debug,
true);
1330 catch (std::exception& e) {
1331 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
1332 "threw an exception: " << e.what();
1333 bannerIsCorrect = 0;
1336 if (bannerIsCorrect) {
1341 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
1342 bannerIsCorrect = 0;
1343 errMsg <<
"The Matrix Market input file must contain a "
1344 "\"coordinate\"-format sparse graph in order to create a "
1345 "Tpetra::CrsGraph object from it, but the file's matrix "
1346 "type is \"" << pBanner->matrixType() <<
"\" instead.";
1351 if (tolerant && pBanner->matrixType() ==
"array") {
1352 bannerIsCorrect = 0;
1353 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
1354 "format sparse graph in order to create a Tpetra::CrsGraph "
1355 "object from it, but the file's matrix type is \"array\" "
1356 "instead. That probably means the file contains dense matrix "
1363 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
1370 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
1371 std::invalid_argument, errMsg.str ());
1373 if (debug && myRank == rootRank) {
1374 cerr <<
"-- Reading dimensions line" << endl;
1382 Tuple<global_ordinal_type, 3> dims =
1383 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
1385 if (debug && myRank == rootRank) {
1386 cerr <<
"-- Making Adder for collecting graph data" << endl;
1393 RCP<graph_adder_type> pAdder =
1394 makeGraphAdder (pComm, pBanner, dims, tolerant, debug);
1396 if (debug && myRank == rootRank) {
1397 cerr <<
"-- Reading graph data" << endl;
1407 int readSuccess = 1;
1408 std::ostringstream errMsg;
1409 if (myRank == rootRank) {
1412 typedef Teuchos::MatrixMarket::CoordPatternReader<graph_adder_type,
1414 reader_type reader (pAdder);
1417 std::pair<bool, std::vector<size_t> > results =
1418 reader.read (in, lineNumber, tolerant, debug);
1419 readSuccess = results.first ? 1 : 0;
1421 catch (std::exception& e) {
1426 broadcast (*pComm, rootRank, ptr (&readSuccess));
1435 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
1436 "Failed to read the Matrix Market sparse graph file: "
1440 if (debug && myRank == rootRank) {
1441 cerr <<
"-- Successfully read the Matrix Market data" << endl;
1454 if (debug && myRank == rootRank) {
1455 cerr <<
"-- Tolerant mode: rebroadcasting graph dimensions"
1457 <<
"----- Dimensions before: "
1458 << dims[0] <<
" x " << dims[1]
1462 Tuple<global_ordinal_type, 2> updatedDims;
1463 if (myRank == rootRank) {
1470 std::max (dims[0], pAdder->getAdder()->numRows());
1471 updatedDims[1] = pAdder->getAdder()->numCols();
1473 broadcast (*pComm, rootRank, updatedDims);
1474 dims[0] = updatedDims[0];
1475 dims[1] = updatedDims[1];
1476 if (debug && myRank == rootRank) {
1477 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
1491 if (myRank == rootRank) {
1498 if (dims[0] < pAdder->getAdder ()->numRows ()) {
1502 broadcast (*pComm, 0, ptr (&dimsMatch));
1503 if (dimsMatch == 0) {
1510 Tuple<global_ordinal_type, 2> addersDims;
1511 if (myRank == rootRank) {
1512 addersDims[0] = pAdder->getAdder()->numRows();
1513 addersDims[1] = pAdder->getAdder()->numCols();
1515 broadcast (*pComm, 0, addersDims);
1516 TEUCHOS_TEST_FOR_EXCEPTION(
1517 dimsMatch == 0, std::runtime_error,
1518 "The graph metadata says that the graph is " << dims[0] <<
" x "
1519 << dims[1] <<
", but the actual data says that the graph is "
1520 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
1521 "data includes more rows than reported in the metadata. This "
1522 "is not allowed when parsing in strict mode. Parse the graph in "
1523 "tolerant mode to ignore the metadata when it disagrees with the "
1529 RCP<map_type> proc0Map;
1531 if(Teuchos::is_null(rowMap)) {
1535 indexBase = rowMap->getIndexBase();
1537 if(myRank == rootRank) {
1538 proc0Map = rcp(
new map_type(dims[0],dims[0],indexBase,pComm));
1541 proc0Map = rcp(
new map_type(dims[0],0,indexBase,pComm));
1545 std::map<global_ordinal_type, size_t> numEntriesPerRow_map;
1546 if (myRank == rootRank) {
1547 const auto& entries = pAdder()->getAdder()->getEntries();
1552 for (
const auto& entry : entries) {
1554 ++numEntriesPerRow_map[gblRow];
1558 Teuchos::Array<size_t> numEntriesPerRow (proc0Map->getNodeNumElements ());
1559 for (
const auto& ent : numEntriesPerRow_map) {
1561 numEntriesPerRow[lclRow] = ent.second;
1568 std::map<global_ordinal_type, size_t> empty_map;
1569 std::swap (numEntriesPerRow_map, empty_map);
1572 RCP<sparse_graph_type> proc0Graph =
1574 StaticProfile,constructorParams));
1575 if(myRank == rootRank) {
1576 typedef Teuchos::MatrixMarket::Raw::GraphElement<global_ordinal_type> element_type;
1579 const std::vector<element_type>& entries =
1580 pAdder->getAdder()->getEntries();
1583 for(
size_t curPos=0; curPos<entries.size(); curPos++) {
1584 const element_type& curEntry = entries[curPos];
1587 Teuchos::ArrayView<const global_ordinal_type> colView(&curCol,1);
1588 proc0Graph->insertGlobalIndices(curRow,colView);
1591 proc0Graph->fillComplete();
1593 RCP<sparse_graph_type> distGraph;
1594 if(Teuchos::is_null(rowMap))
1597 RCP<map_type> distMap =
1598 rcp(
new map_type(dims[0],0,pComm,GloballyDistributed));
1601 distGraph = rcp(
new sparse_graph_type(distMap,colMap,0,StaticProfile,constructorParams));
1604 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1605 import_type importer (proc0Map, distMap);
1608 distGraph->doImport(*proc0Graph,importer,
INSERT);
1611 distGraph = rcp(
new sparse_graph_type(rowMap,colMap,0,StaticProfile,constructorParams));
1614 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1615 import_type importer (proc0Map, rowMap);
1618 distGraph->doImport(*proc0Graph,importer,
INSERT);
1648 static Teuchos::RCP<sparse_graph_type>
1650 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1651 const bool callFillComplete=
true,
1652 const bool tolerant=
false,
1653 const bool debug=
false)
1655 using Teuchos::broadcast;
1656 using Teuchos::outArg;
1664 if (comm->getRank () == 0) {
1666 in.open (filename.c_str ());
1667 opened = in.is_open();
1673 broadcast<int, int> (*comm, 0, outArg (opened));
1674 TEUCHOS_TEST_FOR_EXCEPTION
1675 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1676 "Failed to open file \"" << filename <<
"\" on Process 0.");
1714 static Teuchos::RCP<sparse_graph_type>
1716 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1717 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1718 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1719 const bool tolerant=
false,
1720 const bool debug=
false)
1722 using Teuchos::broadcast;
1723 using Teuchos::outArg;
1731 if (pComm->getRank () == 0) {
1733 in.open (filename.c_str ());
1734 opened = in.is_open();
1740 broadcast<int, int> (*pComm, 0, outArg (opened));
1741 TEUCHOS_TEST_FOR_EXCEPTION
1742 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1743 "Failed to open file \"" << filename <<
"\" on Process 0.");
1744 if (pComm->getRank () == 0) {
1745 in.open (filename.c_str ());
1749 fillCompleteParams, tolerant, debug);
1793 static Teuchos::RCP<sparse_graph_type>
1795 const Teuchos::RCP<const map_type>& rowMap,
1796 Teuchos::RCP<const map_type>& colMap,
1797 const Teuchos::RCP<const map_type>& domainMap,
1798 const Teuchos::RCP<const map_type>& rangeMap,
1799 const bool callFillComplete=
true,
1800 const bool tolerant=
false,
1801 const bool debug=
false)
1803 using Teuchos::broadcast;
1804 using Teuchos::Comm;
1805 using Teuchos::outArg;
1808 TEUCHOS_TEST_FOR_EXCEPTION
1809 (rowMap.is_null (), std::invalid_argument,
1810 "Input rowMap must be nonnull.");
1811 RCP<const Comm<int> > comm = rowMap->getComm ();
1812 if (comm.is_null ()) {
1815 return Teuchos::null;
1824 if (comm->getRank () == 0) {
1826 in.open (filename.c_str ());
1827 opened = in.is_open();
1833 broadcast<int, int> (*comm, 0, outArg (opened));
1834 TEUCHOS_TEST_FOR_EXCEPTION
1835 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1836 "Failed to open file \"" << filename <<
"\" on Process 0.");
1838 callFillComplete, tolerant, debug);
1866 static Teuchos::RCP<sparse_graph_type>
1868 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1869 const bool callFillComplete=
true,
1870 const bool tolerant=
false,
1871 const bool debug=
false)
1873 Teuchos::RCP<const map_type> fakeRowMap;
1874 Teuchos::RCP<const map_type> fakeColMap;
1875 Teuchos::RCP<Teuchos::ParameterList> fakeCtorParams;
1877 Teuchos::RCP<sparse_graph_type> graph =
1878 readSparseGraphHelper (in, pComm,
1879 fakeRowMap, fakeColMap,
1880 fakeCtorParams, tolerant, debug);
1881 if (callFillComplete) {
1882 graph->fillComplete ();
1917 static Teuchos::RCP<sparse_graph_type>
1919 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1920 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1921 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1922 const bool tolerant=
false,
1923 const bool debug=
false)
1925 Teuchos::RCP<const map_type> fakeRowMap;
1926 Teuchos::RCP<const map_type> fakeColMap;
1927 Teuchos::RCP<sparse_graph_type> graph =
1928 readSparseGraphHelper (in, pComm,
1929 fakeRowMap, fakeColMap,
1930 constructorParams, tolerant, debug);
1931 graph->fillComplete (fillCompleteParams);
1976 static Teuchos::RCP<sparse_graph_type>
1978 const Teuchos::RCP<const map_type>& rowMap,
1979 Teuchos::RCP<const map_type>& colMap,
1980 const Teuchos::RCP<const map_type>& domainMap,
1981 const Teuchos::RCP<const map_type>& rangeMap,
1982 const bool callFillComplete=
true,
1983 const bool tolerant=
false,
1984 const bool debug=
false)
1986 Teuchos::RCP<sparse_graph_type> graph =
1987 readSparseGraphHelper (in, rowMap->getComm (),
1988 rowMap, colMap, Teuchos::null, tolerant,
1990 if (callFillComplete) {
1991 graph->fillComplete (domainMap, rangeMap);
1996#include "MatrixMarket_TpetraNew.hpp"
2021 static Teuchos::RCP<sparse_matrix_type>
2023 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2024 const bool callFillComplete=
true,
2025 const bool tolerant=
false,
2026 const bool debug=
false)
2028 const int myRank = pComm->getRank ();
2033 in.open (filename.c_str ());
2041 return readSparse (in, pComm, callFillComplete, tolerant, debug);
2076 static Teuchos::RCP<sparse_matrix_type>
2078 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2079 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
2080 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
2081 const bool tolerant=
false,
2082 const bool debug=
false)
2085 if (pComm->getRank () == 0) {
2086 in.open (filename.c_str ());
2088 return readSparse (in, pComm, constructorParams,
2089 fillCompleteParams, tolerant, debug);
2130 static Teuchos::RCP<sparse_matrix_type>
2132 const Teuchos::RCP<const map_type>& rowMap,
2133 Teuchos::RCP<const map_type>& colMap,
2134 const Teuchos::RCP<const map_type>& domainMap,
2135 const Teuchos::RCP<const map_type>& rangeMap,
2136 const bool callFillComplete=
true,
2137 const bool tolerant=
false,
2138 const bool debug=
false)
2140 using Teuchos::broadcast;
2141 using Teuchos::Comm;
2142 using Teuchos::outArg;
2145 TEUCHOS_TEST_FOR_EXCEPTION(
2146 rowMap.is_null (), std::invalid_argument,
2147 "Row Map must be nonnull.");
2149 RCP<const Comm<int> > comm = rowMap->getComm ();
2150 const int myRank = comm->getRank ();
2160 in.open (filename.c_str ());
2161 opened = in.is_open();
2167 broadcast<int, int> (*comm, 0, outArg (opened));
2168 TEUCHOS_TEST_FOR_EXCEPTION(
2169 opened == 0, std::runtime_error,
2170 "readSparseFile: Failed to open file \"" << filename <<
"\" on "
2172 return readSparse (in, rowMap, colMap, domainMap, rangeMap,
2173 callFillComplete, tolerant, debug);
2201 static Teuchos::RCP<sparse_matrix_type>
2203 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2204 const bool callFillComplete=
true,
2205 const bool tolerant=
false,
2206 const bool debug=
false)
2208 using Teuchos::MatrixMarket::Banner;
2209 using Teuchos::arcp;
2210 using Teuchos::ArrayRCP;
2211 using Teuchos::broadcast;
2212 using Teuchos::null;
2215 using Teuchos::REDUCE_MAX;
2216 using Teuchos::reduceAll;
2217 using Teuchos::Tuple;
2220 typedef Teuchos::ScalarTraits<scalar_type> STS;
2222 const bool extraDebug =
false;
2223 const int myRank = pComm->getRank ();
2224 const int rootRank = 0;
2229 size_t lineNumber = 1;
2231 if (debug && myRank == rootRank) {
2232 cerr <<
"Matrix Market reader: readSparse:" << endl
2233 <<
"-- Reading banner line" << endl;
2242 RCP<const Banner> pBanner;
2248 int bannerIsCorrect = 1;
2249 std::ostringstream errMsg;
2251 if (myRank == rootRank) {
2254 pBanner = readBanner (in, lineNumber, tolerant, debug);
2256 catch (std::exception& e) {
2257 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2258 "threw an exception: " << e.what();
2259 bannerIsCorrect = 0;
2262 if (bannerIsCorrect) {
2267 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
2268 bannerIsCorrect = 0;
2269 errMsg <<
"The Matrix Market input file must contain a "
2270 "\"coordinate\"-format sparse matrix in order to create a "
2271 "Tpetra::CrsMatrix object from it, but the file's matrix "
2272 "type is \"" << pBanner->matrixType() <<
"\" instead.";
2277 if (tolerant && pBanner->matrixType() ==
"array") {
2278 bannerIsCorrect = 0;
2279 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2280 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2281 "object from it, but the file's matrix type is \"array\" "
2282 "instead. That probably means the file contains dense matrix "
2289 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
2296 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
2297 std::invalid_argument, errMsg.str ());
2299 if (debug && myRank == rootRank) {
2300 cerr <<
"-- Reading dimensions line" << endl;
2308 Tuple<global_ordinal_type, 3> dims =
2309 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
2311 if (debug && myRank == rootRank) {
2312 cerr <<
"-- Making Adder for collecting matrix data" << endl;
2317 RCP<adder_type> pAdder =
2318 makeAdder (pComm, pBanner, dims, tolerant, debug);
2320 if (debug && myRank == rootRank) {
2321 cerr <<
"-- Reading matrix data" << endl;
2331 int readSuccess = 1;
2332 std::ostringstream errMsg;
2333 if (myRank == rootRank) {
2336 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2338 reader_type reader (pAdder);
2341 std::pair<bool, std::vector<size_t> > results =
2342 reader.read (in, lineNumber, tolerant, debug);
2343 readSuccess = results.first ? 1 : 0;
2345 catch (std::exception& e) {
2350 broadcast (*pComm, rootRank, ptr (&readSuccess));
2359 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
2360 "Failed to read the Matrix Market sparse matrix file: "
2364 if (debug && myRank == rootRank) {
2365 cerr <<
"-- Successfully read the Matrix Market data" << endl;
2378 if (debug && myRank == rootRank) {
2379 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2381 <<
"----- Dimensions before: "
2382 << dims[0] <<
" x " << dims[1]
2386 Tuple<global_ordinal_type, 2> updatedDims;
2387 if (myRank == rootRank) {
2394 std::max (dims[0], pAdder->getAdder()->numRows());
2395 updatedDims[1] = pAdder->getAdder()->numCols();
2397 broadcast (*pComm, rootRank, updatedDims);
2398 dims[0] = updatedDims[0];
2399 dims[1] = updatedDims[1];
2400 if (debug && myRank == rootRank) {
2401 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
2415 if (myRank == rootRank) {
2422 if (dims[0] < pAdder->getAdder ()->numRows ()) {
2426 broadcast (*pComm, 0, ptr (&dimsMatch));
2427 if (dimsMatch == 0) {
2434 Tuple<global_ordinal_type, 2> addersDims;
2435 if (myRank == rootRank) {
2436 addersDims[0] = pAdder->getAdder()->numRows();
2437 addersDims[1] = pAdder->getAdder()->numCols();
2439 broadcast (*pComm, 0, addersDims);
2440 TEUCHOS_TEST_FOR_EXCEPTION(
2441 dimsMatch == 0, std::runtime_error,
2442 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
2443 << dims[1] <<
", but the actual data says that the matrix is "
2444 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
2445 "data includes more rows than reported in the metadata. This "
2446 "is not allowed when parsing in strict mode. Parse the matrix in "
2447 "tolerant mode to ignore the metadata when it disagrees with the "
2452 if (debug && myRank == rootRank) {
2453 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
2465 ArrayRCP<size_t> numEntriesPerRow;
2466 ArrayRCP<size_t> rowPtr;
2467 ArrayRCP<global_ordinal_type> colInd;
2468 ArrayRCP<scalar_type> values;
2473 int mergeAndConvertSucceeded = 1;
2474 std::ostringstream errMsg;
2476 if (myRank == rootRank) {
2478 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
2488 const size_type numRows = dims[0];
2491 pAdder->getAdder()->merge ();
2494 const std::vector<element_type>& entries =
2495 pAdder->getAdder()->getEntries();
2498 const size_t numEntries = (size_t)entries.size();
2501 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
2502 <<
" rows and numEntries=" << numEntries
2503 <<
" entries." << endl;
2509 numEntriesPerRow = arcp<size_t> (numRows);
2510 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
2511 rowPtr = arcp<size_t> (numRows+1);
2512 std::fill (rowPtr.begin(), rowPtr.end(), 0);
2513 colInd = arcp<global_ordinal_type> (numEntries);
2514 values = arcp<scalar_type> (numEntries);
2521 for (curPos = 0; curPos < numEntries; ++curPos) {
2522 const element_type& curEntry = entries[curPos];
2524 TEUCHOS_TEST_FOR_EXCEPTION(
2525 curRow < prvRow, std::logic_error,
2526 "Row indices are out of order, even though they are supposed "
2527 "to be sorted. curRow = " << curRow <<
", prvRow = "
2528 << prvRow <<
", at curPos = " << curPos <<
". Please report "
2529 "this bug to the Tpetra developers.");
2530 if (curRow > prvRow) {
2536 numEntriesPerRow[curRow]++;
2537 colInd[curPos] = curEntry.colIndex();
2538 values[curPos] = curEntry.value();
2543 rowPtr[numRows] = numEntries;
2545 catch (std::exception& e) {
2546 mergeAndConvertSucceeded = 0;
2547 errMsg <<
"Failed to merge sparse matrix entries and convert to "
2548 "CSR format: " << e.what();
2551 if (debug && mergeAndConvertSucceeded) {
2553 const size_type numRows = dims[0];
2554 const size_type maxToDisplay = 100;
2556 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
2557 << (numEntriesPerRow.size()-1) <<
"] ";
2558 if (numRows > maxToDisplay) {
2559 cerr <<
"(only showing first and last few entries) ";
2563 if (numRows > maxToDisplay) {
2564 for (size_type k = 0; k < 2; ++k) {
2565 cerr << numEntriesPerRow[k] <<
" ";
2568 for (size_type k = numRows-2; k < numRows-1; ++k) {
2569 cerr << numEntriesPerRow[k] <<
" ";
2573 for (size_type k = 0; k < numRows-1; ++k) {
2574 cerr << numEntriesPerRow[k] <<
" ";
2577 cerr << numEntriesPerRow[numRows-1];
2579 cerr <<
"]" << endl;
2581 cerr <<
"----- Proc 0: rowPtr ";
2582 if (numRows > maxToDisplay) {
2583 cerr <<
"(only showing first and last few entries) ";
2586 if (numRows > maxToDisplay) {
2587 for (size_type k = 0; k < 2; ++k) {
2588 cerr << rowPtr[k] <<
" ";
2591 for (size_type k = numRows-2; k < numRows; ++k) {
2592 cerr << rowPtr[k] <<
" ";
2596 for (size_type k = 0; k < numRows; ++k) {
2597 cerr << rowPtr[k] <<
" ";
2600 cerr << rowPtr[numRows] <<
"]" << endl;
2611 if (debug && myRank == rootRank) {
2612 cerr <<
"-- Making range, domain, and row maps" << endl;
2619 RCP<const map_type> pRangeMap = makeRangeMap (pComm, dims[0]);
2620 RCP<const map_type> pDomainMap =
2621 makeDomainMap (pRangeMap, dims[0], dims[1]);
2622 RCP<const map_type> pRowMap = makeRowMap (null, pComm, dims[0]);
2624 if (debug && myRank == rootRank) {
2625 cerr <<
"-- Distributing the matrix data" << endl;
2639 ArrayRCP<size_t> myNumEntriesPerRow;
2640 ArrayRCP<size_t> myRowPtr;
2641 ArrayRCP<global_ordinal_type> myColInd;
2642 ArrayRCP<scalar_type> myValues;
2644 distribute (myNumEntriesPerRow, myRowPtr, myColInd, myValues, pRowMap,
2645 numEntriesPerRow, rowPtr, colInd, values, debug);
2647 if (debug && myRank == rootRank) {
2648 cerr <<
"-- Inserting matrix entries on each processor";
2649 if (callFillComplete) {
2650 cerr <<
" and calling fillComplete()";
2661 RCP<sparse_matrix_type> pMatrix =
2662 makeMatrix (myNumEntriesPerRow, myRowPtr, myColInd, myValues,
2663 pRowMap, pRangeMap, pDomainMap, callFillComplete);
2668 int localIsNull = pMatrix.is_null () ? 1 : 0;
2669 int globalIsNull = 0;
2670 reduceAll (*pComm, REDUCE_MAX, localIsNull, ptr (&globalIsNull));
2671 TEUCHOS_TEST_FOR_EXCEPTION(globalIsNull != 0, std::logic_error,
2672 "Reader::makeMatrix() returned a null pointer on at least one "
2673 "process. Please report this bug to the Tpetra developers.");
2676 TEUCHOS_TEST_FOR_EXCEPTION(pMatrix.is_null(), std::logic_error,
2677 "Reader::makeMatrix() returned a null pointer. "
2678 "Please report this bug to the Tpetra developers.");
2690 if (callFillComplete) {
2691 const int numProcs = pComm->getSize ();
2693 if (extraDebug && debug) {
2695 pRangeMap->getGlobalNumElements ();
2697 pDomainMap->getGlobalNumElements ();
2698 if (myRank == rootRank) {
2699 cerr <<
"-- Matrix is "
2700 << globalNumRows <<
" x " << globalNumCols
2701 <<
" with " << pMatrix->getGlobalNumEntries()
2702 <<
" entries, and index base "
2703 << pMatrix->getIndexBase() <<
"." << endl;
2706 for (
int p = 0; p < numProcs; ++p) {
2708 cerr <<
"-- Proc " << p <<
" owns "
2709 << pMatrix->getNodeNumCols() <<
" columns, and "
2710 << pMatrix->getNodeNumEntries() <<
" entries." << endl;
2717 if (debug && myRank == rootRank) {
2718 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
2753 static Teuchos::RCP<sparse_matrix_type>
2755 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2756 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
2757 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
2758 const bool tolerant=
false,
2759 const bool debug=
false)
2761 using Teuchos::MatrixMarket::Banner;
2762 using Teuchos::arcp;
2763 using Teuchos::ArrayRCP;
2764 using Teuchos::broadcast;
2765 using Teuchos::null;
2768 using Teuchos::reduceAll;
2769 using Teuchos::Tuple;
2772 typedef Teuchos::ScalarTraits<scalar_type> STS;
2774 const bool extraDebug =
false;
2775 const int myRank = pComm->getRank ();
2776 const int rootRank = 0;
2781 size_t lineNumber = 1;
2783 if (debug && myRank == rootRank) {
2784 cerr <<
"Matrix Market reader: readSparse:" << endl
2785 <<
"-- Reading banner line" << endl;
2794 RCP<const Banner> pBanner;
2800 int bannerIsCorrect = 1;
2801 std::ostringstream errMsg;
2803 if (myRank == rootRank) {
2806 pBanner = readBanner (in, lineNumber, tolerant, debug);
2808 catch (std::exception& e) {
2809 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2810 "threw an exception: " << e.what();
2811 bannerIsCorrect = 0;
2814 if (bannerIsCorrect) {
2819 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
2820 bannerIsCorrect = 0;
2821 errMsg <<
"The Matrix Market input file must contain a "
2822 "\"coordinate\"-format sparse matrix in order to create a "
2823 "Tpetra::CrsMatrix object from it, but the file's matrix "
2824 "type is \"" << pBanner->matrixType() <<
"\" instead.";
2829 if (tolerant && pBanner->matrixType() ==
"array") {
2830 bannerIsCorrect = 0;
2831 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2832 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2833 "object from it, but the file's matrix type is \"array\" "
2834 "instead. That probably means the file contains dense matrix "
2841 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
2848 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
2849 std::invalid_argument, errMsg.str ());
2851 if (debug && myRank == rootRank) {
2852 cerr <<
"-- Reading dimensions line" << endl;
2860 Tuple<global_ordinal_type, 3> dims =
2861 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
2863 if (debug && myRank == rootRank) {
2864 cerr <<
"-- Making Adder for collecting matrix data" << endl;
2869 RCP<adder_type> pAdder =
2870 makeAdder (pComm, pBanner, dims, tolerant, debug);
2872 if (debug && myRank == rootRank) {
2873 cerr <<
"-- Reading matrix data" << endl;
2883 int readSuccess = 1;
2884 std::ostringstream errMsg;
2885 if (myRank == rootRank) {
2888 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2890 reader_type reader (pAdder);
2893 std::pair<bool, std::vector<size_t> > results =
2894 reader.read (in, lineNumber, tolerant, debug);
2895 readSuccess = results.first ? 1 : 0;
2897 catch (std::exception& e) {
2902 broadcast (*pComm, rootRank, ptr (&readSuccess));
2911 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
2912 "Failed to read the Matrix Market sparse matrix file: "
2916 if (debug && myRank == rootRank) {
2917 cerr <<
"-- Successfully read the Matrix Market data" << endl;
2930 if (debug && myRank == rootRank) {
2931 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2933 <<
"----- Dimensions before: "
2934 << dims[0] <<
" x " << dims[1]
2938 Tuple<global_ordinal_type, 2> updatedDims;
2939 if (myRank == rootRank) {
2946 std::max (dims[0], pAdder->getAdder()->numRows());
2947 updatedDims[1] = pAdder->getAdder()->numCols();
2949 broadcast (*pComm, rootRank, updatedDims);
2950 dims[0] = updatedDims[0];
2951 dims[1] = updatedDims[1];
2952 if (debug && myRank == rootRank) {
2953 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
2967 if (myRank == rootRank) {
2974 if (dims[0] < pAdder->getAdder ()->numRows ()) {
2978 broadcast (*pComm, 0, ptr (&dimsMatch));
2979 if (dimsMatch == 0) {
2986 Tuple<global_ordinal_type, 2> addersDims;
2987 if (myRank == rootRank) {
2988 addersDims[0] = pAdder->getAdder()->numRows();
2989 addersDims[1] = pAdder->getAdder()->numCols();
2991 broadcast (*pComm, 0, addersDims);
2992 TEUCHOS_TEST_FOR_EXCEPTION(
2993 dimsMatch == 0, std::runtime_error,
2994 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
2995 << dims[1] <<
", but the actual data says that the matrix is "
2996 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
2997 "data includes more rows than reported in the metadata. This "
2998 "is not allowed when parsing in strict mode. Parse the matrix in "
2999 "tolerant mode to ignore the metadata when it disagrees with the "
3004 if (debug && myRank == rootRank) {
3005 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
3017 ArrayRCP<size_t> numEntriesPerRow;
3018 ArrayRCP<size_t> rowPtr;
3019 ArrayRCP<global_ordinal_type> colInd;
3020 ArrayRCP<scalar_type> values;
3025 int mergeAndConvertSucceeded = 1;
3026 std::ostringstream errMsg;
3028 if (myRank == rootRank) {
3030 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
3040 const size_type numRows = dims[0];
3043 pAdder->getAdder()->merge ();
3046 const std::vector<element_type>& entries =
3047 pAdder->getAdder()->getEntries();
3050 const size_t numEntries = (size_t)entries.size();
3053 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
3054 <<
" rows and numEntries=" << numEntries
3055 <<
" entries." << endl;
3061 numEntriesPerRow = arcp<size_t> (numRows);
3062 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
3063 rowPtr = arcp<size_t> (numRows+1);
3064 std::fill (rowPtr.begin(), rowPtr.end(), 0);
3065 colInd = arcp<global_ordinal_type> (numEntries);
3066 values = arcp<scalar_type> (numEntries);
3073 for (curPos = 0; curPos < numEntries; ++curPos) {
3074 const element_type& curEntry = entries[curPos];
3076 TEUCHOS_TEST_FOR_EXCEPTION(
3077 curRow < prvRow, std::logic_error,
3078 "Row indices are out of order, even though they are supposed "
3079 "to be sorted. curRow = " << curRow <<
", prvRow = "
3080 << prvRow <<
", at curPos = " << curPos <<
". Please report "
3081 "this bug to the Tpetra developers.");
3082 if (curRow > prvRow) {
3088 numEntriesPerRow[curRow]++;
3089 colInd[curPos] = curEntry.colIndex();
3090 values[curPos] = curEntry.value();
3095 rowPtr[numRows] = numEntries;
3097 catch (std::exception& e) {
3098 mergeAndConvertSucceeded = 0;
3099 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3100 "CSR format: " << e.what();
3103 if (debug && mergeAndConvertSucceeded) {
3105 const size_type numRows = dims[0];
3106 const size_type maxToDisplay = 100;
3108 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3109 << (numEntriesPerRow.size()-1) <<
"] ";
3110 if (numRows > maxToDisplay) {
3111 cerr <<
"(only showing first and last few entries) ";
3115 if (numRows > maxToDisplay) {
3116 for (size_type k = 0; k < 2; ++k) {
3117 cerr << numEntriesPerRow[k] <<
" ";
3120 for (size_type k = numRows-2; k < numRows-1; ++k) {
3121 cerr << numEntriesPerRow[k] <<
" ";
3125 for (size_type k = 0; k < numRows-1; ++k) {
3126 cerr << numEntriesPerRow[k] <<
" ";
3129 cerr << numEntriesPerRow[numRows-1];
3131 cerr <<
"]" << endl;
3133 cerr <<
"----- Proc 0: rowPtr ";
3134 if (numRows > maxToDisplay) {
3135 cerr <<
"(only showing first and last few entries) ";
3138 if (numRows > maxToDisplay) {
3139 for (size_type k = 0; k < 2; ++k) {
3140 cerr << rowPtr[k] <<
" ";
3143 for (size_type k = numRows-2; k < numRows; ++k) {
3144 cerr << rowPtr[k] <<
" ";
3148 for (size_type k = 0; k < numRows; ++k) {
3149 cerr << rowPtr[k] <<
" ";
3152 cerr << rowPtr[numRows] <<
"]" << endl;
3163 if (debug && myRank == rootRank) {
3164 cerr <<
"-- Making range, domain, and row maps" << endl;
3171 RCP<const map_type> pRangeMap = makeRangeMap (pComm, dims[0]);
3172 RCP<const map_type> pDomainMap =
3173 makeDomainMap (pRangeMap, dims[0], dims[1]);
3174 RCP<const map_type> pRowMap = makeRowMap (null, pComm, dims[0]);
3176 if (debug && myRank == rootRank) {
3177 cerr <<
"-- Distributing the matrix data" << endl;
3191 ArrayRCP<size_t> myNumEntriesPerRow;
3192 ArrayRCP<size_t> myRowPtr;
3193 ArrayRCP<global_ordinal_type> myColInd;
3194 ArrayRCP<scalar_type> myValues;
3196 distribute (myNumEntriesPerRow, myRowPtr, myColInd, myValues, pRowMap,
3197 numEntriesPerRow, rowPtr, colInd, values, debug);
3199 if (debug && myRank == rootRank) {
3200 cerr <<
"-- Inserting matrix entries on each process "
3201 "and calling fillComplete()" << endl;
3210 Teuchos::RCP<sparse_matrix_type> pMatrix =
3211 makeMatrix (myNumEntriesPerRow, myRowPtr, myColInd, myValues,
3212 pRowMap, pRangeMap, pDomainMap, constructorParams,
3213 fillCompleteParams);
3218 int localIsNull = pMatrix.is_null () ? 1 : 0;
3219 int globalIsNull = 0;
3220 reduceAll (*pComm, Teuchos::REDUCE_MAX, localIsNull, ptr (&globalIsNull));
3221 TEUCHOS_TEST_FOR_EXCEPTION(globalIsNull != 0, std::logic_error,
3222 "Reader::makeMatrix() returned a null pointer on at least one "
3223 "process. Please report this bug to the Tpetra developers.");
3226 TEUCHOS_TEST_FOR_EXCEPTION(pMatrix.is_null(), std::logic_error,
3227 "Reader::makeMatrix() returned a null pointer. "
3228 "Please report this bug to the Tpetra developers.");
3237 if (extraDebug && debug) {
3238 const int numProcs = pComm->getSize ();
3240 pRangeMap->getGlobalNumElements();
3242 pDomainMap->getGlobalNumElements();
3243 if (myRank == rootRank) {
3244 cerr <<
"-- Matrix is "
3245 << globalNumRows <<
" x " << globalNumCols
3246 <<
" with " << pMatrix->getGlobalNumEntries()
3247 <<
" entries, and index base "
3248 << pMatrix->getIndexBase() <<
"." << endl;
3251 for (
int p = 0; p < numProcs; ++p) {
3253 cerr <<
"-- Proc " << p <<
" owns "
3254 << pMatrix->getNodeNumCols() <<
" columns, and "
3255 << pMatrix->getNodeNumEntries() <<
" entries." << endl;
3261 if (debug && myRank == rootRank) {
3262 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
3309 static Teuchos::RCP<sparse_matrix_type>
3311 const Teuchos::RCP<const map_type>& rowMap,
3312 Teuchos::RCP<const map_type>& colMap,
3313 const Teuchos::RCP<const map_type>& domainMap,
3314 const Teuchos::RCP<const map_type>& rangeMap,
3315 const bool callFillComplete=
true,
3316 const bool tolerant=
false,
3317 const bool debug=
false)
3319 using Teuchos::MatrixMarket::Banner;
3320 using Teuchos::arcp;
3321 using Teuchos::ArrayRCP;
3322 using Teuchos::ArrayView;
3324 using Teuchos::broadcast;
3325 using Teuchos::Comm;
3326 using Teuchos::null;
3329 using Teuchos::reduceAll;
3330 using Teuchos::Tuple;
3333 typedef Teuchos::ScalarTraits<scalar_type> STS;
3335 RCP<const Comm<int> > pComm = rowMap->getComm ();
3336 const int myRank = pComm->getRank ();
3337 const int rootRank = 0;
3338 const bool extraDebug =
false;
3343 TEUCHOS_TEST_FOR_EXCEPTION(
3344 rowMap.is_null (), std::invalid_argument,
3345 "Row Map must be nonnull.");
3346 TEUCHOS_TEST_FOR_EXCEPTION(
3347 rangeMap.is_null (), std::invalid_argument,
3348 "Range Map must be nonnull.");
3349 TEUCHOS_TEST_FOR_EXCEPTION(
3350 domainMap.is_null (), std::invalid_argument,
3351 "Domain Map must be nonnull.");
3352 TEUCHOS_TEST_FOR_EXCEPTION(
3353 rowMap->getComm().getRawPtr() != pComm.getRawPtr(),
3354 std::invalid_argument,
3355 "The specified row Map's communicator (rowMap->getComm())"
3356 "differs from the given separately supplied communicator pComm.");
3357 TEUCHOS_TEST_FOR_EXCEPTION(
3358 domainMap->getComm().getRawPtr() != pComm.getRawPtr(),
3359 std::invalid_argument,
3360 "The specified domain Map's communicator (domainMap->getComm())"
3361 "differs from the given separately supplied communicator pComm.");
3362 TEUCHOS_TEST_FOR_EXCEPTION(
3363 rangeMap->getComm().getRawPtr() != pComm.getRawPtr(),
3364 std::invalid_argument,
3365 "The specified range Map's communicator (rangeMap->getComm())"
3366 "differs from the given separately supplied communicator pComm.");
3371 size_t lineNumber = 1;
3373 if (debug && myRank == rootRank) {
3374 cerr <<
"Matrix Market reader: readSparse:" << endl
3375 <<
"-- Reading banner line" << endl;
3384 RCP<const Banner> pBanner;
3390 int bannerIsCorrect = 1;
3391 std::ostringstream errMsg;
3393 if (myRank == rootRank) {
3396 pBanner = readBanner (in, lineNumber, tolerant, debug);
3398 catch (std::exception& e) {
3399 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
3400 "threw an exception: " << e.what();
3401 bannerIsCorrect = 0;
3404 if (bannerIsCorrect) {
3409 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
3410 bannerIsCorrect = 0;
3411 errMsg <<
"The Matrix Market input file must contain a "
3412 "\"coordinate\"-format sparse matrix in order to create a "
3413 "Tpetra::CrsMatrix object from it, but the file's matrix "
3414 "type is \"" << pBanner->matrixType() <<
"\" instead.";
3419 if (tolerant && pBanner->matrixType() ==
"array") {
3420 bannerIsCorrect = 0;
3421 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
3422 "format sparse matrix in order to create a Tpetra::CrsMatrix "
3423 "object from it, but the file's matrix type is \"array\" "
3424 "instead. That probably means the file contains dense matrix "
3431 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
3438 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
3439 std::invalid_argument, errMsg.str ());
3441 if (debug && myRank == rootRank) {
3442 cerr <<
"-- Reading dimensions line" << endl;
3450 Tuple<global_ordinal_type, 3> dims =
3451 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
3453 if (debug && myRank == rootRank) {
3454 cerr <<
"-- Making Adder for collecting matrix data" << endl;
3461 RCP<adder_type> pAdder =
3462 makeAdder (pComm, pBanner, dims, tolerant, debug);
3464 if (debug && myRank == rootRank) {
3465 cerr <<
"-- Reading matrix data" << endl;
3475 int readSuccess = 1;
3476 std::ostringstream errMsg;
3477 if (myRank == rootRank) {
3480 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
3482 reader_type reader (pAdder);
3485 std::pair<bool, std::vector<size_t> > results =
3486 reader.read (in, lineNumber, tolerant, debug);
3487 readSuccess = results.first ? 1 : 0;
3489 catch (std::exception& e) {
3494 broadcast (*pComm, rootRank, ptr (&readSuccess));
3503 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
3504 "Failed to read the Matrix Market sparse matrix file: "
3508 if (debug && myRank == rootRank) {
3509 cerr <<
"-- Successfully read the Matrix Market data" << endl;
3522 if (debug && myRank == rootRank) {
3523 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
3525 <<
"----- Dimensions before: "
3526 << dims[0] <<
" x " << dims[1]
3530 Tuple<global_ordinal_type, 2> updatedDims;
3531 if (myRank == rootRank) {
3538 std::max (dims[0], pAdder->getAdder()->numRows());
3539 updatedDims[1] = pAdder->getAdder()->numCols();
3541 broadcast (*pComm, rootRank, updatedDims);
3542 dims[0] = updatedDims[0];
3543 dims[1] = updatedDims[1];
3544 if (debug && myRank == rootRank) {
3545 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
3559 if (myRank == rootRank) {
3566 if (dims[0] < pAdder->getAdder ()->numRows ()) {
3570 broadcast (*pComm, 0, ptr (&dimsMatch));
3571 if (dimsMatch == 0) {
3578 Tuple<global_ordinal_type, 2> addersDims;
3579 if (myRank == rootRank) {
3580 addersDims[0] = pAdder->getAdder()->numRows();
3581 addersDims[1] = pAdder->getAdder()->numCols();
3583 broadcast (*pComm, 0, addersDims);
3584 TEUCHOS_TEST_FOR_EXCEPTION(
3585 dimsMatch == 0, std::runtime_error,
3586 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
3587 << dims[1] <<
", but the actual data says that the matrix is "
3588 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
3589 "data includes more rows than reported in the metadata. This "
3590 "is not allowed when parsing in strict mode. Parse the matrix in "
3591 "tolerant mode to ignore the metadata when it disagrees with the "
3596 if (debug && myRank == rootRank) {
3597 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
3609 ArrayRCP<size_t> numEntriesPerRow;
3610 ArrayRCP<size_t> rowPtr;
3611 ArrayRCP<global_ordinal_type> colInd;
3612 ArrayRCP<scalar_type> values;
3613 size_t maxNumEntriesPerRow = 0;
3618 int mergeAndConvertSucceeded = 1;
3619 std::ostringstream errMsg;
3621 if (myRank == rootRank) {
3623 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
3633 const size_type numRows = dims[0];
3636 pAdder->getAdder()->merge ();
3639 const std::vector<element_type>& entries =
3640 pAdder->getAdder()->getEntries();
3643 const size_t numEntries = (size_t)entries.size();
3646 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
3647 <<
" rows and numEntries=" << numEntries
3648 <<
" entries." << endl;
3654 numEntriesPerRow = arcp<size_t> (numRows);
3655 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
3656 rowPtr = arcp<size_t> (numRows+1);
3657 std::fill (rowPtr.begin(), rowPtr.end(), 0);
3658 colInd = arcp<global_ordinal_type> (numEntries);
3659 values = arcp<scalar_type> (numEntries);
3666 for (curPos = 0; curPos < numEntries; ++curPos) {
3667 const element_type& curEntry = entries[curPos];
3669 TEUCHOS_TEST_FOR_EXCEPTION(
3670 curRow < prvRow, std::logic_error,
3671 "Row indices are out of order, even though they are supposed "
3672 "to be sorted. curRow = " << curRow <<
", prvRow = "
3673 << prvRow <<
", at curPos = " << curPos <<
". Please report "
3674 "this bug to the Tpetra developers.");
3675 if (curRow > prvRow) {
3681 numEntriesPerRow[curRow]++;
3682 colInd[curPos] = curEntry.colIndex();
3683 values[curPos] = curEntry.value();
3688 rowPtr[numRows] = numEntries;
3690 catch (std::exception& e) {
3691 mergeAndConvertSucceeded = 0;
3692 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3693 "CSR format: " << e.what();
3696 if (debug && mergeAndConvertSucceeded) {
3698 const size_type numRows = dims[0];
3699 const size_type maxToDisplay = 100;
3701 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3702 << (numEntriesPerRow.size()-1) <<
"] ";
3703 if (numRows > maxToDisplay) {
3704 cerr <<
"(only showing first and last few entries) ";
3708 if (numRows > maxToDisplay) {
3709 for (size_type k = 0; k < 2; ++k) {
3710 cerr << numEntriesPerRow[k] <<
" ";
3713 for (size_type k = numRows-2; k < numRows-1; ++k) {
3714 cerr << numEntriesPerRow[k] <<
" ";
3718 for (size_type k = 0; k < numRows-1; ++k) {
3719 cerr << numEntriesPerRow[k] <<
" ";
3722 cerr << numEntriesPerRow[numRows-1];
3724 cerr <<
"]" << endl;
3726 cerr <<
"----- Proc 0: rowPtr ";
3727 if (numRows > maxToDisplay) {
3728 cerr <<
"(only showing first and last few entries) ";
3731 if (numRows > maxToDisplay) {
3732 for (size_type k = 0; k < 2; ++k) {
3733 cerr << rowPtr[k] <<
" ";
3736 for (size_type k = numRows-2; k < numRows; ++k) {
3737 cerr << rowPtr[k] <<
" ";
3741 for (size_type k = 0; k < numRows; ++k) {
3742 cerr << rowPtr[k] <<
" ";
3745 cerr << rowPtr[numRows] <<
"]" << endl;
3747 cerr <<
"----- Proc 0: colInd = [";
3748 for (
size_t k = 0; k < rowPtr[numRows]; ++k) {
3749 cerr << colInd[k] <<
" ";
3751 cerr <<
"]" << endl;
3765 if (debug && myRank == rootRank) {
3766 cerr <<
"-- Verifying Maps" << endl;
3768 TEUCHOS_TEST_FOR_EXCEPTION(
3769 as<global_size_t> (dims[0]) != rangeMap->getGlobalNumElements(),
3770 std::invalid_argument,
3771 "The range Map has " << rangeMap->getGlobalNumElements ()
3772 <<
" entries, but the matrix has a global number of rows " << dims[0]
3774 TEUCHOS_TEST_FOR_EXCEPTION(
3775 as<global_size_t> (dims[1]) != domainMap->getGlobalNumElements (),
3776 std::invalid_argument,
3777 "The domain Map has " << domainMap->getGlobalNumElements ()
3778 <<
" entries, but the matrix has a global number of columns "
3782 RCP<Teuchos::FancyOStream> err = debug ?
3783 Teuchos::getFancyOStream (Teuchos::rcpFromRef (cerr)) : null;
3785 RCP<const map_type> gatherRowMap = Details::computeGatherMap (rowMap, err, debug);
3786 ArrayView<const global_ordinal_type> myRows =
3787 gatherRowMap->getNodeElementList ();
3788 const size_type myNumRows = myRows.size ();
3791 ArrayRCP<size_t> gatherNumEntriesPerRow = arcp<size_t>(myNumRows);
3792 for (size_type i_ = 0; i_ < myNumRows; i_++) {
3793 gatherNumEntriesPerRow[i_] = numEntriesPerRow[myRows[i_]-indexBase];
3794 if (gatherNumEntriesPerRow[i_] > maxNumEntriesPerRow)
3795 maxNumEntriesPerRow = gatherNumEntriesPerRow[i_];
3801 RCP<sparse_matrix_type> A_proc0 =
3803 Tpetra::StaticProfile));
3804 if (myRank == rootRank) {
3806 cerr <<
"-- Proc 0: Filling gather matrix" << endl;
3809 cerr <<
"---- Rows: " << Teuchos::toString (myRows) << endl;
3813 for (size_type i_ = 0; i_ < myNumRows; ++i_) {
3814 size_type i = myRows[i_] - indexBase;
3816 const size_type curPos = as<size_type> (rowPtr[i]);
3818 ArrayView<global_ordinal_type> curColInd =
3819 colInd.view (curPos, curNumEntries);
3820 ArrayView<scalar_type> curValues =
3821 values.view (curPos, curNumEntries);
3824 for (size_type k = 0; k < curNumEntries; ++k) {
3825 curColInd[k] += indexBase;
3828 cerr <<
"------ Columns: " << Teuchos::toString (curColInd) << endl;
3829 cerr <<
"------ Values: " << Teuchos::toString (curValues) << endl;
3832 if (curNumEntries > 0) {
3833 A_proc0->insertGlobalValues (myRows[i_], curColInd, curValues);
3839 numEntriesPerRow = null;
3845 broadcast<int,size_t> (*pComm, 0, &maxNumEntriesPerRow);
3847 RCP<sparse_matrix_type> A;
3848 if (colMap.is_null ()) {
3854 export_type exp (gatherRowMap, rowMap);
3855 A->doExport (*A_proc0, exp,
INSERT);
3857 if (callFillComplete) {
3858 A->fillComplete (domainMap, rangeMap);
3870 if (callFillComplete) {
3871 const int numProcs = pComm->getSize ();
3873 if (extraDebug && debug) {
3874 const global_size_t globalNumRows = rangeMap->getGlobalNumElements ();
3875 const global_size_t globalNumCols = domainMap->getGlobalNumElements ();
3876 if (myRank == rootRank) {
3877 cerr <<
"-- Matrix is "
3878 << globalNumRows <<
" x " << globalNumCols
3879 <<
" with " << A->getGlobalNumEntries()
3880 <<
" entries, and index base "
3881 << A->getIndexBase() <<
"." << endl;
3884 for (
int p = 0; p < numProcs; ++p) {
3886 cerr <<
"-- Proc " << p <<
" owns "
3887 << A->getNodeNumCols() <<
" columns, and "
3888 << A->getNodeNumEntries() <<
" entries." << endl;
3895 if (debug && myRank == rootRank) {
3896 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
3930 static Teuchos::RCP<multivector_type>
3932 const Teuchos::RCP<const comm_type>& comm,
3933 Teuchos::RCP<const map_type>& map,
3934 const bool tolerant=
false,
3935 const bool debug=
false)
3937 using Teuchos::broadcast;
3938 using Teuchos::outArg;
3942 if (comm->getRank() == 0) {
3944 in.open (filename.c_str ());
3945 opened = in.is_open();
3951 broadcast<int, int> (*comm, 0, outArg (opened));
3952 TEUCHOS_TEST_FOR_EXCEPTION(
3953 opened == 0, std::runtime_error,
3954 "readDenseFile: Failed to open file \"" << filename <<
"\" on "
3956 return readDense (in, comm, map, tolerant, debug);
3989 static Teuchos::RCP<vector_type>
3991 const Teuchos::RCP<const comm_type>& comm,
3992 Teuchos::RCP<const map_type>& map,
3993 const bool tolerant=
false,
3994 const bool debug=
false)
3996 using Teuchos::broadcast;
3997 using Teuchos::outArg;
4001 if (comm->getRank() == 0) {
4003 in.open (filename.c_str ());
4004 opened = in.is_open();
4010 broadcast<int, int> (*comm, 0, outArg (opened));
4011 TEUCHOS_TEST_FOR_EXCEPTION(
4012 opened == 0, std::runtime_error,
4013 "readVectorFile: Failed to open file \"" << filename <<
"\" on "
4015 return readVector (in, comm, map, tolerant, debug);
4085 static Teuchos::RCP<multivector_type>
4087 const Teuchos::RCP<const comm_type>& comm,
4088 Teuchos::RCP<const map_type>& map,
4089 const bool tolerant=
false,
4090 const bool debug=
false)
4092 Teuchos::RCP<Teuchos::FancyOStream> err =
4093 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4094 return readDenseImpl<scalar_type> (in, comm, map, err, tolerant, debug);
4099 static Teuchos::RCP<vector_type>
4101 const Teuchos::RCP<const comm_type>& comm,
4102 Teuchos::RCP<const map_type>& map,
4103 const bool tolerant=
false,
4104 const bool debug=
false)
4106 Teuchos::RCP<Teuchos::FancyOStream> err =
4107 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4108 return readVectorImpl<scalar_type> (in, comm, map, err, tolerant, debug);
4132 static Teuchos::RCP<const map_type>
4134 const Teuchos::RCP<const comm_type>& comm,
4135 const bool tolerant=
false,
4136 const bool debug=
false)
4138 using Teuchos::inOutArg;
4139 using Teuchos::broadcast;
4143 if (comm->getRank () == 0) {
4144 in.open (filename.c_str ());
4149 broadcast<int, int> (*comm, 0, inOutArg (success));
4150 TEUCHOS_TEST_FOR_EXCEPTION(
4151 success == 0, std::runtime_error,
4152 "Tpetra::MatrixMarket::Reader::readMapFile: "
4153 "Failed to read file \"" << filename <<
"\" on Process 0.");
4154 return readMap (in, comm, tolerant, debug);
4159 template<
class MultiVectorScalarType>
4164 readDenseImpl (std::istream& in,
4165 const Teuchos::RCP<const comm_type>& comm,
4166 Teuchos::RCP<const map_type>& map,
4167 const Teuchos::RCP<Teuchos::FancyOStream>& err,
4168 const bool tolerant=
false,
4169 const bool debug=
false)
4171 using Teuchos::MatrixMarket::Banner;
4172 using Teuchos::MatrixMarket::checkCommentLine;
4173 using Teuchos::ArrayRCP;
4175 using Teuchos::broadcast;
4176 using Teuchos::outArg;
4178 using Teuchos::Tuple;
4180 typedef MultiVectorScalarType ST;
4184 typedef Teuchos::ScalarTraits<ST> STS;
4185 typedef typename STS::magnitudeType MT;
4186 typedef Teuchos::ScalarTraits<MT> STM;
4191 const int myRank = comm->getRank ();
4193 if (! err.is_null ()) {
4197 *err << myRank <<
": readDenseImpl" << endl;
4199 if (! err.is_null ()) {
4233 size_t lineNumber = 1;
4236 std::ostringstream exMsg;
4237 int localBannerReadSuccess = 1;
4238 int localDimsReadSuccess = 1;
4243 *err << myRank <<
": readDenseImpl: Reading banner line (dense)" << endl;
4249 RCP<const Banner> pBanner;
4251 pBanner = readBanner (in, lineNumber, tolerant, debug);
4252 }
catch (std::exception& e) {
4254 localBannerReadSuccess = 0;
4257 if (localBannerReadSuccess) {
4258 if (pBanner->matrixType () !=
"array") {
4259 exMsg <<
"The Matrix Market file does not contain dense matrix "
4260 "data. Its banner (first) line says that its matrix type is \""
4261 << pBanner->matrixType () <<
"\", rather that the required "
4263 localBannerReadSuccess = 0;
4264 }
else if (pBanner->dataType() ==
"pattern") {
4265 exMsg <<
"The Matrix Market file's banner (first) "
4266 "line claims that the matrix's data type is \"pattern\". This does "
4267 "not make sense for a dense matrix, yet the file reports the matrix "
4268 "as dense. The only valid data types for a dense matrix are "
4269 "\"real\", \"complex\", and \"integer\".";
4270 localBannerReadSuccess = 0;
4274 dims[2] = encodeDataType (pBanner->dataType ());
4280 if (localBannerReadSuccess) {
4282 *err << myRank <<
": readDenseImpl: Reading dimensions line (dense)" << endl;
4291 bool commentLine =
true;
4293 while (commentLine) {
4296 if (in.eof () || in.fail ()) {
4297 exMsg <<
"Unable to get array dimensions line (at all) from line "
4298 << lineNumber <<
" of input stream. The input stream "
4299 <<
"claims that it is "
4300 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4301 localDimsReadSuccess = 0;
4304 if (getline (in, line)) {
4311 size_t start = 0, size = 0;
4312 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4319 std::istringstream istr (line);
4323 if (istr.eof () || istr.fail ()) {
4324 exMsg <<
"Unable to read any data from line " << lineNumber
4325 <<
" of input; the line should contain the matrix dimensions "
4326 <<
"\"<numRows> <numCols>\".";
4327 localDimsReadSuccess = 0;
4332 exMsg <<
"Failed to get number of rows from line "
4333 << lineNumber <<
" of input; the line should contains the "
4334 <<
"matrix dimensions \"<numRows> <numCols>\".";
4335 localDimsReadSuccess = 0;
4337 dims[0] = theNumRows;
4339 exMsg <<
"No more data after number of rows on line "
4340 << lineNumber <<
" of input; the line should contain the "
4341 <<
"matrix dimensions \"<numRows> <numCols>\".";
4342 localDimsReadSuccess = 0;
4347 exMsg <<
"Failed to get number of columns from line "
4348 << lineNumber <<
" of input; the line should contain "
4349 <<
"the matrix dimensions \"<numRows> <numCols>\".";
4350 localDimsReadSuccess = 0;
4352 dims[1] = theNumCols;
4363 Tuple<GO, 5> bannerDimsReadResult;
4365 bannerDimsReadResult[0] = dims[0];
4366 bannerDimsReadResult[1] = dims[1];
4367 bannerDimsReadResult[2] = dims[2];
4368 bannerDimsReadResult[3] = localBannerReadSuccess;
4369 bannerDimsReadResult[4] = localDimsReadSuccess;
4373 broadcast (*comm, 0, bannerDimsReadResult);
4375 TEUCHOS_TEST_FOR_EXCEPTION(
4376 bannerDimsReadResult[3] == 0, std::runtime_error,
4377 "Failed to read banner line: " << exMsg.str ());
4378 TEUCHOS_TEST_FOR_EXCEPTION(
4379 bannerDimsReadResult[4] == 0, std::runtime_error,
4380 "Failed to read matrix dimensions line: " << exMsg.str ());
4382 dims[0] = bannerDimsReadResult[0];
4383 dims[1] = bannerDimsReadResult[1];
4384 dims[2] = bannerDimsReadResult[2];
4389 const size_t numCols =
static_cast<size_t> (dims[1]);
4394 RCP<const map_type> proc0Map;
4395 if (map.is_null ()) {
4399 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
4400 const size_t localNumRows = (myRank == 0) ? numRows : 0;
4402 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
4406 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
4410 RCP<MV> X = createMultiVector<ST, LO, GO, NT> (proc0Map, numCols);
4416 int localReadDataSuccess = 1;
4420 *err << myRank <<
": readDenseImpl: Reading matrix data (dense)"
4425 TEUCHOS_TEST_FOR_EXCEPTION(
4426 ! X->isConstantStride (), std::logic_error,
4427 "Can't get a 1-D view of the entries of the MultiVector X on "
4428 "Process 0, because the stride between the columns of X is not "
4429 "constant. This shouldn't happen because we just created X and "
4430 "haven't filled it in yet. Please report this bug to the Tpetra "
4437 ArrayRCP<ST> X_view = X->get1dViewNonConst ();
4438 TEUCHOS_TEST_FOR_EXCEPTION(
4439 as<global_size_t> (X_view.size ()) < numRows * numCols,
4441 "The view of X has size " << X_view <<
" which is not enough to "
4442 "accommodate the expected number of entries numRows*numCols = "
4443 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
4444 "Please report this bug to the Tpetra developers.");
4445 const size_t stride = X->getStride ();
4452 const bool isComplex = (dims[2] == 1);
4453 size_type count = 0, curRow = 0, curCol = 0;
4456 while (getline (in, line)) {
4460 size_t start = 0, size = 0;
4461 const bool commentLine =
4462 checkCommentLine (line, start, size, lineNumber, tolerant);
4463 if (! commentLine) {
4469 if (count >= X_view.size()) {
4474 TEUCHOS_TEST_FOR_EXCEPTION(
4475 count >= X_view.size(),
4477 "The Matrix Market input stream has more data in it than "
4478 "its metadata reported. Current line number is "
4479 << lineNumber <<
".");
4488 const size_t pos = line.substr (start, size).find (
':');
4489 if (pos != std::string::npos) {
4493 std::istringstream istr (line.substr (start, size));
4497 if (istr.eof() || istr.fail()) {
4504 TEUCHOS_TEST_FOR_EXCEPTION(
4505 ! tolerant && (istr.eof() || istr.fail()),
4507 "Line " << lineNumber <<
" of the Matrix Market file is "
4508 "empty, or we cannot read from it for some other reason.");
4511 ST val = STS::zero();
4514 MT real = STM::zero(), imag = STM::zero();
4531 TEUCHOS_TEST_FOR_EXCEPTION(
4532 ! tolerant && istr.eof(), std::runtime_error,
4533 "Failed to get the real part of a complex-valued matrix "
4534 "entry from line " << lineNumber <<
" of the Matrix Market "
4540 }
else if (istr.eof()) {
4541 TEUCHOS_TEST_FOR_EXCEPTION(
4542 ! tolerant && istr.eof(), std::runtime_error,
4543 "Missing imaginary part of a complex-valued matrix entry "
4544 "on line " << lineNumber <<
" of the Matrix Market file.");
4550 TEUCHOS_TEST_FOR_EXCEPTION(
4551 ! tolerant && istr.fail(), std::runtime_error,
4552 "Failed to get the imaginary part of a complex-valued "
4553 "matrix entry from line " << lineNumber <<
" of the "
4554 "Matrix Market file.");
4561 TEUCHOS_TEST_FOR_EXCEPTION(
4562 ! tolerant && istr.fail(), std::runtime_error,
4563 "Failed to get a real-valued matrix entry from line "
4564 << lineNumber <<
" of the Matrix Market file.");
4567 if (istr.fail() && tolerant) {
4573 TEUCHOS_TEST_FOR_EXCEPTION(
4574 ! tolerant && istr.fail(), std::runtime_error,
4575 "Failed to read matrix data from line " << lineNumber
4576 <<
" of the Matrix Market file.");
4579 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
4581 curRow = count % numRows;
4582 curCol = count / numRows;
4583 X_view[curRow + curCol*stride] = val;
4588 TEUCHOS_TEST_FOR_EXCEPTION(
4589 ! tolerant &&
static_cast<global_size_t> (count) < numRows * numCols,
4591 "The Matrix Market metadata reports that the dense matrix is "
4592 << numRows <<
" x " << numCols <<
", and thus has "
4593 << numRows*numCols <<
" total entries, but we only found " << count
4594 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
4595 }
catch (std::exception& e) {
4597 localReadDataSuccess = 0;
4602 *err << myRank <<
": readDenseImpl: done reading data" << endl;
4606 int globalReadDataSuccess = localReadDataSuccess;
4607 broadcast (*comm, 0, outArg (globalReadDataSuccess));
4608 TEUCHOS_TEST_FOR_EXCEPTION(
4609 globalReadDataSuccess == 0, std::runtime_error,
4610 "Failed to read the multivector's data: " << exMsg.str ());
4615 if (comm->getSize () == 1 && map.is_null ()) {
4617 if (! err.is_null ()) {
4621 *err << myRank <<
": readDenseImpl: done" << endl;
4623 if (! err.is_null ()) {
4630 *err << myRank <<
": readDenseImpl: Creating target MV" << endl;
4634 RCP<MV> Y = createMultiVector<ST, LO, GO, NT> (map, numCols);
4637 *err << myRank <<
": readDenseImpl: Creating Export" << endl;
4643 Export<LO, GO, NT> exporter (proc0Map, map, err);
4646 *err << myRank <<
": readDenseImpl: Exporting" << endl;
4649 Y->doExport (*X, exporter,
INSERT);
4651 if (! err.is_null ()) {
4655 *err << myRank <<
": readDenseImpl: done" << endl;
4657 if (! err.is_null ()) {
4666 template<
class VectorScalarType>
4671 readVectorImpl (std::istream& in,
4672 const Teuchos::RCP<const comm_type>& comm,
4673 Teuchos::RCP<const map_type>& map,
4674 const Teuchos::RCP<Teuchos::FancyOStream>& err,
4675 const bool tolerant=
false,
4676 const bool debug=
false)
4678 using Teuchos::MatrixMarket::Banner;
4679 using Teuchos::MatrixMarket::checkCommentLine;
4681 using Teuchos::broadcast;
4682 using Teuchos::outArg;
4684 using Teuchos::Tuple;
4686 typedef VectorScalarType ST;
4690 typedef Teuchos::ScalarTraits<ST> STS;
4691 typedef typename STS::magnitudeType MT;
4692 typedef Teuchos::ScalarTraits<MT> STM;
4697 const int myRank = comm->getRank ();
4699 if (! err.is_null ()) {
4703 *err << myRank <<
": readVectorImpl" << endl;
4705 if (! err.is_null ()) {
4739 size_t lineNumber = 1;
4742 std::ostringstream exMsg;
4743 int localBannerReadSuccess = 1;
4744 int localDimsReadSuccess = 1;
4749 *err << myRank <<
": readVectorImpl: Reading banner line (dense)" << endl;
4755 RCP<const Banner> pBanner;
4757 pBanner = readBanner (in, lineNumber, tolerant, debug);
4758 }
catch (std::exception& e) {
4760 localBannerReadSuccess = 0;
4763 if (localBannerReadSuccess) {
4764 if (pBanner->matrixType () !=
"array") {
4765 exMsg <<
"The Matrix Market file does not contain dense matrix "
4766 "data. Its banner (first) line says that its matrix type is \""
4767 << pBanner->matrixType () <<
"\", rather that the required "
4769 localBannerReadSuccess = 0;
4770 }
else if (pBanner->dataType() ==
"pattern") {
4771 exMsg <<
"The Matrix Market file's banner (first) "
4772 "line claims that the matrix's data type is \"pattern\". This does "
4773 "not make sense for a dense matrix, yet the file reports the matrix "
4774 "as dense. The only valid data types for a dense matrix are "
4775 "\"real\", \"complex\", and \"integer\".";
4776 localBannerReadSuccess = 0;
4780 dims[2] = encodeDataType (pBanner->dataType ());
4786 if (localBannerReadSuccess) {
4788 *err << myRank <<
": readVectorImpl: Reading dimensions line (dense)" << endl;
4797 bool commentLine =
true;
4799 while (commentLine) {
4802 if (in.eof () || in.fail ()) {
4803 exMsg <<
"Unable to get array dimensions line (at all) from line "
4804 << lineNumber <<
" of input stream. The input stream "
4805 <<
"claims that it is "
4806 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4807 localDimsReadSuccess = 0;
4810 if (getline (in, line)) {
4817 size_t start = 0, size = 0;
4818 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4825 std::istringstream istr (line);
4829 if (istr.eof () || istr.fail ()) {
4830 exMsg <<
"Unable to read any data from line " << lineNumber
4831 <<
" of input; the line should contain the matrix dimensions "
4832 <<
"\"<numRows> <numCols>\".";
4833 localDimsReadSuccess = 0;
4838 exMsg <<
"Failed to get number of rows from line "
4839 << lineNumber <<
" of input; the line should contains the "
4840 <<
"matrix dimensions \"<numRows> <numCols>\".";
4841 localDimsReadSuccess = 0;
4843 dims[0] = theNumRows;
4845 exMsg <<
"No more data after number of rows on line "
4846 << lineNumber <<
" of input; the line should contain the "
4847 <<
"matrix dimensions \"<numRows> <numCols>\".";
4848 localDimsReadSuccess = 0;
4853 exMsg <<
"Failed to get number of columns from line "
4854 << lineNumber <<
" of input; the line should contain "
4855 <<
"the matrix dimensions \"<numRows> <numCols>\".";
4856 localDimsReadSuccess = 0;
4858 dims[1] = theNumCols;
4868 exMsg <<
"File does not contain a 1D Vector";
4869 localDimsReadSuccess = 0;
4875 Tuple<GO, 5> bannerDimsReadResult;
4877 bannerDimsReadResult[0] = dims[0];
4878 bannerDimsReadResult[1] = dims[1];
4879 bannerDimsReadResult[2] = dims[2];
4880 bannerDimsReadResult[3] = localBannerReadSuccess;
4881 bannerDimsReadResult[4] = localDimsReadSuccess;
4886 broadcast (*comm, 0, bannerDimsReadResult);
4888 TEUCHOS_TEST_FOR_EXCEPTION(
4889 bannerDimsReadResult[3] == 0, std::runtime_error,
4890 "Failed to read banner line: " << exMsg.str ());
4891 TEUCHOS_TEST_FOR_EXCEPTION(
4892 bannerDimsReadResult[4] == 0, std::runtime_error,
4893 "Failed to read matrix dimensions line: " << exMsg.str ());
4895 dims[0] = bannerDimsReadResult[0];
4896 dims[1] = bannerDimsReadResult[1];
4897 dims[2] = bannerDimsReadResult[2];
4902 const size_t numCols =
static_cast<size_t> (dims[1]);
4907 RCP<const map_type> proc0Map;
4908 if (map.is_null ()) {
4912 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
4913 const size_t localNumRows = (myRank == 0) ? numRows : 0;
4915 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
4919 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
4923 RCP<MV> X = createVector<ST, LO, GO, NT> (proc0Map);
4929 int localReadDataSuccess = 1;
4933 *err << myRank <<
": readVectorImpl: Reading matrix data (dense)"
4938 TEUCHOS_TEST_FOR_EXCEPTION(
4939 ! X->isConstantStride (), std::logic_error,
4940 "Can't get a 1-D view of the entries of the MultiVector X on "
4941 "Process 0, because the stride between the columns of X is not "
4942 "constant. This shouldn't happen because we just created X and "
4943 "haven't filled it in yet. Please report this bug to the Tpetra "
4950 Teuchos::ArrayRCP<ST> X_view = X->get1dViewNonConst ();
4951 TEUCHOS_TEST_FOR_EXCEPTION(
4952 as<global_size_t> (X_view.size ()) < numRows * numCols,
4954 "The view of X has size " << X_view <<
" which is not enough to "
4955 "accommodate the expected number of entries numRows*numCols = "
4956 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
4957 "Please report this bug to the Tpetra developers.");
4958 const size_t stride = X->getStride ();
4965 const bool isComplex = (dims[2] == 1);
4966 size_type count = 0, curRow = 0, curCol = 0;
4969 while (getline (in, line)) {
4973 size_t start = 0, size = 0;
4974 const bool commentLine =
4975 checkCommentLine (line, start, size, lineNumber, tolerant);
4976 if (! commentLine) {
4982 if (count >= X_view.size()) {
4987 TEUCHOS_TEST_FOR_EXCEPTION(
4988 count >= X_view.size(),
4990 "The Matrix Market input stream has more data in it than "
4991 "its metadata reported. Current line number is "
4992 << lineNumber <<
".");
5001 const size_t pos = line.substr (start, size).find (
':');
5002 if (pos != std::string::npos) {
5006 std::istringstream istr (line.substr (start, size));
5010 if (istr.eof() || istr.fail()) {
5017 TEUCHOS_TEST_FOR_EXCEPTION(
5018 ! tolerant && (istr.eof() || istr.fail()),
5020 "Line " << lineNumber <<
" of the Matrix Market file is "
5021 "empty, or we cannot read from it for some other reason.");
5024 ST val = STS::zero();
5027 MT real = STM::zero(), imag = STM::zero();
5044 TEUCHOS_TEST_FOR_EXCEPTION(
5045 ! tolerant && istr.eof(), std::runtime_error,
5046 "Failed to get the real part of a complex-valued matrix "
5047 "entry from line " << lineNumber <<
" of the Matrix Market "
5053 }
else if (istr.eof()) {
5054 TEUCHOS_TEST_FOR_EXCEPTION(
5055 ! tolerant && istr.eof(), std::runtime_error,
5056 "Missing imaginary part of a complex-valued matrix entry "
5057 "on line " << lineNumber <<
" of the Matrix Market file.");
5063 TEUCHOS_TEST_FOR_EXCEPTION(
5064 ! tolerant && istr.fail(), std::runtime_error,
5065 "Failed to get the imaginary part of a complex-valued "
5066 "matrix entry from line " << lineNumber <<
" of the "
5067 "Matrix Market file.");
5074 TEUCHOS_TEST_FOR_EXCEPTION(
5075 ! tolerant && istr.fail(), std::runtime_error,
5076 "Failed to get a real-valued matrix entry from line "
5077 << lineNumber <<
" of the Matrix Market file.");
5080 if (istr.fail() && tolerant) {
5086 TEUCHOS_TEST_FOR_EXCEPTION(
5087 ! tolerant && istr.fail(), std::runtime_error,
5088 "Failed to read matrix data from line " << lineNumber
5089 <<
" of the Matrix Market file.");
5092 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
5094 curRow = count % numRows;
5095 curCol = count / numRows;
5096 X_view[curRow + curCol*stride] = val;
5101 TEUCHOS_TEST_FOR_EXCEPTION(
5102 ! tolerant &&
static_cast<global_size_t> (count) < numRows * numCols,
5104 "The Matrix Market metadata reports that the dense matrix is "
5105 << numRows <<
" x " << numCols <<
", and thus has "
5106 << numRows*numCols <<
" total entries, but we only found " << count
5107 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
5108 }
catch (std::exception& e) {
5110 localReadDataSuccess = 0;
5115 *err << myRank <<
": readVectorImpl: done reading data" << endl;
5119 int globalReadDataSuccess = localReadDataSuccess;
5120 broadcast (*comm, 0, outArg (globalReadDataSuccess));
5121 TEUCHOS_TEST_FOR_EXCEPTION(
5122 globalReadDataSuccess == 0, std::runtime_error,
5123 "Failed to read the multivector's data: " << exMsg.str ());
5128 if (comm->getSize () == 1 && map.is_null ()) {
5130 if (! err.is_null ()) {
5134 *err << myRank <<
": readVectorImpl: done" << endl;
5136 if (! err.is_null ()) {
5143 *err << myRank <<
": readVectorImpl: Creating target MV" << endl;
5147 RCP<MV> Y = createVector<ST, LO, GO, NT> (map);
5150 *err << myRank <<
": readVectorImpl: Creating Export" << endl;
5156 Export<LO, GO, NT> exporter (proc0Map, map, err);
5159 *err << myRank <<
": readVectorImpl: Exporting" << endl;
5162 Y->doExport (*X, exporter,
INSERT);
5164 if (! err.is_null ()) {
5168 *err << myRank <<
": readVectorImpl: done" << endl;
5170 if (! err.is_null ()) {
5198 static Teuchos::RCP<const map_type>
5200 const Teuchos::RCP<const comm_type>& comm,
5201 const bool tolerant=
false,
5202 const bool debug=
false)
5204 Teuchos::RCP<Teuchos::FancyOStream> err =
5205 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
5206 return readMap (in, comm, err, tolerant, debug);
5235 static Teuchos::RCP<const map_type>
5237 const Teuchos::RCP<const comm_type>& comm,
5238 const Teuchos::RCP<Teuchos::FancyOStream>& err,
5239 const bool tolerant=
false,
5240 const bool debug=
false)
5242 using Teuchos::arcp;
5243 using Teuchos::Array;
5244 using Teuchos::ArrayRCP;
5246 using Teuchos::broadcast;
5247 using Teuchos::Comm;
5248 using Teuchos::CommRequest;
5249 using Teuchos::inOutArg;
5250 using Teuchos::ireceive;
5251 using Teuchos::outArg;
5253 using Teuchos::receive;
5254 using Teuchos::reduceAll;
5255 using Teuchos::REDUCE_MIN;
5256 using Teuchos::isend;
5257 using Teuchos::SerialComm;
5258 using Teuchos::toString;
5259 using Teuchos::wait;
5262 typedef ptrdiff_t int_type;
5268 const int numProcs = comm->getSize ();
5269 const int myRank = comm->getRank ();
5271 if (err.is_null ()) {
5275 std::ostringstream os;
5276 os << myRank <<
": readMap: " << endl;
5279 if (err.is_null ()) {
5285 const int sizeTag = 1339;
5287 const int dataTag = 1340;
5293 RCP<CommRequest<int> > sizeReq;
5294 RCP<CommRequest<int> > dataReq;
5299 ArrayRCP<int_type> numGidsToRecv (1);
5300 numGidsToRecv[0] = 0;
5302 sizeReq = ireceive<int, int_type> (numGidsToRecv, 0, sizeTag, *comm);
5305 int readSuccess = 1;
5306 std::ostringstream exMsg;
5315 RCP<const Comm<int> > proc0Comm (
new SerialComm<int> ());
5317 RCP<const map_type> dataMap;
5321 data = readDenseImpl<GO> (in, proc0Comm, dataMap, err, tolerant, debug);
5323 if (data.is_null ()) {
5325 exMsg <<
"readDenseImpl() returned null." << endl;
5327 }
catch (std::exception& e) {
5329 exMsg << e.what () << endl;
5335 std::map<int, Array<GO> > pid2gids;
5340 int_type globalNumGIDs = 0;
5350 if (myRank == 0 && readSuccess == 1) {
5351 if (data->getNumVectors () == 2) {
5352 ArrayRCP<const GO> GIDs = data->getData (0);
5353 ArrayRCP<const GO> PIDs = data->getData (1);
5354 globalNumGIDs = GIDs.size ();
5358 if (globalNumGIDs > 0) {
5359 const int pid =
static_cast<int> (PIDs[0]);
5361 if (pid < 0 || pid >= numProcs) {
5363 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5364 <<
"Encountered invalid PID " << pid <<
"." << endl;
5367 const GO gid = GIDs[0];
5368 pid2gids[pid].push_back (gid);
5372 if (readSuccess == 1) {
5375 for (size_type k = 1; k < globalNumGIDs; ++k) {
5376 const int pid =
static_cast<int> (PIDs[k]);
5377 if (pid < 0 || pid >= numProcs) {
5379 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5380 <<
"Encountered invalid PID " << pid <<
"." << endl;
5383 const int_type gid = GIDs[k];
5384 pid2gids[pid].push_back (gid);
5385 if (gid < indexBase) {
5392 else if (data->getNumVectors () == 1) {
5393 if (data->getGlobalLength () % 2 !=
static_cast<GST
> (0)) {
5395 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data has the "
5396 "wrong format (for Map format 2.0). The global number of rows "
5397 "in the MultiVector must be even (divisible by 2)." << endl;
5400 ArrayRCP<const GO> theData = data->getData (0);
5401 globalNumGIDs =
static_cast<GO
> (data->getGlobalLength ()) /
5402 static_cast<GO
> (2);
5406 if (globalNumGIDs > 0) {
5407 const int pid =
static_cast<int> (theData[1]);
5408 if (pid < 0 || pid >= numProcs) {
5410 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5411 <<
"Encountered invalid PID " << pid <<
"." << endl;
5414 const GO gid = theData[0];
5415 pid2gids[pid].push_back (gid);
5421 for (int_type k = 1; k < globalNumGIDs; ++k) {
5422 const int pid =
static_cast<int> (theData[2*k + 1]);
5423 if (pid < 0 || pid >= numProcs) {
5425 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5426 <<
"Encountered invalid PID " << pid <<
"." << endl;
5429 const GO gid = theData[2*k];
5430 pid2gids[pid].push_back (gid);
5431 if (gid < indexBase) {
5440 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data must have "
5441 "either 1 column (for the new Map format 2.0) or 2 columns (for "
5442 "the old Map format 1.0).";
5449 int_type readResults[3];
5450 readResults[0] =
static_cast<int_type
> (indexBase);
5451 readResults[1] =
static_cast<int_type
> (globalNumGIDs);
5452 readResults[2] =
static_cast<int_type
> (readSuccess);
5453 broadcast<int, int_type> (*comm, 0, 3, readResults);
5455 indexBase =
static_cast<GO
> (readResults[0]);
5456 globalNumGIDs =
static_cast<int_type
> (readResults[1]);
5457 readSuccess =
static_cast<int> (readResults[2]);
5463 TEUCHOS_TEST_FOR_EXCEPTION(
5464 readSuccess != 1, std::runtime_error,
5465 "Tpetra::MatrixMarket::readMap: Reading the Map failed with the "
5466 "following exception message: " << exMsg.str ());
5470 for (
int p = 1; p < numProcs; ++p) {
5471 ArrayRCP<int_type> numGidsToSend (1);
5473 auto it = pid2gids.find (p);
5474 if (it == pid2gids.end ()) {
5475 numGidsToSend[0] = 0;
5477 numGidsToSend[0] = it->second.size ();
5479 sizeReq = isend<int, int_type> (numGidsToSend, p, sizeTag, *comm);
5480 wait<int> (*comm, outArg (sizeReq));
5485 wait<int> (*comm, outArg (sizeReq));
5490 ArrayRCP<GO> myGids;
5491 int_type myNumGids = 0;
5493 GO* myGidsRaw = NULL;
5495 typename std::map<int, Array<GO> >::iterator it = pid2gids.find (0);
5496 if (it != pid2gids.end ()) {
5497 myGidsRaw = it->second.getRawPtr ();
5498 myNumGids = it->second.size ();
5500 myGids = arcp<GO> (myGidsRaw, 0, myNumGids,
false);
5504 myNumGids = numGidsToRecv[0];
5505 myGids = arcp<GO> (myNumGids);
5512 if (myNumGids > 0) {
5513 dataReq = ireceive<int, GO> (myGids, 0, dataTag, *comm);
5517 for (
int p = 1; p < numProcs; ++p) {
5519 ArrayRCP<GO> sendGids;
5520 GO* sendGidsRaw = NULL;
5521 int_type numSendGids = 0;
5523 typename std::map<int, Array<GO> >::iterator it = pid2gids.find (p);
5524 if (it != pid2gids.end ()) {
5525 numSendGids = it->second.size ();
5526 sendGidsRaw = it->second.getRawPtr ();
5527 sendGids = arcp<GO> (sendGidsRaw, 0, numSendGids,
false);
5530 if (numSendGids > 0) {
5531 dataReq = isend<int, GO> (sendGids, p, dataTag, *comm);
5533 wait<int> (*comm, outArg (dataReq));
5535 else if (myRank == p) {
5537 wait<int> (*comm, outArg (dataReq));
5542 std::ostringstream os;
5543 os << myRank <<
": readMap: creating Map" << endl;
5546 const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
5547 RCP<const map_type> newMap;
5554 std::ostringstream errStrm;
5556 newMap = rcp (
new map_type (INVALID, myGids (), indexBase, comm));
5558 catch (std::exception& e) {
5560 errStrm <<
"Process " << comm->getRank () <<
" threw an exception: "
5561 << e.what () << std::endl;
5565 errStrm <<
"Process " << comm->getRank () <<
" threw an exception "
5566 "not a subclass of std::exception" << std::endl;
5568 Teuchos::reduceAll<int, int> (*comm, Teuchos::REDUCE_MIN,
5569 lclSuccess, Teuchos::outArg (gblSuccess));
5570 if (gblSuccess != 1) {
5573 TEUCHOS_TEST_FOR_EXCEPTION(gblSuccess != 1, std::runtime_error,
"Map constructor failed!");
5575 if (err.is_null ()) {
5579 std::ostringstream os;
5580 os << myRank <<
": readMap: done" << endl;
5583 if (err.is_null ()) {
5603 encodeDataType (
const std::string& dataType)
5605 if (dataType ==
"real" || dataType ==
"integer") {
5607 }
else if (dataType ==
"complex") {
5609 }
else if (dataType ==
"pattern") {
5615 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
5616 "Unrecognized Matrix Market data type \"" << dataType
5617 <<
"\". We should never get here. "
5618 "Please report this bug to the Tpetra developers.");
5651 template<
class SparseMatrixType>
5656 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
5718 const std::string& matrixName,
5719 const std::string& matrixDescription,
5720 const bool debug=
false)
5722 Teuchos::RCP<const Teuchos::Comm<int> > comm = matrix.getComm ();
5723 TEUCHOS_TEST_FOR_EXCEPTION
5724 (comm.is_null (), std::invalid_argument,
5725 "The input matrix's communicator (Teuchos::Comm object) is null.");
5726 const int myRank = comm->getRank ();
5731 out.open (filename.c_str ());
5733 writeSparse (out, matrix, matrixName, matrixDescription, debug);
5742 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
5743 const std::string& matrixName,
5744 const std::string& matrixDescription,
5745 const bool debug=
false)
5747 TEUCHOS_TEST_FOR_EXCEPTION
5748 (pMatrix.is_null (), std::invalid_argument,
5749 "The input matrix is null.");
5751 matrixDescription, debug);
5776 const bool debug=
false)
5784 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
5785 const bool debug=
false)
5823 const std::string& matrixName,
5824 const std::string& matrixDescription,
5825 const bool debug=
false)
5827 using Teuchos::ArrayView;
5828 using Teuchos::Comm;
5829 using Teuchos::FancyOStream;
5830 using Teuchos::getFancyOStream;
5831 using Teuchos::null;
5833 using Teuchos::rcpFromRef;
5839 using STS =
typename Teuchos::ScalarTraits<ST>;
5845 Teuchos::SetScientific<ST> sci (out);
5848 RCP<const Comm<int> > comm = matrix.getComm ();
5849 TEUCHOS_TEST_FOR_EXCEPTION(
5850 comm.is_null (), std::invalid_argument,
5851 "The input matrix's communicator (Teuchos::Comm object) is null.");
5852 const int myRank = comm->getRank ();
5855 RCP<FancyOStream> err =
5856 debug ? getFancyOStream (rcpFromRef (std::cerr)) : null;
5858 std::ostringstream os;
5859 os << myRank <<
": writeSparse" << endl;
5862 os <<
"-- " << myRank <<
": past barrier" << endl;
5867 const bool debugPrint = debug && myRank == 0;
5869 RCP<const map_type> rowMap = matrix.getRowMap ();
5870 RCP<const map_type> colMap = matrix.getColMap ();
5871 RCP<const map_type> domainMap = matrix.getDomainMap ();
5872 RCP<const map_type> rangeMap = matrix.getRangeMap ();
5874 const global_size_t numRows = rangeMap->getGlobalNumElements ();
5875 const global_size_t numCols = domainMap->getGlobalNumElements ();
5877 if (debug && myRank == 0) {
5878 std::ostringstream os;
5879 os <<
"-- Input sparse matrix is:"
5880 <<
"---- " << numRows <<
" x " << numCols << endl
5882 << (matrix.isGloballyIndexed() ?
"Globally" :
"Locally")
5883 <<
" indexed." << endl
5884 <<
"---- Its row map has " << rowMap->getGlobalNumElements ()
5885 <<
" elements." << endl
5886 <<
"---- Its col map has " << colMap->getGlobalNumElements ()
5887 <<
" elements." << endl;
5892 const size_t localNumRows = (myRank == 0) ? numRows : 0;
5894 std::ostringstream os;
5895 os <<
"-- " << myRank <<
": making gatherRowMap" << endl;
5898 RCP<const map_type> gatherRowMap =
5899 Details::computeGatherMap (rowMap, err, debug);
5907 const size_t localNumCols = (myRank == 0) ? numCols : 0;
5908 RCP<const map_type> gatherColMap =
5909 Details::computeGatherMap (colMap, err, debug);
5913 import_type importer (rowMap, gatherRowMap);
5918 RCP<sparse_matrix_type> newMatrix =
5920 static_cast<size_t> (0)));
5922 newMatrix->doImport (matrix, importer,
INSERT);
5927 RCP<const map_type> gatherDomainMap =
5928 rcp (
new map_type (numCols, localNumCols,
5929 domainMap->getIndexBase (),
5931 RCP<const map_type> gatherRangeMap =
5932 rcp (
new map_type (numRows, localNumRows,
5933 rangeMap->getIndexBase (),
5935 newMatrix->fillComplete (gatherDomainMap, gatherRangeMap);
5939 cerr <<
"-- Output sparse matrix is:"
5940 <<
"---- " << newMatrix->getRangeMap ()->getGlobalNumElements ()
5942 << newMatrix->getDomainMap ()->getGlobalNumElements ()
5944 << newMatrix->getGlobalNumEntries () <<
" entries;" << endl
5946 << (newMatrix->isGloballyIndexed () ?
"Globally" :
"Locally")
5947 <<
" indexed." << endl
5948 <<
"---- Its row map has "
5949 << newMatrix->getRowMap ()->getGlobalNumElements ()
5950 <<
" elements, with index base "
5951 << newMatrix->getRowMap ()->getIndexBase () <<
"." << endl
5952 <<
"---- Its col map has "
5953 << newMatrix->getColMap ()->getGlobalNumElements ()
5954 <<
" elements, with index base "
5955 << newMatrix->getColMap ()->getIndexBase () <<
"." << endl
5956 <<
"---- Element count of output matrix's column Map may differ "
5957 <<
"from that of the input matrix's column Map, if some columns "
5958 <<
"of the matrix contain no entries." << endl;
5971 out <<
"%%MatrixMarket matrix coordinate "
5972 << (STS::isComplex ?
"complex" :
"real")
5973 <<
" general" << endl;
5976 if (matrixName !=
"") {
5977 printAsComment (out, matrixName);
5979 if (matrixDescription !=
"") {
5980 printAsComment (out, matrixDescription);
5990 out << newMatrix->getRangeMap ()->getGlobalNumElements () <<
" "
5991 << newMatrix->getDomainMap ()->getGlobalNumElements () <<
" "
5992 << newMatrix->getGlobalNumEntries () << endl;
5997 const GO rowIndexBase = gatherRowMap->getIndexBase ();
5998 const GO colIndexBase = newMatrix->getColMap()->getIndexBase ();
6006 if (newMatrix->isGloballyIndexed()) {
6009 const GO minAllGlobalIndex = gatherRowMap->getMinAllGlobalIndex ();
6010 const GO maxAllGlobalIndex = gatherRowMap->getMaxAllGlobalIndex ();
6011 for (GO globalRowIndex = minAllGlobalIndex;
6012 globalRowIndex <= maxAllGlobalIndex;
6014 typename sparse_matrix_type::global_inds_host_view_type ind;
6015 typename sparse_matrix_type::values_host_view_type val;
6016 newMatrix->getGlobalRowView (globalRowIndex, ind, val);
6017 for (
size_t ii = 0; ii < ind.extent(0); ii++) {
6018 const GO globalColIndex = ind(ii);
6021 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6022 << (globalColIndex + 1 - colIndexBase) <<
" ";
6023 if (STS::isComplex) {
6024 out << STS::real (val(ii)) <<
" " << STS::imag (val(ii));
6033 using OTG = Teuchos::OrdinalTraits<GO>;
6034 for (LO localRowIndex = gatherRowMap->getMinLocalIndex();
6035 localRowIndex <= gatherRowMap->getMaxLocalIndex();
6038 const GO globalRowIndex =
6039 gatherRowMap->getGlobalElement (localRowIndex);
6040 TEUCHOS_TEST_FOR_EXCEPTION(
6041 globalRowIndex == OTG::invalid(), std::logic_error,
6042 "Failed to convert the supposed local row index "
6043 << localRowIndex <<
" into a global row index. "
6044 "Please report this bug to the Tpetra developers.");
6045 typename sparse_matrix_type::local_inds_host_view_type ind;
6046 typename sparse_matrix_type::values_host_view_type val;
6047 newMatrix->getLocalRowView (localRowIndex, ind, val);
6048 for (
size_t ii = 0; ii < ind.extent(0); ii++) {
6050 const GO globalColIndex =
6051 newMatrix->getColMap()->getGlobalElement (ind(ii));
6052 TEUCHOS_TEST_FOR_EXCEPTION(
6053 globalColIndex == OTG::invalid(), std::logic_error,
6054 "On local row " << localRowIndex <<
" of the sparse matrix: "
6055 "Failed to convert the supposed local column index "
6056 << ind(ii) <<
" into a global column index. Please report "
6057 "this bug to the Tpetra developers.");
6060 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6061 << (globalColIndex + 1 - colIndexBase) <<
" ";
6062 if (STS::isComplex) {
6063 out << STS::real (val(ii)) <<
" " << STS::imag (val(ii));
6077 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
6078 const std::string& matrixName,
6079 const std::string& matrixDescription,
6080 const bool debug=
false)
6082 TEUCHOS_TEST_FOR_EXCEPTION
6083 (pMatrix.is_null (), std::invalid_argument,
6084 "The input matrix is null.");
6085 writeSparse (out, *pMatrix, matrixName, matrixDescription, debug);
6121 const std::string& graphName,
6122 const std::string& graphDescription,
6123 const bool debug=
false)
6125 using Teuchos::ArrayView;
6126 using Teuchos::Comm;
6127 using Teuchos::FancyOStream;
6128 using Teuchos::getFancyOStream;
6129 using Teuchos::null;
6131 using Teuchos::rcpFromRef;
6142 if (rowMap.is_null ()) {
6145 auto comm = rowMap->getComm ();
6146 if (comm.is_null ()) {
6149 const int myRank = comm->getRank ();
6152 RCP<FancyOStream> err =
6153 debug ? getFancyOStream (rcpFromRef (std::cerr)) : null;
6155 std::ostringstream os;
6156 os << myRank <<
": writeSparseGraph" << endl;
6159 os <<
"-- " << myRank <<
": past barrier" << endl;
6164 const bool debugPrint = debug && myRank == 0;
6171 const global_size_t numRows = rangeMap->getGlobalNumElements ();
6172 const global_size_t numCols = domainMap->getGlobalNumElements ();
6174 if (debug && myRank == 0) {
6175 std::ostringstream os;
6176 os <<
"-- Input sparse graph is:"
6177 <<
"---- " << numRows <<
" x " << numCols <<
" with "
6181 <<
" indexed." << endl
6182 <<
"---- Its row Map has " << rowMap->getGlobalNumElements ()
6183 <<
" elements." << endl
6184 <<
"---- Its col Map has " << colMap->getGlobalNumElements ()
6185 <<
" elements." << endl;
6190 const size_t localNumRows = (myRank == 0) ? numRows : 0;
6192 std::ostringstream os;
6193 os <<
"-- " << myRank <<
": making gatherRowMap" << endl;
6196 auto gatherRowMap = Details::computeGatherMap (rowMap, err, debug);
6204 const size_t localNumCols = (myRank == 0) ? numCols : 0;
6205 auto gatherColMap = Details::computeGatherMap (colMap, err, debug);
6214 static_cast<size_t> (0));
6221 RCP<const map_type> gatherDomainMap =
6222 rcp (
new map_type (numCols, localNumCols,
6223 domainMap->getIndexBase (),
6225 RCP<const map_type> gatherRangeMap =
6226 rcp (
new map_type (numRows, localNumRows,
6227 rangeMap->getIndexBase (),
6229 newGraph.
fillComplete (gatherDomainMap, gatherRangeMap);
6233 cerr <<
"-- Output sparse graph is:"
6234 <<
"---- " << newGraph.
getRangeMap ()->getGlobalNumElements ()
6241 <<
" indexed." << endl
6242 <<
"---- Its row map has "
6243 << newGraph.
getRowMap ()->getGlobalNumElements ()
6244 <<
" elements, with index base "
6245 << newGraph.
getRowMap ()->getIndexBase () <<
"." << endl
6246 <<
"---- Its col map has "
6247 << newGraph.
getColMap ()->getGlobalNumElements ()
6248 <<
" elements, with index base "
6249 << newGraph.
getColMap ()->getIndexBase () <<
"." << endl
6250 <<
"---- Element count of output graph's column Map may differ "
6251 <<
"from that of the input matrix's column Map, if some columns "
6252 <<
"of the matrix contain no entries." << endl;
6266 out <<
"%%MatrixMarket matrix coordinate pattern general" << endl;
6269 if (graphName !=
"") {
6270 printAsComment (out, graphName);
6272 if (graphDescription !=
"") {
6273 printAsComment (out, graphDescription);
6284 out << newGraph.
getRangeMap ()->getGlobalNumElements () <<
" "
6285 << newGraph.
getDomainMap ()->getGlobalNumElements () <<
" "
6291 const GO rowIndexBase = gatherRowMap->getIndexBase ();
6292 const GO colIndexBase = newGraph.
getColMap()->getIndexBase ();
6303 const GO minAllGlobalIndex = gatherRowMap->getMinAllGlobalIndex ();
6304 const GO maxAllGlobalIndex = gatherRowMap->getMaxAllGlobalIndex ();
6305 for (GO globalRowIndex = minAllGlobalIndex;
6306 globalRowIndex <= maxAllGlobalIndex;
6308 typename crs_graph_type::global_inds_host_view_type ind;
6310 for (
size_t ii = 0; ii < ind.extent(0); ii++) {
6311 const GO globalColIndex = ind(ii);
6314 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6315 << (globalColIndex + 1 - colIndexBase) <<
" ";
6321 typedef Teuchos::OrdinalTraits<GO> OTG;
6322 for (LO localRowIndex = gatherRowMap->getMinLocalIndex ();
6323 localRowIndex <= gatherRowMap->getMaxLocalIndex ();
6326 const GO globalRowIndex =
6327 gatherRowMap->getGlobalElement (localRowIndex);
6328 TEUCHOS_TEST_FOR_EXCEPTION
6329 (globalRowIndex == OTG::invalid (), std::logic_error,
"Failed "
6330 "to convert the supposed local row index " << localRowIndex <<
6331 " into a global row index. Please report this bug to the "
6332 "Tpetra developers.");
6333 typename crs_graph_type::local_inds_host_view_type ind;
6335 for (
size_t ii = 0; ii < ind.extent(0); ii++) {
6337 const GO globalColIndex =
6338 newGraph.
getColMap ()->getGlobalElement (ind(ii));
6339 TEUCHOS_TEST_FOR_EXCEPTION(
6340 globalColIndex == OTG::invalid(), std::logic_error,
6341 "On local row " << localRowIndex <<
" of the sparse graph: "
6342 "Failed to convert the supposed local column index "
6343 << ind(ii) <<
" into a global column index. Please report "
6344 "this bug to the Tpetra developers.");
6347 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6348 << (globalColIndex + 1 - colIndexBase) <<
" ";
6364 const bool debug=
false)
6406 const std::string& graphName,
6407 const std::string& graphDescription,
6408 const bool debug=
false)
6411 if (comm.is_null ()) {
6419 const int myRank = comm->getRank ();
6424 out.open (filename.c_str ());
6439 const bool debug=
false)
6454 const Teuchos::RCP<const crs_graph_type>& pGraph,
6455 const std::string& graphName,
6456 const std::string& graphDescription,
6457 const bool debug=
false)
6473 const Teuchos::RCP<const crs_graph_type>& pGraph,
6474 const bool debug=
false)
6504 const bool debug=
false)
6512 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
6513 const bool debug=
false)
6549 const std::string& matrixName,
6550 const std::string& matrixDescription,
6551 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6552 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6554 const int myRank = X.
getMap ().is_null () ? 0 :
6555 (X.
getMap ()->getComm ().is_null () ? 0 :
6556 X.
getMap ()->getComm ()->getRank ());
6560 out.open (filename.c_str());
6563 writeDense (out, X, matrixName, matrixDescription, err, dbg);
6576 const Teuchos::RCP<const multivector_type>& X,
6577 const std::string& matrixName,
6578 const std::string& matrixDescription,
6579 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6580 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6582 TEUCHOS_TEST_FOR_EXCEPTION(
6583 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6584 "writeDenseFile: The input MultiVector X is null.");
6585 writeDenseFile (filename, *X, matrixName, matrixDescription, err, dbg);
6596 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6597 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6609 const Teuchos::RCP<const multivector_type>& X,
6610 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6611 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6613 TEUCHOS_TEST_FOR_EXCEPTION(
6614 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6615 "writeDenseFile: The input MultiVector X is null.");
6653 const std::string& matrixName,
6654 const std::string& matrixDescription,
6655 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6656 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6658 using Teuchos::Comm;
6659 using Teuchos::outArg;
6660 using Teuchos::REDUCE_MAX;
6661 using Teuchos::reduceAll;
6665 RCP<const Comm<int> > comm = X.
getMap ().is_null () ?
6666 Teuchos::null : X.
getMap ()->getComm ();
6667 const int myRank = comm.is_null () ? 0 : comm->getRank ();
6672 const bool debug = ! dbg.is_null ();
6675 std::ostringstream os;
6676 os << myRank <<
": writeDense" << endl;
6681 writeDenseHeader (out, X, matrixName, matrixDescription, err, dbg);
6686 for (
size_t j = 0; j < numVecs; ++j) {
6687 writeDenseColumn (out, * (X.
getVector (j)), err, dbg);
6692 std::ostringstream os;
6693 os << myRank <<
": writeDense: Done" << endl;
6727 writeDenseHeader (std::ostream& out,
6729 const std::string& matrixName,
6730 const std::string& matrixDescription,
6731 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6732 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6734 using Teuchos::Comm;
6735 using Teuchos::outArg;
6737 using Teuchos::REDUCE_MAX;
6738 using Teuchos::reduceAll;
6740 typedef Teuchos::ScalarTraits<scalar_type> STS;
6741 const char prefix[] =
"Tpetra::MatrixMarket::writeDenseHeader: ";
6743 RCP<const Comm<int> > comm = X.getMap ().is_null () ?
6744 Teuchos::null : X.getMap ()->getComm ();
6745 const int myRank = comm.is_null () ? 0 : comm->getRank ();
6752 const bool debug = ! dbg.is_null ();
6756 std::ostringstream os;
6757 os << myRank <<
": writeDenseHeader" << endl;
6776 std::ostringstream hdr;
6778 std::string dataType;
6779 if (STS::isComplex) {
6780 dataType =
"complex";
6781 }
else if (STS::isOrdinal) {
6782 dataType =
"integer";
6786 hdr <<
"%%MatrixMarket matrix array " << dataType <<
" general"
6791 if (matrixName !=
"") {
6792 printAsComment (hdr, matrixName);
6794 if (matrixDescription !=
"") {
6795 printAsComment (hdr, matrixDescription);
6798 hdr << X.getGlobalLength () <<
" " << X.getNumVectors () << endl;
6802 }
catch (std::exception& e) {
6803 if (! err.is_null ()) {
6804 *err << prefix <<
"While writing the Matrix Market header, "
6805 "Process 0 threw an exception: " << e.what () << endl;
6814 reduceAll<int, int> (*comm, REDUCE_MAX, lclErr, outArg (gblErr));
6815 TEUCHOS_TEST_FOR_EXCEPTION(
6816 gblErr == 1, std::runtime_error, prefix <<
"Some error occurred "
6817 "which prevented this method from completing.");
6821 *dbg << myRank <<
": writeDenseHeader: Done" << endl;
6844 writeDenseColumn (std::ostream& out,
6846 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6847 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6849 using Teuchos::arcp;
6850 using Teuchos::Array;
6851 using Teuchos::ArrayRCP;
6852 using Teuchos::ArrayView;
6853 using Teuchos::Comm;
6854 using Teuchos::CommRequest;
6855 using Teuchos::ireceive;
6856 using Teuchos::isend;
6857 using Teuchos::outArg;
6858 using Teuchos::REDUCE_MAX;
6859 using Teuchos::reduceAll;
6861 using Teuchos::TypeNameTraits;
6862 using Teuchos::wait;
6864 typedef Teuchos::ScalarTraits<scalar_type> STS;
6866 const Comm<int>& comm = * (X.getMap ()->getComm ());
6867 const int myRank = comm.getRank ();
6868 const int numProcs = comm.getSize ();
6875 const bool debug = ! dbg.is_null ();
6879 std::ostringstream os;
6880 os << myRank <<
": writeDenseColumn" << endl;
6889 Teuchos::SetScientific<scalar_type> sci (out);
6891 const size_t myNumRows = X.getLocalLength ();
6892 const size_t numCols = X.getNumVectors ();
6895 const int sizeTag = 1337;
6896 const int dataTag = 1338;
6957 RCP<CommRequest<int> > sendReqSize, sendReqData;
6963 Array<ArrayRCP<size_t> > recvSizeBufs (3);
6964 Array<ArrayRCP<scalar_type> > recvDataBufs (3);
6965 Array<RCP<CommRequest<int> > > recvSizeReqs (3);
6966 Array<RCP<CommRequest<int> > > recvDataReqs (3);
6969 ArrayRCP<size_t> sendDataSize (1);
6970 sendDataSize[0] = myNumRows;
6974 std::ostringstream os;
6975 os << myRank <<
": Post receive-size receives from "
6976 "Procs 1 and 2: tag = " << sizeTag << endl;
6980 recvSizeBufs[0].resize (1);
6984 (recvSizeBufs[0])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
6985 recvSizeBufs[1].resize (1);
6986 (recvSizeBufs[1])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
6987 recvSizeBufs[2].resize (1);
6988 (recvSizeBufs[2])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
6991 ireceive<int, size_t> (recvSizeBufs[1], 1, sizeTag, comm);
6995 ireceive<int, size_t> (recvSizeBufs[2], 2, sizeTag, comm);
6998 else if (myRank == 1 || myRank == 2) {
7000 std::ostringstream os;
7001 os << myRank <<
": Post send-size send: size = "
7002 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7009 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7013 std::ostringstream os;
7014 os << myRank <<
": Not posting my send-size send yet" << endl;
7023 std::ostringstream os;
7024 os << myRank <<
": Pack my entries" << endl;
7027 ArrayRCP<scalar_type> sendDataBuf;
7029 sendDataBuf = arcp<scalar_type> (myNumRows * numCols);
7030 X.get1dCopy (sendDataBuf (), myNumRows);
7032 catch (std::exception& e) {
7034 if (! err.is_null ()) {
7035 std::ostringstream os;
7036 os <<
"Process " << myRank <<
": Attempt to pack my MultiVector "
7037 "entries threw an exception: " << e.what () << endl;
7042 std::ostringstream os;
7043 os << myRank <<
": Done packing my entries" << endl;
7052 *dbg << myRank <<
": Post send-data send: tag = " << dataTag
7055 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7063 std::ostringstream os;
7064 os << myRank <<
": Write my entries" << endl;
7070 const size_t printNumRows = myNumRows;
7071 ArrayView<const scalar_type> printData = sendDataBuf ();
7072 const size_t printStride = printNumRows;
7073 if (
static_cast<size_t> (printData.size ()) < printStride * numCols) {
7075 if (! err.is_null ()) {
7076 std::ostringstream os;
7077 os <<
"Process " << myRank <<
": My MultiVector data's size "
7078 << printData.size () <<
" does not match my local dimensions "
7079 << printStride <<
" x " << numCols <<
"." << endl;
7087 for (
size_t col = 0; col < numCols; ++col) {
7088 for (
size_t row = 0; row < printNumRows; ++row) {
7089 if (STS::isComplex) {
7090 out << STS::real (printData[row + col * printStride]) <<
" "
7091 << STS::imag (printData[row + col * printStride]) << endl;
7093 out << printData[row + col * printStride] << endl;
7102 const int recvRank = 1;
7103 const int circBufInd = recvRank % 3;
7105 std::ostringstream os;
7106 os << myRank <<
": Wait on receive-size receive from Process "
7107 << recvRank << endl;
7111 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7115 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7116 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7118 if (! err.is_null ()) {
7119 std::ostringstream os;
7120 os << myRank <<
": Result of receive-size receive from Process "
7121 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7122 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7123 "This should never happen, and suggests that the receive never "
7124 "got posted. Please report this bug to the Tpetra developers."
7139 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7141 std::ostringstream os;
7142 os << myRank <<
": Post receive-data receive from Process "
7143 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7144 << recvDataBufs[circBufInd].size () << endl;
7147 if (! recvSizeReqs[circBufInd].is_null ()) {
7149 if (! err.is_null ()) {
7150 std::ostringstream os;
7151 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7152 "null, before posting the receive-data receive from Process "
7153 << recvRank <<
". This should never happen. Please report "
7154 "this bug to the Tpetra developers." << endl;
7158 recvDataReqs[circBufInd] =
7159 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7160 recvRank, dataTag, comm);
7163 else if (myRank == 1) {
7166 std::ostringstream os;
7167 os << myRank <<
": Wait on my send-size send" << endl;
7170 wait<int> (comm, outArg (sendReqSize));
7176 for (
int p = 1; p < numProcs; ++p) {
7178 if (p + 2 < numProcs) {
7180 const int recvRank = p + 2;
7181 const int circBufInd = recvRank % 3;
7183 std::ostringstream os;
7184 os << myRank <<
": Post receive-size receive from Process "
7185 << recvRank <<
": tag = " << sizeTag << endl;
7188 if (! recvSizeReqs[circBufInd].is_null ()) {
7190 if (! err.is_null ()) {
7191 std::ostringstream os;
7192 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7193 <<
"null, for the receive-size receive from Process "
7194 << recvRank <<
"! This may mean that this process never "
7195 <<
"finished waiting for the receive from Process "
7196 << (recvRank - 3) <<
"." << endl;
7200 recvSizeReqs[circBufInd] =
7201 ireceive<int, size_t> (recvSizeBufs[circBufInd],
7202 recvRank, sizeTag, comm);
7205 if (p + 1 < numProcs) {
7206 const int recvRank = p + 1;
7207 const int circBufInd = recvRank % 3;
7211 std::ostringstream os;
7212 os << myRank <<
": Wait on receive-size receive from Process "
7213 << recvRank << endl;
7216 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7220 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7221 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7223 if (! err.is_null ()) {
7224 std::ostringstream os;
7225 os << myRank <<
": Result of receive-size receive from Process "
7226 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7227 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7228 "This should never happen, and suggests that the receive never "
7229 "got posted. Please report this bug to the Tpetra developers."
7243 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7245 std::ostringstream os;
7246 os << myRank <<
": Post receive-data receive from Process "
7247 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7248 << recvDataBufs[circBufInd].size () << endl;
7251 if (! recvDataReqs[circBufInd].is_null ()) {
7253 if (! err.is_null ()) {
7254 std::ostringstream os;
7255 os << myRank <<
": recvDataReqs[" << circBufInd <<
"] is not "
7256 <<
"null, for the receive-data receive from Process "
7257 << recvRank <<
"! This may mean that this process never "
7258 <<
"finished waiting for the receive from Process "
7259 << (recvRank - 3) <<
"." << endl;
7263 recvDataReqs[circBufInd] =
7264 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7265 recvRank, dataTag, comm);
7269 const int recvRank = p;
7270 const int circBufInd = recvRank % 3;
7272 std::ostringstream os;
7273 os << myRank <<
": Wait on receive-data receive from Process "
7274 << recvRank << endl;
7277 wait<int> (comm, outArg (recvDataReqs[circBufInd]));
7284 std::ostringstream os;
7285 os << myRank <<
": Write entries from Process " << recvRank
7287 *dbg << os.str () << endl;
7289 size_t printNumRows = (recvSizeBufs[circBufInd])[0];
7290 if (printNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7292 if (! err.is_null ()) {
7293 std::ostringstream os;
7294 os << myRank <<
": Result of receive-size receive from Process "
7295 << recvRank <<
" was Teuchos::OrdinalTraits<size_t>::"
7296 "invalid() = " << Teuchos::OrdinalTraits<size_t>::invalid ()
7297 <<
". This should never happen, and suggests that its "
7298 "receive-size receive was never posted. "
7299 "Please report this bug to the Tpetra developers." << endl;
7306 if (printNumRows > 0 && recvDataBufs[circBufInd].is_null ()) {
7308 if (! err.is_null ()) {
7309 std::ostringstream os;
7310 os << myRank <<
": Result of receive-size receive from Proc "
7311 << recvRank <<
" was " << printNumRows <<
" > 0, but "
7312 "recvDataBufs[" << circBufInd <<
"] is null. This should "
7313 "never happen. Please report this bug to the Tpetra "
7314 "developers." << endl;
7324 ArrayView<const scalar_type> printData = (recvDataBufs[circBufInd]) ();
7325 const size_t printStride = printNumRows;
7329 for (
size_t col = 0; col < numCols; ++col) {
7330 for (
size_t row = 0; row < printNumRows; ++row) {
7331 if (STS::isComplex) {
7332 out << STS::real (printData[row + col * printStride]) <<
" "
7333 << STS::imag (printData[row + col * printStride]) << endl;
7335 out << printData[row + col * printStride] << endl;
7340 else if (myRank == p) {
7343 std::ostringstream os;
7344 os << myRank <<
": Wait on my send-data send" << endl;
7347 wait<int> (comm, outArg (sendReqData));
7349 else if (myRank == p + 1) {
7352 std::ostringstream os;
7353 os << myRank <<
": Post send-data send: tag = " << dataTag
7357 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7360 std::ostringstream os;
7361 os << myRank <<
": Wait on my send-size send" << endl;
7364 wait<int> (comm, outArg (sendReqSize));
7366 else if (myRank == p + 2) {
7369 std::ostringstream os;
7370 os << myRank <<
": Post send-size send: size = "
7371 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7374 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7379 reduceAll<int, int> (comm, REDUCE_MAX, lclErr, outArg (gblErr));
7380 TEUCHOS_TEST_FOR_EXCEPTION(
7381 gblErr == 1, std::runtime_error,
"Tpetra::MatrixMarket::writeDense "
7382 "experienced some kind of error and was unable to complete.");
7386 *dbg << myRank <<
": writeDenseColumn: Done" << endl;
7400 const Teuchos::RCP<const multivector_type>& X,
7401 const std::string& matrixName,
7402 const std::string& matrixDescription,
7403 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7404 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7406 TEUCHOS_TEST_FOR_EXCEPTION(
7407 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7408 "writeDense: The input MultiVector X is null.");
7409 writeDense (out, *X, matrixName, matrixDescription, err, dbg);
7420 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7421 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7433 const Teuchos::RCP<const multivector_type>& X,
7434 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7435 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7437 TEUCHOS_TEST_FOR_EXCEPTION(
7438 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7439 "writeDense: The input MultiVector X is null.");
7465 Teuchos::RCP<Teuchos::FancyOStream> err =
7466 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
7481 const Teuchos::RCP<Teuchos::FancyOStream>& err,
7482 const bool debug=
false)
7484 using Teuchos::Array;
7485 using Teuchos::ArrayRCP;
7486 using Teuchos::ArrayView;
7487 using Teuchos::Comm;
7488 using Teuchos::CommRequest;
7489 using Teuchos::ireceive;
7490 using Teuchos::isend;
7492 using Teuchos::TypeNameTraits;
7493 using Teuchos::wait;
7496 typedef int pid_type;
7511 typedef ptrdiff_t int_type;
7512 TEUCHOS_TEST_FOR_EXCEPTION(
7513 sizeof (GO) >
sizeof (int_type), std::logic_error,
7514 "The global ordinal type GO=" << TypeNameTraits<GO>::name ()
7515 <<
" is too big for ptrdiff_t. sizeof(GO) = " <<
sizeof (GO)
7516 <<
" > sizeof(ptrdiff_t) = " <<
sizeof (ptrdiff_t) <<
".");
7517 TEUCHOS_TEST_FOR_EXCEPTION(
7518 sizeof (pid_type) >
sizeof (int_type), std::logic_error,
7519 "The (MPI) process rank type pid_type=" <<
7520 TypeNameTraits<pid_type>::name () <<
" is too big for ptrdiff_t. "
7521 "sizeof(pid_type) = " <<
sizeof (pid_type) <<
" > sizeof(ptrdiff_t)"
7522 " = " <<
sizeof (ptrdiff_t) <<
".");
7524 const Comm<int>& comm = * (map.
getComm ());
7525 const int myRank = comm.getRank ();
7526 const int numProcs = comm.getSize ();
7528 if (! err.is_null ()) {
7532 std::ostringstream os;
7533 os << myRank <<
": writeMap" << endl;
7536 if (! err.is_null ()) {
7543 const int sizeTag = 1337;
7544 const int dataTag = 1338;
7603 RCP<CommRequest<int> > sendReqSize, sendReqData;
7609 Array<ArrayRCP<int_type> > recvSizeBufs (3);
7610 Array<ArrayRCP<int_type> > recvDataBufs (3);
7611 Array<RCP<CommRequest<int> > > recvSizeReqs (3);
7612 Array<RCP<CommRequest<int> > > recvDataReqs (3);
7615 ArrayRCP<int_type> sendDataSize (1);
7616 sendDataSize[0] = myNumRows;
7620 std::ostringstream os;
7621 os << myRank <<
": Post receive-size receives from "
7622 "Procs 1 and 2: tag = " << sizeTag << endl;
7626 recvSizeBufs[0].resize (1);
7627 (recvSizeBufs[0])[0] = -1;
7628 recvSizeBufs[1].resize (1);
7629 (recvSizeBufs[1])[0] = -1;
7630 recvSizeBufs[2].resize (1);
7631 (recvSizeBufs[2])[0] = -1;
7634 ireceive<int, int_type> (recvSizeBufs[1], 1, sizeTag, comm);
7638 ireceive<int, int_type> (recvSizeBufs[2], 2, sizeTag, comm);
7641 else if (myRank == 1 || myRank == 2) {
7643 std::ostringstream os;
7644 os << myRank <<
": Post send-size send: size = "
7645 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7652 sendReqSize = isend<int, int_type> (sendDataSize, 0, sizeTag, comm);
7656 std::ostringstream os;
7657 os << myRank <<
": Not posting my send-size send yet" << endl;
7668 std::ostringstream os;
7669 os << myRank <<
": Pack my GIDs and PIDs" << endl;
7673 ArrayRCP<int_type> sendDataBuf (myNumRows * 2);
7676 const int_type myMinGblIdx =
7678 for (
size_t k = 0; k < myNumRows; ++k) {
7679 const int_type gid = myMinGblIdx +
static_cast<int_type
> (k);
7680 const int_type pid =
static_cast<int_type
> (myRank);
7681 sendDataBuf[2*k] = gid;
7682 sendDataBuf[2*k+1] = pid;
7687 for (
size_t k = 0; k < myNumRows; ++k) {
7688 const int_type gid =
static_cast<int_type
> (myGblInds[k]);
7689 const int_type pid =
static_cast<int_type
> (myRank);
7690 sendDataBuf[2*k] = gid;
7691 sendDataBuf[2*k+1] = pid;
7696 std::ostringstream os;
7697 os << myRank <<
": Done packing my GIDs and PIDs" << endl;
7704 *err << myRank <<
": Post send-data send: tag = " << dataTag
7707 sendReqData = isend<int, int_type> (sendDataBuf, 0, dataTag, comm);
7712 *err << myRank <<
": Write MatrixMarket header" << endl;
7717 std::ostringstream hdr;
7721 hdr <<
"%%MatrixMarket matrix array integer general" << endl
7722 <<
"% Format: Version 2.0" << endl
7724 <<
"% This file encodes a Tpetra::Map." << endl
7725 <<
"% It is stored as a dense vector, with twice as many " << endl
7726 <<
"% entries as the global number of GIDs (global indices)." << endl
7727 <<
"% (GID, PID) pairs are stored contiguously, where the PID " << endl
7728 <<
"% is the rank of the process owning that GID." << endl
7733 std::ostringstream os;
7734 os << myRank <<
": Write my GIDs and PIDs" << endl;
7740 const int_type printNumRows = myNumRows;
7741 ArrayView<const int_type> printData = sendDataBuf ();
7742 for (int_type k = 0; k < printNumRows; ++k) {
7743 const int_type gid = printData[2*k];
7744 const int_type pid = printData[2*k+1];
7745 out << gid << endl << pid << endl;
7751 const int recvRank = 1;
7752 const int circBufInd = recvRank % 3;
7754 std::ostringstream os;
7755 os << myRank <<
": Wait on receive-size receive from Process "
7756 << recvRank << endl;
7760 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7764 const int_type recvNumRows = (recvSizeBufs[circBufInd])[0];
7765 if (debug && recvNumRows == -1) {
7766 std::ostringstream os;
7767 os << myRank <<
": Result of receive-size receive from Process "
7768 << recvRank <<
" is -1. This should never happen, and "
7769 "suggests that the receive never got posted. Please report "
7770 "this bug to the Tpetra developers." << endl;
7775 recvDataBufs[circBufInd].resize (recvNumRows * 2);
7777 std::ostringstream os;
7778 os << myRank <<
": Post receive-data receive from Process "
7779 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7780 << recvDataBufs[circBufInd].size () << endl;
7783 if (! recvSizeReqs[circBufInd].is_null ()) {
7784 std::ostringstream os;
7785 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7786 "null, before posting the receive-data receive from Process "
7787 << recvRank <<
". This should never happen. Please report "
7788 "this bug to the Tpetra developers." << endl;
7791 recvDataReqs[circBufInd] =
7792 ireceive<int, int_type> (recvDataBufs[circBufInd],
7793 recvRank, dataTag, comm);
7796 else if (myRank == 1) {
7799 std::ostringstream os;
7800 os << myRank <<
": Wait on my send-size send" << endl;
7803 wait<int> (comm, outArg (sendReqSize));
7809 for (
int p = 1; p < numProcs; ++p) {
7811 if (p + 2 < numProcs) {
7813 const int recvRank = p + 2;
7814 const int circBufInd = recvRank % 3;
7816 std::ostringstream os;
7817 os << myRank <<
": Post receive-size receive from Process "
7818 << recvRank <<
": tag = " << sizeTag << endl;
7821 if (! recvSizeReqs[circBufInd].is_null ()) {
7822 std::ostringstream os;
7823 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7824 <<
"null, for the receive-size receive from Process "
7825 << recvRank <<
"! This may mean that this process never "
7826 <<
"finished waiting for the receive from Process "
7827 << (recvRank - 3) <<
"." << endl;
7830 recvSizeReqs[circBufInd] =
7831 ireceive<int, int_type> (recvSizeBufs[circBufInd],
7832 recvRank, sizeTag, comm);
7835 if (p + 1 < numProcs) {
7836 const int recvRank = p + 1;
7837 const int circBufInd = recvRank % 3;
7841 std::ostringstream os;
7842 os << myRank <<
": Wait on receive-size receive from Process "
7843 << recvRank << endl;
7846 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7850 const int_type recvNumRows = (recvSizeBufs[circBufInd])[0];
7851 if (debug && recvNumRows == -1) {
7852 std::ostringstream os;
7853 os << myRank <<
": Result of receive-size receive from Process "
7854 << recvRank <<
" is -1. This should never happen, and "
7855 "suggests that the receive never got posted. Please report "
7856 "this bug to the Tpetra developers." << endl;
7861 recvDataBufs[circBufInd].resize (recvNumRows * 2);
7863 std::ostringstream os;
7864 os << myRank <<
": Post receive-data receive from Process "
7865 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7866 << recvDataBufs[circBufInd].size () << endl;
7869 if (! recvDataReqs[circBufInd].is_null ()) {
7870 std::ostringstream os;
7871 os << myRank <<
": recvDataReqs[" << circBufInd <<
"] is not "
7872 <<
"null, for the receive-data receive from Process "
7873 << recvRank <<
"! This may mean that this process never "
7874 <<
"finished waiting for the receive from Process "
7875 << (recvRank - 3) <<
"." << endl;
7878 recvDataReqs[circBufInd] =
7879 ireceive<int, int_type> (recvDataBufs[circBufInd],
7880 recvRank, dataTag, comm);
7884 const int recvRank = p;
7885 const int circBufInd = recvRank % 3;
7887 std::ostringstream os;
7888 os << myRank <<
": Wait on receive-data receive from Process "
7889 << recvRank << endl;
7892 wait<int> (comm, outArg (recvDataReqs[circBufInd]));
7899 std::ostringstream os;
7900 os << myRank <<
": Write GIDs and PIDs from Process "
7901 << recvRank << endl;
7902 *err << os.str () << endl;
7904 const int_type printNumRows = (recvSizeBufs[circBufInd])[0];
7905 if (debug && printNumRows == -1) {
7906 std::ostringstream os;
7907 os << myRank <<
": Result of receive-size receive from Process "
7908 << recvRank <<
" was -1. This should never happen, and "
7909 "suggests that its receive-size receive was never posted. "
7910 "Please report this bug to the Tpetra developers." << endl;
7913 if (debug && printNumRows > 0 && recvDataBufs[circBufInd].is_null ()) {
7914 std::ostringstream os;
7915 os << myRank <<
": Result of receive-size receive from Proc "
7916 << recvRank <<
" was " << printNumRows <<
" > 0, but "
7917 "recvDataBufs[" << circBufInd <<
"] is null. This should "
7918 "never happen. Please report this bug to the Tpetra "
7919 "developers." << endl;
7922 ArrayView<const int_type> printData = (recvDataBufs[circBufInd]) ();
7923 for (int_type k = 0; k < printNumRows; ++k) {
7924 const int_type gid = printData[2*k];
7925 const int_type pid = printData[2*k+1];
7926 out << gid << endl << pid << endl;
7929 else if (myRank == p) {
7932 std::ostringstream os;
7933 os << myRank <<
": Wait on my send-data send" << endl;
7936 wait<int> (comm, outArg (sendReqData));
7938 else if (myRank == p + 1) {
7941 std::ostringstream os;
7942 os << myRank <<
": Post send-data send: tag = " << dataTag
7946 sendReqData = isend<int, int_type> (sendDataBuf, 0, dataTag, comm);
7949 std::ostringstream os;
7950 os << myRank <<
": Wait on my send-size send" << endl;
7953 wait<int> (comm, outArg (sendReqSize));
7955 else if (myRank == p + 2) {
7958 std::ostringstream os;
7959 os << myRank <<
": Post send-size send: size = "
7960 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7963 sendReqSize = isend<int, int_type> (sendDataSize, 0, sizeTag, comm);
7967 if (! err.is_null ()) {
7971 *err << myRank <<
": writeMap: Done" << endl;
7973 if (! err.is_null ()) {
7983 const int myRank = map.
getComm ()->getRank ();
7986 out.open (filename.c_str());
8019 printAsComment (std::ostream& out,
const std::string& str)
8022 std::istringstream inpstream (str);
8025 while (getline (inpstream, line)) {
8026 if (! line.empty()) {
8029 if (line[0] ==
'%') {
8030 out << line << endl;
8033 out <<
"%% " << line << endl;
8061 Teuchos::ParameterList pl;
8087 Teuchos::ParameterList pl;
8130 const Teuchos::ParameterList& params)
8133 std::string tmpFile =
"__TMP__" + fileName;
8134 const int myRank = A.
getDomainMap()->getComm()->getRank();
8135 bool precisionChanged=
false;
8145 if (std::ifstream(tmpFile))
8146 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
8147 "writeOperator: temporary file " << tmpFile <<
" already exists");
8148 out.open(tmpFile.c_str());
8149 if (params.isParameter(
"precision")) {
8150 oldPrecision = out.precision(params.get<
int>(
"precision"));
8151 precisionChanged=
true;
8155 const std::string header = writeOperatorImpl(out, A, params);
8158 if (precisionChanged)
8159 out.precision(oldPrecision);
8161 out.open(fileName.c_str(), std::ios::binary);
8162 bool printMatrixMarketHeader =
true;
8163 if (params.isParameter(
"print MatrixMarket header"))
8164 printMatrixMarketHeader = params.get<
bool>(
"print MatrixMarket header");
8165 if (printMatrixMarketHeader && myRank == 0) {
8170 std::ifstream src(tmpFile, std::ios_base::binary);
8174 remove(tmpFile.c_str());
8219 const Teuchos::ParameterList& params)
8221 const int myRank = A.
getDomainMap ()->getComm ()->getRank ();
8238 std::ostringstream tmpOut;
8240 if (params.isParameter (
"precision") && params.isType<
int> (
"precision")) {
8241 (void) tmpOut.precision (params.get<
int> (
"precision"));
8245 const std::string header = writeOperatorImpl (tmpOut, A, params);
8248 bool printMatrixMarketHeader =
true;
8249 if (params.isParameter (
"print MatrixMarket header") &&
8250 params.isType<
bool> (
"print MatrixMarket header")) {
8251 printMatrixMarketHeader = params.get<
bool> (
"print MatrixMarket header");
8253 if (printMatrixMarketHeader && myRank == 0) {
8269 out << tmpOut.str ();
8283 writeOperatorImpl (std::ostream& os,
8284 const operator_type& A,
8285 const Teuchos::ParameterList& params)
8289 using Teuchos::ArrayRCP;
8290 using Teuchos::Array;
8295 typedef Teuchos::OrdinalTraits<LO> TLOT;
8296 typedef Teuchos::OrdinalTraits<GO> TGOT;
8300 const map_type& domainMap = *(A.getDomainMap());
8301 RCP<const map_type> rangeMap = A.getRangeMap();
8302 RCP<const Teuchos::Comm<int> > comm = rangeMap->getComm();
8303 const int myRank = comm->getRank();
8304 const size_t numProcs = comm->getSize();
8307 if (params.isParameter(
"probing size"))
8308 numMVs = params.get<
int>(
"probing size");
8311 GO minColGid = domainMap.getMinAllGlobalIndex();
8312 GO maxColGid = domainMap.getMaxAllGlobalIndex();
8317 GO numGlobElts = maxColGid - minColGid + TGOT::one();
8318 GO numChunks = numGlobElts / numMVs;
8319 GO rem = numGlobElts % numMVs;
8320 GO indexBase = rangeMap->getIndexBase();
8322 int offsetToUseInPrinting = 1 - indexBase;
8323 if (params.isParameter(
"zero-based indexing")) {
8324 if (params.get<
bool>(
"zero-based indexing") ==
true)
8325 offsetToUseInPrinting = -indexBase;
8329 size_t numLocalRangeEntries = rangeMap->getNodeNumElements();
8332 RCP<const map_type> allGidsMap = rcp(
new map_type(TGOT::invalid(), numLocalRangeEntries,
8335 mv_type_go allGids(allGidsMap,1);
8336 Teuchos::ArrayRCP<GO> allGidsData = allGids.getDataNonConst(0);
8338 for (
size_t i=0; i<numLocalRangeEntries; i++)
8339 allGidsData[i] = rangeMap->getGlobalElement(i);
8340 allGidsData = Teuchos::null;
8343 GO numTargetMapEntries=TGOT::zero();
8344 Teuchos::Array<GO> importGidList;
8346 numTargetMapEntries = rangeMap->getGlobalNumElements();
8347 importGidList.reserve(numTargetMapEntries);
8348 for (GO j=0; j<numTargetMapEntries; ++j) importGidList.push_back(j + indexBase);
8350 importGidList.reserve(numTargetMapEntries);
8352 RCP<map_type> importGidMap = rcp(
new map_type(TGOT::invalid(), importGidList(), indexBase, comm));
8355 import_type gidImporter(allGidsMap, importGidMap);
8356 mv_type_go importedGids(importGidMap, 1);
8357 importedGids.doImport(allGids, gidImporter,
INSERT);
8360 ArrayRCP<const GO> importedGidsData = importedGids.getData(0);
8361 RCP<const map_type> importMap = rcp(
new map_type(TGOT::invalid(), importedGidsData(), indexBase, comm) );
8364 import_type importer(rangeMap, importMap);
8366 RCP<mv_type> colsOnPid0 = rcp(
new mv_type(importMap,numMVs));
8368 RCP<mv_type> ei = rcp(
new mv_type(A.getDomainMap(),numMVs));
8369 RCP<mv_type> colsA = rcp(
new mv_type(A.getRangeMap(),numMVs));
8371 Array<GO> globalColsArray, localColsArray;
8372 globalColsArray.reserve(numMVs);
8373 localColsArray.reserve(numMVs);
8375 ArrayRCP<ArrayRCP<Scalar> > eiData(numMVs);
8376 for (
size_t i=0; i<numMVs; ++i)
8377 eiData[i] = ei->getDataNonConst(i);
8382 for (GO k=0; k<numChunks; ++k) {
8383 for (
size_t j=0; j<numMVs; ++j ) {
8385 GO curGlobalCol = minColGid + k*numMVs + j;
8386 globalColsArray.push_back(curGlobalCol);
8388 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8389 if (curLocalCol != TLOT::invalid()) {
8390 eiData[j][curLocalCol] = TGOT::one();
8391 localColsArray.push_back(curLocalCol);
8397 A.apply(*ei,*colsA);
8399 colsOnPid0->doImport(*colsA,importer,
INSERT);
8402 globalNnz += writeColumns(os,*colsOnPid0, numMVs, importedGidsData(),
8403 globalColsArray, offsetToUseInPrinting);
8406 for (
size_t j=0; j<numMVs; ++j ) {
8407 for (
int i=0; i<localColsArray.size(); ++i)
8408 eiData[j][localColsArray[i]] = TGOT::zero();
8410 globalColsArray.clear();
8411 localColsArray.clear();
8419 for (
int j=0; j<rem; ++j ) {
8420 GO curGlobalCol = maxColGid - rem + j + TGOT::one();
8421 globalColsArray.push_back(curGlobalCol);
8423 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8424 if (curLocalCol != TLOT::invalid()) {
8425 eiData[j][curLocalCol] = TGOT::one();
8426 localColsArray.push_back(curLocalCol);
8432 A.apply(*ei,*colsA);
8434 colsOnPid0->doImport(*colsA,importer,
INSERT);
8436 globalNnz += writeColumns(os,*colsOnPid0, rem, importedGidsData(),
8437 globalColsArray, offsetToUseInPrinting);
8440 for (
int j=0; j<rem; ++j ) {
8441 for (
int i=0; i<localColsArray.size(); ++i)
8442 eiData[j][localColsArray[i]] = TGOT::zero();
8444 globalColsArray.clear();
8445 localColsArray.clear();
8454 std::ostringstream oss;
8456 oss <<
"%%MatrixMarket matrix coordinate ";
8457 if (Teuchos::ScalarTraits<typename operator_type::scalar_type>::isComplex) {
8462 oss <<
" general" << std::endl;
8463 oss <<
"% Tpetra::Operator" << std::endl;
8464 std::time_t now = std::time(NULL);
8465 oss <<
"% time stamp: " << ctime(&now);
8466 oss <<
"% written from " << numProcs <<
" processes" << std::endl;
8467 size_t numRows = rangeMap->getGlobalNumElements();
8468 size_t numCols = domainMap.getGlobalNumElements();
8469 oss << numRows <<
" " << numCols <<
" " << globalNnz << std::endl;
8476 writeColumns(std::ostream& os, mv_type
const &colsA,
size_t const &numCols,
8477 Teuchos::ArrayView<const global_ordinal_type>
const &rowGids,
8478 Teuchos::Array<global_ordinal_type>
const &colsArray,
8483 typedef Teuchos::ScalarTraits<Scalar> STS;
8486 const Scalar zero = STS::zero();
8487 const size_t numRows = colsA.getGlobalLength();
8488 for (
size_t j=0; j<numCols; ++j) {
8489 Teuchos::ArrayRCP<const Scalar>
const curCol = colsA.getData(j);
8490 const GO J = colsArray[j];
8491 for (
size_t i=0; i<numRows; ++i) {
8492 const Scalar val = curCol[i];
8494 os << rowGids[i]+indexBase <<
" " << J+indexBase <<
" " << val << std::endl;
From a distributed map build a map with all GIDs on the root node.
Declaration of a function that prints strings from each process.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
Teuchos::RCP< const map_type > getColMap() const override
Returns the Map that describes the column distribution in this graph.
global_size_t getGlobalNumEntries() const override
Returns the global number of entries in the graph.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
Returns the communicator.
void fillComplete(const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Tell the graph that you are done changing its structure.
Teuchos::RCP< const map_type > getDomainMap() const override
Returns the Map associated with the domain of this graph.
void getGlobalRowView(const global_ordinal_type gblRow, global_inds_host_view_type &gblColInds) const override
Get a const view of the given global row's global column indices.
Teuchos::RCP< const map_type > getRangeMap() const override
Returns the Map associated with the domain of this graph.
Teuchos::RCP< const map_type > getRowMap() const override
Returns the Map that describes the row distribution in this graph.
void getLocalRowView(const LocalOrdinal lclRow, local_inds_host_view_type &lclColInds) const override
Get a const view of the given local row's local column indices.
bool isGloballyIndexed() const override
Whether the graph's column indices are stored as global indices.
void doImport(const SrcDistObject &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, const CombineMode CM, const bool restrictedMode=false)
Import data into this object using an Import object ("forward mode").
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
A parallel distribution of indices over processes.
Teuchos::ArrayView< const global_ordinal_type > getNodeElementList() const
Return a NONOWNING view of the global indices owned by this process.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
global_ordinal_type getMinGlobalIndex() const
The minimum global index owned by the calling process.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
bool isContiguous() const
True if this Map is distributed contiguously, else false.
size_t getNodeNumElements() const
The number of elements belonging to the calling process.
Matrix Market file reader for CrsMatrix and MultiVector.
MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > multivector_type
The MultiVector specialization associated with SparseMatrixType.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream.
SparseMatrixType::global_ordinal_type global_ordinal_type
static Teuchos::RCP< vector_type > readVector(std::istream &in, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read Vector from the given Matrix Market input stream.
static Teuchos::RCP< const map_type > readMap(std::istream &in, const Teuchos::RCP< const comm_type > &comm, const Teuchos::RCP< Teuchos::FancyOStream > &err, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given input stream, with optional debugging output stream.
Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > vector_type
The Vector specialization associated with SparseMatrixType.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream, with provided Maps.
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file.
SparseMatrixType::node_type node_type
The fourth template parameter of CrsMatrix and MultiVector.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream.
static Teuchos::RCP< vector_type > readVectorFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read a Vector from the given Matrix Market file.
static Teuchos::RCP< multivector_type > readDense(std::istream &in, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read dense matrix (as a MultiVector) from the given Matrix Market input stream.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream, with provided Maps.
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file.
static Teuchos::RCP< multivector_type > readDenseFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read dense matrix (as a MultiVector) from the given Matrix Market file.
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file, with provided Maps.
SparseMatrixType::local_ordinal_type local_ordinal_type
SparseMatrixType sparse_matrix_type
This class' template parameter; a specialization of CrsMatrix.
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market input stream.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market input stream.
static Teuchos::RCP< const map_type > readMapFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given Matrix Market file.
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file.
SparseMatrixType::scalar_type scalar_type
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file, with provided Maps.
CrsGraph< local_ordinal_type, global_ordinal_type, node_type > sparse_graph_type
The CrsGraph specialization associated with SparseMatrixType.
static Teuchos::RCP< const map_type > readMap(std::istream &in, const Teuchos::RCP< const comm_type > &comm, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given input stream.
Matrix Market file writer for CrsMatrix and MultiVector.
static void writeMap(std::ostream &out, const map_type &map, const bool debug=false)
Print the Map to the given output stream.
static void writeDense(std::ostream &out, const Teuchos::RCP< const multivector_type > &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and or description.
static void writeSparseFile(const std::string &filename, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Only for backwards compatibility; prefer the overload above.
CrsGraph< local_ordinal_type, global_ordinal_type, node_type > crs_graph_type
Specialization of Tpetra::CrsGraph that matches SparseMatrixType.
static void writeSparseFile(const std::string &filename, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const bool debug=false)
Only for backwards compatibility; prefer the overload above.
static void writeSparseGraphFile(const std::string &filename, const crs_graph_type &graph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), with no comments.
static void writeDense(std::ostream &out, const multivector_type &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
static void writeSparseGraphFile(const std::string &filename, const Teuchos::RCP< const crs_graph_type > &pGraph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), with no comments,...
SparseMatrixType sparse_matrix_type
Template parameter of this class; specialization of CrsMatrix.
static void writeSparse(std::ostream &out, const sparse_matrix_type &matrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Print the sparse matrix in Matrix Market format, with comments.
SparseMatrixType::scalar_type scalar_type
Type of the entries of the sparse matrix.
static void writeSparseGraphFile(const std::string &filename, const Teuchos::RCP< const crs_graph_type > &pGraph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), taking the graph by T...
static void writeDenseFile(const std::string &filename, const Teuchos::RCP< const multivector_type > &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
static void writeDense(std::ostream &out, const Teuchos::RCP< const multivector_type > &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
SparseMatrixType::global_ordinal_type global_ordinal_type
Type of indices as read from the Matrix Market file.
SparseMatrixType::node_type node_type
The Kokkos Node type; fourth template parameter of Tpetra::CrsMatrix.
static void writeDense(std::ostream &out, const multivector_type &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static void writeSparse(std::ostream &out, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Only for backwards compatibility; prefer the overload above.
static void writeOperator(std::ostream &out, const operator_type &A)
Write a Tpetra::Operator to an output stream.
static void writeOperator(const std::string &fileName, operator_type const &A)
Write a Tpetra::Operator to a file.
static void writeOperator(std::ostream &out, const operator_type &A, const Teuchos::ParameterList ¶ms)
Write a Tpetra::Operator to an output stream, with options.
static void writeDenseFile(const std::string &filename, const multivector_type &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
static void writeDenseFile(const std::string &filename, const multivector_type &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static void writeSparseGraph(std::ostream &out, const crs_graph_type &graph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given output stream, with no comments.
static void writeOperator(const std::string &fileName, const operator_type &A, const Teuchos::ParameterList ¶ms)
Write a Tpetra::Operator to a file, with options.
MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > multivector_type
Specialization of Tpetra::MultiVector that matches SparseMatrixType.
static void writeSparse(std::ostream &out, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const bool debug=false)
Only for backwards compatibility; prefer the overload above.
static void writeMapFile(const std::string &filename, const map_type &map)
Write the Map to the given file.
static void writeDenseFile(const std::string &filename, const Teuchos::RCP< const multivector_type > &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
Map< local_ordinal_type, global_ordinal_type, node_type > map_type
Specialization of Tpetra::Map that matches SparseMatrixType.
SparseMatrixType::local_ordinal_type local_ordinal_type
Type of the local indices of the sparse matrix.
static void writeSparseFile(const std::string &filename, const sparse_matrix_type &matrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Print the sparse matrix in Matrix Market format, with comments.
static void writeSparseGraph(std::ostream &out, const crs_graph_type &graph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given output stream.
static void writeSparseGraphFile(const std::string &filename, const crs_graph_type &graph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename).
static void writeMap(std::ostream &out, const map_type &map, const Teuchos::RCP< Teuchos::FancyOStream > &err, const bool debug=false)
Print the Map to the given output stream out.
static void writeSparseFile(const std::string &filename, const sparse_matrix_type &matrix, const bool debug=false)
Print the sparse matrix in Matrix Market format.
static void writeSparse(std::ostream &out, const sparse_matrix_type &matrix, const bool debug=false)
Print the sparse matrix in Matrix Market format.
One or more distributed dense vectors.
Teuchos::RCP< const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVector(const size_t j) const
Return a Vector which is a const view of column j.
size_t getNumVectors() const
Number of columns in the multivector.
Abstract interface for operators (e.g., matrices and preconditioners).
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X....
A distributed dense vector.
Matrix Market file readers and writers for sparse and dense matrices (as CrsMatrix resp....
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator,...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
size_t global_size_t
Global size_t object.
@ INSERT
Insert new values that don't currently exist.