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->getLocalElementList();
439 const size_type myNumRows = myRows.size();
440 TEUCHOS_TEST_FOR_EXCEPTION(
static_cast<size_t>(myNumRows) !=
441 pRowMap->getLocalNumElements(),
443 "pRowMap->getLocalElementList().size() = "
445 <<
" != pRowMap->getLocalNumElements() = "
446 << pRowMap->getLocalNumElements() <<
". "
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->getLocalElementList().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 =
810 ArrayView<const GO> myRows = pRowMap->getLocalElementList ();
811 const size_type myNumRows = myRows.size ();
814 const GO indexBase = pRowMap->getIndexBase ();
815 for (size_type i = 0; i < myNumRows; ++i) {
816 const size_type myCurPos = myRowPtr[i];
818 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
819 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
822 for (size_type k = 0; k < curNumEntries; ++k) {
823 curColInd[k] += indexBase;
826 if (curNumEntries > 0) {
827 A->insertGlobalValues (myRows[i], curColInd, curValues);
834 myNumEntriesPerRow = null;
839 if (callFillComplete) {
840 A->fillComplete (pDomainMap, pRangeMap);
850 static Teuchos::RCP<sparse_matrix_type>
851 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
852 Teuchos::ArrayRCP<size_t>& myRowPtr,
853 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
854 Teuchos::ArrayRCP<scalar_type>& myValues,
855 const Teuchos::RCP<const map_type>& pRowMap,
856 const Teuchos::RCP<const map_type>& pRangeMap,
857 const Teuchos::RCP<const map_type>& pDomainMap,
858 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
859 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams)
861 using Teuchos::ArrayView;
875 TEUCHOS_TEST_FOR_EXCEPTION(
876 myRowPtr.is_null(), std::logic_error,
877 "makeMatrix: myRowPtr array is null. "
878 "Please report this bug to the Tpetra developers.");
879 TEUCHOS_TEST_FOR_EXCEPTION(
880 pDomainMap.is_null(), std::logic_error,
881 "makeMatrix: domain map is null. "
882 "Please report this bug to the Tpetra developers.");
883 TEUCHOS_TEST_FOR_EXCEPTION(
884 pRangeMap.is_null(), std::logic_error,
885 "makeMatrix: range map is null. "
886 "Please report this bug to the Tpetra developers.");
887 TEUCHOS_TEST_FOR_EXCEPTION(
888 pRowMap.is_null(), std::logic_error,
889 "makeMatrix: row map is null. "
890 "Please report this bug to the Tpetra developers.");
894 RCP<sparse_matrix_type> A =
900 ArrayView<const GO> myRows = pRowMap->getLocalElementList();
901 const size_type myNumRows = myRows.size();
904 const GO indexBase = pRowMap->getIndexBase ();
905 for (size_type i = 0; i < myNumRows; ++i) {
906 const size_type myCurPos = myRowPtr[i];
908 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
909 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
912 for (size_type k = 0; k < curNumEntries; ++k) {
913 curColInd[k] += indexBase;
915 if (curNumEntries > 0) {
916 A->insertGlobalValues (myRows[i], curColInd, curValues);
923 myNumEntriesPerRow = null;
928 A->fillComplete (pDomainMap, pRangeMap, fillCompleteParams);
936 static Teuchos::RCP<sparse_matrix_type>
937 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
938 Teuchos::ArrayRCP<size_t>& myRowPtr,
939 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
940 Teuchos::ArrayRCP<scalar_type>& myValues,
941 const Teuchos::RCP<const map_type>& rowMap,
942 Teuchos::RCP<const map_type>& colMap,
943 const Teuchos::RCP<const map_type>& domainMap,
944 const Teuchos::RCP<const map_type>& rangeMap,
945 const bool callFillComplete =
true)
947 using Teuchos::ArrayView;
956 RCP<sparse_matrix_type> A;
957 if (colMap.is_null ()) {
965 ArrayView<const GO> myRows = rowMap->getLocalElementList ();
966 const size_type myNumRows = myRows.size ();
969 const GO indexBase = rowMap->getIndexBase ();
970 for (size_type i = 0; i < myNumRows; ++i) {
971 const size_type myCurPos = myRowPtr[i];
972 const size_type curNumEntries = as<size_type> (myNumEntriesPerRow[i]);
973 ArrayView<GO> curColInd = myColInd.view (myCurPos, curNumEntries);
974 ArrayView<scalar_type> curValues = myValues.view (myCurPos, curNumEntries);
977 for (size_type k = 0; k < curNumEntries; ++k) {
978 curColInd[k] += indexBase;
980 if (curNumEntries > 0) {
981 A->insertGlobalValues (myRows[i], curColInd, curValues);
988 myNumEntriesPerRow = null;
993 if (callFillComplete) {
994 A->fillComplete (domainMap, rangeMap);
995 if (colMap.is_null ()) {
996 colMap = A->getColMap ();
1020 static Teuchos::RCP<const Teuchos::MatrixMarket::Banner>
1021 readBanner (std::istream& in,
1023 const bool tolerant=
false,
1025 const bool isGraph=
false)
1027 using Teuchos::MatrixMarket::Banner;
1032 typedef Teuchos::ScalarTraits<scalar_type> STS;
1034 RCP<Banner> pBanner;
1038 const bool readFailed = ! getline(in, line);
1039 TEUCHOS_TEST_FOR_EXCEPTION(readFailed, std::invalid_argument,
1040 "Failed to get Matrix Market banner line from input.");
1047 pBanner = rcp (
new Banner (line, tolerant));
1048 }
catch (std::exception& e) {
1050 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1051 "Matrix Market banner line contains syntax error(s): "
1055 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->objectType() !=
"matrix",
1056 std::invalid_argument,
"The Matrix Market file does not contain "
1057 "matrix data. Its Banner (first) line says that its object type is \""
1058 << pBanner->matrixType() <<
"\", rather than the required \"matrix\".");
1062 TEUCHOS_TEST_FOR_EXCEPTION(
1063 ! STS::isComplex && pBanner->dataType() ==
"complex",
1064 std::invalid_argument,
1065 "The Matrix Market file contains complex-valued data, but you are "
1066 "trying to read it into a matrix containing entries of the real-"
1067 "valued Scalar type \""
1068 << Teuchos::TypeNameTraits<scalar_type>::name() <<
"\".");
1069 TEUCHOS_TEST_FOR_EXCEPTION(
1071 pBanner->dataType() !=
"real" &&
1072 pBanner->dataType() !=
"complex" &&
1073 pBanner->dataType() !=
"integer",
1074 std::invalid_argument,
1075 "When reading Matrix Market data into a Tpetra::CrsMatrix, the "
1076 "Matrix Market file may not contain a \"pattern\" matrix. A "
1077 "pattern matrix is really just a graph with no weights. It "
1078 "should be stored in a CrsGraph, not a CrsMatrix.");
1080 TEUCHOS_TEST_FOR_EXCEPTION(
1082 pBanner->dataType() !=
"pattern",
1083 std::invalid_argument,
1084 "When reading Matrix Market data into a Tpetra::CrsGraph, the "
1085 "Matrix Market file must contain a \"pattern\" matrix.");
1112 static Teuchos::Tuple<global_ordinal_type, 3>
1113 readCoordDims (std::istream& in,
1115 const Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1116 const Teuchos::RCP<const comm_type>& pComm,
1117 const bool tolerant =
false,
1120 using Teuchos::MatrixMarket::readCoordinateDimensions;
1121 using Teuchos::Tuple;
1126 Tuple<global_ordinal_type, 3> dims;
1132 bool success =
false;
1133 if (pComm->getRank() == 0) {
1134 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->matrixType() !=
"coordinate",
1135 std::invalid_argument,
"The Tpetra::CrsMatrix Matrix Market reader "
1136 "only accepts \"coordinate\" (sparse) matrix data.");
1140 success = readCoordinateDimensions (in, numRows, numCols,
1141 numNonzeros, lineNumber,
1147 dims[2] = numNonzeros;
1155 int the_success = success ? 1 : 0;
1156 Teuchos::broadcast (*pComm, 0, &the_success);
1157 success = (the_success == 1);
1162 Teuchos::broadcast (*pComm, 0, dims);
1170 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1171 "Error reading Matrix Market sparse matrix: failed to read "
1172 "coordinate matrix dimensions.");
1187 typedef Teuchos::MatrixMarket::SymmetrizingAdder<Teuchos::MatrixMarket::Raw::Adder<scalar_type, global_ordinal_type> > adder_type;
1189 typedef Teuchos::MatrixMarket::SymmetrizingGraphAdder<Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> > graph_adder_type;
1216 static Teuchos::RCP<adder_type>
1217 makeAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1218 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1219 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1220 const bool tolerant=
false,
1221 const bool debug=
false)
1223 if (pComm->getRank () == 0) {
1224 typedef Teuchos::MatrixMarket::Raw::Adder<
scalar_type,
1227 Teuchos::RCP<raw_adder_type> pRaw =
1228 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1230 return Teuchos::rcp (
new adder_type (pRaw, pBanner->symmType ()));
1233 return Teuchos::null;
1262 static Teuchos::RCP<graph_adder_type>
1263 makeGraphAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1264 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1265 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1266 const bool tolerant=
false,
1267 const bool debug=
false)
1269 if (pComm->getRank () == 0) {
1270 typedef Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> raw_adder_type;
1271 Teuchos::RCP<raw_adder_type> pRaw =
1272 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1274 return Teuchos::rcp (
new graph_adder_type (pRaw, pBanner->symmType ()));
1277 return Teuchos::null;
1282 static Teuchos::RCP<sparse_graph_type>
1283 readSparseGraphHelper (std::istream& in,
1284 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1285 const Teuchos::RCP<const map_type>& rowMap,
1286 Teuchos::RCP<const map_type>& colMap,
1287 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1288 const bool tolerant,
1291 using Teuchos::MatrixMarket::Banner;
1294 using Teuchos::Tuple;
1298 const int myRank = pComm->getRank ();
1299 const int rootRank = 0;
1304 size_t lineNumber = 1;
1306 if (debug && myRank == rootRank) {
1307 cerr <<
"Matrix Market reader: readGraph:" << endl
1308 <<
"-- Reading banner line" << endl;
1317 RCP<const Banner> pBanner;
1323 int bannerIsCorrect = 1;
1324 std::ostringstream errMsg;
1326 if (myRank == rootRank) {
1329 pBanner = readBanner (in, lineNumber, tolerant, debug,
true);
1331 catch (std::exception& e) {
1332 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
1333 "threw an exception: " << e.what();
1334 bannerIsCorrect = 0;
1337 if (bannerIsCorrect) {
1342 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
1343 bannerIsCorrect = 0;
1344 errMsg <<
"The Matrix Market input file must contain a "
1345 "\"coordinate\"-format sparse graph in order to create a "
1346 "Tpetra::CrsGraph object from it, but the file's matrix "
1347 "type is \"" << pBanner->matrixType() <<
"\" instead.";
1352 if (tolerant && pBanner->matrixType() ==
"array") {
1353 bannerIsCorrect = 0;
1354 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
1355 "format sparse graph in order to create a Tpetra::CrsGraph "
1356 "object from it, but the file's matrix type is \"array\" "
1357 "instead. That probably means the file contains dense matrix "
1364 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
1371 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
1372 std::invalid_argument, errMsg.str ());
1374 if (debug && myRank == rootRank) {
1375 cerr <<
"-- Reading dimensions line" << endl;
1383 Tuple<global_ordinal_type, 3> dims =
1384 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
1386 if (debug && myRank == rootRank) {
1387 cerr <<
"-- Making Adder for collecting graph data" << endl;
1394 RCP<graph_adder_type> pAdder =
1395 makeGraphAdder (pComm, pBanner, dims, tolerant, debug);
1397 if (debug && myRank == rootRank) {
1398 cerr <<
"-- Reading graph data" << endl;
1408 int readSuccess = 1;
1409 std::ostringstream errMsg;
1410 if (myRank == rootRank) {
1413 typedef Teuchos::MatrixMarket::CoordPatternReader<graph_adder_type,
1415 reader_type reader (pAdder);
1418 std::pair<bool, std::vector<size_t> > results =
1419 reader.read (in, lineNumber, tolerant, debug);
1420 readSuccess = results.first ? 1 : 0;
1422 catch (std::exception& e) {
1427 broadcast (*pComm, rootRank, ptr (&readSuccess));
1436 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
1437 "Failed to read the Matrix Market sparse graph file: "
1441 if (debug && myRank == rootRank) {
1442 cerr <<
"-- Successfully read the Matrix Market data" << endl;
1455 if (debug && myRank == rootRank) {
1456 cerr <<
"-- Tolerant mode: rebroadcasting graph dimensions"
1458 <<
"----- Dimensions before: "
1459 << dims[0] <<
" x " << dims[1]
1463 Tuple<global_ordinal_type, 2> updatedDims;
1464 if (myRank == rootRank) {
1471 std::max (dims[0], pAdder->getAdder()->numRows());
1472 updatedDims[1] = pAdder->getAdder()->numCols();
1474 broadcast (*pComm, rootRank, updatedDims);
1475 dims[0] = updatedDims[0];
1476 dims[1] = updatedDims[1];
1477 if (debug && myRank == rootRank) {
1478 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
1492 if (myRank == rootRank) {
1499 if (dims[0] < pAdder->getAdder ()->numRows ()) {
1503 broadcast (*pComm, 0, ptr (&dimsMatch));
1504 if (dimsMatch == 0) {
1511 Tuple<global_ordinal_type, 2> addersDims;
1512 if (myRank == rootRank) {
1513 addersDims[0] = pAdder->getAdder()->numRows();
1514 addersDims[1] = pAdder->getAdder()->numCols();
1516 broadcast (*pComm, 0, addersDims);
1517 TEUCHOS_TEST_FOR_EXCEPTION(
1518 dimsMatch == 0, std::runtime_error,
1519 "The graph metadata says that the graph is " << dims[0] <<
" x "
1520 << dims[1] <<
", but the actual data says that the graph is "
1521 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
1522 "data includes more rows than reported in the metadata. This "
1523 "is not allowed when parsing in strict mode. Parse the graph in "
1524 "tolerant mode to ignore the metadata when it disagrees with the "
1530 RCP<map_type> proc0Map;
1532 if(Teuchos::is_null(rowMap)) {
1536 indexBase = rowMap->getIndexBase();
1538 if(myRank == rootRank) {
1539 proc0Map = rcp(
new map_type(dims[0],dims[0],indexBase,pComm));
1542 proc0Map = rcp(
new map_type(dims[0],0,indexBase,pComm));
1546 std::map<global_ordinal_type, size_t> numEntriesPerRow_map;
1547 if (myRank == rootRank) {
1548 const auto& entries = pAdder()->getAdder()->getEntries();
1553 for (
const auto& entry : entries) {
1555 ++numEntriesPerRow_map[gblRow];
1559 Teuchos::Array<size_t> numEntriesPerRow (proc0Map->getLocalNumElements ());
1560 for (
const auto& ent : numEntriesPerRow_map) {
1562 numEntriesPerRow[lclRow] = ent.second;
1569 std::map<global_ordinal_type, size_t> empty_map;
1570 std::swap (numEntriesPerRow_map, empty_map);
1573 RCP<sparse_graph_type> proc0Graph =
1575 constructorParams));
1576 if(myRank == rootRank) {
1577 typedef Teuchos::MatrixMarket::Raw::GraphElement<global_ordinal_type> element_type;
1580 const std::vector<element_type>& entries =
1581 pAdder->getAdder()->getEntries();
1584 for(
size_t curPos=0; curPos<entries.size(); curPos++) {
1585 const element_type& curEntry = entries[curPos];
1588 Teuchos::ArrayView<const global_ordinal_type> colView(&curCol,1);
1589 proc0Graph->insertGlobalIndices(curRow,colView);
1592 proc0Graph->fillComplete();
1594 RCP<sparse_graph_type> distGraph;
1595 if(Teuchos::is_null(rowMap))
1598 RCP<map_type> distMap =
1599 rcp(
new map_type(dims[0],0,pComm,GloballyDistributed));
1605 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1606 import_type importer (proc0Map, distMap);
1609 distGraph->doImport(*proc0Graph,importer,
INSERT);
1615 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1616 import_type importer (proc0Map, rowMap);
1619 distGraph->doImport(*proc0Graph,importer,
INSERT);
1649 static Teuchos::RCP<sparse_graph_type>
1651 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1654 const bool debug=
false)
1656 using Teuchos::broadcast;
1657 using Teuchos::outArg;
1665 if (comm->getRank () == 0) {
1676 (
opened == 0, std::runtime_error,
"readSparseGraphFile: "
1677 "Failed to open file \"" <<
filename <<
"\" on Process 0.");
1715 static Teuchos::RCP<sparse_graph_type>
1717 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
1721 const bool debug=
false)
1723 using Teuchos::broadcast;
1724 using Teuchos::outArg;
1732 if (
pComm->getRank () == 0) {
1743 (
opened == 0, std::runtime_error,
"readSparseGraphFile: "
1744 "Failed to open file \"" <<
filename <<
"\" on Process 0.");
1745 if (
pComm->getRank () == 0) {
1794 static Teuchos::RCP<sparse_graph_type>
1796 const Teuchos::RCP<const map_type>& rowMap,
1797 Teuchos::RCP<const map_type>& colMap,
1798 const Teuchos::RCP<const map_type>& domainMap,
1799 const Teuchos::RCP<const map_type>&
rangeMap,
1802 const bool debug=
false)
1804 using Teuchos::broadcast;
1805 using Teuchos::Comm;
1806 using Teuchos::outArg;
1810 (rowMap.is_null (), std::invalid_argument,
1811 "Input rowMap must be nonnull.");
1813 if (comm.is_null ()) {
1816 return Teuchos::null;
1825 if (comm->getRank () == 0) {
1836 (
opened == 0, std::runtime_error,
"readSparseGraphFile: "
1837 "Failed to open file \"" <<
filename <<
"\" on Process 0.");
1867 static Teuchos::RCP<sparse_graph_type>
1869 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
1872 const bool debug=
false)
1878 Teuchos::RCP<sparse_graph_type>
graph =
1879 readSparseGraphHelper (
in,
pComm,
1883 graph->fillComplete ();
1918 static Teuchos::RCP<sparse_graph_type>
1920 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
1924 const bool debug=
false)
1928 Teuchos::RCP<sparse_graph_type>
graph =
1929 readSparseGraphHelper (
in,
pComm,
1977 static Teuchos::RCP<sparse_graph_type>
1979 const Teuchos::RCP<const map_type>& rowMap,
1980 Teuchos::RCP<const map_type>& colMap,
1981 const Teuchos::RCP<const map_type>& domainMap,
1982 const Teuchos::RCP<const map_type>&
rangeMap,
1985 const bool debug=
false)
1987 Teuchos::RCP<sparse_graph_type>
graph =
1988 readSparseGraphHelper (
in, rowMap->getComm (),
1989 rowMap, colMap, Teuchos::null,
tolerant,
1997#include "MatrixMarket_TpetraNew.hpp"
2022 static Teuchos::RCP<sparse_matrix_type>
2024 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2027 const bool debug=
false)
2077 static Teuchos::RCP<sparse_matrix_type>
2079 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2083 const bool debug=
false)
2086 if (
pComm->getRank () == 0) {
2131 static Teuchos::RCP<sparse_matrix_type>
2133 const Teuchos::RCP<const map_type>& rowMap,
2134 Teuchos::RCP<const map_type>& colMap,
2135 const Teuchos::RCP<const map_type>& domainMap,
2136 const Teuchos::RCP<const map_type>&
rangeMap,
2139 const bool debug=
false)
2141 using Teuchos::broadcast;
2142 using Teuchos::Comm;
2143 using Teuchos::outArg;
2147 rowMap.is_null (), std::invalid_argument,
2148 "Row Map must be nonnull.");
2151 const int myRank = comm->getRank ();
2170 opened == 0, std::runtime_error,
2171 "readSparseFile: Failed to open file \"" <<
filename <<
"\" on "
2202 static Teuchos::RCP<sparse_matrix_type>
2204 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2207 const bool debug=
false)
2209 using Teuchos::MatrixMarket::Banner;
2210 using Teuchos::arcp;
2211 using Teuchos::ArrayRCP;
2212 using Teuchos::broadcast;
2213 using Teuchos::null;
2216 using Teuchos::REDUCE_MAX;
2217 using Teuchos::reduceAll;
2218 using Teuchos::Tuple;
2221 typedef Teuchos::ScalarTraits<scalar_type> STS;
2233 cerr <<
"Matrix Market reader: readSparse:" <<
endl
2234 <<
"-- Reading banner line" <<
endl;
2250 std::ostringstream
errMsg;
2257 catch (std::exception&
e) {
2258 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2259 "threw an exception: " <<
e.what();
2270 errMsg <<
"The Matrix Market input file must contain a "
2271 "\"coordinate\"-format sparse matrix in order to create a "
2272 "Tpetra::CrsMatrix object from it, but the file's matrix "
2273 "type is \"" <<
pBanner->matrixType() <<
"\" instead.";
2280 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2281 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2282 "object from it, but the file's matrix type is \"array\" "
2283 "instead. That probably means the file contains dense matrix "
2298 std::invalid_argument,
errMsg.str ());
2301 cerr <<
"-- Reading dimensions line" <<
endl;
2313 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
2322 cerr <<
"-- Reading matrix data" <<
endl;
2333 std::ostringstream
errMsg;
2337 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2342 std::pair<bool, std::vector<size_t> >
results =
2346 catch (std::exception&
e) {
2361 "Failed to read the Matrix Market sparse matrix file: "
2366 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
2380 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2382 <<
"----- Dimensions before: "
2395 std::max (
dims[0],
pAdder->getAdder()->numRows());
2402 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
2423 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
2443 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
2444 <<
dims[1] <<
", but the actual data says that the matrix is "
2446 "data includes more rows than reported in the metadata. This "
2447 "is not allowed when parsing in strict mode. Parse the matrix in "
2448 "tolerant mode to ignore the metadata when it disagrees with the "
2454 cerr <<
"-- Converting matrix data into CSR format on Proc 0" <<
endl;
2475 std::ostringstream
errMsg;
2479 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
2492 pAdder->getAdder()->merge ();
2495 const std::vector<element_type>& entries =
2496 pAdder->getAdder()->getEntries();
2499 const size_t numEntries = (
size_t)entries.size();
2502 cerr <<
"----- Proc 0: Matrix has numRows=" <<
numRows
2503 <<
" rows and numEntries=" << numEntries
2504 <<
" entries." <<
endl;
2527 "Row indices are out of order, even though they are supposed "
2528 "to be sorted. curRow = " <<
curRow <<
", prvRow = "
2529 <<
prvRow <<
", at curPos = " <<
curPos <<
". Please report "
2530 "this bug to the Tpetra developers.");
2546 catch (std::exception&
e) {
2548 errMsg <<
"Failed to merge sparse matrix entries and convert to "
2549 "CSR format: " <<
e.what();
2557 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
2560 cerr <<
"(only showing first and last few entries) ";
2565 for (size_type
k = 0;
k < 2; ++
k) {
2582 cerr <<
"----- Proc 0: rowPtr ";
2584 cerr <<
"(only showing first and last few entries) ";
2588 for (size_type
k = 0;
k < 2; ++
k) {
2613 cerr <<
"-- Making range, domain, and row maps" <<
endl;
2626 cerr <<
"-- Distributing the matrix data" <<
endl;
2649 cerr <<
"-- Inserting matrix entries on each processor";
2651 cerr <<
" and calling fillComplete()";
2673 "Reader::makeMatrix() returned a null pointer on at least one "
2674 "process. Please report this bug to the Tpetra developers.");
2678 "Reader::makeMatrix() returned a null pointer. "
2679 "Please report this bug to the Tpetra developers.");
2700 cerr <<
"-- Matrix is "
2702 <<
" with " <<
pMatrix->getGlobalNumEntries()
2703 <<
" entries, and index base "
2709 cerr <<
"-- Proc " <<
p <<
" owns "
2710 <<
pMatrix->getLocalNumCols() <<
" columns, and "
2711 <<
pMatrix->getLocalNumEntries() <<
" entries." <<
endl;
2719 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
2754 static Teuchos::RCP<sparse_matrix_type>
2756 const Teuchos::RCP<
const Teuchos::Comm<int> >&
pComm,
2760 const bool debug=
false)
2762 using Teuchos::MatrixMarket::Banner;
2763 using Teuchos::arcp;
2764 using Teuchos::ArrayRCP;
2765 using Teuchos::broadcast;
2766 using Teuchos::null;
2769 using Teuchos::reduceAll;
2770 using Teuchos::Tuple;
2773 typedef Teuchos::ScalarTraits<scalar_type> STS;
2785 cerr <<
"Matrix Market reader: readSparse:" <<
endl
2786 <<
"-- Reading banner line" <<
endl;
2802 std::ostringstream
errMsg;
2809 catch (std::exception&
e) {
2810 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2811 "threw an exception: " <<
e.what();
2822 errMsg <<
"The Matrix Market input file must contain a "
2823 "\"coordinate\"-format sparse matrix in order to create a "
2824 "Tpetra::CrsMatrix object from it, but the file's matrix "
2825 "type is \"" <<
pBanner->matrixType() <<
"\" instead.";
2832 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2833 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2834 "object from it, but the file's matrix type is \"array\" "
2835 "instead. That probably means the file contains dense matrix "
2850 std::invalid_argument,
errMsg.str ());
2853 cerr <<
"-- Reading dimensions line" <<
endl;
2865 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
2874 cerr <<
"-- Reading matrix data" <<
endl;
2885 std::ostringstream
errMsg;
2889 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2894 std::pair<bool, std::vector<size_t> >
results =
2898 catch (std::exception&
e) {
2913 "Failed to read the Matrix Market sparse matrix file: "
2918 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
2932 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2934 <<
"----- Dimensions before: "
2947 std::max (
dims[0],
pAdder->getAdder()->numRows());
2954 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
2975 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
2995 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
2996 <<
dims[1] <<
", but the actual data says that the matrix is "
2998 "data includes more rows than reported in the metadata. This "
2999 "is not allowed when parsing in strict mode. Parse the matrix in "
3000 "tolerant mode to ignore the metadata when it disagrees with the "
3006 cerr <<
"-- Converting matrix data into CSR format on Proc 0" <<
endl;
3027 std::ostringstream
errMsg;
3031 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
3044 pAdder->getAdder()->merge ();
3047 const std::vector<element_type>& entries =
3048 pAdder->getAdder()->getEntries();
3051 const size_t numEntries = (
size_t)entries.size();
3054 cerr <<
"----- Proc 0: Matrix has numRows=" <<
numRows
3055 <<
" rows and numEntries=" << numEntries
3056 <<
" entries." <<
endl;
3079 "Row indices are out of order, even though they are supposed "
3080 "to be sorted. curRow = " <<
curRow <<
", prvRow = "
3081 <<
prvRow <<
", at curPos = " <<
curPos <<
". Please report "
3082 "this bug to the Tpetra developers.");
3098 catch (std::exception&
e) {
3100 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3101 "CSR format: " <<
e.what();
3109 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3112 cerr <<
"(only showing first and last few entries) ";
3117 for (size_type
k = 0;
k < 2; ++
k) {
3134 cerr <<
"----- Proc 0: rowPtr ";
3136 cerr <<
"(only showing first and last few entries) ";
3140 for (size_type
k = 0;
k < 2; ++
k) {
3165 cerr <<
"-- Making range, domain, and row maps" <<
endl;
3178 cerr <<
"-- Distributing the matrix data" <<
endl;
3201 cerr <<
"-- Inserting matrix entries on each process "
3202 "and calling fillComplete()" <<
endl;
3211 Teuchos::RCP<sparse_matrix_type>
pMatrix =
3223 "Reader::makeMatrix() returned a null pointer on at least one "
3224 "process. Please report this bug to the Tpetra developers.");
3228 "Reader::makeMatrix() returned a null pointer. "
3229 "Please report this bug to the Tpetra developers.");
3245 cerr <<
"-- Matrix is "
3247 <<
" with " <<
pMatrix->getGlobalNumEntries()
3248 <<
" entries, and index base "
3254 cerr <<
"-- Proc " <<
p <<
" owns "
3255 <<
pMatrix->getLocalNumCols() <<
" columns, and "
3256 <<
pMatrix->getLocalNumEntries() <<
" entries." <<
endl;
3263 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
3310 static Teuchos::RCP<sparse_matrix_type>
3312 const Teuchos::RCP<const map_type>& rowMap,
3313 Teuchos::RCP<const map_type>& colMap,
3314 const Teuchos::RCP<const map_type>& domainMap,
3315 const Teuchos::RCP<const map_type>&
rangeMap,
3318 const bool debug=
false)
3320 using Teuchos::MatrixMarket::Banner;
3321 using Teuchos::arcp;
3322 using Teuchos::ArrayRCP;
3323 using Teuchos::ArrayView;
3325 using Teuchos::broadcast;
3326 using Teuchos::Comm;
3327 using Teuchos::null;
3330 using Teuchos::reduceAll;
3331 using Teuchos::Tuple;
3334 typedef Teuchos::ScalarTraits<scalar_type> STS;
3345 rowMap.is_null (), std::invalid_argument,
3346 "Row Map must be nonnull.");
3348 rangeMap.is_null (), std::invalid_argument,
3349 "Range Map must be nonnull.");
3351 domainMap.is_null (), std::invalid_argument,
3352 "Domain Map must be nonnull.");
3354 rowMap->getComm().getRawPtr() !=
pComm.getRawPtr(),
3355 std::invalid_argument,
3356 "The specified row Map's communicator (rowMap->getComm())"
3357 "differs from the given separately supplied communicator pComm.");
3359 domainMap->getComm().getRawPtr() !=
pComm.getRawPtr(),
3360 std::invalid_argument,
3361 "The specified domain Map's communicator (domainMap->getComm())"
3362 "differs from the given separately supplied communicator pComm.");
3365 std::invalid_argument,
3366 "The specified range Map's communicator (rangeMap->getComm())"
3367 "differs from the given separately supplied communicator pComm.");
3375 cerr <<
"Matrix Market reader: readSparse:" <<
endl
3376 <<
"-- Reading banner line" <<
endl;
3392 std::ostringstream
errMsg;
3399 catch (std::exception&
e) {
3400 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
3401 "threw an exception: " <<
e.what();
3412 errMsg <<
"The Matrix Market input file must contain a "
3413 "\"coordinate\"-format sparse matrix in order to create a "
3414 "Tpetra::CrsMatrix object from it, but the file's matrix "
3415 "type is \"" <<
pBanner->matrixType() <<
"\" instead.";
3422 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
3423 "format sparse matrix in order to create a Tpetra::CrsMatrix "
3424 "object from it, but the file's matrix type is \"array\" "
3425 "instead. That probably means the file contains dense matrix "
3440 std::invalid_argument,
errMsg.str ());
3443 cerr <<
"-- Reading dimensions line" <<
endl;
3455 cerr <<
"-- Making Adder for collecting matrix data" <<
endl;
3466 cerr <<
"-- Reading matrix data" <<
endl;
3477 std::ostringstream
errMsg;
3481 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
3486 std::pair<bool, std::vector<size_t> >
results =
3490 catch (std::exception&
e) {
3505 "Failed to read the Matrix Market sparse matrix file: "
3510 cerr <<
"-- Successfully read the Matrix Market data" <<
endl;
3524 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
3526 <<
"----- Dimensions before: "
3539 std::max (
dims[0],
pAdder->getAdder()->numRows());
3546 cerr <<
"----- Dimensions after: " <<
dims[0] <<
" x "
3567 if (
dims[0] <
pAdder->getAdder ()->numRows ()) {
3587 "The matrix metadata says that the matrix is " <<
dims[0] <<
" x "
3588 <<
dims[1] <<
", but the actual data says that the matrix is "
3590 "data includes more rows than reported in the metadata. This "
3591 "is not allowed when parsing in strict mode. Parse the matrix in "
3592 "tolerant mode to ignore the metadata when it disagrees with the "
3598 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.");
3688 catch (std::exception&
e) {
3690 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3691 "CSR format: " <<
e.what();
3699 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3702 cerr <<
"(only showing first and last few entries) ";
3707 for (size_type
k = 0;
k < 2; ++
k) {
3724 cerr <<
"----- Proc 0: rowPtr ";
3726 cerr <<
"(only showing first and last few entries) ";
3730 for (size_type
k = 0;
k < 2; ++
k) {
3745 cerr <<
"----- Proc 0: colInd = [";
3764 cerr <<
"-- Verifying Maps" <<
endl;
3768 std::invalid_argument,
3769 "The range Map has " <<
rangeMap->getGlobalNumElements ()
3770 <<
" entries, but the matrix has a global number of rows " <<
dims[0]
3774 std::invalid_argument,
3775 "The domain Map has " << domainMap->getGlobalNumElements ()
3776 <<
" entries, but the matrix has a global number of columns "
3781 Teuchos::getFancyOStream (Teuchos::rcpFromRef (
cerr)) :
null;
3801 cerr <<
"-- Proc 0: Filling gather matrix" <<
endl;
3861 if (colMap.is_null ()) {
3887 cerr <<
"-- Matrix is "
3889 <<
" with " <<
A->getGlobalNumEntries()
3890 <<
" entries, and index base "
3891 <<
A->getIndexBase() <<
"." <<
endl;
3896 cerr <<
"-- Proc " <<
p <<
" owns "
3897 <<
A->getLocalNumCols() <<
" columns, and "
3898 <<
A->getLocalNumEntries() <<
" entries." <<
endl;
3906 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
3941 static Teuchos::RCP<multivector_type>
3943 const Teuchos::RCP<const comm_type>& comm,
3944 Teuchos::RCP<const map_type>&
map,
3946 const bool debug=
false,
3949 using Teuchos::broadcast;
3950 using Teuchos::outArg;
3954 if (comm->getRank() == 0) {
3959 in.open (
filename.c_str (), std::ios::binary);
3968 opened == 0, std::runtime_error,
3969 "readDenseFile: Failed to open file \"" <<
filename <<
"\" on "
4004 static Teuchos::RCP<vector_type>
4006 const Teuchos::RCP<const comm_type>& comm,
4007 Teuchos::RCP<const map_type>&
map,
4009 const bool debug=
false)
4011 using Teuchos::broadcast;
4012 using Teuchos::outArg;
4016 if (comm->getRank() == 0) {
4027 opened == 0, std::runtime_error,
4028 "readVectorFile: Failed to open file \"" <<
filename <<
"\" on "
4102 static Teuchos::RCP<multivector_type>
4104 const Teuchos::RCP<const comm_type>& comm,
4105 Teuchos::RCP<const map_type>&
map,
4107 const bool debug=
false,
4110 Teuchos::RCP<Teuchos::FancyOStream>
err =
4111 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4117 static Teuchos::RCP<vector_type>
4119 const Teuchos::RCP<const comm_type>& comm,
4120 Teuchos::RCP<const map_type>&
map,
4122 const bool debug=
false)
4124 Teuchos::RCP<Teuchos::FancyOStream>
err =
4125 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4151 static Teuchos::RCP<const map_type>
4153 const Teuchos::RCP<const comm_type>& comm,
4155 const bool debug=
false,
4158 using Teuchos::inOutArg;
4159 using Teuchos::broadcast;
4163 if (comm->getRank () == 0) {
4165 in.open (
filename.c_str (), std::ios::binary);
4174 success == 0, std::runtime_error,
4175 "Tpetra::MatrixMarket::Reader::readMapFile: "
4176 "Failed to read file \"" <<
filename <<
"\" on Process 0.");
4182 template<
class MultiVectorScalarType>
4187 readDenseImpl (std::istream&
in,
4188 const Teuchos::RCP<const comm_type>& comm,
4189 Teuchos::RCP<const map_type>&
map,
4190 const Teuchos::RCP<Teuchos::FancyOStream>&
err,
4192 const bool debug=
false,
4195 using Teuchos::MatrixMarket::Banner;
4196 using Teuchos::MatrixMarket::checkCommentLine;
4197 using Teuchos::ArrayRCP;
4199 using Teuchos::broadcast;
4200 using Teuchos::outArg;
4202 using Teuchos::Tuple;
4208 typedef Teuchos::ScalarTraits<ST> STS;
4209 typedef typename STS::magnitudeType
MT;
4210 typedef Teuchos::ScalarTraits<MT> STM;
4215 const int myRank = comm->getRank ();
4217 if (!
err.is_null ()) {
4223 if (! err.is_null ()) {
4258 size_t lineNumber = 1;
4261 std::ostringstream exMsg;
4262 int localBannerReadSuccess = 1;
4263 int localDimsReadSuccess = 1;
4269 *err << myRank <<
": readDenseImpl: Reading banner line (dense)" << endl;
4275 RCP<const Banner> pBanner;
4277 pBanner = readBanner (in, lineNumber, tolerant, debug);
4278 }
catch (std::exception& e) {
4280 localBannerReadSuccess = 0;
4283 if (localBannerReadSuccess) {
4284 if (pBanner->matrixType () !=
"array") {
4285 exMsg <<
"The Matrix Market file does not contain dense matrix "
4286 "data. Its banner (first) line says that its matrix type is \""
4287 << pBanner->matrixType () <<
"\", rather that the required "
4289 localBannerReadSuccess = 0;
4290 }
else if (pBanner->dataType() ==
"pattern") {
4291 exMsg <<
"The Matrix Market file's banner (first) "
4292 "line claims that the matrix's data type is \"pattern\". This does "
4293 "not make sense for a dense matrix, yet the file reports the matrix "
4294 "as dense. The only valid data types for a dense matrix are "
4295 "\"real\", \"complex\", and \"integer\".";
4296 localBannerReadSuccess = 0;
4300 dims[2] = encodeDataType (pBanner->dataType ());
4306 if (localBannerReadSuccess) {
4308 *err << myRank <<
": readDenseImpl: Reading dimensions line (dense)" << endl;
4317 bool commentLine =
true;
4319 while (commentLine) {
4322 if (in.eof () || in.fail ()) {
4323 exMsg <<
"Unable to get array dimensions line (at all) from line "
4324 << lineNumber <<
" of input stream. The input stream "
4325 <<
"claims that it is "
4326 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4327 localDimsReadSuccess = 0;
4330 if (getline (in, line)) {
4337 size_t start = 0, size = 0;
4338 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4345 std::istringstream istr (line);
4349 if (istr.eof () || istr.fail ()) {
4350 exMsg <<
"Unable to read any data from line " << lineNumber
4351 <<
" of input; the line should contain the matrix dimensions "
4352 <<
"\"<numRows> <numCols>\".";
4353 localDimsReadSuccess = 0;
4358 exMsg <<
"Failed to get number of rows from line "
4359 << lineNumber <<
" of input; the line should contains the "
4360 <<
"matrix dimensions \"<numRows> <numCols>\".";
4361 localDimsReadSuccess = 0;
4363 dims[0] = theNumRows;
4365 exMsg <<
"No more data after number of rows on line "
4366 << lineNumber <<
" of input; the line should contain the "
4367 <<
"matrix dimensions \"<numRows> <numCols>\".";
4368 localDimsReadSuccess = 0;
4373 exMsg <<
"Failed to get number of columns from line "
4374 << lineNumber <<
" of input; the line should contain "
4375 <<
"the matrix dimensions \"<numRows> <numCols>\".";
4376 localDimsReadSuccess = 0;
4378 dims[1] = theNumCols;
4387 in.read(
reinterpret_cast<char*
>(&numRows),
sizeof(numRows));
4388 in.read(
reinterpret_cast<char*
>(&numCols),
sizeof(numCols));
4389 dims[0] = Teuchos::as<GO>(numRows);
4390 dims[1] = Teuchos::as<GO>(numCols);
4391 if ((
typeid(ST) ==
typeid(
double)) || Teuchos::ScalarTraits<ST>::isOrdinal) {
4393 }
else if (Teuchos::ScalarTraits<ST>::isComplex) {
4396 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
4397 "Unrecognized Matrix Market data type. "
4398 "We should never get here. "
4399 "Please report this bug to the Tpetra developers.");
4401 localDimsReadSuccess =
true;
4402 localDimsReadSuccess =
true;
4409 Tuple<GO, 5> bannerDimsReadResult;
4411 bannerDimsReadResult[0] = dims[0];
4412 bannerDimsReadResult[1] = dims[1];
4413 bannerDimsReadResult[2] = dims[2];
4414 bannerDimsReadResult[3] = localBannerReadSuccess;
4415 bannerDimsReadResult[4] = localDimsReadSuccess;
4419 broadcast (*comm, 0, bannerDimsReadResult);
4421 TEUCHOS_TEST_FOR_EXCEPTION(
4422 bannerDimsReadResult[3] == 0, std::runtime_error,
4423 "Failed to read banner line: " << exMsg.str ());
4424 TEUCHOS_TEST_FOR_EXCEPTION(
4425 bannerDimsReadResult[4] == 0, std::runtime_error,
4426 "Failed to read matrix dimensions line: " << exMsg.str ());
4428 dims[0] = bannerDimsReadResult[0];
4429 dims[1] = bannerDimsReadResult[1];
4430 dims[2] = bannerDimsReadResult[2];
4435 const size_t numCols =
static_cast<size_t> (dims[1]);
4440 RCP<const map_type> proc0Map;
4441 if (map.is_null ()) {
4445 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
4446 const size_t localNumRows = (myRank == 0) ? numRows : 0;
4448 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
4452 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
4456 RCP<MV> X = createMultiVector<ST, LO, GO, NT> (proc0Map, numCols);
4462 int localReadDataSuccess = 1;
4467 *err << myRank <<
": readDenseImpl: Reading matrix data (dense)"
4472 TEUCHOS_TEST_FOR_EXCEPTION(
4473 ! X->isConstantStride (), std::logic_error,
4474 "Can't get a 1-D view of the entries of the MultiVector X on "
4475 "Process 0, because the stride between the columns of X is not "
4476 "constant. This shouldn't happen because we just created X and "
4477 "haven't filled it in yet. Please report this bug to the Tpetra "
4484 ArrayRCP<ST> X_view = X->get1dViewNonConst ();
4485 TEUCHOS_TEST_FOR_EXCEPTION(
4486 as<global_size_t> (X_view.size ()) < numRows * numCols,
4488 "The view of X has size " << X_view.size() <<
" which is not enough to "
4489 "accommodate the expected number of entries numRows*numCols = "
4490 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
4491 "Please report this bug to the Tpetra developers.");
4492 const size_t stride = X->getStride ();
4499 const bool isComplex = (dims[2] == 1);
4500 size_type count = 0, curRow = 0, curCol = 0;
4503 while (getline (in, line)) {
4507 size_t start = 0, size = 0;
4508 const bool commentLine =
4509 checkCommentLine (line, start, size, lineNumber, tolerant);
4510 if (! commentLine) {
4516 if (count >= X_view.size()) {
4521 TEUCHOS_TEST_FOR_EXCEPTION(
4522 count >= X_view.size(),
4524 "The Matrix Market input stream has more data in it than "
4525 "its metadata reported. Current line number is "
4526 << lineNumber <<
".");
4535 const size_t pos = line.substr (start, size).find (
':');
4536 if (pos != std::string::npos) {
4540 std::istringstream istr (line.substr (start, size));
4544 if (istr.eof() || istr.fail()) {
4551 TEUCHOS_TEST_FOR_EXCEPTION(
4552 ! tolerant && (istr.eof() || istr.fail()),
4554 "Line " << lineNumber <<
" of the Matrix Market file is "
4555 "empty, or we cannot read from it for some other reason.");
4558 ST val = STS::zero();
4561 MT real = STM::zero(), imag = STM::zero();
4578 TEUCHOS_TEST_FOR_EXCEPTION(
4579 ! tolerant && istr.eof(), std::runtime_error,
4580 "Failed to get the real part of a complex-valued matrix "
4581 "entry from line " << lineNumber <<
" of the Matrix Market "
4587 }
else if (istr.eof()) {
4588 TEUCHOS_TEST_FOR_EXCEPTION(
4589 ! tolerant && istr.eof(), std::runtime_error,
4590 "Missing imaginary part of a complex-valued matrix entry "
4591 "on line " << lineNumber <<
" of the Matrix Market file.");
4597 TEUCHOS_TEST_FOR_EXCEPTION(
4598 ! tolerant && istr.fail(), std::runtime_error,
4599 "Failed to get the imaginary part of a complex-valued "
4600 "matrix entry from line " << lineNumber <<
" of the "
4601 "Matrix Market file.");
4608 TEUCHOS_TEST_FOR_EXCEPTION(
4609 ! tolerant && istr.fail(), std::runtime_error,
4610 "Failed to get a real-valued matrix entry from line "
4611 << lineNumber <<
" of the Matrix Market file.");
4614 if (istr.fail() && tolerant) {
4620 TEUCHOS_TEST_FOR_EXCEPTION(
4621 ! tolerant && istr.fail(), std::runtime_error,
4622 "Failed to read matrix data from line " << lineNumber
4623 <<
" of the Matrix Market file.");
4626 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
4628 curRow = count % numRows;
4629 curCol = count / numRows;
4630 X_view[curRow + curCol*stride] = val;
4635 TEUCHOS_TEST_FOR_EXCEPTION(
4636 ! tolerant &&
static_cast<global_size_t> (count) < numRows * numCols,
4638 "The Matrix Market metadata reports that the dense matrix is "
4639 << numRows <<
" x " << numCols <<
", and thus has "
4640 << numRows*numCols <<
" total entries, but we only found " << count
4641 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
4642 }
catch (std::exception& e) {
4644 localReadDataSuccess = 0;
4649 *err << myRank <<
": readDenseImpl: Reading matrix data (dense)"
4654 TEUCHOS_TEST_FOR_EXCEPTION(
4655 ! X->isConstantStride (), std::logic_error,
4656 "Can't get a 1-D view of the entries of the MultiVector X on "
4657 "Process 0, because the stride between the columns of X is not "
4658 "constant. This shouldn't happen because we just created X and "
4659 "haven't filled it in yet. Please report this bug to the Tpetra "
4666 auto X_view = X->getLocalViewHost (Access::OverwriteAll);
4668 TEUCHOS_TEST_FOR_EXCEPTION(
4669 as<global_size_t> (X_view.extent(0)) < numRows,
4671 "The view of X has " << X_view.extent(0) <<
" rows which is not enough to "
4672 "accommodate the expected number of entries numRows = "
4674 "Please report this bug to the Tpetra developers.");
4675 TEUCHOS_TEST_FOR_EXCEPTION(
4676 as<global_size_t> (X_view.extent(1)) < numCols,
4678 "The view of X has " << X_view.extent(1) <<
" colums which is not enough to "
4679 "accommodate the expected number of entries numRows = "
4681 "Please report this bug to the Tpetra developers.");
4683 for (
size_t curRow = 0; curRow < numRows; ++curRow) {
4684 for (
size_t curCol = 0; curCol < numCols; ++curCol) {
4685 if (Teuchos::ScalarTraits<ST>::isOrdinal){
4687 in.read(
reinterpret_cast<char*
>(&val),
sizeof(val));
4688 X_view(curRow, curCol) = val;
4691 in.read(
reinterpret_cast<char*
>(&val),
sizeof(val));
4692 X_view(curRow, curCol) = val;
4700 *err << myRank <<
": readDenseImpl: done reading data" << endl;
4704 int globalReadDataSuccess = localReadDataSuccess;
4705 broadcast (*comm, 0, outArg (globalReadDataSuccess));
4706 TEUCHOS_TEST_FOR_EXCEPTION(
4707 globalReadDataSuccess == 0, std::runtime_error,
4708 "Failed to read the multivector's data: " << exMsg.str ());
4713 if (comm->getSize () == 1 && map.is_null ()) {
4715 if (! err.is_null ()) {
4719 *err << myRank <<
": readDenseImpl: done" << endl;
4721 if (! err.is_null ()) {
4728 *err << myRank <<
": readDenseImpl: Creating target MV" << endl;
4732 RCP<MV> Y = createMultiVector<ST, LO, GO, NT> (map, numCols);
4735 *err << myRank <<
": readDenseImpl: Creating Export" << endl;
4741 Export<LO, GO, NT> exporter (proc0Map, map, err);
4744 *err << myRank <<
": readDenseImpl: Exporting" << endl;
4747 Y->doExport (*X, exporter,
INSERT);
4749 if (! err.is_null ()) {
4753 *err << myRank <<
": readDenseImpl: done" << endl;
4755 if (! err.is_null ()) {
4764 template<
class VectorScalarType>
4769 readVectorImpl (std::istream& in,
4770 const Teuchos::RCP<const comm_type>& comm,
4771 Teuchos::RCP<const map_type>& map,
4772 const Teuchos::RCP<Teuchos::FancyOStream>& err,
4773 const bool tolerant=
false,
4774 const bool debug=
false)
4776 using Teuchos::MatrixMarket::Banner;
4777 using Teuchos::MatrixMarket::checkCommentLine;
4779 using Teuchos::broadcast;
4780 using Teuchos::outArg;
4782 using Teuchos::Tuple;
4784 typedef VectorScalarType ST;
4788 typedef Teuchos::ScalarTraits<ST> STS;
4789 typedef typename STS::magnitudeType MT;
4790 typedef Teuchos::ScalarTraits<MT> STM;
4795 const int myRank = comm->getRank ();
4797 if (! err.is_null ()) {
4801 *err << myRank <<
": readVectorImpl" << endl;
4803 if (! err.is_null ()) {
4837 size_t lineNumber = 1;
4840 std::ostringstream exMsg;
4841 int localBannerReadSuccess = 1;
4842 int localDimsReadSuccess = 1;
4847 *err << myRank <<
": readVectorImpl: Reading banner line (dense)" << endl;
4853 RCP<const Banner> pBanner;
4855 pBanner = readBanner (in, lineNumber, tolerant, debug);
4856 }
catch (std::exception& e) {
4858 localBannerReadSuccess = 0;
4861 if (localBannerReadSuccess) {
4862 if (pBanner->matrixType () !=
"array") {
4863 exMsg <<
"The Matrix Market file does not contain dense matrix "
4864 "data. Its banner (first) line says that its matrix type is \""
4865 << pBanner->matrixType () <<
"\", rather that the required "
4867 localBannerReadSuccess = 0;
4868 }
else if (pBanner->dataType() ==
"pattern") {
4869 exMsg <<
"The Matrix Market file's banner (first) "
4870 "line claims that the matrix's data type is \"pattern\". This does "
4871 "not make sense for a dense matrix, yet the file reports the matrix "
4872 "as dense. The only valid data types for a dense matrix are "
4873 "\"real\", \"complex\", and \"integer\".";
4874 localBannerReadSuccess = 0;
4878 dims[2] = encodeDataType (pBanner->dataType ());
4884 if (localBannerReadSuccess) {
4886 *err << myRank <<
": readVectorImpl: Reading dimensions line (dense)" << endl;
4895 bool commentLine =
true;
4897 while (commentLine) {
4900 if (in.eof () || in.fail ()) {
4901 exMsg <<
"Unable to get array dimensions line (at all) from line "
4902 << lineNumber <<
" of input stream. The input stream "
4903 <<
"claims that it is "
4904 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4905 localDimsReadSuccess = 0;
4908 if (getline (in, line)) {
4915 size_t start = 0, size = 0;
4916 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4923 std::istringstream istr (line);
4927 if (istr.eof () || istr.fail ()) {
4928 exMsg <<
"Unable to read any data from line " << lineNumber
4929 <<
" of input; the line should contain the matrix dimensions "
4930 <<
"\"<numRows> <numCols>\".";
4931 localDimsReadSuccess = 0;
4936 exMsg <<
"Failed to get number of rows from line "
4937 << lineNumber <<
" of input; the line should contains the "
4938 <<
"matrix dimensions \"<numRows> <numCols>\".";
4939 localDimsReadSuccess = 0;
4941 dims[0] = theNumRows;
4943 exMsg <<
"No more data after number of rows on line "
4944 << lineNumber <<
" of input; the line should contain the "
4945 <<
"matrix dimensions \"<numRows> <numCols>\".";
4946 localDimsReadSuccess = 0;
4951 exMsg <<
"Failed to get number of columns from line "
4952 << lineNumber <<
" of input; the line should contain "
4953 <<
"the matrix dimensions \"<numRows> <numCols>\".";
4954 localDimsReadSuccess = 0;
4956 dims[1] = theNumCols;
4966 exMsg <<
"File does not contain a 1D Vector";
4967 localDimsReadSuccess = 0;
4973 Tuple<GO, 5> bannerDimsReadResult;
4975 bannerDimsReadResult[0] = dims[0];
4976 bannerDimsReadResult[1] = dims[1];
4977 bannerDimsReadResult[2] = dims[2];
4978 bannerDimsReadResult[3] = localBannerReadSuccess;
4979 bannerDimsReadResult[4] = localDimsReadSuccess;
4984 broadcast (*comm, 0, bannerDimsReadResult);
4986 TEUCHOS_TEST_FOR_EXCEPTION(
4987 bannerDimsReadResult[3] == 0, std::runtime_error,
4988 "Failed to read banner line: " << exMsg.str ());
4989 TEUCHOS_TEST_FOR_EXCEPTION(
4990 bannerDimsReadResult[4] == 0, std::runtime_error,
4991 "Failed to read matrix dimensions line: " << exMsg.str ());
4993 dims[0] = bannerDimsReadResult[0];
4994 dims[1] = bannerDimsReadResult[1];
4995 dims[2] = bannerDimsReadResult[2];
5000 const size_t numCols =
static_cast<size_t> (dims[1]);
5005 RCP<const map_type> proc0Map;
5006 if (map.is_null ()) {
5010 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
5011 const size_t localNumRows = (myRank == 0) ? numRows : 0;
5013 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
5017 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
5021 RCP<MV> X = createVector<ST, LO, GO, NT> (proc0Map);
5027 int localReadDataSuccess = 1;
5031 *err << myRank <<
": readVectorImpl: Reading matrix data (dense)"
5036 TEUCHOS_TEST_FOR_EXCEPTION(
5037 ! X->isConstantStride (), std::logic_error,
5038 "Can't get a 1-D view of the entries of the MultiVector X on "
5039 "Process 0, because the stride between the columns of X is not "
5040 "constant. This shouldn't happen because we just created X and "
5041 "haven't filled it in yet. Please report this bug to the Tpetra "
5048 Teuchos::ArrayRCP<ST> X_view = X->get1dViewNonConst ();
5049 TEUCHOS_TEST_FOR_EXCEPTION(
5050 as<global_size_t> (X_view.size ()) < numRows * numCols,
5052 "The view of X has size " << X_view <<
" which is not enough to "
5053 "accommodate the expected number of entries numRows*numCols = "
5054 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
5055 "Please report this bug to the Tpetra developers.");
5056 const size_t stride = X->getStride ();
5063 const bool isComplex = (dims[2] == 1);
5064 size_type count = 0, curRow = 0, curCol = 0;
5067 while (getline (in, line)) {
5071 size_t start = 0, size = 0;
5072 const bool commentLine =
5073 checkCommentLine (line, start, size, lineNumber, tolerant);
5074 if (! commentLine) {
5080 if (count >= X_view.size()) {
5085 TEUCHOS_TEST_FOR_EXCEPTION(
5086 count >= X_view.size(),
5088 "The Matrix Market input stream has more data in it than "
5089 "its metadata reported. Current line number is "
5090 << lineNumber <<
".");
5099 const size_t pos = line.substr (start, size).find (
':');
5100 if (pos != std::string::npos) {
5104 std::istringstream istr (line.substr (start, size));
5108 if (istr.eof() || istr.fail()) {
5115 TEUCHOS_TEST_FOR_EXCEPTION(
5116 ! tolerant && (istr.eof() || istr.fail()),
5118 "Line " << lineNumber <<
" of the Matrix Market file is "
5119 "empty, or we cannot read from it for some other reason.");
5122 ST val = STS::zero();
5125 MT real = STM::zero(), imag = STM::zero();
5142 TEUCHOS_TEST_FOR_EXCEPTION(
5143 ! tolerant && istr.eof(), std::runtime_error,
5144 "Failed to get the real part of a complex-valued matrix "
5145 "entry from line " << lineNumber <<
" of the Matrix Market "
5151 }
else if (istr.eof()) {
5152 TEUCHOS_TEST_FOR_EXCEPTION(
5153 ! tolerant && istr.eof(), std::runtime_error,
5154 "Missing imaginary part of a complex-valued matrix entry "
5155 "on line " << lineNumber <<
" of the Matrix Market file.");
5161 TEUCHOS_TEST_FOR_EXCEPTION(
5162 ! tolerant && istr.fail(), std::runtime_error,
5163 "Failed to get the imaginary part of a complex-valued "
5164 "matrix entry from line " << lineNumber <<
" of the "
5165 "Matrix Market file.");
5172 TEUCHOS_TEST_FOR_EXCEPTION(
5173 ! tolerant && istr.fail(), std::runtime_error,
5174 "Failed to get a real-valued matrix entry from line "
5175 << lineNumber <<
" of the Matrix Market file.");
5178 if (istr.fail() && tolerant) {
5184 TEUCHOS_TEST_FOR_EXCEPTION(
5185 ! tolerant && istr.fail(), std::runtime_error,
5186 "Failed to read matrix data from line " << lineNumber
5187 <<
" of the Matrix Market file.");
5190 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
5192 curRow = count % numRows;
5193 curCol = count / numRows;
5194 X_view[curRow + curCol*stride] = val;
5199 TEUCHOS_TEST_FOR_EXCEPTION(
5200 ! tolerant &&
static_cast<global_size_t> (count) < numRows * numCols,
5202 "The Matrix Market metadata reports that the dense matrix is "
5203 << numRows <<
" x " << numCols <<
", and thus has "
5204 << numRows*numCols <<
" total entries, but we only found " << count
5205 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
5206 }
catch (std::exception& e) {
5208 localReadDataSuccess = 0;
5213 *err << myRank <<
": readVectorImpl: done reading data" << endl;
5217 int globalReadDataSuccess = localReadDataSuccess;
5218 broadcast (*comm, 0, outArg (globalReadDataSuccess));
5219 TEUCHOS_TEST_FOR_EXCEPTION(
5220 globalReadDataSuccess == 0, std::runtime_error,
5221 "Failed to read the multivector's data: " << exMsg.str ());
5226 if (comm->getSize () == 1 && map.is_null ()) {
5228 if (! err.is_null ()) {
5232 *err << myRank <<
": readVectorImpl: done" << endl;
5234 if (! err.is_null ()) {
5241 *err << myRank <<
": readVectorImpl: Creating target MV" << endl;
5245 RCP<MV> Y = createVector<ST, LO, GO, NT> (map);
5248 *err << myRank <<
": readVectorImpl: Creating Export" << endl;
5254 Export<LO, GO, NT> exporter (proc0Map, map, err);
5257 *err << myRank <<
": readVectorImpl: Exporting" << endl;
5260 Y->doExport (*X, exporter,
INSERT);
5262 if (! err.is_null ()) {
5266 *err << myRank <<
": readVectorImpl: done" << endl;
5268 if (! err.is_null ()) {
5297 static Teuchos::RCP<const map_type>
5299 const Teuchos::RCP<const comm_type>& comm,
5301 const bool debug=
false,
5304 Teuchos::RCP<Teuchos::FancyOStream>
err =
5305 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
5336 static Teuchos::RCP<const map_type>
5338 const Teuchos::RCP<const comm_type>& comm,
5339 const Teuchos::RCP<Teuchos::FancyOStream>&
err,
5341 const bool debug=
false,
5344 using Teuchos::arcp;
5345 using Teuchos::Array;
5346 using Teuchos::ArrayRCP;
5348 using Teuchos::broadcast;
5349 using Teuchos::Comm;
5350 using Teuchos::CommRequest;
5351 using Teuchos::inOutArg;
5352 using Teuchos::ireceive;
5353 using Teuchos::outArg;
5355 using Teuchos::receive;
5356 using Teuchos::reduceAll;
5357 using Teuchos::REDUCE_MIN;
5358 using Teuchos::isend;
5359 using Teuchos::SerialComm;
5360 using Teuchos::toString;
5361 using Teuchos::wait;
5370 const int numProcs = comm->getSize ();
5371 const int myRank = comm->getRank ();
5373 if (
err.is_null ()) {
5377 std::ostringstream
os;
5381 if (
err.is_null ()) {
5408 std::ostringstream
exMsg;
5425 if (
data.is_null ()) {
5427 exMsg <<
"readDenseImpl() returned null." <<
endl;
5429 }
catch (std::exception&
e) {
5437 std::map<int, Array<GO> >
pid2gids;
5453 if (
data->getNumVectors () == 2) {
5461 const int pid =
static_cast<int> (
PIDs[0]);
5465 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5466 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5478 const int pid =
static_cast<int> (
PIDs[
k]);
5481 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5482 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5494 else if (
data->getNumVectors () == 1) {
5495 if (
data->getGlobalLength () % 2 !=
static_cast<GST> (0)) {
5497 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data has the "
5498 "wrong format (for Map format 2.0). The global number of rows "
5499 "in the MultiVector must be even (divisible by 2)." <<
endl;
5504 static_cast<GO
> (2);
5509 const int pid =
static_cast<int> (
theData[1]);
5512 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5513 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5524 const int pid =
static_cast<int> (
theData[2*
k + 1]);
5527 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5528 <<
"Encountered invalid PID " <<
pid <<
"." <<
endl;
5542 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data must have "
5543 "either 1 column (for the new Map format 2.0) or 2 columns (for "
5544 "the old Map format 1.0).";
5567 "Tpetra::MatrixMarket::readMap: Reading the Map failed with the "
5568 "following exception message: " <<
exMsg.str ());
5597 typename std::map<int, Array<GO> >::iterator
it =
pid2gids.find (0);
5625 typename std::map<int, Array<GO> >::iterator
it =
pid2gids.find (
p);
5644 std::ostringstream
os;
5648 const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
5660 catch (std::exception&
e) {
5662 errStrm <<
"Process " << comm->getRank () <<
" threw an exception: "
5663 <<
e.what () << std::endl;
5667 errStrm <<
"Process " << comm->getRank () <<
" threw an exception "
5668 "not a subclass of std::exception" << std::endl;
5670 Teuchos::reduceAll<int, int> (*comm, Teuchos::REDUCE_MIN,
5677 if (
err.is_null ()) {
5681 std::ostringstream
os;
5685 if (
err.is_null ()) {
5705 encodeDataType (
const std::string&
dataType)
5709 }
else if (
dataType ==
"complex") {
5711 }
else if (dataType ==
"pattern") {
5717 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
5718 "Unrecognized Matrix Market data type \"" << dataType
5719 <<
"\". We should never get here. "
5720 "Please report this bug to the Tpetra developers.");
5755 static Teuchos::RCP<sparse_matrix_type>
5758 const Teuchos::RCP<const map_type>& rowMap,
5759 Teuchos::RCP<const map_type>& colMap,
5760 const Teuchos::RCP<const map_type>& domainMap,
5761 const Teuchos::RCP<const map_type>&
rangeMap,
5765 const bool debug=
false)
5770 using STS =
typename Teuchos::ScalarTraits<ST>;
5772 using Teuchos::ArrayRCP;
5773 using Teuchos::arcp;
5781 rowMap.is_null (), std::invalid_argument,
5782 "Row Map must be nonnull.");
5783 Teuchos::RCP<const Teuchos::Comm<int> > comm = rowMap->getComm();
5785 (comm.is_null (), std::invalid_argument,
5786 "The input row map's communicator (Teuchos::Comm object) is null.");
5788 rangeMap.is_null (), std::invalid_argument,
5789 "Range Map must be nonnull.");
5791 domainMap.is_null (), std::invalid_argument,
5792 "Domain Map must be nonnull.");
5794 domainMap->getComm().getRawPtr() != comm.getRawPtr(),
5795 std::invalid_argument,
5796 "The specified domain Map's communicator (domainMap->getComm())"
5797 "differs from row Map's communicator");
5799 rangeMap->getComm().getRawPtr() != comm.getRawPtr(),
5800 std::invalid_argument,
5801 "The specified range Map's communicator (rangeMap->getComm())"
5802 "differs from row Map's communicator");
5805 const int myRank = comm->getRank();
5806 const int numProc = comm->getSize();
5817 std::ostringstream
errMsg;
5834 using Teuchos::MatrixMarket::Banner;
5840 catch (std::exception&
e) {
5841 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
5842 "threw an exception: " <<
e.what();
5852 errMsg <<
"The Matrix Market input file must contain a "
5853 "\"coordinate\"-format sparse matrix in order to create a "
5854 "Tpetra::CrsMatrix object from it, but the file's matrix "
5855 "type is \"" <<
pBanner->matrixType() <<
"\" instead.";
5862 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
5863 "format sparse matrix in order to create a Tpetra::CrsMatrix "
5864 "object from it, but the file's matrix type is \"array\" "
5865 "instead. That probably means the file contains dense matrix "
5871 using Teuchos::MatrixMarket::readCoordinateDimensions;
5878 "# rows in file does not match rowmap.");
5880 "# rows in file does not match colmap.");
5884 typedef Teuchos::MatrixMarket::Raw::Adder<scalar_type,global_ordinal_type>
raw_adder_type;
5886 Teuchos::RCP<raw_adder_type>
pRaw =
5891 std::cerr <<
"-- Reading matrix data" << std::endl;
5896 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
5906 catch (std::exception&
e) {
5913 typedef Teuchos::MatrixMarket::Raw::Element<scalar_type,global_ordinal_type>
element_type;
5916 pAdder->getAdder()->merge ();
5919 const std::vector<element_type>& entries =
pAdder->getAdder()->getEntries();
5922 const size_t numEntries = (
size_t)entries.size();
5925 std::cerr <<
"----- Proc "<<
myRank<<
": Matrix has numRows=" <<
numRows
5926 <<
" rows and numEntries=" << numEntries
5927 <<
" entries." << std::endl;
5945 LO
INVALID = Teuchos::OrdinalTraits<LO>::invalid();
5955 "Current global row "<<
curRow <<
" is invalid.");
5958 "Row indices are out of order, even though they are supposed "
5959 "to be sorted. curRow = " <<
l_curRow <<
", prvRow = "
5961 "this bug to the Tpetra developers.");
5988 if(colMap.is_null()) {
5990 for(
size_t i=0;
i<rowMap->getLocalNumElements();
i++) {
5991 GO
g_row = rowMap->getGlobalElement(
i);
6000 throw std::runtime_error(
"Reading with a column map is not yet implemented");
6051 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
6115 const bool debug=
false)
6117 Teuchos::RCP<const Teuchos::Comm<int> > comm =
matrix.getComm ();
6119 (comm.is_null (), std::invalid_argument,
6120 "The input matrix's communicator (Teuchos::Comm object) is null.");
6121 const int myRank = comm->getRank ();
6137 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
6140 const bool debug=
false)
6143 (
pMatrix.is_null (), std::invalid_argument,
6144 "The input matrix is null.");
6171 const bool debug=
false)
6179 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
6180 const bool debug=
false)
6220 const bool debug=
false)
6222 using Teuchos::ArrayView;
6223 using Teuchos::Comm;
6224 using Teuchos::FancyOStream;
6225 using Teuchos::getFancyOStream;
6226 using Teuchos::null;
6228 using Teuchos::rcpFromRef;
6234 using STS =
typename Teuchos::ScalarTraits<ST>;
6240 Teuchos::SetScientific<ST>
sci (
out);
6245 comm.is_null (), std::invalid_argument,
6246 "The input matrix's communicator (Teuchos::Comm object) is null.");
6247 const int myRank = comm->getRank ();
6253 std::ostringstream
os;
6270 const global_size_t numCols = domainMap->getGlobalNumElements ();
6272 if (debug &&
myRank == 0) {
6273 std::ostringstream
os;
6274 os <<
"-- Input sparse matrix is:"
6277 << (
matrix.isGloballyIndexed() ?
"Globally" :
"Locally")
6278 <<
" indexed." <<
endl
6279 <<
"---- Its row map has " << rowMap->getGlobalNumElements ()
6280 <<
" elements." <<
endl
6281 <<
"---- Its col map has " << colMap->getGlobalNumElements ()
6282 <<
" elements." <<
endl;
6289 std::ostringstream
os;
6290 os <<
"-- " <<
myRank <<
": making gatherRowMap" <<
endl;
6294 Details::computeGatherMap (rowMap,
err, debug);
6304 Details::computeGatherMap (colMap,
err, debug);
6315 static_cast<size_t> (0)));
6324 domainMap->getIndexBase (),
6334 cerr <<
"-- Output sparse matrix is:"
6335 <<
"---- " <<
newMatrix->getRangeMap ()->getGlobalNumElements ()
6337 <<
newMatrix->getDomainMap ()->getGlobalNumElements ()
6341 << (
newMatrix->isGloballyIndexed () ?
"Globally" :
"Locally")
6342 <<
" indexed." <<
endl
6343 <<
"---- Its row map has "
6344 <<
newMatrix->getRowMap ()->getGlobalNumElements ()
6345 <<
" elements, with index base "
6347 <<
"---- Its col map has "
6348 <<
newMatrix->getColMap ()->getGlobalNumElements ()
6349 <<
" elements, with index base "
6351 <<
"---- Element count of output matrix's column Map may differ "
6352 <<
"from that of the input matrix's column Map, if some columns "
6353 <<
"of the matrix contain no entries." <<
endl;
6366 out <<
"%%MatrixMarket matrix coordinate "
6367 << (STS::isComplex ?
"complex" :
"real")
6368 <<
" general" <<
endl;
6385 out <<
newMatrix->getRangeMap ()->getGlobalNumElements () <<
" "
6386 <<
newMatrix->getDomainMap ()->getGlobalNumElements () <<
" "
6409 typename sparse_matrix_type::global_inds_host_view_type
ind;
6410 typename sparse_matrix_type::values_host_view_type
val;
6412 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6418 if (STS::isComplex) {
6428 using OTG = Teuchos::OrdinalTraits<GO>;
6437 "Failed to convert the supposed local row index "
6439 "Please report this bug to the Tpetra developers.");
6440 typename sparse_matrix_type::local_inds_host_view_type
ind;
6441 typename sparse_matrix_type::values_host_view_type
val;
6443 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6449 "On local row " <<
localRowIndex <<
" of the sparse matrix: "
6450 "Failed to convert the supposed local column index "
6451 <<
ind(
ii) <<
" into a global column index. Please report "
6452 "this bug to the Tpetra developers.");
6457 if (STS::isComplex) {
6472 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
6475 const bool debug=
false)
6478 (
pMatrix.is_null (), std::invalid_argument,
6479 "The input matrix is null.");
6518 const bool debug=
false)
6520 using Teuchos::ArrayView;
6521 using Teuchos::Comm;
6522 using Teuchos::FancyOStream;
6523 using Teuchos::getFancyOStream;
6524 using Teuchos::null;
6526 using Teuchos::rcpFromRef;
6536 auto rowMap =
graph.getRowMap ();
6537 if (rowMap.is_null ()) {
6540 auto comm = rowMap->getComm ();
6541 if (comm.is_null ()) {
6544 const int myRank = comm->getRank ();
6550 std::ostringstream
os;
6562 auto colMap =
graph.getColMap ();
6563 auto domainMap =
graph.getDomainMap ();
6567 const global_size_t numCols = domainMap->getGlobalNumElements ();
6569 if (debug &&
myRank == 0) {
6570 std::ostringstream
os;
6571 os <<
"-- Input sparse graph is:"
6572 <<
"---- " <<
numRows <<
" x " << numCols <<
" with "
6573 <<
graph.getGlobalNumEntries () <<
" entries;" <<
endl
6575 << (
graph.isGloballyIndexed () ?
"Globally" :
"Locally")
6576 <<
" indexed." <<
endl
6577 <<
"---- Its row Map has " << rowMap->getGlobalNumElements ()
6578 <<
" elements." <<
endl
6579 <<
"---- Its col Map has " << colMap->getGlobalNumElements ()
6580 <<
" elements." <<
endl;
6587 std::ostringstream
os;
6588 os <<
"-- " <<
myRank <<
": making gatherRowMap" <<
endl;
6609 static_cast<size_t> (0));
6618 domainMap->getIndexBase (),
6628 cerr <<
"-- Output sparse graph is:"
6629 <<
"---- " <<
newGraph.getRangeMap ()->getGlobalNumElements ()
6631 <<
newGraph.getDomainMap ()->getGlobalNumElements ()
6633 <<
newGraph.getGlobalNumEntries () <<
" entries;" <<
endl
6635 << (
newGraph.isGloballyIndexed () ?
"Globally" :
"Locally")
6636 <<
" indexed." <<
endl
6637 <<
"---- Its row map has "
6638 <<
newGraph.getRowMap ()->getGlobalNumElements ()
6639 <<
" elements, with index base "
6640 <<
newGraph.getRowMap ()->getIndexBase () <<
"." <<
endl
6641 <<
"---- Its col map has "
6642 <<
newGraph.getColMap ()->getGlobalNumElements ()
6643 <<
" elements, with index base "
6644 <<
newGraph.getColMap ()->getIndexBase () <<
"." <<
endl
6645 <<
"---- Element count of output graph's column Map may differ "
6646 <<
"from that of the input matrix's column Map, if some columns "
6647 <<
"of the matrix contain no entries." <<
endl;
6661 out <<
"%%MatrixMarket matrix coordinate pattern general" <<
endl;
6679 out <<
newGraph.getRangeMap ()->getGlobalNumElements () <<
" "
6680 <<
newGraph.getDomainMap ()->getGlobalNumElements () <<
" "
6695 if (
newGraph.isGloballyIndexed ()) {
6703 typename crs_graph_type::global_inds_host_view_type
ind;
6705 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6716 typedef Teuchos::OrdinalTraits<GO>
OTG;
6725 "to convert the supposed local row index " <<
localRowIndex <<
6726 " into a global row index. Please report this bug to the "
6727 "Tpetra developers.");
6728 typename crs_graph_type::local_inds_host_view_type
ind;
6730 for (
size_t ii = 0;
ii <
ind.extent(0);
ii++) {
6736 "On local row " <<
localRowIndex <<
" of the sparse graph: "
6737 "Failed to convert the supposed local column index "
6738 <<
ind(
ii) <<
" into a global column index. Please report "
6739 "this bug to the Tpetra developers.");
6759 const bool debug=
false)
6803 const bool debug=
false)
6805 auto comm =
graph.getComm ();
6806 if (comm.is_null ()) {
6814 const int myRank = comm->getRank ();
6834 const bool debug=
false)
6849 const Teuchos::RCP<const crs_graph_type>&
pGraph,
6852 const bool debug=
false)
6868 const Teuchos::RCP<const crs_graph_type>&
pGraph,
6869 const bool debug=
false)
6899 const bool debug=
false)
6907 const Teuchos::RCP<const sparse_matrix_type>&
pMatrix,
6908 const bool debug=
false)
6946 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
6947 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
6949 const int myRank =
X.getMap ().is_null () ? 0 :
6950 (
X.getMap ()->getComm ().is_null () ? 0 :
6951 X.getMap ()->getComm ()->getRank ());
6971 const Teuchos::RCP<const multivector_type>&
X,
6974 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
6975 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
6978 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6979 "writeDenseFile: The input MultiVector X is null.");
6991 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
6992 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7004 const Teuchos::RCP<const multivector_type>&
X,
7005 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7006 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7009 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7010 "writeDenseFile: The input MultiVector X is null.");
7050 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7051 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7053 using Teuchos::Comm;
7054 using Teuchos::outArg;
7055 using Teuchos::REDUCE_MAX;
7056 using Teuchos::reduceAll;
7061 Teuchos::null :
X.getMap ()->getComm ();
7062 const int myRank = comm.is_null () ? 0 : comm->getRank ();
7067 const bool debug = !
dbg.is_null ();
7070 std::ostringstream
os;
7080 const size_t numVecs =
X.getNumVectors ();
7082 writeDenseColumn (
out, * (
X.getVector (
j)),
err,
dbg);
7087 std::ostringstream
os;
7122 writeDenseHeader (std::ostream&
out,
7126 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7127 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7129 using Teuchos::Comm;
7130 using Teuchos::outArg;
7132 using Teuchos::REDUCE_MAX;
7133 using Teuchos::reduceAll;
7135 typedef Teuchos::ScalarTraits<scalar_type> STS;
7136 const char prefix[] =
"Tpetra::MatrixMarket::writeDenseHeader: ";
7139 Teuchos::null :
X.getMap ()->getComm ();
7140 const int myRank = comm.is_null () ? 0 : comm->getRank ();
7147 const bool debug = !
dbg.is_null ();
7151 std::ostringstream
os;
7171 std::ostringstream
hdr;
7174 if (STS::isComplex) {
7176 }
else if (STS::isOrdinal) {
7177 dataType =
"integer";
7181 hdr <<
"%%MatrixMarket matrix array " << dataType <<
" general"
7186 if (matrixName !=
"") {
7187 printAsComment (hdr, matrixName);
7189 if (matrixDescription !=
"") {
7190 printAsComment (hdr, matrixDescription);
7193 hdr << X.getGlobalLength () <<
" " << X.getNumVectors () << endl;
7197 }
catch (std::exception& e) {
7198 if (! err.is_null ()) {
7199 *err << prefix <<
"While writing the Matrix Market header, "
7200 "Process 0 threw an exception: " << e.what () << endl;
7209 reduceAll<int, int> (*comm, REDUCE_MAX, lclErr, outArg (gblErr));
7210 TEUCHOS_TEST_FOR_EXCEPTION(
7211 gblErr == 1, std::runtime_error, prefix <<
"Some error occurred "
7212 "which prevented this method from completing.");
7216 *dbg << myRank <<
": writeDenseHeader: Done" << endl;
7239 writeDenseColumn (std::ostream& out,
7241 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7242 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7244 using Teuchos::arcp;
7245 using Teuchos::Array;
7246 using Teuchos::ArrayRCP;
7247 using Teuchos::ArrayView;
7248 using Teuchos::Comm;
7249 using Teuchos::CommRequest;
7250 using Teuchos::ireceive;
7251 using Teuchos::isend;
7252 using Teuchos::outArg;
7253 using Teuchos::REDUCE_MAX;
7254 using Teuchos::reduceAll;
7256 using Teuchos::TypeNameTraits;
7257 using Teuchos::wait;
7259 typedef Teuchos::ScalarTraits<scalar_type> STS;
7261 const Comm<int>& comm = * (X.getMap ()->getComm ());
7262 const int myRank = comm.getRank ();
7263 const int numProcs = comm.getSize ();
7270 const bool debug = ! dbg.is_null ();
7274 std::ostringstream os;
7275 os << myRank <<
": writeDenseColumn" << endl;
7284 Teuchos::SetScientific<scalar_type> sci (out);
7286 const size_t myNumRows = X.getLocalLength ();
7287 const size_t numCols = X.getNumVectors ();
7290 const int sizeTag = 1337;
7291 const int dataTag = 1338;
7352 RCP<CommRequest<int> > sendReqSize, sendReqData;
7358 Array<ArrayRCP<size_t> > recvSizeBufs (3);
7359 Array<ArrayRCP<scalar_type> > recvDataBufs (3);
7360 Array<RCP<CommRequest<int> > > recvSizeReqs (3);
7361 Array<RCP<CommRequest<int> > > recvDataReqs (3);
7364 ArrayRCP<size_t> sendDataSize (1);
7365 sendDataSize[0] = myNumRows;
7369 std::ostringstream os;
7370 os << myRank <<
": Post receive-size receives from "
7371 "Procs 1 and 2: tag = " << sizeTag << endl;
7375 recvSizeBufs[0].resize (1);
7379 (recvSizeBufs[0])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7380 recvSizeBufs[1].resize (1);
7381 (recvSizeBufs[1])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7382 recvSizeBufs[2].resize (1);
7383 (recvSizeBufs[2])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7386 ireceive<int, size_t> (recvSizeBufs[1], 1, sizeTag, comm);
7390 ireceive<int, size_t> (recvSizeBufs[2], 2, sizeTag, comm);
7393 else if (myRank == 1 || myRank == 2) {
7395 std::ostringstream os;
7396 os << myRank <<
": Post send-size send: size = "
7397 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7404 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7408 std::ostringstream os;
7409 os << myRank <<
": Not posting my send-size send yet" << endl;
7418 std::ostringstream os;
7419 os << myRank <<
": Pack my entries" << endl;
7422 ArrayRCP<scalar_type> sendDataBuf;
7424 sendDataBuf = arcp<scalar_type> (myNumRows * numCols);
7425 X.get1dCopy (sendDataBuf (), myNumRows);
7427 catch (std::exception& e) {
7429 if (! err.is_null ()) {
7430 std::ostringstream os;
7431 os <<
"Process " << myRank <<
": Attempt to pack my MultiVector "
7432 "entries threw an exception: " << e.what () << endl;
7437 std::ostringstream os;
7438 os << myRank <<
": Done packing my entries" << endl;
7447 *dbg << myRank <<
": Post send-data send: tag = " << dataTag
7450 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7458 std::ostringstream os;
7459 os << myRank <<
": Write my entries" << endl;
7465 const size_t printNumRows = myNumRows;
7466 ArrayView<const scalar_type> printData = sendDataBuf ();
7467 const size_t printStride = printNumRows;
7468 if (
static_cast<size_t> (printData.size ()) < printStride * numCols) {
7470 if (! err.is_null ()) {
7471 std::ostringstream os;
7472 os <<
"Process " << myRank <<
": My MultiVector data's size "
7473 << printData.size () <<
" does not match my local dimensions "
7474 << printStride <<
" x " << numCols <<
"." << endl;
7482 for (
size_t col = 0; col < numCols; ++col) {
7483 for (
size_t row = 0; row < printNumRows; ++row) {
7484 if (STS::isComplex) {
7485 out << STS::real (printData[row + col * printStride]) <<
" "
7486 << STS::imag (printData[row + col * printStride]) << endl;
7488 out << printData[row + col * printStride] << endl;
7497 const int recvRank = 1;
7498 const int circBufInd = recvRank % 3;
7500 std::ostringstream os;
7501 os << myRank <<
": Wait on receive-size receive from Process "
7502 << recvRank << endl;
7506 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7510 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7511 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7513 if (! err.is_null ()) {
7514 std::ostringstream os;
7515 os << myRank <<
": Result of receive-size receive from Process "
7516 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7517 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7518 "This should never happen, and suggests that the receive never "
7519 "got posted. Please report this bug to the Tpetra developers."
7534 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7536 std::ostringstream os;
7537 os << myRank <<
": Post receive-data receive from Process "
7538 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7539 << recvDataBufs[circBufInd].size () << endl;
7542 if (! recvSizeReqs[circBufInd].is_null ()) {
7544 if (! err.is_null ()) {
7545 std::ostringstream os;
7546 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7547 "null, before posting the receive-data receive from Process "
7548 << recvRank <<
". This should never happen. Please report "
7549 "this bug to the Tpetra developers." << endl;
7553 recvDataReqs[circBufInd] =
7554 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7555 recvRank, dataTag, comm);
7558 else if (myRank == 1) {
7561 std::ostringstream os;
7562 os << myRank <<
": Wait on my send-size send" << endl;
7565 wait<int> (comm, outArg (sendReqSize));
7571 for (
int p = 1; p < numProcs; ++p) {
7573 if (p + 2 < numProcs) {
7575 const int recvRank = p + 2;
7576 const int circBufInd = recvRank % 3;
7578 std::ostringstream os;
7579 os << myRank <<
": Post receive-size receive from Process "
7580 << recvRank <<
": tag = " << sizeTag << endl;
7583 if (! recvSizeReqs[circBufInd].is_null ()) {
7585 if (! err.is_null ()) {
7586 std::ostringstream os;
7587 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7588 <<
"null, for the receive-size receive from Process "
7589 << recvRank <<
"! This may mean that this process never "
7590 <<
"finished waiting for the receive from Process "
7591 << (recvRank - 3) <<
"." << endl;
7595 recvSizeReqs[circBufInd] =
7596 ireceive<int, size_t> (recvSizeBufs[circBufInd],
7597 recvRank, sizeTag, comm);
7600 if (p + 1 < numProcs) {
7601 const int recvRank = p + 1;
7602 const int circBufInd = recvRank % 3;
7606 std::ostringstream os;
7607 os << myRank <<
": Wait on receive-size receive from Process "
7608 << recvRank << endl;
7611 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7615 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7616 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7618 if (! err.is_null ()) {
7619 std::ostringstream os;
7620 os << myRank <<
": Result of receive-size receive from Process "
7621 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7622 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7623 "This should never happen, and suggests that the receive never "
7624 "got posted. Please report this bug to the Tpetra developers."
7638 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7640 std::ostringstream os;
7641 os << myRank <<
": Post receive-data receive from Process "
7642 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7643 << recvDataBufs[circBufInd].size () << endl;
7646 if (! recvDataReqs[circBufInd].is_null ()) {
7648 if (! err.is_null ()) {
7649 std::ostringstream os;
7650 os << myRank <<
": recvDataReqs[" << circBufInd <<
"] is not "
7651 <<
"null, for the receive-data receive from Process "
7652 << recvRank <<
"! This may mean that this process never "
7653 <<
"finished waiting for the receive from Process "
7654 << (recvRank - 3) <<
"." << endl;
7658 recvDataReqs[circBufInd] =
7659 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7660 recvRank, dataTag, comm);
7664 const int recvRank = p;
7665 const int circBufInd = recvRank % 3;
7667 std::ostringstream os;
7668 os << myRank <<
": Wait on receive-data receive from Process "
7669 << recvRank << endl;
7672 wait<int> (comm, outArg (recvDataReqs[circBufInd]));
7679 std::ostringstream os;
7680 os << myRank <<
": Write entries from Process " << recvRank
7682 *dbg << os.str () << endl;
7684 size_t printNumRows = (recvSizeBufs[circBufInd])[0];
7685 if (printNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7687 if (! err.is_null ()) {
7688 std::ostringstream os;
7689 os << myRank <<
": Result of receive-size receive from Process "
7690 << recvRank <<
" was Teuchos::OrdinalTraits<size_t>::"
7691 "invalid() = " << Teuchos::OrdinalTraits<size_t>::invalid ()
7692 <<
". This should never happen, and suggests that its "
7693 "receive-size receive was never posted. "
7694 "Please report this bug to the Tpetra developers." << endl;
7701 if (printNumRows > 0 && recvDataBufs[circBufInd].is_null ()) {
7703 if (! err.is_null ()) {
7704 std::ostringstream os;
7705 os << myRank <<
": Result of receive-size receive from Proc "
7706 << recvRank <<
" was " << printNumRows <<
" > 0, but "
7707 "recvDataBufs[" << circBufInd <<
"] is null. This should "
7708 "never happen. Please report this bug to the Tpetra "
7709 "developers." << endl;
7719 ArrayView<const scalar_type> printData = (recvDataBufs[circBufInd]) ();
7720 const size_t printStride = printNumRows;
7724 for (
size_t col = 0; col < numCols; ++col) {
7725 for (
size_t row = 0; row < printNumRows; ++row) {
7726 if (STS::isComplex) {
7727 out << STS::real (printData[row + col * printStride]) <<
" "
7728 << STS::imag (printData[row + col * printStride]) << endl;
7730 out << printData[row + col * printStride] << endl;
7735 else if (myRank == p) {
7738 std::ostringstream os;
7739 os << myRank <<
": Wait on my send-data send" << endl;
7742 wait<int> (comm, outArg (sendReqData));
7744 else if (myRank == p + 1) {
7747 std::ostringstream os;
7748 os << myRank <<
": Post send-data send: tag = " << dataTag
7752 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7755 std::ostringstream os;
7756 os << myRank <<
": Wait on my send-size send" << endl;
7759 wait<int> (comm, outArg (sendReqSize));
7761 else if (myRank == p + 2) {
7764 std::ostringstream os;
7765 os << myRank <<
": Post send-size send: size = "
7766 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7769 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7774 reduceAll<int, int> (comm, REDUCE_MAX, lclErr, outArg (gblErr));
7775 TEUCHOS_TEST_FOR_EXCEPTION(
7776 gblErr == 1, std::runtime_error,
"Tpetra::MatrixMarket::writeDense "
7777 "experienced some kind of error and was unable to complete.");
7781 *dbg << myRank <<
": writeDenseColumn: Done" << endl;
7795 const Teuchos::RCP<const multivector_type>&
X,
7798 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7799 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7802 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7803 "writeDense: The input MultiVector X is null.");
7815 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7816 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7828 const Teuchos::RCP<const multivector_type>&
X,
7829 const Teuchos::RCP<Teuchos::FancyOStream>&
err = Teuchos::null,
7830 const Teuchos::RCP<Teuchos::FancyOStream>&
dbg = Teuchos::null)
7833 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7834 "writeDense: The input MultiVector X is null.");
7860 Teuchos::RCP<Teuchos::FancyOStream>
err =
7861 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
7876 const Teuchos::RCP<Teuchos::FancyOStream>&
err,
7877 const bool debug=
false)
7879 using Teuchos::Array;
7880 using Teuchos::ArrayRCP;
7881 using Teuchos::ArrayView;
7882 using Teuchos::Comm;
7883 using Teuchos::CommRequest;
7884 using Teuchos::ireceive;
7885 using Teuchos::isend;
7887 using Teuchos::TypeNameTraits;
7888 using Teuchos::wait;
7908 sizeof (GO) >
sizeof (
int_type), std::logic_error,
7910 <<
" is too big for ptrdiff_t. sizeof(GO) = " <<
sizeof (GO)
7911 <<
" > sizeof(ptrdiff_t) = " <<
sizeof (
ptrdiff_t) <<
".");
7914 "The (MPI) process rank type pid_type=" <<
7916 "sizeof(pid_type) = " <<
sizeof (
pid_type) <<
" > sizeof(ptrdiff_t)"
7920 const int myRank = comm.getRank ();
7921 const int numProcs = comm.getSize ();
7923 if (!
err.is_null ()) {
7927 std::ostringstream
os;
7931 if (!
err.is_null ()) {
8015 std::ostringstream
os;
8016 os <<
myRank <<
": Post receive-size receives from "
8038 std::ostringstream
os;
8039 os <<
myRank <<
": Post send-size send: size = "
8051 std::ostringstream
os;
8052 os <<
myRank <<
": Not posting my send-size send yet" <<
endl;
8063 std::ostringstream
os;
8070 if (
map.isContiguous ()) {
8091 std::ostringstream
os;
8092 os <<
myRank <<
": Done packing my GIDs and PIDs" <<
endl;
8112 std::ostringstream
hdr;
8116 hdr <<
"%%MatrixMarket matrix array integer general" <<
endl
8117 <<
"% Format: Version 2.0" <<
endl
8119 <<
"% This file encodes a Tpetra::Map." <<
endl
8120 <<
"% It is stored as a dense vector, with twice as many " <<
endl
8121 <<
"% entries as the global number of GIDs (global indices)." <<
endl
8122 <<
"% (GID, PID) pairs are stored contiguously, where the PID " <<
endl
8123 <<
"% is the rank of the process owning that GID." <<
endl
8124 << (2 *
map.getGlobalNumElements ()) <<
" " << 1 <<
endl;
8128 std::ostringstream
os;
8149 std::ostringstream
os;
8150 os <<
myRank <<
": Wait on receive-size receive from Process "
8161 std::ostringstream
os;
8162 os <<
myRank <<
": Result of receive-size receive from Process "
8163 <<
recvRank <<
" is -1. This should never happen, and "
8164 "suggests that the receive never got posted. Please report "
8165 "this bug to the Tpetra developers." <<
endl;
8172 std::ostringstream
os;
8173 os <<
myRank <<
": Post receive-data receive from Process "
8179 std::ostringstream
os;
8181 "null, before posting the receive-data receive from Process "
8182 <<
recvRank <<
". This should never happen. Please report "
8183 "this bug to the Tpetra developers." <<
endl;
8194 std::ostringstream
os;
8211 std::ostringstream
os;
8212 os <<
myRank <<
": Post receive-size receive from Process "
8217 std::ostringstream
os;
8219 <<
"null, for the receive-size receive from Process "
8220 <<
recvRank <<
"! This may mean that this process never "
8221 <<
"finished waiting for the receive from Process "
8236 std::ostringstream
os;
8237 os <<
myRank <<
": Wait on receive-size receive from Process "
8247 std::ostringstream
os;
8248 os <<
myRank <<
": Result of receive-size receive from Process "
8249 <<
recvRank <<
" is -1. This should never happen, and "
8250 "suggests that the receive never got posted. Please report "
8251 "this bug to the Tpetra developers." <<
endl;
8258 std::ostringstream
os;
8259 os <<
myRank <<
": Post receive-data receive from Process "
8265 std::ostringstream
os;
8267 <<
"null, for the receive-data receive from Process "
8268 <<
recvRank <<
"! This may mean that this process never "
8269 <<
"finished waiting for the receive from Process "
8282 std::ostringstream
os;
8283 os <<
myRank <<
": Wait on receive-data receive from Process "
8294 std::ostringstream
os;
8295 os <<
myRank <<
": Write GIDs and PIDs from Process "
8301 std::ostringstream
os;
8302 os <<
myRank <<
": Result of receive-size receive from Process "
8303 <<
recvRank <<
" was -1. This should never happen, and "
8304 "suggests that its receive-size receive was never posted. "
8305 "Please report this bug to the Tpetra developers." <<
endl;
8309 std::ostringstream
os;
8310 os <<
myRank <<
": Result of receive-size receive from Proc "
8312 "recvDataBufs[" <<
circBufInd <<
"] is null. This should "
8313 "never happen. Please report this bug to the Tpetra "
8314 "developers." <<
endl;
8327 std::ostringstream
os;
8336 std::ostringstream
os;
8344 std::ostringstream
os;
8353 std::ostringstream
os;
8354 os <<
myRank <<
": Post send-size send: size = "
8362 if (!
err.is_null ()) {
8368 if (!
err.is_null ()) {
8378 const int myRank =
map.getComm ()->getRank ();
8414 printAsComment (std::ostream&
out,
const std::string&
str)
8421 if (!
line.empty()) {
8424 if (
line[0] ==
'%') {
8456 Teuchos::ParameterList
pl;
8482 Teuchos::ParameterList
pl;
8525 const Teuchos::ParameterList&
params)
8529 const int myRank =
A.getDomainMap()->getComm()->getRank();
8542 "writeOperator: temporary file " <<
tmpFile <<
" already exists");
8544 if (
params.isParameter(
"precision")) {
8558 if (
params.isParameter(
"print MatrixMarket header"))
8565 std::ifstream src(
tmpFile, std::ios_base::binary);
8614 const Teuchos::ParameterList&
params)
8616 const int myRank =
A.getDomainMap ()->getComm ()->getRank ();
8633 std::ostringstream
tmpOut;
8635 if (
params.isParameter (
"precision") &&
params.isType<
int> (
"precision")) {
8644 if (
params.isParameter (
"print MatrixMarket header") &&
8645 params.isType<
bool> (
"print MatrixMarket header")) {
8678 writeOperatorImpl (std::ostream&
os,
8679 const operator_type&
A,
8680 const Teuchos::ParameterList&
params)
8684 using Teuchos::ArrayRCP;
8685 using Teuchos::Array;
8690 typedef Teuchos::OrdinalTraits<LO>
TLOT;
8691 typedef Teuchos::OrdinalTraits<GO>
TGOT;
8695 const map_type& domainMap = *(
A.getDomainMap());
8698 const int myRank = comm->getRank();
8699 const size_t numProcs = comm->getSize();
8702 if (
params.isParameter(
"probing size"))
8706 GO
minColGid = domainMap.getMinAllGlobalIndex();
8707 GO
maxColGid = domainMap.getMaxAllGlobalIndex();
8718 if (
params.isParameter(
"zero-based indexing")) {
8719 if (
params.get<
bool>(
"zero-based indexing") ==
true)
8745 importGidList.reserve(numTargetMapEntries);
8747 RCP<map_type> importGidMap = rcp(
new map_type(TGOT::invalid(), importGidList(), indexBase, comm));
8750 import_type gidImporter(allGidsMap, importGidMap);
8751 mv_type_go importedGids(importGidMap, 1);
8752 importedGids.doImport(allGids, gidImporter,
INSERT);
8755 ArrayRCP<const GO> importedGidsData = importedGids.getData(0);
8756 RCP<const map_type> importMap = rcp(
new map_type(TGOT::invalid(), importedGidsData(), indexBase, comm) );
8759 import_type importer(rangeMap, importMap);
8761 RCP<mv_type> colsOnPid0 = rcp(
new mv_type(importMap,numMVs));
8763 RCP<mv_type> ei = rcp(
new mv_type(A.getDomainMap(),numMVs));
8764 RCP<mv_type> colsA = rcp(
new mv_type(A.getRangeMap(),numMVs));
8766 Array<GO> globalColsArray, localColsArray;
8767 globalColsArray.reserve(numMVs);
8768 localColsArray.reserve(numMVs);
8770 ArrayRCP<ArrayRCP<Scalar> > eiData(numMVs);
8771 for (
size_t i=0; i<numMVs; ++i)
8772 eiData[i] = ei->getDataNonConst(i);
8777 for (GO k=0; k<numChunks; ++k) {
8778 for (
size_t j=0; j<numMVs; ++j ) {
8780 GO curGlobalCol = minColGid + k*numMVs + j;
8781 globalColsArray.push_back(curGlobalCol);
8783 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8784 if (curLocalCol != TLOT::invalid()) {
8785 eiData[j][curLocalCol] = TGOT::one();
8786 localColsArray.push_back(curLocalCol);
8791 for (
size_t i=0; i<numMVs; ++i)
8792 eiData[i] = Teuchos::null;
8794 A.apply(*ei,*colsA);
8796 colsOnPid0->doImport(*colsA,importer,
INSERT);
8799 globalNnz += writeColumns(os,*colsOnPid0, numMVs, importedGidsData(),
8800 globalColsArray, offsetToUseInPrinting);
8803 for (
size_t i=0; i<numMVs; ++i)
8804 eiData[i] = ei->getDataNonConst(i);
8805 for (
size_t j=0; j<numMVs; ++j ) {
8806 GO curGlobalCol = minColGid + k*numMVs + j;
8808 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8809 if (curLocalCol != TLOT::invalid()) {
8810 eiData[j][curLocalCol] = TGOT::one();
8815 for (
size_t j=0; j<numMVs; ++j ) {
8816 for (
int i=0; i<localColsArray.size(); ++i)
8817 eiData[j][localColsArray[i]] = TGOT::zero();
8819 globalColsArray.clear();
8820 localColsArray.clear();
8828 for (
int j=0; j<rem; ++j ) {
8829 GO curGlobalCol = maxColGid - rem + j + TGOT::one();
8830 globalColsArray.push_back(curGlobalCol);
8832 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8833 if (curLocalCol != TLOT::invalid()) {
8834 eiData[j][curLocalCol] = TGOT::one();
8835 localColsArray.push_back(curLocalCol);
8840 for (
size_t i=0; i<numMVs; ++i)
8841 eiData[i] = Teuchos::null;
8843 A.apply(*ei,*colsA);
8845 colsOnPid0->doImport(*colsA,importer,
INSERT);
8847 globalNnz += writeColumns(os,*colsOnPid0, rem, importedGidsData(),
8848 globalColsArray, offsetToUseInPrinting);
8851 for (
size_t i=0; i<numMVs; ++i)
8852 eiData[i] = ei->getDataNonConst(i);
8853 for (
int j=0; j<rem; ++j ) {
8854 GO curGlobalCol = maxColGid - rem + j + TGOT::one();
8856 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8857 if (curLocalCol != TLOT::invalid()) {
8858 eiData[j][curLocalCol] = TGOT::one();
8863 for (
int j=0; j<rem; ++j ) {
8864 for (
int i=0; i<localColsArray.size(); ++i)
8865 eiData[j][localColsArray[i]] = TGOT::zero();
8867 globalColsArray.clear();
8868 localColsArray.clear();
8877 std::ostringstream oss;
8879 oss <<
"%%MatrixMarket matrix coordinate ";
8880 if (Teuchos::ScalarTraits<typename operator_type::scalar_type>::isComplex) {
8885 oss <<
" general" << std::endl;
8886 oss <<
"% Tpetra::Operator" << std::endl;
8887 std::time_t now = std::time(NULL);
8888 oss <<
"% time stamp: " << ctime(&now);
8889 oss <<
"% written from " << numProcs <<
" processes" << std::endl;
8890 size_t numRows = rangeMap->getGlobalNumElements();
8891 size_t numCols = domainMap.getGlobalNumElements();
8892 oss << numRows <<
" " << numCols <<
" " << globalNnz << std::endl;
8899 writeColumns(std::ostream& os, mv_type
const &colsA,
size_t const &numCols,
8900 Teuchos::ArrayView<const global_ordinal_type>
const &rowGids,
8901 Teuchos::Array<global_ordinal_type>
const &colsArray,
8906 typedef Teuchos::ScalarTraits<Scalar> STS;
8909 const Scalar zero = STS::zero();
8910 const size_t numRows = colsA.getGlobalLength();
8911 for (
size_t j=0; j<numCols; ++j) {
8912 Teuchos::ArrayRCP<const Scalar>
const curCol = colsA.getData(j);
8913 const GO J = colsArray[j];
8914 for (
size_t i=0; i<numRows; ++i) {
8915 const Scalar val = curCol[i];
8917 os << rowGids[i]+indexBase <<
" " << J+indexBase <<
" " << val << std::endl;
8945 const bool debug=
false) {
8950 using STS =
typename Teuchos::ScalarTraits<ST>;
8954 Teuchos::RCP<const Teuchos::Comm<int> > comm =
matrix.getComm ();
8956 (comm.is_null (), std::invalid_argument,
8957 "The input matrix's communicator (Teuchos::Comm object) is null.");
8959 (
matrix.isGloballyIndexed() || !
matrix.isFillComplete(), std::invalid_argument,
8960 "The input matrix must not be GloballyIndexed and must be fillComplete.");
8963 const int myRank = comm->getRank();
8964 const int numProc = comm->getSize();
8986 out <<
"%%MatrixMarket matrix coordinate "
8987 << (STS::isComplex ?
"complex" :
"real")
8988 <<
" general" << std::endl;
9008 Teuchos::SetScientific<ST>
sci (
out);
9013 typename sparse_matrix_type::local_inds_host_view_type indices;
9014 typename sparse_matrix_type::values_host_view_type
values;
9016 for (
size_t ii = 0;
ii < indices.extent(0);
ii++) {
9017 const GO
g_col = colMap->getGlobalElement(indices(
ii));
9022 if (STS::isComplex) {