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,
282 pComm, GloballyDistributed));
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).");
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,
1653 const bool debug=
false)
1655 using Teuchos::broadcast;
1656 using Teuchos::outArg;
1664 if (comm->getRank () == 0) {
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,
1720 const bool debug=
false)
1722 using Teuchos::broadcast;
1723 using Teuchos::outArg;
1731 if (
pComm->getRank () == 0) {
1742 (
opened == 0, std::runtime_error,
"readSparseGraphFile: "
1743 "Failed to open file \"" <<
filename <<
"\" on Process 0.");
1744 if (
pComm->getRank () == 0) {
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,
1801 const bool debug=
false)
1803 using Teuchos::broadcast;
1804 using Teuchos::Comm;
1805 using Teuchos::outArg;
1809 (rowMap.is_null (), std::invalid_argument,
1810 "Input rowMap must be nonnull.");
1812 if (comm.is_null ()) {
1815 return Teuchos::null;
1824 if (comm->getRank () == 0) {
1835 (
opened == 0, std::runtime_error,
"readSparseGraphFile: "
1836 "Failed to open file \"" <<
filename <<
"\" on Process 0.");
1866 static Teuchos::RCP<sparse_graph_type>
1868 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
1871 const bool debug=
false)
1877 Teuchos::RCP<sparse_graph_type>
graph =
1878 readSparseGraphHelper (
in,
pComm,
1882 graph->fillComplete ();
1917 static Teuchos::RCP<sparse_graph_type>
1919 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
1923 const bool debug=
false)
1927 Teuchos::RCP<sparse_graph_type>
graph =
1928 readSparseGraphHelper (
in,
pComm,
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,
1984 const bool debug=
false)
1986 Teuchos::RCP<sparse_graph_type>
graph =
1987 readSparseGraphHelper (
in, rowMap->getComm (),
1988 rowMap, colMap, Teuchos::null,
tolerant,
1996#include "MatrixMarket_TpetraNew.hpp"
2021 static Teuchos::RCP<sparse_matrix_type>
2023 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2026 const bool debug=
false)
2076 static Teuchos::RCP<sparse_matrix_type>
2078 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2082 const bool debug=
false)
2085 if (
pComm->getRank () == 0) {
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,
2138 const bool debug=
false)
2140 using Teuchos::broadcast;
2141 using Teuchos::Comm;
2142 using Teuchos::outArg;
2146 rowMap.is_null (), std::invalid_argument,
2147 "Row Map must be nonnull.");
2150 const int myRank = comm->getRank ();
2169 opened == 0, std::runtime_error,
2170 "readSparseFile: Failed to open file \"" <<
filename <<
"\" on "
2201 static Teuchos::RCP<sparse_matrix_type>
2203 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
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;
2232 cerr <<
"Matrix Market reader: readSparse:" <<
endl
2233 <<
"-- Reading banner line" <<
endl;
2249 std::ostringstream
errMsg;
2256 catch (std::exception&
e) {
2257 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2258 "threw an exception: " <<
e.what();
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.";
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 "
2297 std::invalid_argument,
errMsg.str ());
2300 cerr <<
"-- Reading dimensions line" <<
endl;
2312 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
2321 cerr <<
"-- Reading matrix data" <<
endl;
2332 std::ostringstream
errMsg;
2336 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2341 std::pair<bool, std::vector<size_t> >
results =
2345 catch (std::exception&
e) {
2360 "Failed to read the Matrix Market sparse matrix file: "
2365 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
2379 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2381 <<
"----- Dimensions before: "
2394 std::max (
dims[0],
pAdder->getAdder()->numRows());
2401 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
2422 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
2442 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
2443 <<
dims[1] <<
", but the actual data says that the matrix is "
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 "
2453 cerr <<
"-- Converting matrix data into CSR format on Proc 0" <<
endl;
2474 std::ostringstream
errMsg;
2478 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
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;
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.");
2545 catch (std::exception&
e) {
2547 errMsg <<
"Failed to merge sparse matrix entries and convert to "
2548 "CSR format: " <<
e.what();
2556 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
2559 cerr <<
"(only showing first and last few entries) ";
2564 for (size_type
k = 0;
k < 2; ++
k) {
2581 cerr <<
"----- Proc 0: rowPtr ";
2583 cerr <<
"(only showing first and last few entries) ";
2587 for (size_type
k = 0;
k < 2; ++
k) {
2612 cerr <<
"-- Making range, domain, and row maps" <<
endl;
2625 cerr <<
"-- Distributing the matrix data" <<
endl;
2648 cerr <<
"-- Inserting matrix entries on each processor";
2650 cerr <<
" and calling fillComplete()";
2672 "Reader::makeMatrix() returned a null pointer on at least one "
2673 "process. Please report this bug to the Tpetra developers.");
2677 "Reader::makeMatrix() returned a null pointer. "
2678 "Please report this bug to the Tpetra developers.");
2699 cerr <<
"-- Matrix is "
2701 <<
" with " <<
pMatrix->getGlobalNumEntries()
2702 <<
" entries, and index base "
2708 cerr <<
"-- Proc " <<
p <<
" owns "
2709 <<
pMatrix->getNodeNumCols() <<
" columns, and "
2710 <<
pMatrix->getNodeNumEntries() <<
" entries." <<
endl;
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,
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;
2784 cerr <<
"Matrix Market reader: readSparse:" <<
endl
2785 <<
"-- Reading banner line" <<
endl;
2801 std::ostringstream
errMsg;
2808 catch (std::exception&
e) {
2809 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2810 "threw an exception: " <<
e.what();
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.";
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 "
2849 std::invalid_argument,
errMsg.str ());
2852 cerr <<
"-- Reading dimensions line" <<
endl;
2864 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
2873 cerr <<
"-- Reading matrix data" <<
endl;
2884 std::ostringstream
errMsg;
2888 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2893 std::pair<bool, std::vector<size_t> >
results =
2897 catch (std::exception&
e) {
2912 "Failed to read the Matrix Market sparse matrix file: "
2917 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
2931 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2933 <<
"----- Dimensions before: "
2946 std::max (
dims[0],
pAdder->getAdder()->numRows());
2953 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
2974 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
2994 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
2995 <<
dims[1] <<
", but the actual data says that the matrix is "
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 "
3005 cerr <<
"-- Converting matrix data into CSR format on Proc 0" <<
endl;
3026 std::ostringstream
errMsg;
3030 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
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;
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.");
3097 catch (std::exception&
e) {
3099 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3100 "CSR format: " <<
e.what();
3108 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3111 cerr <<
"(only showing first and last few entries) ";
3116 for (size_type
k = 0;
k < 2; ++
k) {
3133 cerr <<
"----- Proc 0: rowPtr ";
3135 cerr <<
"(only showing first and last few entries) ";
3139 for (size_type
k = 0;
k < 2; ++
k) {
3164 cerr <<
"-- Making range, domain, and row maps" <<
endl;
3177 cerr <<
"-- Distributing the matrix data" <<
endl;
3200 cerr <<
"-- Inserting matrix entries on each process "
3201 "and calling fillComplete()" <<
endl;
3210 Teuchos::RCP<sparse_matrix_type>
pMatrix =
3222 "Reader::makeMatrix() returned a null pointer on at least one "
3223 "process. Please report this bug to the Tpetra developers.");
3227 "Reader::makeMatrix() returned a null pointer. "
3228 "Please report this bug to the Tpetra developers.");
3244 cerr <<
"-- Matrix is "
3246 <<
" with " <<
pMatrix->getGlobalNumEntries()
3247 <<
" entries, and index base "
3253 cerr <<
"-- Proc " <<
p <<
" owns "
3254 <<
pMatrix->getNodeNumCols() <<
" columns, and "
3255 <<
pMatrix->getNodeNumEntries() <<
" entries." <<
endl;
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,
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;
3344 rowMap.is_null (), std::invalid_argument,
3345 "Row Map must be nonnull.");
3347 rangeMap.is_null (), std::invalid_argument,
3348 "Range Map must be nonnull.");
3350 domainMap.is_null (), std::invalid_argument,
3351 "Domain Map must be nonnull.");
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.");
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.");
3364 std::invalid_argument,
3365 "The specified range Map's communicator (rangeMap->getComm())"
3366 "differs from the given separately supplied communicator pComm.");
3374 cerr <<
"Matrix Market reader: readSparse:" <<
endl
3375 <<
"-- Reading banner line" <<
endl;
3391 std::ostringstream
errMsg;
3398 catch (std::exception&
e) {
3399 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
3400 "threw an exception: " <<
e.what();
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.";
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 "
3439 std::invalid_argument,
errMsg.str ());
3442 cerr <<
"-- Reading dimensions line" <<
endl;
3454 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
3465 cerr <<
"-- Reading matrix data" <<
endl;
3476 std::ostringstream
errMsg;
3480 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
3485 std::pair<bool, std::vector<size_t> >
results =
3489 catch (std::exception&
e) {
3504 "Failed to read the Matrix Market sparse matrix file: "
3509 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
3523 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
3525 <<
"----- Dimensions before: "
3538 std::max (
dims[0],
pAdder->getAdder()->numRows());
3545 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
3566 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
3586 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
3587 <<
dims[1] <<
", but the actual data says that the matrix is "
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 "
3597 cerr <<
"-- Converting matrix data into CSR format on Proc 0" <<
endl;
3619 std::ostringstream
errMsg;
3623 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
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;
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.");
3690 catch (std::exception&
e) {
3692 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3693 "CSR format: " <<
e.what();
3701 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3704 cerr <<
"(only showing first and last few entries) ";
3709 for (size_type
k = 0;
k < 2; ++
k) {
3726 cerr <<
"----- Proc 0: rowPtr ";
3728 cerr <<
"(only showing first and last few entries) ";
3732 for (size_type
k = 0;
k < 2; ++
k) {
3747 cerr <<
"----- Proc 0: colInd = [";
3766 cerr <<
"-- Verifying Maps" <<
endl;
3770 std::invalid_argument,
3771 "The range Map has " <<
rangeMap->getGlobalNumElements ()
3772 <<
" entries, but the matrix has a global number of rows " <<
dims[0]
3776 std::invalid_argument,
3777 "The domain Map has " << domainMap->getGlobalNumElements ()
3778 <<
" entries, but the matrix has a global number of columns "
3783 Teuchos::getFancyOStream (Teuchos::rcpFromRef (
cerr)) :
null;
3803 Tpetra::StaticProfile));
3806 cerr <<
"-- Proc 0: Filling gather matrix" <<
endl;
3848 if (colMap.is_null ()) {
3877 cerr <<
"-- Matrix is "
3879 <<
" with " <<
A->getGlobalNumEntries()
3880 <<
" entries, and index base "
3881 <<
A->getIndexBase() <<
"." <<
endl;
3886 cerr <<
"-- Proc " <<
p <<
" owns "
3887 <<
A->getNodeNumCols() <<
" columns, and "
3888 <<
A->getNodeNumEntries() <<
" entries." <<
endl;
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,
3935 const bool debug=
false)
3937 using Teuchos::broadcast;
3938 using Teuchos::outArg;
3942 if (comm->getRank() == 0) {
3953 opened == 0, std::runtime_error,
3954 "readDenseFile: Failed to open file \"" <<
filename <<
"\" on "
3989 static Teuchos::RCP<vector_type>
3991 const Teuchos::RCP<const comm_type>& comm,
3992 Teuchos::RCP<const map_type>&
map,
3994 const bool debug=
false)
3996 using Teuchos::broadcast;
3997 using Teuchos::outArg;
4001 if (comm->getRank() == 0) {
4012 opened == 0, std::runtime_error,
4013 "readVectorFile: Failed to open file \"" <<
filename <<
"\" on "
4085 static Teuchos::RCP<multivector_type>
4087 const Teuchos::RCP<const comm_type>& comm,
4088 Teuchos::RCP<const map_type>&
map,
4090 const bool debug=
false)
4092 Teuchos::RCP<Teuchos::FancyOStream>
err =
4093 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4099 static Teuchos::RCP<vector_type>
4101 const Teuchos::RCP<const comm_type>& comm,
4102 Teuchos::RCP<const map_type>&
map,
4104 const bool debug=
false)
4106 Teuchos::RCP<Teuchos::FancyOStream>
err =
4107 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4132 static Teuchos::RCP<const map_type>
4134 const Teuchos::RCP<const comm_type>& comm,
4136 const bool debug=
false)
4138 using Teuchos::inOutArg;
4139 using Teuchos::broadcast;
4143 if (comm->getRank () == 0) {
4151 success == 0, std::runtime_error,
4152 "Tpetra::MatrixMarket::Reader::readMapFile: "
4153 "Failed to read file \"" <<
filename <<
"\" on Process 0.");
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,
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;
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 ()) {
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,
5202 const bool debug=
false)
5204 Teuchos::RCP<Teuchos::FancyOStream>
err =
5205 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
5235 static Teuchos::RCP<const map_type>
5237 const Teuchos::RCP<const comm_type>& comm,
5238 const Teuchos::RCP<Teuchos::FancyOStream>&
err,
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;
5268 const int numProcs = comm->getSize ();
5269 const int myRank = comm->getRank ();
5271 if (
err.is_null ()) {
5275 std::ostringstream
os;
5279 if (
err.is_null ()) {
5306 std::ostringstream
exMsg;
5323 if (
data.is_null ()) {
5325 exMsg <<
"readDenseImpl() returned null." <<
endl;
5327 }
catch (std::exception&
e) {
5335 std::map<int, Array<GO> >
pid2gids;
5351 if (
data->getNumVectors () == 2) {
5359 const int pid =
static_cast<int> (
PIDs[0]);
5363 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5364 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5376 const int pid =
static_cast<int> (
PIDs[
k]);
5379 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5380 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
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;
5402 static_cast<GO
> (2);
5407 const int pid =
static_cast<int> (
theData[1]);
5410 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5411 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5422 const int pid =
static_cast<int> (
theData[2*
k + 1]);
5425 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5426 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
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).";
5465 "Tpetra::MatrixMarket::readMap: Reading the Map failed with the "
5466 "following exception message: " <<
exMsg.str ());
5495 typename std::map<int, Array<GO> >::iterator
it =
pid2gids.find (0);
5523 typename std::map<int, Array<GO> >::iterator
it =
pid2gids.find (
p);
5542 std::ostringstream
os;
5546 const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
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,
5575 if (
err.is_null ()) {
5579 std::ostringstream
os;
5583 if (
err.is_null ()) {
5603 encodeDataType (
const std::string&
dataType)
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.");
5656 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
5720 const bool debug=
false)
5722 Teuchos::RCP<const Teuchos::Comm<int> > comm =
matrix.getComm ();
5724 (comm.is_null (), std::invalid_argument,
5725 "The input matrix's communicator (Teuchos::Comm object) is null.");
5726 const int myRank = comm->getRank ();
5742 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
5745 const bool debug=
false)
5748 (
pMatrix.is_null (), std::invalid_argument,
5749 "The input matrix is null.");
5776 const bool debug=
false)
5784 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
5785 const bool debug=
false)
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);
5850 comm.is_null (), std::invalid_argument,
5851 "The input matrix's communicator (Teuchos::Comm object) is null.");
5852 const int myRank = comm->getRank ();
5858 std::ostringstream
os;
5875 const global_size_t numCols = domainMap->getGlobalNumElements ();
5877 if (debug &&
myRank == 0) {
5878 std::ostringstream
os;
5879 os <<
"-- Input sparse matrix is:"
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;
5894 std::ostringstream
os;
5895 os <<
"-- " <<
myRank <<
": making gatherRowMap" <<
endl;
5899 Details::computeGatherMap (rowMap,
err, debug);
5909 Details::computeGatherMap (colMap,
err, debug);
5920 static_cast<size_t> (0)));
5929 domainMap->getIndexBase (),
5939 cerr <<
"-- Output sparse matrix is:"
5940 <<
"---- " <<
newMatrix->getRangeMap ()->getGlobalNumElements ()
5942 <<
newMatrix->getDomainMap ()->getGlobalNumElements ()
5946 << (
newMatrix->isGloballyIndexed () ?
"Globally" :
"Locally")
5947 <<
" indexed." <<
endl
5948 <<
"---- Its row map has "
5949 <<
newMatrix->getRowMap ()->getGlobalNumElements ()
5950 <<
" elements, with index base "
5952 <<
"---- Its col map has "
5953 <<
newMatrix->getColMap ()->getGlobalNumElements ()
5954 <<
" elements, with index base "
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;
5990 out <<
newMatrix->getRangeMap ()->getGlobalNumElements () <<
" "
5991 <<
newMatrix->getDomainMap ()->getGlobalNumElements () <<
" "
6014 typename sparse_matrix_type::global_inds_host_view_type
ind;
6015 typename sparse_matrix_type::values_host_view_type
val;
6017 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6023 if (STS::isComplex) {
6033 using OTG = Teuchos::OrdinalTraits<GO>;
6042 "Failed to convert the supposed local 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;
6048 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
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.");
6062 if (STS::isComplex) {
6077 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
6080 const bool debug=
false)
6083 (
pMatrix.is_null (), std::invalid_argument,
6084 "The input matrix is null.");
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;
6141 auto rowMap =
graph.getRowMap ();
6142 if (rowMap.is_null ()) {
6145 auto comm = rowMap->getComm ();
6146 if (comm.is_null ()) {
6149 const int myRank = comm->getRank ();
6155 std::ostringstream
os;
6167 auto colMap =
graph.getColMap ();
6168 auto domainMap =
graph.getDomainMap ();
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 "
6178 <<
graph.getGlobalNumEntries () <<
" entries;" <<
endl
6180 << (
graph.isGloballyIndexed () ?
"Globally" :
"Locally")
6181 <<
" indexed." <<
endl
6182 <<
"---- Its row Map has " << rowMap->getGlobalNumElements ()
6183 <<
" elements." <<
endl
6184 <<
"---- Its col Map has " << colMap->getGlobalNumElements ()
6185 <<
" elements." <<
endl;
6192 std::ostringstream
os;
6193 os <<
"-- " <<
myRank <<
": making gatherRowMap" <<
endl;
6214 static_cast<size_t> (0));
6223 domainMap->getIndexBase (),
6233 cerr <<
"-- Output sparse graph is:"
6234 <<
"---- " <<
newGraph.getRangeMap ()->getGlobalNumElements ()
6236 <<
newGraph.getDomainMap ()->getGlobalNumElements ()
6238 <<
newGraph.getGlobalNumEntries () <<
" entries;" <<
endl
6240 << (
newGraph.isGloballyIndexed () ?
"Globally" :
"Locally")
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;
6284 out <<
newGraph.getRangeMap ()->getGlobalNumElements () <<
" "
6285 <<
newGraph.getDomainMap ()->getGlobalNumElements () <<
" "
6300 if (
newGraph.isGloballyIndexed ()) {
6308 typename crs_graph_type::global_inds_host_view_type
ind;
6310 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6321 typedef Teuchos::OrdinalTraits<GO>
OTG;
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++) {
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.");
6364 const bool debug=
false)
6408 const bool debug=
false)
6410 auto comm =
graph.getComm ();
6411 if (comm.is_null ()) {
6419 const int myRank = comm->getRank ();
6439 const bool debug=
false)
6454 const Teuchos::RCP<const crs_graph_type>&
pGraph,
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)
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 ());
6576 const Teuchos::RCP<const multivector_type>&
X,
6579 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
6580 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
6583 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6584 "writeDenseFile: The input MultiVector X is null.");
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)
6614 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6615 "writeDenseFile: The input MultiVector X is null.");
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;
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;
6685 const size_t numVecs =
X.getNumVectors ();
6687 writeDenseColumn (
out, * (
X.getVector (
j)),
err,
dbg);
6692 std::ostringstream
os;
6727 writeDenseHeader (std::ostream&
out,
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: ";
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;
6776 std::ostringstream
hdr;
6779 if (STS::isComplex) {
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,
7403 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7404 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7407 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7408 "writeDense: The input MultiVector X is null.");
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)
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;
7513 sizeof (GO) >
sizeof (
int_type), std::logic_error,
7515 <<
" is too big for ptrdiff_t. sizeof(GO) = " <<
sizeof (GO)
7516 <<
" > sizeof(ptrdiff_t) = " <<
sizeof (
ptrdiff_t) <<
".");
7519 "The (MPI) process rank type pid_type=" <<
7521 "sizeof(pid_type) = " <<
sizeof (
pid_type) <<
" > sizeof(ptrdiff_t)"
7525 const int myRank = comm.getRank ();
7526 const int numProcs = comm.getSize ();
7528 if (!
err.is_null ()) {
7532 std::ostringstream
os;
7536 if (!
err.is_null ()) {
7620 std::ostringstream
os;
7621 os <<
myRank <<
": Post receive-size receives from "
7643 std::ostringstream
os;
7644 os <<
myRank <<
": Post send-size send: size = "
7656 std::ostringstream
os;
7657 os <<
myRank <<
": Not posting my send-size send yet" <<
endl;
7668 std::ostringstream
os;
7675 if (
map.isContiguous ()) {
7696 std::ostringstream
os;
7697 os <<
myRank <<
": Done packing my GIDs and PIDs" <<
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
7729 << (2 *
map.getGlobalNumElements ()) <<
" " << 1 <<
endl;
7733 std::ostringstream
os;
7754 std::ostringstream
os;
7755 os <<
myRank <<
": Wait on receive-size receive from Process "
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;
7777 std::ostringstream
os;
7778 os <<
myRank <<
": Post receive-data receive from Process "
7784 std::ostringstream
os;
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;
7799 std::ostringstream
os;
7816 std::ostringstream
os;
7817 os <<
myRank <<
": Post receive-size receive from Process "
7822 std::ostringstream
os;
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 "
7841 std::ostringstream
os;
7842 os <<
myRank <<
": Wait on receive-size receive from Process "
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;
7863 std::ostringstream
os;
7864 os <<
myRank <<
": Post receive-data receive from Process "
7870 std::ostringstream
os;
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 "
7887 std::ostringstream
os;
7888 os <<
myRank <<
": Wait on receive-data receive from Process "
7899 std::ostringstream
os;
7900 os <<
myRank <<
": Write GIDs and PIDs from Process "
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;
7914 std::ostringstream
os;
7915 os <<
myRank <<
": Result of receive-size receive from Proc "
7917 "recvDataBufs[" <<
circBufInd <<
"] is null. This should "
7918 "never happen. Please report this bug to the Tpetra "
7919 "developers." <<
endl;
7932 std::ostringstream
os;
7941 std::ostringstream
os;
7949 std::ostringstream
os;
7958 std::ostringstream
os;
7959 os <<
myRank <<
": Post send-size send: size = "
7967 if (!
err.is_null ()) {
7973 if (!
err.is_null ()) {
7983 const int myRank =
map.getComm ()->getRank ();
8019 printAsComment (std::ostream&
out,
const std::string&
str)
8026 if (!
line.empty()) {
8029 if (
line[0] ==
'%') {
8061 Teuchos::ParameterList
pl;
8087 Teuchos::ParameterList
pl;
8130 const Teuchos::ParameterList&
params)
8134 const int myRank =
A.getDomainMap()->getComm()->getRank();
8147 "writeOperator: temporary file " <<
tmpFile <<
" already exists");
8149 if (
params.isParameter(
"precision")) {
8163 if (
params.isParameter(
"print MatrixMarket header"))
8170 std::ifstream src(
tmpFile, std::ios_base::binary);
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")) {
8249 if (
params.isParameter (
"print MatrixMarket header") &&
8250 params.isType<
bool> (
"print MatrixMarket header")) {
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());
8303 const int myRank = comm->getRank();
8304 const size_t numProcs = comm->getSize();
8307 if (
params.isParameter(
"probing size"))
8311 GO
minColGid = domainMap.getMinAllGlobalIndex();
8312 GO
maxColGid = domainMap.getMaxAllGlobalIndex();
8323 if (
params.isParameter(
"zero-based indexing")) {
8324 if (
params.get<
bool>(
"zero-based indexing") ==
true)
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;