42 #ifndef __MatrixMarket_Tpetra_hpp
43 #define __MatrixMarket_Tpetra_hpp
56 #include "Tpetra_Details_gathervPrint.hpp"
57 #include "Tpetra_CrsMatrix.hpp"
58 #include "Tpetra_Operator.hpp"
59 #include "Tpetra_Vector.hpp"
61 #include "Teuchos_MatrixMarket_Raw_Adder.hpp"
62 #include "Teuchos_MatrixMarket_Raw_Graph_Adder.hpp"
63 #include "Teuchos_MatrixMarket_SymmetrizingAdder.hpp"
64 #include "Teuchos_MatrixMarket_SymmetrizingGraphAdder.hpp"
65 #include "Teuchos_MatrixMarket_assignScalar.hpp"
66 #include "Teuchos_MatrixMarket_Banner.hpp"
67 #include "Teuchos_MatrixMarket_CoordDataReader.hpp"
68 #include "Teuchos_SetScientific.hpp"
108 namespace MatrixMarket {
164 template<
class SparseMatrixType>
169 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
184 typedef typename SparseMatrixType::global_ordinal_type
206 typedef Teuchos::Comm<int> comm_type;
209 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
211 typedef Teuchos::RCP<const comm_type> comm_ptr TPETRA_DEPRECATED;
212 typedef Teuchos::RCP<const map_type> map_ptr TPETRA_DEPRECATED;
213 typedef Teuchos::RCP<node_type> node_ptr TPETRA_DEPRECATED;
214 #endif // TPETRA_ENABLE_DEPRECATED_CODE
222 typedef Teuchos::ArrayRCP<int>::size_type size_type;
234 static Teuchos::RCP<const map_type>
235 makeRangeMap (
const Teuchos::RCP<const comm_type>& pComm,
239 return rcp (
new map_type (static_cast<global_size_t> (numRows),
240 static_cast<global_ordinal_type> (0),
241 pComm, GloballyDistributed));
271 static Teuchos::RCP<const map_type>
272 makeRowMap (
const Teuchos::RCP<const map_type>& pRowMap,
273 const Teuchos::RCP<const comm_type>& pComm,
278 if (pRowMap.is_null ()) {
279 return rcp (
new map_type (static_cast<global_size_t> (numRows),
280 static_cast<global_ordinal_type> (0),
281 pComm, GloballyDistributed));
284 TEUCHOS_TEST_FOR_EXCEPTION
285 (! pRowMap->isDistributed () && pComm->getSize () > 1,
286 std::invalid_argument,
"The specified row map is not distributed, "
287 "but the given communicator includes more than one process (in "
288 "fact, there are " << pComm->getSize () <<
" processes).");
289 TEUCHOS_TEST_FOR_EXCEPTION
290 (pRowMap->getComm () != pComm, std::invalid_argument,
291 "The specified row Map's communicator (pRowMap->getComm()) "
292 "differs from the given separately supplied communicator pComm.");
311 static Teuchos::RCP<const map_type>
312 makeDomainMap (
const Teuchos::RCP<const map_type>& pRangeMap,
321 if (numRows == numCols) {
324 return createUniformContigMapWithNode<LO,GO,NT> (numCols,
325 pRangeMap->getComm ());
402 distribute (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
403 Teuchos::ArrayRCP<size_t>& myRowPtr,
404 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
405 Teuchos::ArrayRCP<scalar_type>& myValues,
406 const Teuchos::RCP<const map_type>& pRowMap,
407 Teuchos::ArrayRCP<size_t>& numEntriesPerRow,
408 Teuchos::ArrayRCP<size_t>& rowPtr,
409 Teuchos::ArrayRCP<global_ordinal_type>& colInd,
410 Teuchos::ArrayRCP<scalar_type>& values,
411 const bool debug=
false)
414 using Teuchos::ArrayRCP;
415 using Teuchos::ArrayView;
418 using Teuchos::CommRequest;
421 using Teuchos::receive;
426 const bool extraDebug =
false;
427 RCP<const comm_type> pComm = pRowMap->getComm ();
428 const int numProcs = pComm->getSize ();
429 const int myRank = pComm->getRank ();
430 const int rootRank = 0;
437 ArrayView<const GO> myRows = pRowMap->getNodeElementList();
438 const size_type myNumRows = myRows.size();
439 TEUCHOS_TEST_FOR_EXCEPTION(static_cast<size_t>(myNumRows) !=
440 pRowMap->getNodeNumElements(),
442 "pRowMap->getNodeElementList().size() = "
444 <<
" != pRowMap->getNodeNumElements() = "
445 << pRowMap->getNodeNumElements() <<
". "
446 "Please report this bug to the Tpetra developers.");
447 TEUCHOS_TEST_FOR_EXCEPTION(myRank == 0 && numEntriesPerRow.size() < myNumRows,
449 "On Proc 0: numEntriesPerRow.size() = "
450 << numEntriesPerRow.size()
451 <<
" != pRowMap->getNodeElementList().size() = "
452 << myNumRows <<
". Please report this bug to the "
453 "Tpetra developers.");
457 myNumEntriesPerRow = arcp<size_t> (myNumRows);
459 if (myRank != rootRank) {
463 send (*pComm, myNumRows, rootRank);
464 if (myNumRows != 0) {
468 send (*pComm, static_cast<int> (myNumRows),
469 myRows.getRawPtr(), rootRank);
479 receive (*pComm, rootRank,
480 static_cast<int> (myNumRows),
481 myNumEntriesPerRow.getRawPtr());
486 std::accumulate (myNumEntriesPerRow.begin(),
487 myNumEntriesPerRow.end(), 0);
493 myColInd = arcp<GO> (myNumEntries);
494 myValues = arcp<scalar_type> (myNumEntries);
495 if (myNumEntries > 0) {
498 receive (*pComm, rootRank,
499 static_cast<int> (myNumEntries),
500 myColInd.getRawPtr());
501 receive (*pComm, rootRank,
502 static_cast<int> (myNumEntries),
503 myValues.getRawPtr());
509 cerr <<
"-- Proc 0: Copying my data from global arrays" << endl;
513 for (size_type k = 0; k < myNumRows; ++k) {
514 const GO myCurRow = myRows[k];
516 myNumEntriesPerRow[k] = numEntriesInThisRow;
518 if (extraDebug && debug) {
519 cerr <<
"Proc " << pRowMap->getComm ()->getRank ()
520 <<
": myNumEntriesPerRow[0.." << (myNumRows-1) <<
"] = [";
521 for (size_type k = 0; k < myNumRows; ++k) {
522 cerr << myNumEntriesPerRow[k];
523 if (k < myNumRows-1) {
531 std::accumulate (myNumEntriesPerRow.begin(),
532 myNumEntriesPerRow.end(), 0);
534 cerr <<
"-- Proc 0: I own " << myNumRows <<
" rows and "
535 << myNumEntries <<
" entries" << endl;
537 myColInd = arcp<GO> (myNumEntries);
538 myValues = arcp<scalar_type> (myNumEntries);
546 for (size_type k = 0; k < myNumRows;
547 myCurPos += myNumEntriesPerRow[k], ++k) {
549 const GO myRow = myRows[k];
550 const size_t curPos = rowPtr[myRow];
553 if (curNumEntries > 0) {
554 ArrayView<GO> colIndView = colInd (curPos, curNumEntries);
555 ArrayView<GO> myColIndView = myColInd (myCurPos, curNumEntries);
556 std::copy (colIndView.begin(), colIndView.end(),
557 myColIndView.begin());
559 ArrayView<scalar_type> valuesView =
560 values (curPos, curNumEntries);
561 ArrayView<scalar_type> myValuesView =
562 myValues (myCurPos, curNumEntries);
563 std::copy (valuesView.begin(), valuesView.end(),
564 myValuesView.begin());
569 for (
int p = 1; p < numProcs; ++p) {
571 cerr <<
"-- Proc 0: Processing proc " << p << endl;
574 size_type theirNumRows = 0;
579 receive (*pComm, p, &theirNumRows);
581 cerr <<
"-- Proc 0: Proc " << p <<
" owns "
582 << theirNumRows <<
" rows" << endl;
584 if (theirNumRows != 0) {
589 ArrayRCP<GO> theirRows = arcp<GO> (theirNumRows);
590 receive (*pComm, p, as<int> (theirNumRows),
591 theirRows.getRawPtr ());
600 const global_size_t numRows = pRowMap->getGlobalNumElements ();
601 const GO indexBase = pRowMap->getIndexBase ();
602 bool theirRowsValid =
true;
603 for (size_type k = 0; k < theirNumRows; ++k) {
604 if (theirRows[k] < indexBase ||
605 as<global_size_t> (theirRows[k] - indexBase) >= numRows) {
606 theirRowsValid =
false;
609 if (! theirRowsValid) {
610 TEUCHOS_TEST_FOR_EXCEPTION(
611 ! theirRowsValid, std::logic_error,
612 "Proc " << p <<
" has at least one invalid row index. "
613 "Here are all of them: " <<
614 Teuchos::toString (theirRows ()) <<
". Valid row index "
615 "range (zero-based): [0, " << (numRows - 1) <<
"].");
630 ArrayRCP<size_t> theirNumEntriesPerRow;
631 theirNumEntriesPerRow = arcp<size_t> (theirNumRows);
632 for (size_type k = 0; k < theirNumRows; ++k) {
633 theirNumEntriesPerRow[k] = numEntriesPerRow[theirRows[k]];
640 send (*pComm, static_cast<int> (theirNumRows),
641 theirNumEntriesPerRow.getRawPtr(), p);
645 std::accumulate (theirNumEntriesPerRow.begin(),
646 theirNumEntriesPerRow.end(), 0);
649 cerr <<
"-- Proc 0: Proc " << p <<
" owns "
650 << theirNumEntries <<
" entries" << endl;
655 if (theirNumEntries == 0) {
664 ArrayRCP<GO> theirColInd (theirNumEntries);
665 ArrayRCP<scalar_type> theirValues (theirNumEntries);
673 for (size_type k = 0; k < theirNumRows;
674 theirCurPos += theirNumEntriesPerRow[k], k++) {
676 const GO theirRow = theirRows[k];
682 if (curNumEntries > 0) {
683 ArrayView<GO> colIndView =
684 colInd (curPos, curNumEntries);
685 ArrayView<GO> theirColIndView =
686 theirColInd (theirCurPos, curNumEntries);
687 std::copy (colIndView.begin(), colIndView.end(),
688 theirColIndView.begin());
690 ArrayView<scalar_type> valuesView =
691 values (curPos, curNumEntries);
692 ArrayView<scalar_type> theirValuesView =
693 theirValues (theirCurPos, curNumEntries);
694 std::copy (valuesView.begin(), valuesView.end(),
695 theirValuesView.begin());
702 send (*pComm, static_cast<int> (theirNumEntries),
703 theirColInd.getRawPtr(), p);
704 send (*pComm, static_cast<int> (theirNumEntries),
705 theirValues.getRawPtr(), p);
708 cerr <<
"-- Proc 0: Finished with proc " << p << endl;
716 numEntriesPerRow = null;
721 if (debug && myRank == 0) {
722 cerr <<
"-- Proc 0: About to fill in myRowPtr" << endl;
730 myRowPtr = arcp<size_t> (myNumRows+1);
732 for (size_type k = 1; k < myNumRows+1; ++k) {
733 myRowPtr[k] = myRowPtr[k-1] + myNumEntriesPerRow[k-1];
735 if (extraDebug && debug) {
736 cerr <<
"Proc " << Teuchos::rank (*(pRowMap->getComm()))
737 <<
": myRowPtr[0.." << myNumRows <<
"] = [";
738 for (size_type k = 0; k < myNumRows+1; ++k) {
744 cerr <<
"]" << endl << endl;
747 if (debug && myRank == 0) {
748 cerr <<
"-- Proc 0: Done with distribute" << endl;
765 static Teuchos::RCP<sparse_matrix_type>
766 makeMatrix (Teuchos::ArrayRCP<size_t>& myNumEntriesPerRow,
767 Teuchos::ArrayRCP<size_t>& myRowPtr,
768 Teuchos::ArrayRCP<global_ordinal_type>& myColInd,
769 Teuchos::ArrayRCP<scalar_type>& myValues,
770 const Teuchos::RCP<const map_type>& pRowMap,
771 const Teuchos::RCP<const map_type>& pRangeMap,
772 const Teuchos::RCP<const map_type>& pDomainMap,
773 const bool callFillComplete =
true)
775 using Teuchos::ArrayView;
789 TEUCHOS_TEST_FOR_EXCEPTION(myRowPtr.is_null(), std::logic_error,
790 "makeMatrix: myRowPtr array is null. "
791 "Please report this bug to the Tpetra developers.");
792 TEUCHOS_TEST_FOR_EXCEPTION(pDomainMap.is_null(), std::logic_error,
793 "makeMatrix: domain map is null. "
794 "Please report this bug to the Tpetra developers.");
795 TEUCHOS_TEST_FOR_EXCEPTION(pRangeMap.is_null(), std::logic_error,
796 "makeMatrix: range map is null. "
797 "Please report this bug to the Tpetra developers.");
798 TEUCHOS_TEST_FOR_EXCEPTION(pRowMap.is_null(), std::logic_error,
799 "makeMatrix: row map is null. "
800 "Please report this bug to the Tpetra developers.");
804 RCP<sparse_matrix_type> A =
810 ArrayView<const GO> myRows = pRowMap->getNodeElementList ();
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 =
896 StaticProfile, constructorParams));
900 ArrayView<const GO> myRows = pRowMap->getNodeElementList();
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->getNodeElementList ();
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) {
1049 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1050 "Matrix Market banner line contains syntax error(s): "
1053 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->objectType() !=
"matrix",
1054 std::invalid_argument,
"The Matrix Market file does not contain "
1055 "matrix data. Its Banner (first) line says that its object type is \""
1056 << pBanner->matrixType() <<
"\", rather than the required \"matrix\".");
1060 TEUCHOS_TEST_FOR_EXCEPTION(
1061 ! STS::isComplex && pBanner->dataType() ==
"complex",
1062 std::invalid_argument,
1063 "The Matrix Market file contains complex-valued data, but you are "
1064 "trying to read it into a matrix containing entries of the real-"
1065 "valued Scalar type \""
1066 << Teuchos::TypeNameTraits<scalar_type>::name() <<
"\".");
1067 TEUCHOS_TEST_FOR_EXCEPTION(
1069 pBanner->dataType() !=
"real" &&
1070 pBanner->dataType() !=
"complex" &&
1071 pBanner->dataType() !=
"integer",
1072 std::invalid_argument,
1073 "When reading Matrix Market data into a Tpetra::CrsMatrix, the "
1074 "Matrix Market file may not contain a \"pattern\" matrix. A "
1075 "pattern matrix is really just a graph with no weights. It "
1076 "should be stored in a CrsGraph, not a CrsMatrix.");
1078 TEUCHOS_TEST_FOR_EXCEPTION(
1080 pBanner->dataType() !=
"pattern",
1081 std::invalid_argument,
1082 "When reading Matrix Market data into a Tpetra::CrsGraph, the "
1083 "Matrix Market file must contain a \"pattern\" matrix.");
1110 static Teuchos::Tuple<global_ordinal_type, 3>
1111 readCoordDims (std::istream& in,
1113 const Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1114 const Teuchos::RCP<const comm_type>& pComm,
1115 const bool tolerant =
false,
1118 using Teuchos::MatrixMarket::readCoordinateDimensions;
1119 using Teuchos::Tuple;
1124 Tuple<global_ordinal_type, 3> dims;
1130 bool success =
false;
1131 if (pComm->getRank() == 0) {
1132 TEUCHOS_TEST_FOR_EXCEPTION(pBanner->matrixType() !=
"coordinate",
1133 std::invalid_argument,
"The Tpetra::CrsMatrix Matrix Market reader "
1134 "only accepts \"coordinate\" (sparse) matrix data.");
1138 success = readCoordinateDimensions (in, numRows, numCols,
1139 numNonzeros, lineNumber,
1145 dims[2] = numNonzeros;
1153 int the_success = success ? 1 : 0;
1154 Teuchos::broadcast (*pComm, 0, &the_success);
1155 success = (the_success == 1);
1160 Teuchos::broadcast (*pComm, 0, dims);
1168 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
1169 "Error reading Matrix Market sparse matrix: failed to read "
1170 "coordinate matrix dimensions.");
1185 typedef Teuchos::MatrixMarket::SymmetrizingAdder<Teuchos::MatrixMarket::Raw::Adder<scalar_type, global_ordinal_type> > adder_type;
1187 typedef Teuchos::MatrixMarket::SymmetrizingGraphAdder<Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> > graph_adder_type;
1214 static Teuchos::RCP<adder_type>
1215 makeAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1216 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1217 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1218 const bool tolerant=
false,
1219 const bool debug=
false)
1221 if (pComm->getRank () == 0) {
1222 typedef Teuchos::MatrixMarket::Raw::Adder<
scalar_type,
1225 Teuchos::RCP<raw_adder_type> pRaw =
1226 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1228 return Teuchos::rcp (
new adder_type (pRaw, pBanner->symmType ()));
1231 return Teuchos::null;
1260 static Teuchos::RCP<graph_adder_type>
1261 makeGraphAdder (
const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1262 Teuchos::RCP<const Teuchos::MatrixMarket::Banner>& pBanner,
1263 const Teuchos::Tuple<global_ordinal_type, 3>& dims,
1264 const bool tolerant=
false,
1265 const bool debug=
false)
1267 if (pComm->getRank () == 0) {
1268 typedef Teuchos::MatrixMarket::Raw::GraphAdder<global_ordinal_type> raw_adder_type;
1269 Teuchos::RCP<raw_adder_type> pRaw =
1270 Teuchos::rcp (
new raw_adder_type (dims[0], dims[1], dims[2],
1272 return Teuchos::rcp (
new graph_adder_type (pRaw, pBanner->symmType ()));
1275 return Teuchos::null;
1280 static Teuchos::RCP<sparse_graph_type>
1281 readSparseGraphHelper (std::istream& in,
1282 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1283 const Teuchos::RCP<const map_type>& rowMap,
1284 Teuchos::RCP<const map_type>& colMap,
1285 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1286 const bool tolerant,
1289 using Teuchos::MatrixMarket::Banner;
1292 using Teuchos::Tuple;
1296 const int myRank = pComm->getRank ();
1297 const int rootRank = 0;
1302 size_t lineNumber = 1;
1304 if (debug && myRank == rootRank) {
1305 cerr <<
"Matrix Market reader: readGraph:" << endl
1306 <<
"-- Reading banner line" << endl;
1315 RCP<const Banner> pBanner;
1321 int bannerIsCorrect = 1;
1322 std::ostringstream errMsg;
1324 if (myRank == rootRank) {
1327 pBanner = readBanner (in, lineNumber, tolerant, debug,
true);
1329 catch (std::exception& e) {
1330 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
1331 "threw an exception: " << e.what();
1332 bannerIsCorrect = 0;
1335 if (bannerIsCorrect) {
1340 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
1341 bannerIsCorrect = 0;
1342 errMsg <<
"The Matrix Market input file must contain a "
1343 "\"coordinate\"-format sparse graph in order to create a "
1344 "Tpetra::CrsGraph object from it, but the file's matrix "
1345 "type is \"" << pBanner->matrixType() <<
"\" instead.";
1350 if (tolerant && pBanner->matrixType() ==
"array") {
1351 bannerIsCorrect = 0;
1352 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
1353 "format sparse graph in order to create a Tpetra::CrsGraph "
1354 "object from it, but the file's matrix type is \"array\" "
1355 "instead. That probably means the file contains dense matrix "
1362 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
1369 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
1370 std::invalid_argument, errMsg.str ());
1372 if (debug && myRank == rootRank) {
1373 cerr <<
"-- Reading dimensions line" << endl;
1381 Tuple<global_ordinal_type, 3> dims =
1382 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
1384 if (debug && myRank == rootRank) {
1385 cerr <<
"-- Making Adder for collecting graph data" << endl;
1392 RCP<graph_adder_type> pAdder =
1393 makeGraphAdder (pComm, pBanner, dims, tolerant, debug);
1395 if (debug && myRank == rootRank) {
1396 cerr <<
"-- Reading graph data" << endl;
1406 int readSuccess = 1;
1407 std::ostringstream errMsg;
1408 if (myRank == rootRank) {
1411 typedef Teuchos::MatrixMarket::CoordPatternReader<graph_adder_type,
1413 reader_type reader (pAdder);
1416 std::pair<bool, std::vector<size_t> > results =
1417 reader.read (in, lineNumber, tolerant, debug);
1418 readSuccess = results.first ? 1 : 0;
1420 catch (std::exception& e) {
1425 broadcast (*pComm, rootRank, ptr (&readSuccess));
1434 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
1435 "Failed to read the Matrix Market sparse graph file: "
1439 if (debug && myRank == rootRank) {
1440 cerr <<
"-- Successfully read the Matrix Market data" << endl;
1453 if (debug && myRank == rootRank) {
1454 cerr <<
"-- Tolerant mode: rebroadcasting graph dimensions"
1456 <<
"----- Dimensions before: "
1457 << dims[0] <<
" x " << dims[1]
1461 Tuple<global_ordinal_type, 2> updatedDims;
1462 if (myRank == rootRank) {
1469 std::max (dims[0], pAdder->getAdder()->numRows());
1470 updatedDims[1] = pAdder->getAdder()->numCols();
1472 broadcast (*pComm, rootRank, updatedDims);
1473 dims[0] = updatedDims[0];
1474 dims[1] = updatedDims[1];
1475 if (debug && myRank == rootRank) {
1476 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
1490 if (myRank == rootRank) {
1497 if (dims[0] < pAdder->getAdder ()->numRows ()) {
1501 broadcast (*pComm, 0, ptr (&dimsMatch));
1502 if (dimsMatch == 0) {
1509 Tuple<global_ordinal_type, 2> addersDims;
1510 if (myRank == rootRank) {
1511 addersDims[0] = pAdder->getAdder()->numRows();
1512 addersDims[1] = pAdder->getAdder()->numCols();
1514 broadcast (*pComm, 0, addersDims);
1515 TEUCHOS_TEST_FOR_EXCEPTION(
1516 dimsMatch == 0, std::runtime_error,
1517 "The graph metadata says that the graph is " << dims[0] <<
" x "
1518 << dims[1] <<
", but the actual data says that the graph is "
1519 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
1520 "data includes more rows than reported in the metadata. This "
1521 "is not allowed when parsing in strict mode. Parse the graph in "
1522 "tolerant mode to ignore the metadata when it disagrees with the "
1528 RCP<map_type> proc0Map;
1530 if(Teuchos::is_null(rowMap)) {
1534 indexBase = rowMap->getIndexBase();
1536 if(myRank == rootRank) {
1537 proc0Map = rcp(
new map_type(dims[0],dims[0],indexBase,pComm));
1540 proc0Map = rcp(
new map_type(dims[0],0,indexBase,pComm));
1544 std::map<global_ordinal_type, size_t> numEntriesPerRow_map;
1545 if (myRank == rootRank) {
1546 const auto& entries = pAdder()->getAdder()->getEntries();
1551 for (
const auto& entry : entries) {
1553 ++numEntriesPerRow_map[gblRow];
1557 Teuchos::Array<size_t> numEntriesPerRow (proc0Map->getNodeNumElements ());
1558 for (
const auto& ent : numEntriesPerRow_map) {
1560 numEntriesPerRow[lclRow] = ent.second;
1567 std::map<global_ordinal_type, size_t> empty_map;
1568 std::swap (numEntriesPerRow_map, empty_map);
1571 RCP<sparse_graph_type> proc0Graph =
1573 StaticProfile,constructorParams));
1574 if(myRank == rootRank) {
1575 typedef Teuchos::MatrixMarket::Raw::GraphElement<global_ordinal_type> element_type;
1578 const std::vector<element_type>& entries =
1579 pAdder->getAdder()->getEntries();
1582 for(
size_t curPos=0; curPos<entries.size(); curPos++) {
1583 const element_type& curEntry = entries[curPos];
1586 Teuchos::ArrayView<const global_ordinal_type> colView(&curCol,1);
1587 proc0Graph->insertGlobalIndices(curRow,colView);
1590 proc0Graph->fillComplete();
1592 RCP<sparse_graph_type> distGraph;
1593 if(Teuchos::is_null(rowMap))
1596 RCP<map_type> distMap =
1597 rcp(
new map_type(dims[0],0,pComm,GloballyDistributed));
1600 distGraph = rcp(
new sparse_graph_type(distMap,colMap,0,StaticProfile,constructorParams));
1603 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1604 import_type importer (proc0Map, distMap);
1607 distGraph->doImport(*proc0Graph,importer,
INSERT);
1610 distGraph = rcp(
new sparse_graph_type(rowMap,colMap,0,StaticProfile,constructorParams));
1613 typedef Import<local_ordinal_type, global_ordinal_type, node_type> import_type;
1614 import_type importer (proc0Map, rowMap);
1617 distGraph->doImport(*proc0Graph,importer,
INSERT);
1647 static Teuchos::RCP<sparse_graph_type>
1649 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1650 const bool callFillComplete=
true,
1651 const bool tolerant=
false,
1652 const bool debug=
false)
1654 using Teuchos::broadcast;
1655 using Teuchos::outArg;
1663 if (comm->getRank () == 0) {
1665 in.open (filename.c_str ());
1672 broadcast<int, int> (*comm, 0, outArg (opened));
1673 TEUCHOS_TEST_FOR_EXCEPTION
1674 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1675 "Failed to open file \"" << filename <<
"\" on Process 0.");
1684 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
1685 static Teuchos::RCP<sparse_graph_type> TPETRA_DEPRECATED
1693 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
1694 const Teuchos::RCP<node_type>& ,
1695 const bool callFillComplete=
true,
1696 const bool tolerant=
false,
1697 const bool debug=
false)
1701 callFillComplete, tolerant, debug);
1703 #endif // TPETRA_ENABLE_DEPRECATED_CODE
1733 static Teuchos::RCP<sparse_graph_type>
1735 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1736 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1737 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1738 const bool tolerant=
false,
1739 const bool debug=
false)
1741 using Teuchos::broadcast;
1742 using Teuchos::outArg;
1750 if (pComm->getRank () == 0) {
1752 in.open (filename.c_str ());
1759 broadcast<int, int> (*pComm, 0, outArg (opened));
1760 TEUCHOS_TEST_FOR_EXCEPTION
1761 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1762 "Failed to open file \"" << filename <<
"\" on Process 0.");
1763 if (pComm->getRank () == 0) {
1764 in.open (filename.c_str ());
1768 fillCompleteParams, tolerant, debug);
1774 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
1775 static Teuchos::RCP<sparse_graph_type> TPETRA_DEPRECATED
1783 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1784 const Teuchos::RCP<node_type>& ,
1785 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1786 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1787 const bool tolerant=
false,
1788 const bool debug=
false)
1792 constructorParams, fillCompleteParams,
1795 #endif // TPETRA_ENABLE_DEPRECATED_CODE
1834 static Teuchos::RCP<sparse_graph_type>
1836 const Teuchos::RCP<const map_type>& rowMap,
1837 Teuchos::RCP<const map_type>& colMap,
1838 const Teuchos::RCP<const map_type>& domainMap,
1839 const Teuchos::RCP<const map_type>& rangeMap,
1840 const bool callFillComplete=
true,
1841 const bool tolerant=
false,
1842 const bool debug=
false)
1844 using Teuchos::broadcast;
1845 using Teuchos::Comm;
1846 using Teuchos::outArg;
1849 TEUCHOS_TEST_FOR_EXCEPTION
1850 (rowMap.is_null (), std::invalid_argument,
1851 "Input rowMap must be nonnull.");
1852 RCP<const Comm<int> > comm = rowMap->getComm ();
1853 if (comm.is_null ()) {
1856 return Teuchos::null;
1865 if (comm->getRank () == 0) {
1867 in.open (filename.c_str ());
1874 broadcast<int, int> (*comm, 0, outArg (opened));
1875 TEUCHOS_TEST_FOR_EXCEPTION
1876 (opened == 0, std::runtime_error,
"readSparseGraphFile: "
1877 "Failed to open file \"" << filename <<
"\" on Process 0.");
1879 callFillComplete, tolerant, debug);
1907 static Teuchos::RCP<sparse_graph_type>
1909 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1910 const bool callFillComplete=
true,
1911 const bool tolerant=
false,
1912 const bool debug=
false)
1914 Teuchos::RCP<const map_type> fakeRowMap;
1915 Teuchos::RCP<const map_type> fakeColMap;
1916 Teuchos::RCP<Teuchos::ParameterList> fakeCtorParams;
1918 Teuchos::RCP<sparse_graph_type> graph =
1919 readSparseGraphHelper (in, pComm,
1920 fakeRowMap, fakeColMap,
1921 fakeCtorParams, tolerant, debug);
1922 if (callFillComplete) {
1923 graph->fillComplete ();
1928 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
1929 static Teuchos::RCP<sparse_graph_type> TPETRA_DEPRECATED
1932 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1933 const Teuchos::RCP<node_type>& ,
1934 const bool callFillComplete=
true,
1935 const bool tolerant=
false,
1936 const bool debug=
false)
1942 #endif // TPETRA_ENABLE_DEPRECATED_CODE
1973 static Teuchos::RCP<sparse_graph_type>
1975 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1976 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1977 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1978 const bool tolerant=
false,
1979 const bool debug=
false)
1981 Teuchos::RCP<const map_type> fakeRowMap;
1982 Teuchos::RCP<const map_type> fakeColMap;
1983 Teuchos::RCP<sparse_graph_type> graph =
1984 readSparseGraphHelper (in, pComm,
1985 fakeRowMap, fakeColMap,
1986 constructorParams, tolerant, debug);
1987 graph->fillComplete (fillCompleteParams);
1991 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
1992 static Teuchos::RCP<sparse_graph_type> TPETRA_DEPRECATED
1995 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
1996 const Teuchos::RCP<node_type>& ,
1997 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
1998 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
1999 const bool tolerant=
false,
2000 const bool debug=
false)
2004 fillCompleteParams, tolerant, debug);
2006 #endif // TPETRA_ENABLE_DEPRECATED_CODE
2048 static Teuchos::RCP<sparse_graph_type>
2050 const Teuchos::RCP<const map_type>& rowMap,
2051 Teuchos::RCP<const map_type>& colMap,
2052 const Teuchos::RCP<const map_type>& domainMap,
2053 const Teuchos::RCP<const map_type>& rangeMap,
2054 const bool callFillComplete=
true,
2055 const bool tolerant=
false,
2056 const bool debug=
false)
2058 Teuchos::RCP<sparse_graph_type> graph =
2059 readSparseGraphHelper (in, rowMap->getComm (),
2060 rowMap, colMap, Teuchos::null, tolerant,
2062 if (callFillComplete) {
2063 graph->fillComplete (domainMap, rangeMap);
2091 static Teuchos::RCP<sparse_matrix_type>
2093 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2094 const bool callFillComplete=
true,
2095 const bool tolerant=
false,
2096 const bool debug=
false)
2098 const int myRank = pComm->getRank ();
2103 in.open (filename.c_str ());
2111 return readSparse (in, pComm, callFillComplete, tolerant, debug);
2117 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
2118 static Teuchos::RCP<sparse_matrix_type> TPETRA_DEPRECATED
2121 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2122 const Teuchos::RCP<node_type>& ,
2123 const bool callFillComplete=
true,
2124 const bool tolerant=
false,
2125 const bool debug=
false)
2127 return readSparseFile (filename, pComm, callFillComplete, tolerant, debug);
2129 #endif // TPETRA_ENABLE_DEPRECATED_CODE
2159 static Teuchos::RCP<sparse_matrix_type>
2161 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2162 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
2163 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
2164 const bool tolerant=
false,
2165 const bool debug=
false)
2168 if (pComm->getRank () == 0) {
2169 in.open (filename.c_str ());
2171 return readSparse (in, pComm, constructorParams,
2172 fillCompleteParams, tolerant, debug);
2175 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
2176 static Teuchos::RCP<sparse_matrix_type> TPETRA_DEPRECATED
2179 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2180 const Teuchos::RCP<node_type>& ,
2181 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
2182 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
2183 const bool tolerant=
false,
2184 const bool debug=
false)
2187 constructorParams, fillCompleteParams,
2190 #endif // TPETRA_ENABLE_DEPRECATED_CODE
2229 static Teuchos::RCP<sparse_matrix_type>
2231 const Teuchos::RCP<const map_type>& rowMap,
2232 Teuchos::RCP<const map_type>& colMap,
2233 const Teuchos::RCP<const map_type>& domainMap,
2234 const Teuchos::RCP<const map_type>& rangeMap,
2235 const bool callFillComplete=
true,
2236 const bool tolerant=
false,
2237 const bool debug=
false)
2239 using Teuchos::broadcast;
2240 using Teuchos::Comm;
2241 using Teuchos::outArg;
2244 TEUCHOS_TEST_FOR_EXCEPTION(
2245 rowMap.is_null (), std::invalid_argument,
2246 "Row Map must be nonnull.");
2248 RCP<const Comm<int> > comm = rowMap->getComm ();
2249 const int myRank = comm->getRank ();
2259 in.open (filename.c_str ());
2266 broadcast<int, int> (*comm, 0, outArg (opened));
2267 TEUCHOS_TEST_FOR_EXCEPTION(
2268 opened == 0, std::runtime_error,
2269 "readSparseFile: Failed to open file \"" << filename <<
"\" on "
2271 return readSparse (in, rowMap, colMap, domainMap, rangeMap,
2272 callFillComplete, tolerant, debug);
2300 static Teuchos::RCP<sparse_matrix_type>
2302 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2303 const bool callFillComplete=
true,
2304 const bool tolerant=
false,
2305 const bool debug=
false)
2307 using Teuchos::MatrixMarket::Banner;
2308 using Teuchos::arcp;
2309 using Teuchos::ArrayRCP;
2310 using Teuchos::broadcast;
2311 using Teuchos::null;
2314 using Teuchos::REDUCE_MAX;
2315 using Teuchos::reduceAll;
2316 using Teuchos::Tuple;
2319 typedef Teuchos::ScalarTraits<scalar_type> STS;
2321 const bool extraDebug =
false;
2322 const int myRank = pComm->getRank ();
2323 const int rootRank = 0;
2328 size_t lineNumber = 1;
2330 if (debug && myRank == rootRank) {
2331 cerr <<
"Matrix Market reader: readSparse:" << endl
2332 <<
"-- Reading banner line" << endl;
2341 RCP<const Banner> pBanner;
2347 int bannerIsCorrect = 1;
2348 std::ostringstream errMsg;
2350 if (myRank == rootRank) {
2353 pBanner = readBanner (in, lineNumber, tolerant, debug);
2355 catch (std::exception& e) {
2356 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2357 "threw an exception: " << e.what();
2358 bannerIsCorrect = 0;
2361 if (bannerIsCorrect) {
2366 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
2367 bannerIsCorrect = 0;
2368 errMsg <<
"The Matrix Market input file must contain a "
2369 "\"coordinate\"-format sparse matrix in order to create a "
2370 "Tpetra::CrsMatrix object from it, but the file's matrix "
2371 "type is \"" << pBanner->matrixType() <<
"\" instead.";
2376 if (tolerant && pBanner->matrixType() ==
"array") {
2377 bannerIsCorrect = 0;
2378 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2379 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2380 "object from it, but the file's matrix type is \"array\" "
2381 "instead. That probably means the file contains dense matrix "
2388 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
2395 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
2396 std::invalid_argument, errMsg.str ());
2398 if (debug && myRank == rootRank) {
2399 cerr <<
"-- Reading dimensions line" << endl;
2407 Tuple<global_ordinal_type, 3> dims =
2408 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
2410 if (debug && myRank == rootRank) {
2411 cerr <<
"-- Making Adder for collecting matrix data" << endl;
2416 RCP<adder_type> pAdder =
2417 makeAdder (pComm, pBanner, dims, tolerant, debug);
2419 if (debug && myRank == rootRank) {
2420 cerr <<
"-- Reading matrix data" << endl;
2430 int readSuccess = 1;
2431 std::ostringstream errMsg;
2432 if (myRank == rootRank) {
2435 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
2437 reader_type reader (pAdder);
2440 std::pair<bool, std::vector<size_t> > results =
2441 reader.read (in, lineNumber, tolerant, debug);
2442 readSuccess = results.first ? 1 : 0;
2444 catch (std::exception& e) {
2449 broadcast (*pComm, rootRank, ptr (&readSuccess));
2458 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
2459 "Failed to read the Matrix Market sparse matrix file: "
2463 if (debug && myRank == rootRank) {
2464 cerr <<
"-- Successfully read the Matrix Market data" << endl;
2477 if (debug && myRank == rootRank) {
2478 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
2480 <<
"----- Dimensions before: "
2481 << dims[0] <<
" x " << dims[1]
2485 Tuple<global_ordinal_type, 2> updatedDims;
2486 if (myRank == rootRank) {
2493 std::max (dims[0], pAdder->getAdder()->numRows());
2494 updatedDims[1] = pAdder->getAdder()->numCols();
2496 broadcast (*pComm, rootRank, updatedDims);
2497 dims[0] = updatedDims[0];
2498 dims[1] = updatedDims[1];
2499 if (debug && myRank == rootRank) {
2500 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
2514 if (myRank == rootRank) {
2521 if (dims[0] < pAdder->getAdder ()->numRows ()) {
2525 broadcast (*pComm, 0, ptr (&dimsMatch));
2526 if (dimsMatch == 0) {
2533 Tuple<global_ordinal_type, 2> addersDims;
2534 if (myRank == rootRank) {
2535 addersDims[0] = pAdder->getAdder()->numRows();
2536 addersDims[1] = pAdder->getAdder()->numCols();
2538 broadcast (*pComm, 0, addersDims);
2539 TEUCHOS_TEST_FOR_EXCEPTION(
2540 dimsMatch == 0, std::runtime_error,
2541 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
2542 << dims[1] <<
", but the actual data says that the matrix is "
2543 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
2544 "data includes more rows than reported in the metadata. This "
2545 "is not allowed when parsing in strict mode. Parse the matrix in "
2546 "tolerant mode to ignore the metadata when it disagrees with the "
2551 if (debug && myRank == rootRank) {
2552 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
2564 ArrayRCP<size_t> numEntriesPerRow;
2565 ArrayRCP<size_t> rowPtr;
2566 ArrayRCP<global_ordinal_type> colInd;
2567 ArrayRCP<scalar_type> values;
2572 int mergeAndConvertSucceeded = 1;
2573 std::ostringstream errMsg;
2575 if (myRank == rootRank) {
2577 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
2587 const size_type numRows = dims[0];
2590 pAdder->getAdder()->merge ();
2593 const std::vector<element_type>& entries =
2594 pAdder->getAdder()->getEntries();
2597 const size_t numEntries = (size_t)entries.size();
2600 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
2601 <<
" rows and numEntries=" << numEntries
2602 <<
" entries." << endl;
2608 numEntriesPerRow = arcp<size_t> (numRows);
2609 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
2610 rowPtr = arcp<size_t> (numRows+1);
2611 std::fill (rowPtr.begin(), rowPtr.end(), 0);
2612 colInd = arcp<global_ordinal_type> (numEntries);
2613 values = arcp<scalar_type> (numEntries);
2620 for (curPos = 0; curPos < numEntries; ++curPos) {
2621 const element_type& curEntry = entries[curPos];
2623 TEUCHOS_TEST_FOR_EXCEPTION(
2624 curRow < prvRow, std::logic_error,
2625 "Row indices are out of order, even though they are supposed "
2626 "to be sorted. curRow = " << curRow <<
", prvRow = "
2627 << prvRow <<
", at curPos = " << curPos <<
". Please report "
2628 "this bug to the Tpetra developers.");
2629 if (curRow > prvRow) {
2635 numEntriesPerRow[curRow]++;
2636 colInd[curPos] = curEntry.colIndex();
2637 values[curPos] = curEntry.value();
2642 rowPtr[numRows] = numEntries;
2644 catch (std::exception& e) {
2645 mergeAndConvertSucceeded = 0;
2646 errMsg <<
"Failed to merge sparse matrix entries and convert to "
2647 "CSR format: " << e.what();
2650 if (debug && mergeAndConvertSucceeded) {
2652 const size_type numRows = dims[0];
2653 const size_type maxToDisplay = 100;
2655 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
2656 << (numEntriesPerRow.size()-1) <<
"] ";
2657 if (numRows > maxToDisplay) {
2658 cerr <<
"(only showing first and last few entries) ";
2662 if (numRows > maxToDisplay) {
2663 for (size_type k = 0; k < 2; ++k) {
2664 cerr << numEntriesPerRow[k] <<
" ";
2667 for (size_type k = numRows-2; k < numRows-1; ++k) {
2668 cerr << numEntriesPerRow[k] <<
" ";
2672 for (size_type k = 0; k < numRows-1; ++k) {
2673 cerr << numEntriesPerRow[k] <<
" ";
2676 cerr << numEntriesPerRow[numRows-1];
2678 cerr <<
"]" << endl;
2680 cerr <<
"----- Proc 0: rowPtr ";
2681 if (numRows > maxToDisplay) {
2682 cerr <<
"(only showing first and last few entries) ";
2685 if (numRows > maxToDisplay) {
2686 for (size_type k = 0; k < 2; ++k) {
2687 cerr << rowPtr[k] <<
" ";
2690 for (size_type k = numRows-2; k < numRows; ++k) {
2691 cerr << rowPtr[k] <<
" ";
2695 for (size_type k = 0; k < numRows; ++k) {
2696 cerr << rowPtr[k] <<
" ";
2699 cerr << rowPtr[numRows] <<
"]" << endl;
2710 if (debug && myRank == rootRank) {
2711 cerr <<
"-- Making range, domain, and row maps" << endl;
2718 RCP<const map_type> pRangeMap = makeRangeMap (pComm, dims[0]);
2719 RCP<const map_type> pDomainMap =
2720 makeDomainMap (pRangeMap, dims[0], dims[1]);
2721 RCP<const map_type> pRowMap = makeRowMap (null, pComm, dims[0]);
2723 if (debug && myRank == rootRank) {
2724 cerr <<
"-- Distributing the matrix data" << endl;
2738 ArrayRCP<size_t> myNumEntriesPerRow;
2739 ArrayRCP<size_t> myRowPtr;
2740 ArrayRCP<global_ordinal_type> myColInd;
2741 ArrayRCP<scalar_type> myValues;
2743 distribute (myNumEntriesPerRow, myRowPtr, myColInd, myValues, pRowMap,
2744 numEntriesPerRow, rowPtr, colInd, values, debug);
2746 if (debug && myRank == rootRank) {
2747 cerr <<
"-- Inserting matrix entries on each processor";
2748 if (callFillComplete) {
2749 cerr <<
" and calling fillComplete()";
2760 RCP<sparse_matrix_type> pMatrix =
2761 makeMatrix (myNumEntriesPerRow, myRowPtr, myColInd, myValues,
2762 pRowMap, pRangeMap, pDomainMap, callFillComplete);
2767 int localIsNull = pMatrix.is_null () ? 1 : 0;
2768 int globalIsNull = 0;
2769 reduceAll (*pComm, REDUCE_MAX, localIsNull, ptr (&globalIsNull));
2770 TEUCHOS_TEST_FOR_EXCEPTION(globalIsNull != 0, std::logic_error,
2771 "Reader::makeMatrix() returned a null pointer on at least one "
2772 "process. Please report this bug to the Tpetra developers.");
2775 TEUCHOS_TEST_FOR_EXCEPTION(pMatrix.is_null(), std::logic_error,
2776 "Reader::makeMatrix() returned a null pointer. "
2777 "Please report this bug to the Tpetra developers.");
2789 if (callFillComplete) {
2790 const int numProcs = pComm->getSize ();
2792 if (extraDebug && debug) {
2794 pRangeMap->getGlobalNumElements ();
2796 pDomainMap->getGlobalNumElements ();
2797 if (myRank == rootRank) {
2798 cerr <<
"-- Matrix is "
2799 << globalNumRows <<
" x " << globalNumCols
2800 <<
" with " << pMatrix->getGlobalNumEntries()
2801 <<
" entries, and index base "
2802 << pMatrix->getIndexBase() <<
"." << endl;
2805 for (
int p = 0; p < numProcs; ++p) {
2807 cerr <<
"-- Proc " << p <<
" owns "
2808 << pMatrix->getNodeNumCols() <<
" columns, and "
2809 << pMatrix->getNodeNumEntries() <<
" entries." << endl;
2816 if (debug && myRank == rootRank) {
2817 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
2823 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
2824 static Teuchos::RCP<sparse_matrix_type> TPETRA_DEPRECATED
2827 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2828 const Teuchos::RCP<node_type>& ,
2829 const bool callFillComplete=
true,
2830 const bool tolerant=
false,
2831 const bool debug=
false)
2833 return readSparse (in, pComm, callFillComplete, tolerant, debug);
2835 #endif // TPETRA_ENABLE_DEPRECATED_CODE
2865 static Teuchos::RCP<sparse_matrix_type>
2867 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
2868 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
2869 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
2870 const bool tolerant=
false,
2871 const bool debug=
false)
2873 using Teuchos::MatrixMarket::Banner;
2874 using Teuchos::arcp;
2875 using Teuchos::ArrayRCP;
2876 using Teuchos::broadcast;
2877 using Teuchos::null;
2880 using Teuchos::reduceAll;
2881 using Teuchos::Tuple;
2884 typedef Teuchos::ScalarTraits<scalar_type> STS;
2886 const bool extraDebug =
false;
2887 const int myRank = pComm->getRank ();
2888 const int rootRank = 0;
2893 size_t lineNumber = 1;
2895 if (debug && myRank == rootRank) {
2896 cerr <<
"Matrix Market reader: readSparse:" << endl
2897 <<
"-- Reading banner line" << endl;
2906 RCP<const Banner> pBanner;
2912 int bannerIsCorrect = 1;
2913 std::ostringstream errMsg;
2915 if (myRank == rootRank) {
2918 pBanner = readBanner (in, lineNumber, tolerant, debug);
2920 catch (std::exception& e) {
2921 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
2922 "threw an exception: " << e.what();
2923 bannerIsCorrect = 0;
2926 if (bannerIsCorrect) {
2931 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
2932 bannerIsCorrect = 0;
2933 errMsg <<
"The Matrix Market input file must contain a "
2934 "\"coordinate\"-format sparse matrix in order to create a "
2935 "Tpetra::CrsMatrix object from it, but the file's matrix "
2936 "type is \"" << pBanner->matrixType() <<
"\" instead.";
2941 if (tolerant && pBanner->matrixType() ==
"array") {
2942 bannerIsCorrect = 0;
2943 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
2944 "format sparse matrix in order to create a Tpetra::CrsMatrix "
2945 "object from it, but the file's matrix type is \"array\" "
2946 "instead. That probably means the file contains dense matrix "
2953 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
2960 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
2961 std::invalid_argument, errMsg.str ());
2963 if (debug && myRank == rootRank) {
2964 cerr <<
"-- Reading dimensions line" << endl;
2972 Tuple<global_ordinal_type, 3> dims =
2973 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
2975 if (debug && myRank == rootRank) {
2976 cerr <<
"-- Making Adder for collecting matrix data" << endl;
2981 RCP<adder_type> pAdder =
2982 makeAdder (pComm, pBanner, dims, tolerant, debug);
2984 if (debug && myRank == rootRank) {
2985 cerr <<
"-- Reading matrix data" << endl;
2995 int readSuccess = 1;
2996 std::ostringstream errMsg;
2997 if (myRank == rootRank) {
3000 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
3002 reader_type reader (pAdder);
3005 std::pair<bool, std::vector<size_t> > results =
3006 reader.read (in, lineNumber, tolerant, debug);
3007 readSuccess = results.first ? 1 : 0;
3009 catch (std::exception& e) {
3014 broadcast (*pComm, rootRank, ptr (&readSuccess));
3023 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
3024 "Failed to read the Matrix Market sparse matrix file: "
3028 if (debug && myRank == rootRank) {
3029 cerr <<
"-- Successfully read the Matrix Market data" << endl;
3042 if (debug && myRank == rootRank) {
3043 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
3045 <<
"----- Dimensions before: "
3046 << dims[0] <<
" x " << dims[1]
3050 Tuple<global_ordinal_type, 2> updatedDims;
3051 if (myRank == rootRank) {
3058 std::max (dims[0], pAdder->getAdder()->numRows());
3059 updatedDims[1] = pAdder->getAdder()->numCols();
3061 broadcast (*pComm, rootRank, updatedDims);
3062 dims[0] = updatedDims[0];
3063 dims[1] = updatedDims[1];
3064 if (debug && myRank == rootRank) {
3065 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
3079 if (myRank == rootRank) {
3086 if (dims[0] < pAdder->getAdder ()->numRows ()) {
3090 broadcast (*pComm, 0, ptr (&dimsMatch));
3091 if (dimsMatch == 0) {
3098 Tuple<global_ordinal_type, 2> addersDims;
3099 if (myRank == rootRank) {
3100 addersDims[0] = pAdder->getAdder()->numRows();
3101 addersDims[1] = pAdder->getAdder()->numCols();
3103 broadcast (*pComm, 0, addersDims);
3104 TEUCHOS_TEST_FOR_EXCEPTION(
3105 dimsMatch == 0, std::runtime_error,
3106 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
3107 << dims[1] <<
", but the actual data says that the matrix is "
3108 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
3109 "data includes more rows than reported in the metadata. This "
3110 "is not allowed when parsing in strict mode. Parse the matrix in "
3111 "tolerant mode to ignore the metadata when it disagrees with the "
3116 if (debug && myRank == rootRank) {
3117 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
3129 ArrayRCP<size_t> numEntriesPerRow;
3130 ArrayRCP<size_t> rowPtr;
3131 ArrayRCP<global_ordinal_type> colInd;
3132 ArrayRCP<scalar_type> values;
3137 int mergeAndConvertSucceeded = 1;
3138 std::ostringstream errMsg;
3140 if (myRank == rootRank) {
3142 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
3152 const size_type numRows = dims[0];
3155 pAdder->getAdder()->merge ();
3158 const std::vector<element_type>& entries =
3159 pAdder->getAdder()->getEntries();
3162 const size_t numEntries = (size_t)entries.size();
3165 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
3166 <<
" rows and numEntries=" << numEntries
3167 <<
" entries." << endl;
3173 numEntriesPerRow = arcp<size_t> (numRows);
3174 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
3175 rowPtr = arcp<size_t> (numRows+1);
3176 std::fill (rowPtr.begin(), rowPtr.end(), 0);
3177 colInd = arcp<global_ordinal_type> (numEntries);
3178 values = arcp<scalar_type> (numEntries);
3185 for (curPos = 0; curPos < numEntries; ++curPos) {
3186 const element_type& curEntry = entries[curPos];
3188 TEUCHOS_TEST_FOR_EXCEPTION(
3189 curRow < prvRow, std::logic_error,
3190 "Row indices are out of order, even though they are supposed "
3191 "to be sorted. curRow = " << curRow <<
", prvRow = "
3192 << prvRow <<
", at curPos = " << curPos <<
". Please report "
3193 "this bug to the Tpetra developers.");
3194 if (curRow > prvRow) {
3200 numEntriesPerRow[curRow]++;
3201 colInd[curPos] = curEntry.colIndex();
3202 values[curPos] = curEntry.value();
3207 rowPtr[numRows] = numEntries;
3209 catch (std::exception& e) {
3210 mergeAndConvertSucceeded = 0;
3211 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3212 "CSR format: " << e.what();
3215 if (debug && mergeAndConvertSucceeded) {
3217 const size_type numRows = dims[0];
3218 const size_type maxToDisplay = 100;
3220 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3221 << (numEntriesPerRow.size()-1) <<
"] ";
3222 if (numRows > maxToDisplay) {
3223 cerr <<
"(only showing first and last few entries) ";
3227 if (numRows > maxToDisplay) {
3228 for (size_type k = 0; k < 2; ++k) {
3229 cerr << numEntriesPerRow[k] <<
" ";
3232 for (size_type k = numRows-2; k < numRows-1; ++k) {
3233 cerr << numEntriesPerRow[k] <<
" ";
3237 for (size_type k = 0; k < numRows-1; ++k) {
3238 cerr << numEntriesPerRow[k] <<
" ";
3241 cerr << numEntriesPerRow[numRows-1];
3243 cerr <<
"]" << endl;
3245 cerr <<
"----- Proc 0: rowPtr ";
3246 if (numRows > maxToDisplay) {
3247 cerr <<
"(only showing first and last few entries) ";
3250 if (numRows > maxToDisplay) {
3251 for (size_type k = 0; k < 2; ++k) {
3252 cerr << rowPtr[k] <<
" ";
3255 for (size_type k = numRows-2; k < numRows; ++k) {
3256 cerr << rowPtr[k] <<
" ";
3260 for (size_type k = 0; k < numRows; ++k) {
3261 cerr << rowPtr[k] <<
" ";
3264 cerr << rowPtr[numRows] <<
"]" << endl;
3275 if (debug && myRank == rootRank) {
3276 cerr <<
"-- Making range, domain, and row maps" << endl;
3283 RCP<const map_type> pRangeMap = makeRangeMap (pComm, dims[0]);
3284 RCP<const map_type> pDomainMap =
3285 makeDomainMap (pRangeMap, dims[0], dims[1]);
3286 RCP<const map_type> pRowMap = makeRowMap (null, pComm, dims[0]);
3288 if (debug && myRank == rootRank) {
3289 cerr <<
"-- Distributing the matrix data" << endl;
3303 ArrayRCP<size_t> myNumEntriesPerRow;
3304 ArrayRCP<size_t> myRowPtr;
3305 ArrayRCP<global_ordinal_type> myColInd;
3306 ArrayRCP<scalar_type> myValues;
3308 distribute (myNumEntriesPerRow, myRowPtr, myColInd, myValues, pRowMap,
3309 numEntriesPerRow, rowPtr, colInd, values, debug);
3311 if (debug && myRank == rootRank) {
3312 cerr <<
"-- Inserting matrix entries on each process "
3313 "and calling fillComplete()" << endl;
3322 Teuchos::RCP<sparse_matrix_type> pMatrix =
3323 makeMatrix (myNumEntriesPerRow, myRowPtr, myColInd, myValues,
3324 pRowMap, pRangeMap, pDomainMap, constructorParams,
3325 fillCompleteParams);
3330 int localIsNull = pMatrix.is_null () ? 1 : 0;
3331 int globalIsNull = 0;
3332 reduceAll (*pComm, Teuchos::REDUCE_MAX, localIsNull, ptr (&globalIsNull));
3333 TEUCHOS_TEST_FOR_EXCEPTION(globalIsNull != 0, std::logic_error,
3334 "Reader::makeMatrix() returned a null pointer on at least one "
3335 "process. Please report this bug to the Tpetra developers.");
3338 TEUCHOS_TEST_FOR_EXCEPTION(pMatrix.is_null(), std::logic_error,
3339 "Reader::makeMatrix() returned a null pointer. "
3340 "Please report this bug to the Tpetra developers.");
3349 if (extraDebug && debug) {
3350 const int numProcs = pComm->getSize ();
3352 pRangeMap->getGlobalNumElements();
3354 pDomainMap->getGlobalNumElements();
3355 if (myRank == rootRank) {
3356 cerr <<
"-- Matrix is "
3357 << globalNumRows <<
" x " << globalNumCols
3358 <<
" with " << pMatrix->getGlobalNumEntries()
3359 <<
" entries, and index base "
3360 << pMatrix->getIndexBase() <<
"." << endl;
3363 for (
int p = 0; p < numProcs; ++p) {
3365 cerr <<
"-- Proc " << p <<
" owns "
3366 << pMatrix->getNodeNumCols() <<
" columns, and "
3367 << pMatrix->getNodeNumEntries() <<
" entries." << endl;
3373 if (debug && myRank == rootRank) {
3374 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
3380 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
3381 static Teuchos::RCP<sparse_matrix_type> TPETRA_DEPRECATED
3384 const Teuchos::RCP<
const Teuchos::Comm<int> >& pComm,
3385 const Teuchos::RCP<node_type>& ,
3386 const Teuchos::RCP<Teuchos::ParameterList>& constructorParams,
3387 const Teuchos::RCP<Teuchos::ParameterList>& fillCompleteParams,
3388 const bool tolerant=
false,
3389 const bool debug=
false)
3391 return readSparse (in, pComm, constructorParams,
3392 fillCompleteParams, tolerant, debug);
3394 #endif // TPETRA_ENABLE_DEPRECATED_CODE
3436 static Teuchos::RCP<sparse_matrix_type>
3438 const Teuchos::RCP<const map_type>& rowMap,
3439 Teuchos::RCP<const map_type>& colMap,
3440 const Teuchos::RCP<const map_type>& domainMap,
3441 const Teuchos::RCP<const map_type>& rangeMap,
3442 const bool callFillComplete=
true,
3443 const bool tolerant=
false,
3444 const bool debug=
false)
3446 using Teuchos::MatrixMarket::Banner;
3447 using Teuchos::arcp;
3448 using Teuchos::ArrayRCP;
3449 using Teuchos::ArrayView;
3451 using Teuchos::broadcast;
3452 using Teuchos::Comm;
3453 using Teuchos::null;
3456 using Teuchos::reduceAll;
3457 using Teuchos::Tuple;
3460 typedef Teuchos::ScalarTraits<scalar_type> STS;
3462 RCP<const Comm<int> > pComm = rowMap->getComm ();
3463 const int myRank = pComm->getRank ();
3464 const int rootRank = 0;
3465 const bool extraDebug =
false;
3470 TEUCHOS_TEST_FOR_EXCEPTION(
3471 rowMap.is_null (), std::invalid_argument,
3472 "Row Map must be nonnull.");
3473 TEUCHOS_TEST_FOR_EXCEPTION(
3474 rangeMap.is_null (), std::invalid_argument,
3475 "Range Map must be nonnull.");
3476 TEUCHOS_TEST_FOR_EXCEPTION(
3477 domainMap.is_null (), std::invalid_argument,
3478 "Domain Map must be nonnull.");
3479 TEUCHOS_TEST_FOR_EXCEPTION(
3480 rowMap->getComm().getRawPtr() != pComm.getRawPtr(),
3481 std::invalid_argument,
3482 "The specified row Map's communicator (rowMap->getComm())"
3483 "differs from the given separately supplied communicator pComm.");
3484 TEUCHOS_TEST_FOR_EXCEPTION(
3485 domainMap->getComm().getRawPtr() != pComm.getRawPtr(),
3486 std::invalid_argument,
3487 "The specified domain Map's communicator (domainMap->getComm())"
3488 "differs from the given separately supplied communicator pComm.");
3489 TEUCHOS_TEST_FOR_EXCEPTION(
3490 rangeMap->getComm().getRawPtr() != pComm.getRawPtr(),
3491 std::invalid_argument,
3492 "The specified range Map's communicator (rangeMap->getComm())"
3493 "differs from the given separately supplied communicator pComm.");
3498 size_t lineNumber = 1;
3500 if (debug && myRank == rootRank) {
3501 cerr <<
"Matrix Market reader: readSparse:" << endl
3502 <<
"-- Reading banner line" << endl;
3511 RCP<const Banner> pBanner;
3517 int bannerIsCorrect = 1;
3518 std::ostringstream errMsg;
3520 if (myRank == rootRank) {
3523 pBanner = readBanner (in, lineNumber, tolerant, debug);
3525 catch (std::exception& e) {
3526 errMsg <<
"Attempt to read the Matrix Market file's Banner line "
3527 "threw an exception: " << e.what();
3528 bannerIsCorrect = 0;
3531 if (bannerIsCorrect) {
3536 if (! tolerant && pBanner->matrixType() !=
"coordinate") {
3537 bannerIsCorrect = 0;
3538 errMsg <<
"The Matrix Market input file must contain a "
3539 "\"coordinate\"-format sparse matrix in order to create a "
3540 "Tpetra::CrsMatrix object from it, but the file's matrix "
3541 "type is \"" << pBanner->matrixType() <<
"\" instead.";
3546 if (tolerant && pBanner->matrixType() ==
"array") {
3547 bannerIsCorrect = 0;
3548 errMsg <<
"Matrix Market file must contain a \"coordinate\"-"
3549 "format sparse matrix in order to create a Tpetra::CrsMatrix "
3550 "object from it, but the file's matrix type is \"array\" "
3551 "instead. That probably means the file contains dense matrix "
3558 broadcast (*pComm, rootRank, ptr (&bannerIsCorrect));
3565 TEUCHOS_TEST_FOR_EXCEPTION(bannerIsCorrect == 0,
3566 std::invalid_argument, errMsg.str ());
3568 if (debug && myRank == rootRank) {
3569 cerr <<
"-- Reading dimensions line" << endl;
3577 Tuple<global_ordinal_type, 3> dims =
3578 readCoordDims (in, lineNumber, pBanner, pComm, tolerant, debug);
3580 if (debug && myRank == rootRank) {
3581 cerr <<
"-- Making Adder for collecting matrix data" << endl;
3588 RCP<adder_type> pAdder =
3589 makeAdder (pComm, pBanner, dims, tolerant, debug);
3591 if (debug && myRank == rootRank) {
3592 cerr <<
"-- Reading matrix data" << endl;
3602 int readSuccess = 1;
3603 std::ostringstream errMsg;
3604 if (myRank == rootRank) {
3607 typedef Teuchos::MatrixMarket::CoordDataReader<adder_type,
3609 reader_type reader (pAdder);
3612 std::pair<bool, std::vector<size_t> > results =
3613 reader.read (in, lineNumber, tolerant, debug);
3614 readSuccess = results.first ? 1 : 0;
3616 catch (std::exception& e) {
3621 broadcast (*pComm, rootRank, ptr (&readSuccess));
3630 TEUCHOS_TEST_FOR_EXCEPTION(readSuccess == 0, std::runtime_error,
3631 "Failed to read the Matrix Market sparse matrix file: "
3635 if (debug && myRank == rootRank) {
3636 cerr <<
"-- Successfully read the Matrix Market data" << endl;
3649 if (debug && myRank == rootRank) {
3650 cerr <<
"-- Tolerant mode: rebroadcasting matrix dimensions"
3652 <<
"----- Dimensions before: "
3653 << dims[0] <<
" x " << dims[1]
3657 Tuple<global_ordinal_type, 2> updatedDims;
3658 if (myRank == rootRank) {
3665 std::max (dims[0], pAdder->getAdder()->numRows());
3666 updatedDims[1] = pAdder->getAdder()->numCols();
3668 broadcast (*pComm, rootRank, updatedDims);
3669 dims[0] = updatedDims[0];
3670 dims[1] = updatedDims[1];
3671 if (debug && myRank == rootRank) {
3672 cerr <<
"----- Dimensions after: " << dims[0] <<
" x "
3686 if (myRank == rootRank) {
3693 if (dims[0] < pAdder->getAdder ()->numRows ()) {
3697 broadcast (*pComm, 0, ptr (&dimsMatch));
3698 if (dimsMatch == 0) {
3705 Tuple<global_ordinal_type, 2> addersDims;
3706 if (myRank == rootRank) {
3707 addersDims[0] = pAdder->getAdder()->numRows();
3708 addersDims[1] = pAdder->getAdder()->numCols();
3710 broadcast (*pComm, 0, addersDims);
3711 TEUCHOS_TEST_FOR_EXCEPTION(
3712 dimsMatch == 0, std::runtime_error,
3713 "The matrix metadata says that the matrix is " << dims[0] <<
" x "
3714 << dims[1] <<
", but the actual data says that the matrix is "
3715 << addersDims[0] <<
" x " << addersDims[1] <<
". That means the "
3716 "data includes more rows than reported in the metadata. This "
3717 "is not allowed when parsing in strict mode. Parse the matrix in "
3718 "tolerant mode to ignore the metadata when it disagrees with the "
3723 if (debug && myRank == rootRank) {
3724 cerr <<
"-- Converting matrix data into CSR format on Proc 0" << endl;
3736 ArrayRCP<size_t> numEntriesPerRow;
3737 ArrayRCP<size_t> rowPtr;
3738 ArrayRCP<global_ordinal_type> colInd;
3739 ArrayRCP<scalar_type> values;
3744 int mergeAndConvertSucceeded = 1;
3745 std::ostringstream errMsg;
3747 if (myRank == rootRank) {
3749 typedef Teuchos::MatrixMarket::Raw::Element<
scalar_type,
3759 const size_type numRows = dims[0];
3762 pAdder->getAdder()->merge ();
3765 const std::vector<element_type>& entries =
3766 pAdder->getAdder()->getEntries();
3769 const size_t numEntries = (size_t)entries.size();
3772 cerr <<
"----- Proc 0: Matrix has numRows=" << numRows
3773 <<
" rows and numEntries=" << numEntries
3774 <<
" entries." << endl;
3780 numEntriesPerRow = arcp<size_t> (numRows);
3781 std::fill (numEntriesPerRow.begin(), numEntriesPerRow.end(), 0);
3782 rowPtr = arcp<size_t> (numRows+1);
3783 std::fill (rowPtr.begin(), rowPtr.end(), 0);
3784 colInd = arcp<global_ordinal_type> (numEntries);
3785 values = arcp<scalar_type> (numEntries);
3792 for (curPos = 0; curPos < numEntries; ++curPos) {
3793 const element_type& curEntry = entries[curPos];
3795 TEUCHOS_TEST_FOR_EXCEPTION(
3796 curRow < prvRow, std::logic_error,
3797 "Row indices are out of order, even though they are supposed "
3798 "to be sorted. curRow = " << curRow <<
", prvRow = "
3799 << prvRow <<
", at curPos = " << curPos <<
". Please report "
3800 "this bug to the Tpetra developers.");
3801 if (curRow > prvRow) {
3807 numEntriesPerRow[curRow]++;
3808 colInd[curPos] = curEntry.colIndex();
3809 values[curPos] = curEntry.value();
3814 rowPtr[numRows] = numEntries;
3816 catch (std::exception& e) {
3817 mergeAndConvertSucceeded = 0;
3818 errMsg <<
"Failed to merge sparse matrix entries and convert to "
3819 "CSR format: " << e.what();
3822 if (debug && mergeAndConvertSucceeded) {
3824 const size_type numRows = dims[0];
3825 const size_type maxToDisplay = 100;
3827 cerr <<
"----- Proc 0: numEntriesPerRow[0.."
3828 << (numEntriesPerRow.size()-1) <<
"] ";
3829 if (numRows > maxToDisplay) {
3830 cerr <<
"(only showing first and last few entries) ";
3834 if (numRows > maxToDisplay) {
3835 for (size_type k = 0; k < 2; ++k) {
3836 cerr << numEntriesPerRow[k] <<
" ";
3839 for (size_type k = numRows-2; k < numRows-1; ++k) {
3840 cerr << numEntriesPerRow[k] <<
" ";
3844 for (size_type k = 0; k < numRows-1; ++k) {
3845 cerr << numEntriesPerRow[k] <<
" ";
3848 cerr << numEntriesPerRow[numRows-1];
3850 cerr <<
"]" << endl;
3852 cerr <<
"----- Proc 0: rowPtr ";
3853 if (numRows > maxToDisplay) {
3854 cerr <<
"(only showing first and last few entries) ";
3857 if (numRows > maxToDisplay) {
3858 for (size_type k = 0; k < 2; ++k) {
3859 cerr << rowPtr[k] <<
" ";
3862 for (size_type k = numRows-2; k < numRows; ++k) {
3863 cerr << rowPtr[k] <<
" ";
3867 for (size_type k = 0; k < numRows; ++k) {
3868 cerr << rowPtr[k] <<
" ";
3871 cerr << rowPtr[numRows] <<
"]" << endl;
3873 cerr <<
"----- Proc 0: colInd = [";
3874 for (
size_t k = 0; k < rowPtr[numRows]; ++k) {
3875 cerr << colInd[k] <<
" ";
3877 cerr <<
"]" << endl;
3891 if (debug && myRank == rootRank) {
3892 cerr <<
"-- Verifying Maps" << endl;
3894 TEUCHOS_TEST_FOR_EXCEPTION(
3895 as<global_size_t> (dims[0]) != rangeMap->getGlobalNumElements(),
3896 std::invalid_argument,
3897 "The range Map has " << rangeMap->getGlobalNumElements ()
3898 <<
" entries, but the matrix has a global number of rows " << dims[0]
3900 TEUCHOS_TEST_FOR_EXCEPTION(
3901 as<global_size_t> (dims[1]) != domainMap->getGlobalNumElements (),
3902 std::invalid_argument,
3903 "The domain Map has " << domainMap->getGlobalNumElements ()
3904 <<
" entries, but the matrix has a global number of columns "
3908 RCP<Teuchos::FancyOStream> err = debug ?
3909 Teuchos::getFancyOStream (Teuchos::rcpFromRef (cerr)) : null;
3911 RCP<const map_type> gatherRowMap = Details::computeGatherMap (rowMap, err, debug);
3912 ArrayView<const global_ordinal_type> myRows =
3913 gatherRowMap->getNodeElementList ();
3914 const size_type myNumRows = myRows.size ();
3917 ArrayRCP<size_t> gatherNumEntriesPerRow = arcp<size_t>(myNumRows);
3918 for (size_type i_ = 0; i_ < myNumRows; i_++) {
3919 gatherNumEntriesPerRow[i_] = numEntriesPerRow[myRows[i_]-indexBase];
3925 RCP<sparse_matrix_type> A_proc0 =
3927 Tpetra::StaticProfile));
3928 if (myRank == rootRank) {
3930 cerr <<
"-- Proc 0: Filling gather matrix" << endl;
3933 cerr <<
"---- Rows: " << Teuchos::toString (myRows) << endl;
3937 for (size_type i_ = 0; i_ < myNumRows; ++i_) {
3938 size_type i = myRows[i_] - indexBase;
3940 const size_type curPos = as<size_type> (rowPtr[i]);
3942 ArrayView<global_ordinal_type> curColInd =
3943 colInd.view (curPos, curNumEntries);
3944 ArrayView<scalar_type> curValues =
3945 values.view (curPos, curNumEntries);
3948 for (size_type k = 0; k < curNumEntries; ++k) {
3949 curColInd[k] += indexBase;
3952 cerr <<
"------ Columns: " << Teuchos::toString (curColInd) << endl;
3953 cerr <<
"------ Values: " << Teuchos::toString (curValues) << endl;
3956 if (curNumEntries > 0) {
3957 A_proc0->insertGlobalValues (myRows[i_], curColInd, curValues);
3963 numEntriesPerRow = null;
3969 RCP<sparse_matrix_type> A;
3970 if (colMap.is_null ()) {
3976 export_type exp (gatherRowMap, rowMap);
3977 A->doExport (*A_proc0, exp,
INSERT);
3979 if (callFillComplete) {
3980 A->fillComplete (domainMap, rangeMap);
3992 if (callFillComplete) {
3993 const int numProcs = pComm->getSize ();
3995 if (extraDebug && debug) {
3996 const global_size_t globalNumRows = rangeMap->getGlobalNumElements ();
3997 const global_size_t globalNumCols = domainMap->getGlobalNumElements ();
3998 if (myRank == rootRank) {
3999 cerr <<
"-- Matrix is "
4000 << globalNumRows <<
" x " << globalNumCols
4001 <<
" with " << A->getGlobalNumEntries()
4002 <<
" entries, and index base "
4003 << A->getIndexBase() <<
"." << endl;
4006 for (
int p = 0; p < numProcs; ++p) {
4008 cerr <<
"-- Proc " << p <<
" owns "
4009 << A->getNodeNumCols() <<
" columns, and "
4010 << A->getNodeNumEntries() <<
" entries." << endl;
4017 if (debug && myRank == rootRank) {
4018 cerr <<
"-- Done creating the CrsMatrix from the Matrix Market data"
4052 static Teuchos::RCP<multivector_type>
4054 const Teuchos::RCP<const comm_type>& comm,
4055 Teuchos::RCP<const map_type>& map,
4056 const bool tolerant=
false,
4057 const bool debug=
false)
4060 if (comm->getRank () == 0) {
4061 in.open (filename.c_str ());
4063 return readDense (in, comm, map, tolerant, debug);
4066 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
4067 static Teuchos::RCP<multivector_type> TPETRA_DEPRECATED
4070 const Teuchos::RCP<const comm_type>& comm,
4071 const Teuchos::RCP<node_type>& ,
4072 Teuchos::RCP<const map_type>& map,
4073 const bool tolerant=
false,
4074 const bool debug=
false)
4078 #endif // TPETRA_ENABLE_DEPRECATED_CODE
4109 static Teuchos::RCP<vector_type>
4111 const Teuchos::RCP<const comm_type>& comm,
4112 Teuchos::RCP<const map_type>& map,
4113 const bool tolerant=
false,
4114 const bool debug=
false)
4117 if (comm->getRank () == 0) {
4118 in.open (filename.c_str ());
4120 return readVector (in, comm, map, tolerant, debug);
4123 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
4124 static Teuchos::RCP<vector_type> TPETRA_DEPRECATED
4128 const Teuchos::RCP<const comm_type>& comm,
4129 const Teuchos::RCP<node_type>& ,
4130 Teuchos::RCP<const map_type>& map,
4131 const bool tolerant=
false,
4132 const bool debug=
false)
4136 #endif // TPETRA_ENABLE_DEPRECATED_CODE
4204 static Teuchos::RCP<multivector_type>
4206 const Teuchos::RCP<const comm_type>& comm,
4207 Teuchos::RCP<const map_type>& map,
4208 const bool tolerant=
false,
4209 const bool debug=
false)
4211 Teuchos::RCP<Teuchos::FancyOStream> err =
4212 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4213 return readDenseImpl<scalar_type> (in, comm, map, err, tolerant, debug);
4216 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
4217 static Teuchos::RCP<multivector_type> TPETRA_DEPRECATED
4220 const Teuchos::RCP<const comm_type>& comm,
4221 const Teuchos::RCP<node_type>& ,
4222 Teuchos::RCP<const map_type>& map,
4223 const bool tolerant=
false,
4224 const bool debug=
false)
4226 return readDense (in, comm, map, tolerant, debug);
4228 #endif // TPETRA_ENABLE_DEPRECATED_CODE
4231 static Teuchos::RCP<vector_type>
4233 const Teuchos::RCP<const comm_type>& comm,
4234 Teuchos::RCP<const map_type>& map,
4235 const bool tolerant=
false,
4236 const bool debug=
false)
4238 Teuchos::RCP<Teuchos::FancyOStream> err =
4239 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
4240 return readVectorImpl<scalar_type> (in, comm, map, err, tolerant, debug);
4243 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
4244 static Teuchos::RCP<vector_type> TPETRA_DEPRECATED
4247 const Teuchos::RCP<const comm_type>& comm,
4248 const Teuchos::RCP<node_type>& ,
4249 Teuchos::RCP<const map_type>& map,
4250 const bool tolerant=
false,
4251 const bool debug=
false)
4253 return readVector(in, comm, map, tolerant, debug);
4255 #endif // TPETRA_ENABLE_DEPRECATED_CODE
4277 static Teuchos::RCP<const map_type>
4279 const Teuchos::RCP<const comm_type>& comm,
4280 const bool tolerant=
false,
4281 const bool debug=
false)
4283 using Teuchos::inOutArg;
4284 using Teuchos::broadcast;
4288 if (comm->getRank () == 0) {
4289 in.open (filename.c_str ());
4294 broadcast<int, int> (*comm, 0, inOutArg (success));
4295 TEUCHOS_TEST_FOR_EXCEPTION(
4296 success == 0, std::runtime_error,
4297 "Tpetra::MatrixMarket::Reader::readMapFile: "
4298 "Failed to read file \"" << filename <<
"\" on Process 0.");
4299 return readMap (in, comm, tolerant, debug);
4302 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
4303 static Teuchos::RCP<const map_type> TPETRA_DEPRECATED
4307 const Teuchos::RCP<const comm_type>& comm,
4308 const Teuchos::RCP<node_type>& ,
4309 const bool tolerant=
false,
4310 const bool debug=
false)
4312 return readMapFile (filename, comm, tolerant, debug);
4314 #endif // TPETRA_ENABLE_DEPRECATED_CODE
4317 template<
class MultiVectorScalarType>
4322 readDenseImpl (std::istream& in,
4323 const Teuchos::RCP<const comm_type>& comm,
4324 Teuchos::RCP<const map_type>& map,
4325 const Teuchos::RCP<Teuchos::FancyOStream>& err,
4326 const bool tolerant=
false,
4327 const bool debug=
false)
4329 using Teuchos::MatrixMarket::Banner;
4330 using Teuchos::MatrixMarket::checkCommentLine;
4331 using Teuchos::ArrayRCP;
4333 using Teuchos::broadcast;
4334 using Teuchos::outArg;
4336 using Teuchos::Tuple;
4338 typedef MultiVectorScalarType ST;
4342 typedef Teuchos::ScalarTraits<ST> STS;
4343 typedef typename STS::magnitudeType MT;
4344 typedef Teuchos::ScalarTraits<MT> STM;
4349 const int myRank = comm->getRank ();
4351 if (! err.is_null ()) {
4355 *err << myRank <<
": readDenseImpl" << endl;
4357 if (! err.is_null ()) {
4391 size_t lineNumber = 1;
4394 std::ostringstream exMsg;
4395 int localBannerReadSuccess = 1;
4396 int localDimsReadSuccess = 1;
4401 *err << myRank <<
": readDenseImpl: Reading banner line (dense)" << endl;
4407 RCP<const Banner> pBanner;
4409 pBanner = readBanner (in, lineNumber, tolerant, debug);
4410 }
catch (std::exception& e) {
4412 localBannerReadSuccess = 0;
4415 if (localBannerReadSuccess) {
4416 if (pBanner->matrixType () !=
"array") {
4417 exMsg <<
"The Matrix Market file does not contain dense matrix "
4418 "data. Its banner (first) line says that its matrix type is \""
4419 << pBanner->matrixType () <<
"\", rather that the required "
4421 localBannerReadSuccess = 0;
4422 }
else if (pBanner->dataType() ==
"pattern") {
4423 exMsg <<
"The Matrix Market file's banner (first) "
4424 "line claims that the matrix's data type is \"pattern\". This does "
4425 "not make sense for a dense matrix, yet the file reports the matrix "
4426 "as dense. The only valid data types for a dense matrix are "
4427 "\"real\", \"complex\", and \"integer\".";
4428 localBannerReadSuccess = 0;
4432 dims[2] = encodeDataType (pBanner->dataType ());
4438 if (localBannerReadSuccess) {
4440 *err << myRank <<
": readDenseImpl: Reading dimensions line (dense)" << endl;
4449 bool commentLine =
true;
4451 while (commentLine) {
4454 if (in.eof () || in.fail ()) {
4455 exMsg <<
"Unable to get array dimensions line (at all) from line "
4456 << lineNumber <<
" of input stream. The input stream "
4457 <<
"claims that it is "
4458 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4459 localDimsReadSuccess = 0;
4462 if (getline (in, line)) {
4469 size_t start = 0, size = 0;
4470 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4477 std::istringstream istr (line);
4481 if (istr.eof () || istr.fail ()) {
4482 exMsg <<
"Unable to read any data from line " << lineNumber
4483 <<
" of input; the line should contain the matrix dimensions "
4484 <<
"\"<numRows> <numCols>\".";
4485 localDimsReadSuccess = 0;
4490 exMsg <<
"Failed to get number of rows from line "
4491 << lineNumber <<
" of input; the line should contains the "
4492 <<
"matrix dimensions \"<numRows> <numCols>\".";
4493 localDimsReadSuccess = 0;
4495 dims[0] = theNumRows;
4497 exMsg <<
"No more data after number of rows on line "
4498 << lineNumber <<
" of input; the line should contain the "
4499 <<
"matrix dimensions \"<numRows> <numCols>\".";
4500 localDimsReadSuccess = 0;
4505 exMsg <<
"Failed to get number of columns from line "
4506 << lineNumber <<
" of input; the line should contain "
4507 <<
"the matrix dimensions \"<numRows> <numCols>\".";
4508 localDimsReadSuccess = 0;
4510 dims[1] = theNumCols;
4521 Tuple<GO, 5> bannerDimsReadResult;
4523 bannerDimsReadResult[0] = dims[0];
4524 bannerDimsReadResult[1] = dims[1];
4525 bannerDimsReadResult[2] = dims[2];
4526 bannerDimsReadResult[3] = localBannerReadSuccess;
4527 bannerDimsReadResult[4] = localDimsReadSuccess;
4531 broadcast (*comm, 0, bannerDimsReadResult);
4533 TEUCHOS_TEST_FOR_EXCEPTION(
4534 bannerDimsReadResult[3] == 0, std::runtime_error,
4535 "Failed to read banner line: " << exMsg.str ());
4536 TEUCHOS_TEST_FOR_EXCEPTION(
4537 bannerDimsReadResult[4] == 0, std::runtime_error,
4538 "Failed to read matrix dimensions line: " << exMsg.str ());
4540 dims[0] = bannerDimsReadResult[0];
4541 dims[1] = bannerDimsReadResult[1];
4542 dims[2] = bannerDimsReadResult[2];
4547 const size_t numCols =
static_cast<size_t> (dims[1]);
4552 RCP<const map_type> proc0Map;
4553 if (map.is_null ()) {
4557 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
4558 const size_t localNumRows = (myRank == 0) ? numRows : 0;
4560 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
4564 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
4568 RCP<MV> X = createMultiVector<ST, LO, GO, NT> (proc0Map, numCols);
4574 int localReadDataSuccess = 1;
4578 *err << myRank <<
": readDenseImpl: Reading matrix data (dense)"
4583 TEUCHOS_TEST_FOR_EXCEPTION(
4584 ! X->isConstantStride (), std::logic_error,
4585 "Can't get a 1-D view of the entries of the MultiVector X on "
4586 "Process 0, because the stride between the columns of X is not "
4587 "constant. This shouldn't happen because we just created X and "
4588 "haven't filled it in yet. Please report this bug to the Tpetra "
4595 ArrayRCP<ST> X_view = X->get1dViewNonConst ();
4596 TEUCHOS_TEST_FOR_EXCEPTION(
4597 as<global_size_t> (X_view.size ()) < numRows * numCols,
4599 "The view of X has size " << X_view <<
" which is not enough to "
4600 "accommodate the expected number of entries numRows*numCols = "
4601 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
4602 "Please report this bug to the Tpetra developers.");
4603 const size_t stride = X->getStride ();
4610 const bool isComplex = (dims[2] == 1);
4611 size_type count = 0, curRow = 0, curCol = 0;
4614 while (getline (in, line)) {
4618 size_t start = 0, size = 0;
4619 const bool commentLine =
4620 checkCommentLine (line, start, size, lineNumber, tolerant);
4621 if (! commentLine) {
4627 if (count >= X_view.size()) {
4632 TEUCHOS_TEST_FOR_EXCEPTION(
4633 count >= X_view.size(),
4635 "The Matrix Market input stream has more data in it than "
4636 "its metadata reported. Current line number is "
4637 << lineNumber <<
".");
4646 const size_t pos = line.substr (start, size).find (
':');
4647 if (pos != std::string::npos) {
4651 std::istringstream istr (line.substr (start, size));
4655 if (istr.eof() || istr.fail()) {
4662 TEUCHOS_TEST_FOR_EXCEPTION(
4663 ! tolerant && (istr.eof() || istr.fail()),
4665 "Line " << lineNumber <<
" of the Matrix Market file is "
4666 "empty, or we cannot read from it for some other reason.");
4669 ST val = STS::zero();
4672 MT real = STM::zero(), imag = STM::zero();
4689 TEUCHOS_TEST_FOR_EXCEPTION(
4690 ! tolerant && istr.eof(), std::runtime_error,
4691 "Failed to get the real part of a complex-valued matrix "
4692 "entry from line " << lineNumber <<
" of the Matrix Market "
4698 }
else if (istr.eof()) {
4699 TEUCHOS_TEST_FOR_EXCEPTION(
4700 ! tolerant && istr.eof(), std::runtime_error,
4701 "Missing imaginary part of a complex-valued matrix entry "
4702 "on line " << lineNumber <<
" of the Matrix Market file.");
4708 TEUCHOS_TEST_FOR_EXCEPTION(
4709 ! tolerant && istr.fail(), std::runtime_error,
4710 "Failed to get the imaginary part of a complex-valued "
4711 "matrix entry from line " << lineNumber <<
" of the "
4712 "Matrix Market file.");
4719 TEUCHOS_TEST_FOR_EXCEPTION(
4720 ! tolerant && istr.fail(), std::runtime_error,
4721 "Failed to get a real-valued matrix entry from line "
4722 << lineNumber <<
" of the Matrix Market file.");
4725 if (istr.fail() && tolerant) {
4731 TEUCHOS_TEST_FOR_EXCEPTION(
4732 ! tolerant && istr.fail(), std::runtime_error,
4733 "Failed to read matrix data from line " << lineNumber
4734 <<
" of the Matrix Market file.");
4737 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
4739 curRow = count % numRows;
4740 curCol = count / numRows;
4741 X_view[curRow + curCol*stride] = val;
4746 TEUCHOS_TEST_FOR_EXCEPTION(
4747 ! tolerant && static_cast<global_size_t> (count) < numRows * numCols,
4749 "The Matrix Market metadata reports that the dense matrix is "
4750 << numRows <<
" x " << numCols <<
", and thus has "
4751 << numRows*numCols <<
" total entries, but we only found " << count
4752 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
4753 }
catch (std::exception& e) {
4755 localReadDataSuccess = 0;
4760 *err << myRank <<
": readDenseImpl: done reading data" << endl;
4764 int globalReadDataSuccess = localReadDataSuccess;
4765 broadcast (*comm, 0, outArg (globalReadDataSuccess));
4766 TEUCHOS_TEST_FOR_EXCEPTION(
4767 globalReadDataSuccess == 0, std::runtime_error,
4768 "Failed to read the multivector's data: " << exMsg.str ());
4773 if (comm->getSize () == 1 && map.is_null ()) {
4775 if (! err.is_null ()) {
4779 *err << myRank <<
": readDenseImpl: done" << endl;
4781 if (! err.is_null ()) {
4788 *err << myRank <<
": readDenseImpl: Creating target MV" << endl;
4792 RCP<MV> Y = createMultiVector<ST, LO, GO, NT> (map, numCols);
4795 *err << myRank <<
": readDenseImpl: Creating Export" << endl;
4801 Export<LO, GO, NT> exporter (proc0Map, map, err);
4804 *err << myRank <<
": readDenseImpl: Exporting" << endl;
4807 Y->doExport (*X, exporter,
INSERT);
4809 if (! err.is_null ()) {
4813 *err << myRank <<
": readDenseImpl: done" << endl;
4815 if (! err.is_null ()) {
4824 template<
class VectorScalarType>
4829 readVectorImpl (std::istream& in,
4830 const Teuchos::RCP<const comm_type>& comm,
4831 Teuchos::RCP<const map_type>& map,
4832 const Teuchos::RCP<Teuchos::FancyOStream>& err,
4833 const bool tolerant=
false,
4834 const bool debug=
false)
4836 using Teuchos::MatrixMarket::Banner;
4837 using Teuchos::MatrixMarket::checkCommentLine;
4839 using Teuchos::broadcast;
4840 using Teuchos::outArg;
4842 using Teuchos::Tuple;
4844 typedef VectorScalarType ST;
4848 typedef Teuchos::ScalarTraits<ST> STS;
4849 typedef typename STS::magnitudeType MT;
4850 typedef Teuchos::ScalarTraits<MT> STM;
4855 const int myRank = comm->getRank ();
4857 if (! err.is_null ()) {
4861 *err << myRank <<
": readVectorImpl" << endl;
4863 if (! err.is_null ()) {
4897 size_t lineNumber = 1;
4900 std::ostringstream exMsg;
4901 int localBannerReadSuccess = 1;
4902 int localDimsReadSuccess = 1;
4907 *err << myRank <<
": readVectorImpl: Reading banner line (dense)" << endl;
4913 RCP<const Banner> pBanner;
4915 pBanner = readBanner (in, lineNumber, tolerant, debug);
4916 }
catch (std::exception& e) {
4918 localBannerReadSuccess = 0;
4921 if (localBannerReadSuccess) {
4922 if (pBanner->matrixType () !=
"array") {
4923 exMsg <<
"The Matrix Market file does not contain dense matrix "
4924 "data. Its banner (first) line says that its matrix type is \""
4925 << pBanner->matrixType () <<
"\", rather that the required "
4927 localBannerReadSuccess = 0;
4928 }
else if (pBanner->dataType() ==
"pattern") {
4929 exMsg <<
"The Matrix Market file's banner (first) "
4930 "line claims that the matrix's data type is \"pattern\". This does "
4931 "not make sense for a dense matrix, yet the file reports the matrix "
4932 "as dense. The only valid data types for a dense matrix are "
4933 "\"real\", \"complex\", and \"integer\".";
4934 localBannerReadSuccess = 0;
4938 dims[2] = encodeDataType (pBanner->dataType ());
4944 if (localBannerReadSuccess) {
4946 *err << myRank <<
": readVectorImpl: Reading dimensions line (dense)" << endl;
4955 bool commentLine =
true;
4957 while (commentLine) {
4960 if (in.eof () || in.fail ()) {
4961 exMsg <<
"Unable to get array dimensions line (at all) from line "
4962 << lineNumber <<
" of input stream. The input stream "
4963 <<
"claims that it is "
4964 << (in.eof() ?
"at end-of-file." :
"in a failed state.");
4965 localDimsReadSuccess = 0;
4968 if (getline (in, line)) {
4975 size_t start = 0, size = 0;
4976 commentLine = checkCommentLine (line, start, size, lineNumber, tolerant);
4983 std::istringstream istr (line);
4987 if (istr.eof () || istr.fail ()) {
4988 exMsg <<
"Unable to read any data from line " << lineNumber
4989 <<
" of input; the line should contain the matrix dimensions "
4990 <<
"\"<numRows> <numCols>\".";
4991 localDimsReadSuccess = 0;
4996 exMsg <<
"Failed to get number of rows from line "
4997 << lineNumber <<
" of input; the line should contains the "
4998 <<
"matrix dimensions \"<numRows> <numCols>\".";
4999 localDimsReadSuccess = 0;
5001 dims[0] = theNumRows;
5003 exMsg <<
"No more data after number of rows on line "
5004 << lineNumber <<
" of input; the line should contain the "
5005 <<
"matrix dimensions \"<numRows> <numCols>\".";
5006 localDimsReadSuccess = 0;
5011 exMsg <<
"Failed to get number of columns from line "
5012 << lineNumber <<
" of input; the line should contain "
5013 <<
"the matrix dimensions \"<numRows> <numCols>\".";
5014 localDimsReadSuccess = 0;
5016 dims[1] = theNumCols;
5026 exMsg <<
"File does not contain a 1D Vector";
5027 localDimsReadSuccess = 0;
5033 Tuple<GO, 5> bannerDimsReadResult;
5035 bannerDimsReadResult[0] = dims[0];
5036 bannerDimsReadResult[1] = dims[1];
5037 bannerDimsReadResult[2] = dims[2];
5038 bannerDimsReadResult[3] = localBannerReadSuccess;
5039 bannerDimsReadResult[4] = localDimsReadSuccess;
5044 broadcast (*comm, 0, bannerDimsReadResult);
5046 TEUCHOS_TEST_FOR_EXCEPTION(
5047 bannerDimsReadResult[3] == 0, std::runtime_error,
5048 "Failed to read banner line: " << exMsg.str ());
5049 TEUCHOS_TEST_FOR_EXCEPTION(
5050 bannerDimsReadResult[4] == 0, std::runtime_error,
5051 "Failed to read matrix dimensions line: " << exMsg.str ());
5053 dims[0] = bannerDimsReadResult[0];
5054 dims[1] = bannerDimsReadResult[1];
5055 dims[2] = bannerDimsReadResult[2];
5060 const size_t numCols =
static_cast<size_t> (dims[1]);
5065 RCP<const map_type> proc0Map;
5066 if (map.is_null ()) {
5070 map = createUniformContigMapWithNode<LO, GO, NT> (numRows, comm);
5071 const size_t localNumRows = (myRank == 0) ? numRows : 0;
5073 proc0Map = createContigMapWithNode<LO, GO, NT> (numRows, localNumRows,
5077 proc0Map = Details::computeGatherMap<map_type> (map, err, debug);
5081 RCP<MV> X = createVector<ST, LO, GO, NT> (proc0Map);
5087 int localReadDataSuccess = 1;
5091 *err << myRank <<
": readVectorImpl: Reading matrix data (dense)"
5096 TEUCHOS_TEST_FOR_EXCEPTION(
5097 ! X->isConstantStride (), std::logic_error,
5098 "Can't get a 1-D view of the entries of the MultiVector X on "
5099 "Process 0, because the stride between the columns of X is not "
5100 "constant. This shouldn't happen because we just created X and "
5101 "haven't filled it in yet. Please report this bug to the Tpetra "
5108 Teuchos::ArrayRCP<ST> X_view = X->get1dViewNonConst ();
5109 TEUCHOS_TEST_FOR_EXCEPTION(
5110 as<global_size_t> (X_view.size ()) < numRows * numCols,
5112 "The view of X has size " << X_view <<
" which is not enough to "
5113 "accommodate the expected number of entries numRows*numCols = "
5114 << numRows <<
"*" << numCols <<
" = " << numRows*numCols <<
". "
5115 "Please report this bug to the Tpetra developers.");
5116 const size_t stride = X->getStride ();
5123 const bool isComplex = (dims[2] == 1);
5124 size_type count = 0, curRow = 0, curCol = 0;
5127 while (getline (in, line)) {
5131 size_t start = 0, size = 0;
5132 const bool commentLine =
5133 checkCommentLine (line, start, size, lineNumber, tolerant);
5134 if (! commentLine) {
5140 if (count >= X_view.size()) {
5145 TEUCHOS_TEST_FOR_EXCEPTION(
5146 count >= X_view.size(),
5148 "The Matrix Market input stream has more data in it than "
5149 "its metadata reported. Current line number is "
5150 << lineNumber <<
".");
5159 const size_t pos = line.substr (start, size).find (
':');
5160 if (pos != std::string::npos) {
5164 std::istringstream istr (line.substr (start, size));
5168 if (istr.eof() || istr.fail()) {
5175 TEUCHOS_TEST_FOR_EXCEPTION(
5176 ! tolerant && (istr.eof() || istr.fail()),
5178 "Line " << lineNumber <<
" of the Matrix Market file is "
5179 "empty, or we cannot read from it for some other reason.");
5182 ST val = STS::zero();
5185 MT real = STM::zero(), imag = STM::zero();
5202 TEUCHOS_TEST_FOR_EXCEPTION(
5203 ! tolerant && istr.eof(), std::runtime_error,
5204 "Failed to get the real part of a complex-valued matrix "
5205 "entry from line " << lineNumber <<
" of the Matrix Market "
5211 }
else if (istr.eof()) {
5212 TEUCHOS_TEST_FOR_EXCEPTION(
5213 ! tolerant && istr.eof(), std::runtime_error,
5214 "Missing imaginary part of a complex-valued matrix entry "
5215 "on line " << lineNumber <<
" of the Matrix Market file.");
5221 TEUCHOS_TEST_FOR_EXCEPTION(
5222 ! tolerant && istr.fail(), std::runtime_error,
5223 "Failed to get the imaginary part of a complex-valued "
5224 "matrix entry from line " << lineNumber <<
" of the "
5225 "Matrix Market file.");
5232 TEUCHOS_TEST_FOR_EXCEPTION(
5233 ! tolerant && istr.fail(), std::runtime_error,
5234 "Failed to get a real-valued matrix entry from line "
5235 << lineNumber <<
" of the Matrix Market file.");
5238 if (istr.fail() && tolerant) {
5244 TEUCHOS_TEST_FOR_EXCEPTION(
5245 ! tolerant && istr.fail(), std::runtime_error,
5246 "Failed to read matrix data from line " << lineNumber
5247 <<
" of the Matrix Market file.");
5250 Teuchos::MatrixMarket::details::assignScalar<ST> (val, real, imag);
5252 curRow = count % numRows;
5253 curCol = count / numRows;
5254 X_view[curRow + curCol*stride] = val;
5259 TEUCHOS_TEST_FOR_EXCEPTION(
5260 ! tolerant && static_cast<global_size_t> (count) < numRows * numCols,
5262 "The Matrix Market metadata reports that the dense matrix is "
5263 << numRows <<
" x " << numCols <<
", and thus has "
5264 << numRows*numCols <<
" total entries, but we only found " << count
5265 <<
" entr" << (count == 1 ?
"y" :
"ies") <<
" in the file.");
5266 }
catch (std::exception& e) {
5268 localReadDataSuccess = 0;
5273 *err << myRank <<
": readVectorImpl: done reading data" << endl;
5277 int globalReadDataSuccess = localReadDataSuccess;
5278 broadcast (*comm, 0, outArg (globalReadDataSuccess));
5279 TEUCHOS_TEST_FOR_EXCEPTION(
5280 globalReadDataSuccess == 0, std::runtime_error,
5281 "Failed to read the multivector's data: " << exMsg.str ());
5286 if (comm->getSize () == 1 && map.is_null ()) {
5288 if (! err.is_null ()) {
5292 *err << myRank <<
": readVectorImpl: done" << endl;
5294 if (! err.is_null ()) {
5301 *err << myRank <<
": readVectorImpl: Creating target MV" << endl;
5305 RCP<MV> Y = createVector<ST, LO, GO, NT> (map);
5308 *err << myRank <<
": readVectorImpl: Creating Export" << endl;
5314 Export<LO, GO, NT> exporter (proc0Map, map, err);
5317 *err << myRank <<
": readVectorImpl: Exporting" << endl;
5320 Y->doExport (*X, exporter,
INSERT);
5322 if (! err.is_null ()) {
5326 *err << myRank <<
": readVectorImpl: done" << endl;
5328 if (! err.is_null ()) {
5356 static Teuchos::RCP<const map_type>
5358 const Teuchos::RCP<const comm_type>& comm,
5359 const bool tolerant=
false,
5360 const bool debug=
false)
5362 Teuchos::RCP<Teuchos::FancyOStream> err =
5363 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
5364 return readMap (in, comm, err, tolerant, debug);
5367 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
5368 static Teuchos::RCP<const map_type> TPETRA_DEPRECATED
5372 const Teuchos::RCP<const comm_type>& comm,
5373 const Teuchos::RCP<node_type>& ,
5374 const bool tolerant=
false,
5375 const bool debug=
false)
5377 return readMap(in, comm, tolerant, debug);
5379 #endif // TPETRA_ENABLE_DEPRECATED_CODE
5406 static Teuchos::RCP<const map_type>
5408 const Teuchos::RCP<const comm_type>& comm,
5409 const Teuchos::RCP<Teuchos::FancyOStream>& err,
5410 const bool tolerant=
false,
5411 const bool debug=
false)
5413 using Teuchos::arcp;
5414 using Teuchos::Array;
5415 using Teuchos::ArrayRCP;
5417 using Teuchos::broadcast;
5418 using Teuchos::Comm;
5419 using Teuchos::CommRequest;
5420 using Teuchos::inOutArg;
5421 using Teuchos::ireceive;
5422 using Teuchos::outArg;
5424 using Teuchos::receive;
5425 using Teuchos::reduceAll;
5426 using Teuchos::REDUCE_MIN;
5427 using Teuchos::isend;
5428 using Teuchos::SerialComm;
5429 using Teuchos::toString;
5430 using Teuchos::wait;
5433 typedef ptrdiff_t int_type;
5439 const int numProcs = comm->getSize ();
5440 const int myRank = comm->getRank ();
5442 if (err.is_null ()) {
5446 std::ostringstream os;
5447 os << myRank <<
": readMap: " << endl;
5450 if (err.is_null ()) {
5456 const int sizeTag = 1339;
5458 const int dataTag = 1340;
5464 RCP<CommRequest<int> > sizeReq;
5465 RCP<CommRequest<int> > dataReq;
5470 ArrayRCP<int_type> numGidsToRecv (1);
5471 numGidsToRecv[0] = 0;
5473 sizeReq = ireceive<int, int_type> (numGidsToRecv, 0, sizeTag, *comm);
5476 int readSuccess = 1;
5477 std::ostringstream exMsg;
5486 RCP<const Comm<int> > proc0Comm (
new SerialComm<int> ());
5488 RCP<const map_type> dataMap;
5492 data = readDenseImpl<GO> (in, proc0Comm, dataMap, err, tolerant, debug);
5494 if (data.is_null ()) {
5496 exMsg <<
"readDenseImpl() returned null." << endl;
5498 }
catch (std::exception& e) {
5500 exMsg << e.what () << endl;
5506 std::map<int, Array<GO> > pid2gids;
5511 int_type globalNumGIDs = 0;
5521 if (myRank == 0 && readSuccess == 1) {
5522 if (data->getNumVectors () == 2) {
5523 ArrayRCP<const GO> GIDs = data->getData (0);
5524 ArrayRCP<const GO> PIDs = data->getData (1);
5525 globalNumGIDs = GIDs.size ();
5529 if (globalNumGIDs > 0) {
5530 const int pid =
static_cast<int> (PIDs[0]);
5532 if (pid < 0 || pid >= numProcs) {
5534 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5535 <<
"Encountered invalid PID " << pid <<
"." << endl;
5538 const GO gid = GIDs[0];
5539 pid2gids[pid].push_back (gid);
5543 if (readSuccess == 1) {
5546 for (size_type k = 1; k < globalNumGIDs; ++k) {
5547 const int pid =
static_cast<int> (PIDs[k]);
5548 if (pid < 0 || pid >= numProcs) {
5550 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5551 <<
"Encountered invalid PID " << pid <<
"." << endl;
5554 const int_type gid = GIDs[k];
5555 pid2gids[pid].push_back (gid);
5556 if (gid < indexBase) {
5563 else if (data->getNumVectors () == 1) {
5564 if (data->getGlobalLength () % 2 !=
static_cast<GST
> (0)) {
5566 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data has the "
5567 "wrong format (for Map format 2.0). The global number of rows "
5568 "in the MultiVector must be even (divisible by 2)." << endl;
5571 ArrayRCP<const GO> theData = data->getData (0);
5572 globalNumGIDs =
static_cast<GO
> (data->getGlobalLength ()) /
5573 static_cast<GO> (2);
5577 if (globalNumGIDs > 0) {
5578 const int pid =
static_cast<int> (theData[1]);
5579 if (pid < 0 || pid >= numProcs) {
5581 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5582 <<
"Encountered invalid PID " << pid <<
"." << endl;
5585 const GO gid = theData[0];
5586 pid2gids[pid].push_back (gid);
5592 for (int_type k = 1; k < globalNumGIDs; ++k) {
5593 const int pid =
static_cast<int> (theData[2*k + 1]);
5594 if (pid < 0 || pid >= numProcs) {
5596 exMsg <<
"Tpetra::MatrixMarket::readMap: "
5597 <<
"Encountered invalid PID " << pid <<
"." << endl;
5600 const GO gid = theData[2*k];
5601 pid2gids[pid].push_back (gid);
5602 if (gid < indexBase) {
5611 exMsg <<
"Tpetra::MatrixMarket::readMap: Input data must have "
5612 "either 1 column (for the new Map format 2.0) or 2 columns (for "
5613 "the old Map format 1.0).";
5620 int_type readResults[3];
5621 readResults[0] =
static_cast<int_type
> (indexBase);
5622 readResults[1] =
static_cast<int_type
> (globalNumGIDs);
5623 readResults[2] =
static_cast<int_type
> (readSuccess);
5624 broadcast<int, int_type> (*comm, 0, 3, readResults);
5626 indexBase =
static_cast<GO
> (readResults[0]);
5627 globalNumGIDs =
static_cast<int_type
> (readResults[1]);
5628 readSuccess =
static_cast<int> (readResults[2]);
5634 TEUCHOS_TEST_FOR_EXCEPTION(
5635 readSuccess != 1, std::runtime_error,
5636 "Tpetra::MatrixMarket::readMap: Reading the Map failed with the "
5637 "following exception message: " << exMsg.str ());
5641 for (
int p = 1; p < numProcs; ++p) {
5642 ArrayRCP<int_type> numGidsToSend (1);
5644 typename std::map<int, Array<GO> >::const_iterator it = pid2gids.find (p);
5645 if (it == pid2gids.end ()) {
5646 numGidsToSend[0] = 0;
5648 numGidsToSend[0] = it->second.size ();
5650 sizeReq = isend<int, int_type> (numGidsToSend, p, sizeTag, *comm);
5651 wait<int> (*comm, outArg (sizeReq));
5656 wait<int> (*comm, outArg (sizeReq));
5661 ArrayRCP<GO> myGids;
5662 int_type myNumGids = 0;
5664 GO* myGidsRaw = NULL;
5666 typename std::map<int, Array<GO> >::iterator it = pid2gids.find (0);
5667 if (it != pid2gids.end ()) {
5668 myGidsRaw = it->second.getRawPtr ();
5669 myNumGids = it->second.size ();
5671 myGids = arcp<GO> (myGidsRaw, 0, myNumGids,
false);
5675 myNumGids = numGidsToRecv[0];
5676 myGids = arcp<GO> (myNumGids);
5683 if (myNumGids > 0) {
5684 dataReq = ireceive<int, GO> (myGids, 0, dataTag, *comm);
5688 for (
int p = 1; p < numProcs; ++p) {
5690 ArrayRCP<GO> sendGids;
5691 GO* sendGidsRaw = NULL;
5692 int_type numSendGids = 0;
5694 typename std::map<int, Array<GO> >::iterator it = pid2gids.find (p);
5695 if (it != pid2gids.end ()) {
5696 numSendGids = it->second.size ();
5697 sendGidsRaw = it->second.getRawPtr ();
5698 sendGids = arcp<GO> (sendGidsRaw, 0, numSendGids,
false);
5701 if (numSendGids > 0) {
5702 dataReq = isend<int, GO> (sendGids, p, dataTag, *comm);
5704 wait<int> (*comm, outArg (dataReq));
5706 else if (myRank == p) {
5708 wait<int> (*comm, outArg (dataReq));
5713 std::ostringstream os;
5714 os << myRank <<
": readMap: creating Map" << endl;
5717 const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
5718 RCP<const map_type> newMap;
5725 std::ostringstream errStrm;
5727 newMap = rcp (
new map_type (INVALID, myGids (), indexBase, comm));
5729 catch (std::exception& e) {
5731 errStrm <<
"Process " << comm->getRank () <<
" threw an exception: "
5732 << e.what () << std::endl;
5736 errStrm <<
"Process " << comm->getRank () <<
" threw an exception "
5737 "not a subclass of std::exception" << std::endl;
5739 Teuchos::reduceAll<int, int> (*comm, Teuchos::REDUCE_MIN,
5740 lclSuccess, Teuchos::outArg (gblSuccess));
5741 if (gblSuccess != 1) {
5744 TEUCHOS_TEST_FOR_EXCEPTION(gblSuccess != 1, std::runtime_error,
"Map constructor failed!");
5746 if (err.is_null ()) {
5750 std::ostringstream os;
5751 os << myRank <<
": readMap: done" << endl;
5754 if (err.is_null ()) {
5760 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
5761 static Teuchos::RCP<const map_type> TPETRA_DEPRECATED
5765 const Teuchos::RCP<const comm_type>& comm,
5766 const Teuchos::RCP<node_type>& ,
5767 const Teuchos::RCP<Teuchos::FancyOStream>& err,
5768 const bool tolerant=
false,
5769 const bool debug=
false)
5771 return readMap (in, comm, err, tolerant, debug);
5773 #endif // TPETRA_ENABLE_DEPRECATED_CODE
5788 encodeDataType (
const std::string& dataType)
5790 if (dataType ==
"real" || dataType ==
"integer") {
5792 }
else if (dataType ==
"complex") {
5794 }
else if (dataType ==
"pattern") {
5800 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
5801 "Unrecognized Matrix Market data type \"" << dataType
5802 <<
"\". We should never get here. "
5803 "Please report this bug to the Tpetra developers.");
5836 template<
class SparseMatrixType>
5841 typedef Teuchos::RCP<sparse_matrix_type> sparse_matrix_ptr;
5902 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
5903 const std::string& matrixName,
5904 const std::string& matrixDescription,
5905 const bool debug=
false)
5907 TEUCHOS_TEST_FOR_EXCEPTION(
5908 pMatrix.is_null (), std::invalid_argument,
5909 "The input matrix is null.");
5910 Teuchos::RCP<const Teuchos::Comm<int> > comm = pMatrix->getComm ();
5911 TEUCHOS_TEST_FOR_EXCEPTION(
5912 comm.is_null (), std::invalid_argument,
5913 "The input matrix's communicator (Teuchos::Comm object) is null.");
5914 const int myRank = comm->getRank ();
5920 out.open (filename.c_str ());
5922 writeSparse (out, pMatrix, matrixName, matrixDescription, debug);
5950 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
5951 const bool debug=
false)
5988 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
5989 const std::string& matrixName,
5990 const std::string& matrixDescription,
5991 const bool debug=
false)
5993 using Teuchos::ArrayView;
5994 using Teuchos::Comm;
5995 using Teuchos::FancyOStream;
5996 using Teuchos::getFancyOStream;
5997 using Teuchos::null;
5999 using Teuchos::rcpFromRef;
6002 typedef scalar_type ST;
6005 typedef typename Teuchos::ScalarTraits<ST> STS;
6006 typedef typename ArrayView<const LO>::const_iterator lo_iter;
6007 typedef typename ArrayView<const GO>::const_iterator go_iter;
6008 typedef typename ArrayView<const ST>::const_iterator st_iter;
6010 TEUCHOS_TEST_FOR_EXCEPTION(
6011 pMatrix.is_null (), std::invalid_argument,
6012 "The input matrix is null.");
6018 Teuchos::SetScientific<ST> sci (out);
6021 RCP<const Comm<int> > comm = pMatrix->getComm ();
6022 TEUCHOS_TEST_FOR_EXCEPTION(
6023 comm.is_null (), std::invalid_argument,
6024 "The input matrix's communicator (Teuchos::Comm object) is null.");
6025 const int myRank = comm->getRank ();
6028 RCP<FancyOStream> err =
6029 debug ? getFancyOStream (rcpFromRef (std::cerr)) : null;
6031 std::ostringstream os;
6032 os << myRank <<
": writeSparse" << endl;
6035 os <<
"-- " << myRank <<
": past barrier" << endl;
6040 const bool debugPrint = debug && myRank == 0;
6042 RCP<const map_type> rowMap = pMatrix->getRowMap ();
6043 RCP<const map_type> colMap = pMatrix->getColMap ();
6044 RCP<const map_type> domainMap = pMatrix->getDomainMap ();
6045 RCP<const map_type> rangeMap = pMatrix->getRangeMap ();
6047 const global_size_t numRows = rangeMap->getGlobalNumElements ();
6048 const global_size_t numCols = domainMap->getGlobalNumElements ();
6050 if (debug && myRank == 0) {
6051 std::ostringstream os;
6052 os <<
"-- Input sparse matrix is:"
6053 <<
"---- " << numRows <<
" x " << numCols << endl
6055 << (pMatrix->isGloballyIndexed() ?
"Globally" :
"Locally")
6056 <<
" indexed." << endl
6057 <<
"---- Its row map has " << rowMap->getGlobalNumElements ()
6058 <<
" elements." << endl
6059 <<
"---- Its col map has " << colMap->getGlobalNumElements ()
6060 <<
" elements." << endl;
6065 const size_t localNumRows = (myRank == 0) ? numRows : 0;
6067 std::ostringstream os;
6068 os <<
"-- " << myRank <<
": making gatherRowMap" << endl;
6071 RCP<const map_type> gatherRowMap =
6072 Details::computeGatherMap (rowMap, err, debug);
6080 const size_t localNumCols = (myRank == 0) ? numCols : 0;
6081 RCP<const map_type> gatherColMap =
6082 Details::computeGatherMap (colMap, err, debug);
6086 import_type importer (rowMap, gatherRowMap);
6091 RCP<sparse_matrix_type> newMatrix =
6093 static_cast<size_t> (0)));
6095 newMatrix->doImport (*pMatrix, importer,
INSERT);
6100 RCP<const map_type> gatherDomainMap =
6101 rcp (
new map_type (numCols, localNumCols,
6102 domainMap->getIndexBase (),
6104 RCP<const map_type> gatherRangeMap =
6105 rcp (
new map_type (numRows, localNumRows,
6106 rangeMap->getIndexBase (),
6108 newMatrix->fillComplete (gatherDomainMap, gatherRangeMap);
6112 cerr <<
"-- Output sparse matrix is:"
6113 <<
"---- " << newMatrix->getRangeMap ()->getGlobalNumElements ()
6115 << newMatrix->getDomainMap ()->getGlobalNumElements ()
6117 << newMatrix->getGlobalNumEntries () <<
" entries;" << endl
6119 << (newMatrix->isGloballyIndexed () ?
"Globally" :
"Locally")
6120 <<
" indexed." << endl
6121 <<
"---- Its row map has "
6122 << newMatrix->getRowMap ()->getGlobalNumElements ()
6123 <<
" elements, with index base "
6124 << newMatrix->getRowMap ()->getIndexBase () <<
"." << endl
6125 <<
"---- Its col map has "
6126 << newMatrix->getColMap ()->getGlobalNumElements ()
6127 <<
" elements, with index base "
6128 << newMatrix->getColMap ()->getIndexBase () <<
"." << endl
6129 <<
"---- Element count of output matrix's column Map may differ "
6130 <<
"from that of the input matrix's column Map, if some columns "
6131 <<
"of the matrix contain no entries." << endl;
6144 out <<
"%%MatrixMarket matrix coordinate "
6145 << (STS::isComplex ?
"complex" :
"real")
6146 <<
" general" << endl;
6149 if (matrixName !=
"") {
6150 printAsComment (out, matrixName);
6152 if (matrixDescription !=
"") {
6153 printAsComment (out, matrixDescription);
6163 out << newMatrix->getRangeMap ()->getGlobalNumElements () <<
" "
6164 << newMatrix->getDomainMap ()->getGlobalNumElements () <<
" "
6165 << newMatrix->getGlobalNumEntries () << endl;
6170 const GO rowIndexBase = gatherRowMap->getIndexBase ();
6171 const GO colIndexBase = newMatrix->getColMap()->getIndexBase ();
6179 if (newMatrix->isGloballyIndexed()) {
6182 const GO minAllGlobalIndex = gatherRowMap->getMinAllGlobalIndex ();
6183 const GO maxAllGlobalIndex = gatherRowMap->getMaxAllGlobalIndex ();
6184 for (GO globalRowIndex = minAllGlobalIndex;
6185 globalRowIndex <= maxAllGlobalIndex;
6187 ArrayView<const GO> ind;
6188 ArrayView<const ST> val;
6189 newMatrix->getGlobalRowView (globalRowIndex, ind, val);
6190 go_iter indIter = ind.begin ();
6191 st_iter valIter = val.begin ();
6192 for (; indIter != ind.end() && valIter != val.end();
6193 ++indIter, ++valIter) {
6194 const GO globalColIndex = *indIter;
6197 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6198 << (globalColIndex + 1 - colIndexBase) <<
" ";
6199 if (STS::isComplex) {
6200 out << STS::real (*valIter) <<
" " << STS::imag (*valIter);
6208 typedef Teuchos::OrdinalTraits<GO> OTG;
6209 for (LO localRowIndex = gatherRowMap->getMinLocalIndex();
6210 localRowIndex <= gatherRowMap->getMaxLocalIndex();
6213 const GO globalRowIndex =
6214 gatherRowMap->getGlobalElement (localRowIndex);
6215 TEUCHOS_TEST_FOR_EXCEPTION(
6216 globalRowIndex == OTG::invalid(), std::logic_error,
6217 "Failed to convert the supposed local row index "
6218 << localRowIndex <<
" into a global row index. "
6219 "Please report this bug to the Tpetra developers.");
6220 ArrayView<const LO> ind;
6221 ArrayView<const ST> val;
6222 newMatrix->getLocalRowView (localRowIndex, ind, val);
6223 lo_iter indIter = ind.begin ();
6224 st_iter valIter = val.begin ();
6225 for (; indIter != ind.end() && valIter != val.end();
6226 ++indIter, ++valIter) {
6228 const GO globalColIndex =
6229 newMatrix->getColMap()->getGlobalElement (*indIter);
6230 TEUCHOS_TEST_FOR_EXCEPTION(
6231 globalColIndex == OTG::invalid(), std::logic_error,
6232 "On local row " << localRowIndex <<
" of the sparse matrix: "
6233 "Failed to convert the supposed local column index "
6234 << *indIter <<
" into a global column index. Please report "
6235 "this bug to the Tpetra developers.");
6238 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6239 << (globalColIndex + 1 - colIndexBase) <<
" ";
6240 if (STS::isComplex) {
6241 out << STS::real (*valIter) <<
" " << STS::imag (*valIter);
6286 const std::string& graphName,
6287 const std::string& graphDescription,
6288 const bool debug=
false)
6290 using Teuchos::ArrayView;
6291 using Teuchos::Comm;
6292 using Teuchos::FancyOStream;
6293 using Teuchos::getFancyOStream;
6294 using Teuchos::null;
6296 using Teuchos::rcpFromRef;
6307 if (rowMap.is_null ()) {
6310 auto comm = rowMap->getComm ();
6311 if (comm.is_null ()) {
6314 const int myRank = comm->getRank ();
6317 RCP<FancyOStream> err =
6318 debug ? getFancyOStream (rcpFromRef (std::cerr)) : null;
6320 std::ostringstream os;
6321 os << myRank <<
": writeSparseGraph" << endl;
6324 os <<
"-- " << myRank <<
": past barrier" << endl;
6329 const bool debugPrint = debug && myRank == 0;
6336 const global_size_t numRows = rangeMap->getGlobalNumElements ();
6337 const global_size_t numCols = domainMap->getGlobalNumElements ();
6339 if (debug && myRank == 0) {
6340 std::ostringstream os;
6341 os <<
"-- Input sparse graph is:"
6342 <<
"---- " << numRows <<
" x " << numCols <<
" with "
6346 <<
" indexed." << endl
6347 <<
"---- Its row Map has " << rowMap->getGlobalNumElements ()
6348 <<
" elements." << endl
6349 <<
"---- Its col Map has " << colMap->getGlobalNumElements ()
6350 <<
" elements." << endl;
6355 const size_t localNumRows = (myRank == 0) ? numRows : 0;
6357 std::ostringstream os;
6358 os <<
"-- " << myRank <<
": making gatherRowMap" << endl;
6361 auto gatherRowMap = Details::computeGatherMap (rowMap, err, debug);
6369 const size_t localNumCols = (myRank == 0) ? numCols : 0;
6370 auto gatherColMap = Details::computeGatherMap (colMap, err, debug);
6379 static_cast<size_t> (0));
6386 RCP<const map_type> gatherDomainMap =
6387 rcp (
new map_type (numCols, localNumCols,
6388 domainMap->getIndexBase (),
6390 RCP<const map_type> gatherRangeMap =
6391 rcp (
new map_type (numRows, localNumRows,
6392 rangeMap->getIndexBase (),
6394 newGraph.
fillComplete (gatherDomainMap, gatherRangeMap);
6398 cerr <<
"-- Output sparse graph is:"
6399 <<
"---- " << newGraph.
getRangeMap ()->getGlobalNumElements ()
6406 <<
" indexed." << endl
6407 <<
"---- Its row map has "
6408 << newGraph.
getRowMap ()->getGlobalNumElements ()
6409 <<
" elements, with index base "
6410 << newGraph.
getRowMap ()->getIndexBase () <<
"." << endl
6411 <<
"---- Its col map has "
6412 << newGraph.
getColMap ()->getGlobalNumElements ()
6413 <<
" elements, with index base "
6414 << newGraph.
getColMap ()->getIndexBase () <<
"." << endl
6415 <<
"---- Element count of output graph's column Map may differ "
6416 <<
"from that of the input matrix's column Map, if some columns "
6417 <<
"of the matrix contain no entries." << endl;
6431 out <<
"%%MatrixMarket matrix coordinate pattern general" << endl;
6434 if (graphName !=
"") {
6435 printAsComment (out, graphName);
6437 if (graphDescription !=
"") {
6438 printAsComment (out, graphDescription);
6449 out << newGraph.
getRangeMap ()->getGlobalNumElements () <<
" "
6450 << newGraph.
getDomainMap ()->getGlobalNumElements () <<
" "
6456 const GO rowIndexBase = gatherRowMap->getIndexBase ();
6457 const GO colIndexBase = newGraph.
getColMap()->getIndexBase ();
6468 const GO minAllGlobalIndex = gatherRowMap->getMinAllGlobalIndex ();
6469 const GO maxAllGlobalIndex = gatherRowMap->getMaxAllGlobalIndex ();
6470 for (GO globalRowIndex = minAllGlobalIndex;
6471 globalRowIndex <= maxAllGlobalIndex;
6473 ArrayView<const GO> ind;
6475 for (
auto indIter = ind.begin (); indIter != ind.end (); ++indIter) {
6476 const GO globalColIndex = *indIter;
6479 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6480 << (globalColIndex + 1 - colIndexBase) <<
" ";
6486 typedef Teuchos::OrdinalTraits<GO> OTG;
6487 for (LO localRowIndex = gatherRowMap->getMinLocalIndex ();
6488 localRowIndex <= gatherRowMap->getMaxLocalIndex ();
6491 const GO globalRowIndex =
6492 gatherRowMap->getGlobalElement (localRowIndex);
6493 TEUCHOS_TEST_FOR_EXCEPTION
6494 (globalRowIndex == OTG::invalid (), std::logic_error,
"Failed "
6495 "to convert the supposed local row index " << localRowIndex <<
6496 " into a global row index. Please report this bug to the "
6497 "Tpetra developers.");
6498 ArrayView<const LO> ind;
6500 for (
auto indIter = ind.begin (); indIter != ind.end (); ++indIter) {
6502 const GO globalColIndex =
6503 newGraph.
getColMap ()->getGlobalElement (*indIter);
6504 TEUCHOS_TEST_FOR_EXCEPTION(
6505 globalColIndex == OTG::invalid(), std::logic_error,
6506 "On local row " << localRowIndex <<
" of the sparse graph: "
6507 "Failed to convert the supposed local column index "
6508 << *indIter <<
" into a global column index. Please report "
6509 "this bug to the Tpetra developers.");
6512 out << (globalRowIndex + 1 - rowIndexBase) <<
" "
6513 << (globalColIndex + 1 - colIndexBase) <<
" ";
6529 const bool debug=
false)
6571 const std::string& graphName,
6572 const std::string& graphDescription,
6573 const bool debug=
false)
6576 if (comm.is_null ()) {
6584 const int myRank = comm->getRank ();
6589 out.open (filename.c_str ());
6604 const bool debug=
false)
6619 const Teuchos::RCP<const crs_graph_type>& pGraph,
6620 const std::string& graphName,
6621 const std::string& graphDescription,
6622 const bool debug=
false)
6638 const Teuchos::RCP<const crs_graph_type>& pGraph,
6639 const bool debug=
false)
6668 const Teuchos::RCP<const sparse_matrix_type>& pMatrix,
6669 const bool debug=
false)
6705 const std::string& matrixName,
6706 const std::string& matrixDescription,
6707 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6708 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6710 const int myRank = X.
getMap ().is_null () ? 0 :
6711 (X.
getMap ()->getComm ().is_null () ? 0 :
6712 X.
getMap ()->getComm ()->getRank ());
6716 out.open (filename.c_str());
6719 writeDense (out, X, matrixName, matrixDescription, err, dbg);
6732 const Teuchos::RCP<const multivector_type>& X,
6733 const std::string& matrixName,
6734 const std::string& matrixDescription,
6735 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6736 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6738 TEUCHOS_TEST_FOR_EXCEPTION(
6739 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6740 "writeDenseFile: The input MultiVector X is null.");
6741 writeDenseFile (filename, *X, matrixName, matrixDescription, err, dbg);
6752 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6753 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6765 const Teuchos::RCP<const multivector_type>& X,
6766 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6767 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6769 TEUCHOS_TEST_FOR_EXCEPTION(
6770 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
6771 "writeDenseFile: The input MultiVector X is null.");
6809 const std::string& matrixName,
6810 const std::string& matrixDescription,
6811 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6812 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6814 using Teuchos::Comm;
6815 using Teuchos::outArg;
6816 using Teuchos::REDUCE_MAX;
6817 using Teuchos::reduceAll;
6821 RCP<const Comm<int> > comm = X.
getMap ().is_null () ?
6822 Teuchos::null : X.
getMap ()->getComm ();
6823 const int myRank = comm.is_null () ? 0 : comm->getRank ();
6828 const bool debug = ! dbg.is_null ();
6831 std::ostringstream os;
6832 os << myRank <<
": writeDense" << endl;
6837 writeDenseHeader (out, X, matrixName, matrixDescription, err, dbg);
6842 for (
size_t j = 0; j < numVecs; ++j) {
6843 writeDenseColumn (out, * (X.
getVector (j)), err, dbg);
6848 std::ostringstream os;
6849 os << myRank <<
": writeDense: Done" << endl;
6883 writeDenseHeader (std::ostream& out,
6885 const std::string& matrixName,
6886 const std::string& matrixDescription,
6887 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
6888 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
6890 using Teuchos::Comm;
6891 using Teuchos::outArg;
6893 using Teuchos::REDUCE_MAX;
6894 using Teuchos::reduceAll;
6896 typedef Teuchos::ScalarTraits<scalar_type> STS;
6897 const char prefix[] =
"Tpetra::MatrixMarket::writeDenseHeader: ";
6899 RCP<const Comm<int> > comm = X.getMap ().is_null () ?
6900 Teuchos::null : X.getMap ()->getComm ();
6901 const int myRank = comm.is_null () ? 0 : comm->getRank ();
6908 const bool debug = ! dbg.is_null ();
6912 std::ostringstream os;
6913 os << myRank <<
": writeDenseHeader" << endl;
6932 std::ostringstream hdr;
6934 std::string dataType;
6935 if (STS::isComplex) {
6936 dataType =
"complex";
6937 }
else if (STS::isOrdinal) {
6938 dataType =
"integer";
6942 hdr <<
"%%MatrixMarket matrix array " << dataType <<
" general"
6947 if (matrixName !=
"") {
6948 printAsComment (hdr, matrixName);
6950 if (matrixDescription !=
"") {
6951 printAsComment (hdr, matrixDescription);
6954 hdr << X.getGlobalLength () <<
" " << X.getNumVectors () << endl;
6958 }
catch (std::exception& e) {
6959 if (! err.is_null ()) {
6960 *err << prefix <<
"While writing the Matrix Market header, "
6961 "Process 0 threw an exception: " << e.what () << endl;
6970 reduceAll<int, int> (*comm, REDUCE_MAX, lclErr, outArg (gblErr));
6971 TEUCHOS_TEST_FOR_EXCEPTION(
6972 gblErr == 1, std::runtime_error, prefix <<
"Some error occurred "
6973 "which prevented this method from completing.");
6977 *dbg << myRank <<
": writeDenseHeader: Done" << endl;
7000 writeDenseColumn (std::ostream& out,
7002 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7003 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7005 using Teuchos::arcp;
7006 using Teuchos::Array;
7007 using Teuchos::ArrayRCP;
7008 using Teuchos::ArrayView;
7009 using Teuchos::Comm;
7010 using Teuchos::CommRequest;
7011 using Teuchos::ireceive;
7012 using Teuchos::isend;
7013 using Teuchos::outArg;
7014 using Teuchos::REDUCE_MAX;
7015 using Teuchos::reduceAll;
7017 using Teuchos::TypeNameTraits;
7018 using Teuchos::wait;
7020 typedef Teuchos::ScalarTraits<scalar_type> STS;
7022 const Comm<int>& comm = * (X.getMap ()->getComm ());
7023 const int myRank = comm.getRank ();
7024 const int numProcs = comm.getSize ();
7031 const bool debug = ! dbg.is_null ();
7035 std::ostringstream os;
7036 os << myRank <<
": writeDenseColumn" << endl;
7045 Teuchos::SetScientific<scalar_type> sci (out);
7047 const size_t myNumRows = X.getLocalLength ();
7048 const size_t numCols = X.getNumVectors ();
7051 const int sizeTag = 1337;
7052 const int dataTag = 1338;
7113 RCP<CommRequest<int> > sendReqSize, sendReqData;
7119 Array<ArrayRCP<size_t> > recvSizeBufs (3);
7120 Array<ArrayRCP<scalar_type> > recvDataBufs (3);
7121 Array<RCP<CommRequest<int> > > recvSizeReqs (3);
7122 Array<RCP<CommRequest<int> > > recvDataReqs (3);
7125 ArrayRCP<size_t> sendDataSize (1);
7126 sendDataSize[0] = myNumRows;
7130 std::ostringstream os;
7131 os << myRank <<
": Post receive-size receives from "
7132 "Procs 1 and 2: tag = " << sizeTag << endl;
7136 recvSizeBufs[0].resize (1);
7140 (recvSizeBufs[0])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7141 recvSizeBufs[1].resize (1);
7142 (recvSizeBufs[1])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7143 recvSizeBufs[2].resize (1);
7144 (recvSizeBufs[2])[0] = Teuchos::OrdinalTraits<size_t>::invalid ();
7147 ireceive<int, size_t> (recvSizeBufs[1], 1, sizeTag, comm);
7151 ireceive<int, size_t> (recvSizeBufs[2], 2, sizeTag, comm);
7154 else if (myRank == 1 || myRank == 2) {
7156 std::ostringstream os;
7157 os << myRank <<
": Post send-size send: size = "
7158 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7165 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7169 std::ostringstream os;
7170 os << myRank <<
": Not posting my send-size send yet" << endl;
7179 std::ostringstream os;
7180 os << myRank <<
": Pack my entries" << endl;
7183 ArrayRCP<scalar_type> sendDataBuf;
7185 sendDataBuf = arcp<scalar_type> (myNumRows * numCols);
7186 X.get1dCopy (sendDataBuf (), myNumRows);
7188 catch (std::exception& e) {
7190 if (! err.is_null ()) {
7191 std::ostringstream os;
7192 os <<
"Process " << myRank <<
": Attempt to pack my MultiVector "
7193 "entries threw an exception: " << e.what () << endl;
7198 std::ostringstream os;
7199 os << myRank <<
": Done packing my entries" << endl;
7208 *dbg << myRank <<
": Post send-data send: tag = " << dataTag
7211 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7219 std::ostringstream os;
7220 os << myRank <<
": Write my entries" << endl;
7226 const size_t printNumRows = myNumRows;
7227 ArrayView<const scalar_type> printData = sendDataBuf ();
7228 const size_t printStride = printNumRows;
7229 if (static_cast<size_t> (printData.size ()) < printStride * numCols) {
7231 if (! err.is_null ()) {
7232 std::ostringstream os;
7233 os <<
"Process " << myRank <<
": My MultiVector data's size "
7234 << printData.size () <<
" does not match my local dimensions "
7235 << printStride <<
" x " << numCols <<
"." << endl;
7243 for (
size_t col = 0; col < numCols; ++col) {
7244 for (
size_t row = 0; row < printNumRows; ++row) {
7245 if (STS::isComplex) {
7246 out << STS::real (printData[row + col * printStride]) <<
" "
7247 << STS::imag (printData[row + col * printStride]) << endl;
7249 out << printData[row + col * printStride] << endl;
7258 const int recvRank = 1;
7259 const int circBufInd = recvRank % 3;
7261 std::ostringstream os;
7262 os << myRank <<
": Wait on receive-size receive from Process "
7263 << recvRank << endl;
7267 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7271 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7272 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7274 if (! err.is_null ()) {
7275 std::ostringstream os;
7276 os << myRank <<
": Result of receive-size receive from Process "
7277 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7278 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7279 "This should never happen, and suggests that the receive never "
7280 "got posted. Please report this bug to the Tpetra developers."
7295 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7297 std::ostringstream os;
7298 os << myRank <<
": Post receive-data receive from Process "
7299 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7300 << recvDataBufs[circBufInd].size () << endl;
7303 if (! recvSizeReqs[circBufInd].is_null ()) {
7305 if (! err.is_null ()) {
7306 std::ostringstream os;
7307 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7308 "null, before posting the receive-data receive from Process "
7309 << recvRank <<
". This should never happen. Please report "
7310 "this bug to the Tpetra developers." << endl;
7314 recvDataReqs[circBufInd] =
7315 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7316 recvRank, dataTag, comm);
7319 else if (myRank == 1) {
7322 std::ostringstream os;
7323 os << myRank <<
": Wait on my send-size send" << endl;
7326 wait<int> (comm, outArg (sendReqSize));
7332 for (
int p = 1; p < numProcs; ++p) {
7334 if (p + 2 < numProcs) {
7336 const int recvRank = p + 2;
7337 const int circBufInd = recvRank % 3;
7339 std::ostringstream os;
7340 os << myRank <<
": Post receive-size receive from Process "
7341 << recvRank <<
": tag = " << sizeTag << endl;
7344 if (! recvSizeReqs[circBufInd].is_null ()) {
7346 if (! err.is_null ()) {
7347 std::ostringstream os;
7348 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7349 <<
"null, for the receive-size receive from Process "
7350 << recvRank <<
"! This may mean that this process never "
7351 <<
"finished waiting for the receive from Process "
7352 << (recvRank - 3) <<
"." << endl;
7356 recvSizeReqs[circBufInd] =
7357 ireceive<int, size_t> (recvSizeBufs[circBufInd],
7358 recvRank, sizeTag, comm);
7361 if (p + 1 < numProcs) {
7362 const int recvRank = p + 1;
7363 const int circBufInd = recvRank % 3;
7367 std::ostringstream os;
7368 os << myRank <<
": Wait on receive-size receive from Process "
7369 << recvRank << endl;
7372 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7376 size_t recvNumRows = (recvSizeBufs[circBufInd])[0];
7377 if (recvNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7379 if (! err.is_null ()) {
7380 std::ostringstream os;
7381 os << myRank <<
": Result of receive-size receive from Process "
7382 << recvRank <<
" is Teuchos::OrdinalTraits<size_t>::invalid() "
7383 <<
"= " << Teuchos::OrdinalTraits<size_t>::invalid () <<
". "
7384 "This should never happen, and suggests that the receive never "
7385 "got posted. Please report this bug to the Tpetra developers."
7399 recvDataBufs[circBufInd].resize (recvNumRows * numCols);
7401 std::ostringstream os;
7402 os << myRank <<
": Post receive-data receive from Process "
7403 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7404 << recvDataBufs[circBufInd].size () << endl;
7407 if (! recvDataReqs[circBufInd].is_null ()) {
7409 if (! err.is_null ()) {
7410 std::ostringstream os;
7411 os << myRank <<
": recvDataReqs[" << circBufInd <<
"] is not "
7412 <<
"null, for the receive-data receive from Process "
7413 << recvRank <<
"! This may mean that this process never "
7414 <<
"finished waiting for the receive from Process "
7415 << (recvRank - 3) <<
"." << endl;
7419 recvDataReqs[circBufInd] =
7420 ireceive<int, scalar_type> (recvDataBufs[circBufInd],
7421 recvRank, dataTag, comm);
7425 const int recvRank = p;
7426 const int circBufInd = recvRank % 3;
7428 std::ostringstream os;
7429 os << myRank <<
": Wait on receive-data receive from Process "
7430 << recvRank << endl;
7433 wait<int> (comm, outArg (recvDataReqs[circBufInd]));
7440 std::ostringstream os;
7441 os << myRank <<
": Write entries from Process " << recvRank
7443 *dbg << os.str () << endl;
7445 size_t printNumRows = (recvSizeBufs[circBufInd])[0];
7446 if (printNumRows == Teuchos::OrdinalTraits<size_t>::invalid ()) {
7448 if (! err.is_null ()) {
7449 std::ostringstream os;
7450 os << myRank <<
": Result of receive-size receive from Process "
7451 << recvRank <<
" was Teuchos::OrdinalTraits<size_t>::"
7452 "invalid() = " << Teuchos::OrdinalTraits<size_t>::invalid ()
7453 <<
". This should never happen, and suggests that its "
7454 "receive-size receive was never posted. "
7455 "Please report this bug to the Tpetra developers." << endl;
7462 if (printNumRows > 0 && recvDataBufs[circBufInd].is_null ()) {
7464 if (! err.is_null ()) {
7465 std::ostringstream os;
7466 os << myRank <<
": Result of receive-size receive from Proc "
7467 << recvRank <<
" was " << printNumRows <<
" > 0, but "
7468 "recvDataBufs[" << circBufInd <<
"] is null. This should "
7469 "never happen. Please report this bug to the Tpetra "
7470 "developers." << endl;
7480 ArrayView<const scalar_type> printData = (recvDataBufs[circBufInd]) ();
7481 const size_t printStride = printNumRows;
7485 for (
size_t col = 0; col < numCols; ++col) {
7486 for (
size_t row = 0; row < printNumRows; ++row) {
7487 if (STS::isComplex) {
7488 out << STS::real (printData[row + col * printStride]) <<
" "
7489 << STS::imag (printData[row + col * printStride]) << endl;
7491 out << printData[row + col * printStride] << endl;
7496 else if (myRank == p) {
7499 std::ostringstream os;
7500 os << myRank <<
": Wait on my send-data send" << endl;
7503 wait<int> (comm, outArg (sendReqData));
7505 else if (myRank == p + 1) {
7508 std::ostringstream os;
7509 os << myRank <<
": Post send-data send: tag = " << dataTag
7513 sendReqData = isend<int, scalar_type> (sendDataBuf, 0, dataTag, comm);
7516 std::ostringstream os;
7517 os << myRank <<
": Wait on my send-size send" << endl;
7520 wait<int> (comm, outArg (sendReqSize));
7522 else if (myRank == p + 2) {
7525 std::ostringstream os;
7526 os << myRank <<
": Post send-size send: size = "
7527 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7530 sendReqSize = isend<int, size_t> (sendDataSize, 0, sizeTag, comm);
7535 reduceAll<int, int> (comm, REDUCE_MAX, lclErr, outArg (gblErr));
7536 TEUCHOS_TEST_FOR_EXCEPTION(
7537 gblErr == 1, std::runtime_error,
"Tpetra::MatrixMarket::writeDense "
7538 "experienced some kind of error and was unable to complete.");
7542 *dbg << myRank <<
": writeDenseColumn: Done" << endl;
7556 const Teuchos::RCP<const multivector_type>& X,
7557 const std::string& matrixName,
7558 const std::string& matrixDescription,
7559 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7560 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7562 TEUCHOS_TEST_FOR_EXCEPTION(
7563 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7564 "writeDense: The input MultiVector X is null.");
7565 writeDense (out, *X, matrixName, matrixDescription, err, dbg);
7576 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7577 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7589 const Teuchos::RCP<const multivector_type>& X,
7590 const Teuchos::RCP<Teuchos::FancyOStream>& err = Teuchos::null,
7591 const Teuchos::RCP<Teuchos::FancyOStream>& dbg = Teuchos::null)
7593 TEUCHOS_TEST_FOR_EXCEPTION(
7594 X.is_null (), std::invalid_argument,
"Tpetra::MatrixMarket::"
7595 "writeDense: The input MultiVector X is null.");
7621 Teuchos::RCP<Teuchos::FancyOStream> err =
7622 Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr));
7637 const Teuchos::RCP<Teuchos::FancyOStream>& err,
7638 const bool debug=
false)
7640 using Teuchos::Array;
7641 using Teuchos::ArrayRCP;
7642 using Teuchos::ArrayView;
7643 using Teuchos::Comm;
7644 using Teuchos::CommRequest;
7645 using Teuchos::ireceive;
7646 using Teuchos::isend;
7648 using Teuchos::TypeNameTraits;
7649 using Teuchos::wait;
7652 typedef int pid_type;
7667 typedef ptrdiff_t int_type;
7668 TEUCHOS_TEST_FOR_EXCEPTION(
7669 sizeof (GO) >
sizeof (int_type), std::logic_error,
7670 "The global ordinal type GO=" << TypeNameTraits<GO>::name ()
7671 <<
" is too big for ptrdiff_t. sizeof(GO) = " <<
sizeof (GO)
7672 <<
" > sizeof(ptrdiff_t) = " <<
sizeof (ptrdiff_t) <<
".");
7673 TEUCHOS_TEST_FOR_EXCEPTION(
7674 sizeof (pid_type) >
sizeof (int_type), std::logic_error,
7675 "The (MPI) process rank type pid_type=" <<
7676 TypeNameTraits<pid_type>::name () <<
" is too big for ptrdiff_t. "
7677 "sizeof(pid_type) = " <<
sizeof (pid_type) <<
" > sizeof(ptrdiff_t)"
7678 " = " <<
sizeof (ptrdiff_t) <<
".");
7680 const Comm<int>& comm = * (map.
getComm ());
7681 const int myRank = comm.getRank ();
7682 const int numProcs = comm.getSize ();
7684 if (! err.is_null ()) {
7688 std::ostringstream os;
7689 os << myRank <<
": writeMap" << endl;
7692 if (! err.is_null ()) {
7699 const int sizeTag = 1337;
7700 const int dataTag = 1338;
7759 RCP<CommRequest<int> > sendReqSize, sendReqData;
7765 Array<ArrayRCP<int_type> > recvSizeBufs (3);
7766 Array<ArrayRCP<int_type> > recvDataBufs (3);
7767 Array<RCP<CommRequest<int> > > recvSizeReqs (3);
7768 Array<RCP<CommRequest<int> > > recvDataReqs (3);
7771 ArrayRCP<int_type> sendDataSize (1);
7772 sendDataSize[0] = myNumRows;
7776 std::ostringstream os;
7777 os << myRank <<
": Post receive-size receives from "
7778 "Procs 1 and 2: tag = " << sizeTag << endl;
7782 recvSizeBufs[0].resize (1);
7783 (recvSizeBufs[0])[0] = -1;
7784 recvSizeBufs[1].resize (1);
7785 (recvSizeBufs[1])[0] = -1;
7786 recvSizeBufs[2].resize (1);
7787 (recvSizeBufs[2])[0] = -1;
7790 ireceive<int, int_type> (recvSizeBufs[1], 1, sizeTag, comm);
7794 ireceive<int, int_type> (recvSizeBufs[2], 2, sizeTag, comm);
7797 else if (myRank == 1 || myRank == 2) {
7799 std::ostringstream os;
7800 os << myRank <<
": Post send-size send: size = "
7801 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
7808 sendReqSize = isend<int, int_type> (sendDataSize, 0, sizeTag, comm);
7812 std::ostringstream os;
7813 os << myRank <<
": Not posting my send-size send yet" << endl;
7824 std::ostringstream os;
7825 os << myRank <<
": Pack my GIDs and PIDs" << endl;
7829 ArrayRCP<int_type> sendDataBuf (myNumRows * 2);
7832 const int_type myMinGblIdx =
7834 for (
size_t k = 0; k < myNumRows; ++k) {
7835 const int_type gid = myMinGblIdx +
static_cast<int_type
> (k);
7836 const int_type pid =
static_cast<int_type
> (myRank);
7837 sendDataBuf[2*k] = gid;
7838 sendDataBuf[2*k+1] = pid;
7843 for (
size_t k = 0; k < myNumRows; ++k) {
7844 const int_type gid =
static_cast<int_type
> (myGblInds[k]);
7845 const int_type pid =
static_cast<int_type
> (myRank);
7846 sendDataBuf[2*k] = gid;
7847 sendDataBuf[2*k+1] = pid;
7852 std::ostringstream os;
7853 os << myRank <<
": Done packing my GIDs and PIDs" << endl;
7860 *err << myRank <<
": Post send-data send: tag = " << dataTag
7863 sendReqData = isend<int, int_type> (sendDataBuf, 0, dataTag, comm);
7868 *err << myRank <<
": Write MatrixMarket header" << endl;
7873 std::ostringstream hdr;
7877 hdr <<
"%%MatrixMarket matrix array integer general" << endl
7878 <<
"% Format: Version 2.0" << endl
7880 <<
"% This file encodes a Tpetra::Map." << endl
7881 <<
"% It is stored as a dense vector, with twice as many " << endl
7882 <<
"% entries as the global number of GIDs (global indices)." << endl
7883 <<
"% (GID, PID) pairs are stored contiguously, where the PID " << endl
7884 <<
"% is the rank of the process owning that GID." << endl
7889 std::ostringstream os;
7890 os << myRank <<
": Write my GIDs and PIDs" << endl;
7896 const int_type printNumRows = myNumRows;
7897 ArrayView<const int_type> printData = sendDataBuf ();
7898 for (int_type k = 0; k < printNumRows; ++k) {
7899 const int_type gid = printData[2*k];
7900 const int_type pid = printData[2*k+1];
7901 out << gid << endl << pid << endl;
7907 const int recvRank = 1;
7908 const int circBufInd = recvRank % 3;
7910 std::ostringstream os;
7911 os << myRank <<
": Wait on receive-size receive from Process "
7912 << recvRank << endl;
7916 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
7920 const int_type recvNumRows = (recvSizeBufs[circBufInd])[0];
7921 if (debug && recvNumRows == -1) {
7922 std::ostringstream os;
7923 os << myRank <<
": Result of receive-size receive from Process "
7924 << recvRank <<
" is -1. This should never happen, and "
7925 "suggests that the receive never got posted. Please report "
7926 "this bug to the Tpetra developers." << endl;
7931 recvDataBufs[circBufInd].resize (recvNumRows * 2);
7933 std::ostringstream os;
7934 os << myRank <<
": Post receive-data receive from Process "
7935 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
7936 << recvDataBufs[circBufInd].size () << endl;
7939 if (! recvSizeReqs[circBufInd].is_null ()) {
7940 std::ostringstream os;
7941 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7942 "null, before posting the receive-data receive from Process "
7943 << recvRank <<
". This should never happen. Please report "
7944 "this bug to the Tpetra developers." << endl;
7947 recvDataReqs[circBufInd] =
7948 ireceive<int, int_type> (recvDataBufs[circBufInd],
7949 recvRank, dataTag, comm);
7952 else if (myRank == 1) {
7955 std::ostringstream os;
7956 os << myRank <<
": Wait on my send-size send" << endl;
7959 wait<int> (comm, outArg (sendReqSize));
7965 for (
int p = 1; p < numProcs; ++p) {
7967 if (p + 2 < numProcs) {
7969 const int recvRank = p + 2;
7970 const int circBufInd = recvRank % 3;
7972 std::ostringstream os;
7973 os << myRank <<
": Post receive-size receive from Process "
7974 << recvRank <<
": tag = " << sizeTag << endl;
7977 if (! recvSizeReqs[circBufInd].is_null ()) {
7978 std::ostringstream os;
7979 os << myRank <<
": recvSizeReqs[" << circBufInd <<
"] is not "
7980 <<
"null, for the receive-size receive from Process "
7981 << recvRank <<
"! This may mean that this process never "
7982 <<
"finished waiting for the receive from Process "
7983 << (recvRank - 3) <<
"." << endl;
7986 recvSizeReqs[circBufInd] =
7987 ireceive<int, int_type> (recvSizeBufs[circBufInd],
7988 recvRank, sizeTag, comm);
7991 if (p + 1 < numProcs) {
7992 const int recvRank = p + 1;
7993 const int circBufInd = recvRank % 3;
7997 std::ostringstream os;
7998 os << myRank <<
": Wait on receive-size receive from Process "
7999 << recvRank << endl;
8002 wait<int> (comm, outArg (recvSizeReqs[circBufInd]));
8006 const int_type recvNumRows = (recvSizeBufs[circBufInd])[0];
8007 if (debug && recvNumRows == -1) {
8008 std::ostringstream os;
8009 os << myRank <<
": Result of receive-size receive from Process "
8010 << recvRank <<
" is -1. This should never happen, and "
8011 "suggests that the receive never got posted. Please report "
8012 "this bug to the Tpetra developers." << endl;
8017 recvDataBufs[circBufInd].resize (recvNumRows * 2);
8019 std::ostringstream os;
8020 os << myRank <<
": Post receive-data receive from Process "
8021 << recvRank <<
": tag = " << dataTag <<
", buffer size = "
8022 << recvDataBufs[circBufInd].size () << endl;
8025 if (! recvDataReqs[circBufInd].is_null ()) {
8026 std::ostringstream os;
8027 os << myRank <<
": recvDataReqs[" << circBufInd <<
"] is not "
8028 <<
"null, for the receive-data receive from Process "
8029 << recvRank <<
"! This may mean that this process never "
8030 <<
"finished waiting for the receive from Process "
8031 << (recvRank - 3) <<
"." << endl;
8034 recvDataReqs[circBufInd] =
8035 ireceive<int, int_type> (recvDataBufs[circBufInd],
8036 recvRank, dataTag, comm);
8040 const int recvRank = p;
8041 const int circBufInd = recvRank % 3;
8043 std::ostringstream os;
8044 os << myRank <<
": Wait on receive-data receive from Process "
8045 << recvRank << endl;
8048 wait<int> (comm, outArg (recvDataReqs[circBufInd]));
8055 std::ostringstream os;
8056 os << myRank <<
": Write GIDs and PIDs from Process "
8057 << recvRank << endl;
8058 *err << os.str () << endl;
8060 const int_type printNumRows = (recvSizeBufs[circBufInd])[0];
8061 if (debug && printNumRows == -1) {
8062 std::ostringstream os;
8063 os << myRank <<
": Result of receive-size receive from Process "
8064 << recvRank <<
" was -1. This should never happen, and "
8065 "suggests that its receive-size receive was never posted. "
8066 "Please report this bug to the Tpetra developers." << endl;
8069 if (debug && printNumRows > 0 && recvDataBufs[circBufInd].is_null ()) {
8070 std::ostringstream os;
8071 os << myRank <<
": Result of receive-size receive from Proc "
8072 << recvRank <<
" was " << printNumRows <<
" > 0, but "
8073 "recvDataBufs[" << circBufInd <<
"] is null. This should "
8074 "never happen. Please report this bug to the Tpetra "
8075 "developers." << endl;
8078 ArrayView<const int_type> printData = (recvDataBufs[circBufInd]) ();
8079 for (int_type k = 0; k < printNumRows; ++k) {
8080 const int_type gid = printData[2*k];
8081 const int_type pid = printData[2*k+1];
8082 out << gid << endl << pid << endl;
8085 else if (myRank == p) {
8088 std::ostringstream os;
8089 os << myRank <<
": Wait on my send-data send" << endl;
8092 wait<int> (comm, outArg (sendReqData));
8094 else if (myRank == p + 1) {
8097 std::ostringstream os;
8098 os << myRank <<
": Post send-data send: tag = " << dataTag
8102 sendReqData = isend<int, int_type> (sendDataBuf, 0, dataTag, comm);
8105 std::ostringstream os;
8106 os << myRank <<
": Wait on my send-size send" << endl;
8109 wait<int> (comm, outArg (sendReqSize));
8111 else if (myRank == p + 2) {
8114 std::ostringstream os;
8115 os << myRank <<
": Post send-size send: size = "
8116 << sendDataSize[0] <<
", tag = " << sizeTag << endl;
8119 sendReqSize = isend<int, int_type> (sendDataSize, 0, sizeTag, comm);
8123 if (! err.is_null ()) {
8127 *err << myRank <<
": writeMap: Done" << endl;
8129 if (! err.is_null ()) {
8139 const int myRank = map.
getComm ()->getRank ();
8142 out.open (filename.c_str());
8175 printAsComment (std::ostream& out,
const std::string& str)
8178 std::istringstream inpstream (str);
8181 while (getline (inpstream, line)) {
8182 if (! line.empty()) {
8185 if (line[0] ==
'%') {
8186 out << line << endl;
8189 out <<
"%% " << line << endl;
8217 Teuchos::ParameterList pl;
8243 Teuchos::ParameterList pl;
8286 const Teuchos::ParameterList& params)
8289 std::string tmpFile =
"__TMP__" + fileName;
8290 const int myRank = A.
getDomainMap()->getComm()->getRank();
8291 bool precisionChanged=
false;
8301 if (std::ifstream(tmpFile))
8302 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
8303 "writeOperator: temporary file " << tmpFile <<
" already exists");
8304 out.open(tmpFile.c_str());
8305 if (params.isParameter(
"precision")) {
8306 oldPrecision = out.precision(params.get<
int>(
"precision"));
8307 precisionChanged=
true;
8311 const std::string header = writeOperatorImpl(out, A, params);
8314 if (precisionChanged)
8315 out.precision(oldPrecision);
8317 out.open(fileName.c_str(), std::ios::binary);
8318 bool printMatrixMarketHeader =
true;
8319 if (params.isParameter(
"print MatrixMarket header"))
8320 printMatrixMarketHeader = params.get<
bool>(
"print MatrixMarket header");
8321 if (printMatrixMarketHeader && myRank == 0) {
8326 std::ifstream src(tmpFile, std::ios_base::binary);
8330 remove(tmpFile.c_str());
8375 const Teuchos::ParameterList& params)
8377 const int myRank = A.
getDomainMap ()->getComm ()->getRank ();
8394 std::ostringstream tmpOut;
8396 if (params.isParameter (
"precision") && params.isType<
int> (
"precision")) {
8397 (void) tmpOut.precision (params.get<
int> (
"precision"));
8401 const std::string header = writeOperatorImpl (tmpOut, A, params);
8404 bool printMatrixMarketHeader =
true;
8405 if (params.isParameter (
"print MatrixMarket header") &&
8406 params.isType<
bool> (
"print MatrixMarket header")) {
8407 printMatrixMarketHeader = params.get<
bool> (
"print MatrixMarket header");
8409 if (printMatrixMarketHeader && myRank == 0) {
8425 out << tmpOut.str ();
8439 writeOperatorImpl (std::ostream& os,
8440 const operator_type& A,
8441 const Teuchos::ParameterList& params)
8445 using Teuchos::ArrayRCP;
8446 using Teuchos::Array;
8450 typedef scalar_type Scalar;
8451 typedef Teuchos::OrdinalTraits<LO> TLOT;
8452 typedef Teuchos::OrdinalTraits<GO> TGOT;
8456 const map_type& domainMap = *(A.getDomainMap());
8457 RCP<const map_type> rangeMap = A.getRangeMap();
8458 RCP<const Teuchos::Comm<int> > comm = rangeMap->getComm();
8459 const int myRank = comm->getRank();
8460 const size_t numProcs = comm->getSize();
8463 if (params.isParameter(
"probing size"))
8464 numMVs = params.get<
int>(
"probing size");
8467 GO minColGid = domainMap.getMinAllGlobalIndex();
8468 GO maxColGid = domainMap.getMaxAllGlobalIndex();
8473 GO numGlobElts = maxColGid - minColGid + TGOT::one();
8474 GO numChunks = numGlobElts / numMVs;
8475 GO rem = numGlobElts % numMVs;
8476 GO indexBase = rangeMap->getIndexBase();
8478 int offsetToUseInPrinting = 1 - indexBase;
8479 if (params.isParameter(
"zero-based indexing")) {
8480 if (params.get<
bool>(
"zero-based indexing") ==
true)
8481 offsetToUseInPrinting = -indexBase;
8485 size_t numLocalRangeEntries = rangeMap->getNodeNumElements();
8488 RCP<const map_type> allGidsMap = rcp(
new map_type(TGOT::invalid(), numLocalRangeEntries,
8491 mv_type_go allGids(allGidsMap,1);
8492 Teuchos::ArrayRCP<GO> allGidsData = allGids.getDataNonConst(0);
8494 for (
size_t i=0; i<numLocalRangeEntries; i++)
8495 allGidsData[i] = rangeMap->getGlobalElement(i);
8496 allGidsData = Teuchos::null;
8499 GO numTargetMapEntries=TGOT::zero();
8500 Teuchos::Array<GO> importGidList;
8502 numTargetMapEntries = rangeMap->getGlobalNumElements();
8503 importGidList.reserve(numTargetMapEntries);
8504 for (GO j=0; j<numTargetMapEntries; ++j) importGidList.push_back(j + indexBase);
8506 importGidList.reserve(numTargetMapEntries);
8508 RCP<map_type> importGidMap = rcp(
new map_type(TGOT::invalid(), importGidList(), indexBase, comm));
8511 import_type gidImporter(allGidsMap, importGidMap);
8512 mv_type_go importedGids(importGidMap, 1);
8513 importedGids.doImport(allGids, gidImporter,
INSERT);
8516 ArrayRCP<const GO> importedGidsData = importedGids.getData(0);
8517 RCP<const map_type> importMap = rcp(
new map_type(TGOT::invalid(), importedGidsData(), indexBase, comm) );
8520 import_type importer(rangeMap, importMap);
8522 RCP<mv_type> colsOnPid0 = rcp(
new mv_type(importMap,numMVs));
8524 RCP<mv_type> ei = rcp(
new mv_type(A.getDomainMap(),numMVs));
8525 RCP<mv_type> colsA = rcp(
new mv_type(A.getRangeMap(),numMVs));
8527 Array<GO> globalColsArray, localColsArray;
8528 globalColsArray.reserve(numMVs);
8529 localColsArray.reserve(numMVs);
8531 ArrayRCP<ArrayRCP<Scalar> > eiData(numMVs);
8532 for (
size_t i=0; i<numMVs; ++i)
8533 eiData[i] = ei->getDataNonConst(i);
8538 for (GO k=0; k<numChunks; ++k) {
8539 for (
size_t j=0; j<numMVs; ++j ) {
8541 GO curGlobalCol = minColGid + k*numMVs + j;
8542 globalColsArray.push_back(curGlobalCol);
8544 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8545 if (curLocalCol != TLOT::invalid()) {
8546 eiData[j][curLocalCol] = TGOT::one();
8547 localColsArray.push_back(curLocalCol);
8553 A.apply(*ei,*colsA);
8555 colsOnPid0->doImport(*colsA,importer,
INSERT);
8558 globalNnz += writeColumns(os,*colsOnPid0, numMVs, importedGidsData(),
8559 globalColsArray, offsetToUseInPrinting);
8562 for (
size_t j=0; j<numMVs; ++j ) {
8563 for (
int i=0; i<localColsArray.size(); ++i)
8564 eiData[j][localColsArray[i]] = TGOT::zero();
8566 globalColsArray.clear();
8567 localColsArray.clear();
8575 for (
int j=0; j<rem; ++j ) {
8576 GO curGlobalCol = maxColGid - rem + j + TGOT::one();
8577 globalColsArray.push_back(curGlobalCol);
8579 LO curLocalCol = domainMap.getLocalElement(curGlobalCol);
8580 if (curLocalCol != TLOT::invalid()) {
8581 eiData[j][curLocalCol] = TGOT::one();
8582 localColsArray.push_back(curLocalCol);
8588 A.apply(*ei,*colsA);
8590 colsOnPid0->doImport(*colsA,importer,
INSERT);
8592 globalNnz += writeColumns(os,*colsOnPid0, rem, importedGidsData(),
8593 globalColsArray, offsetToUseInPrinting);
8596 for (
int j=0; j<rem; ++j ) {
8597 for (
int i=0; i<localColsArray.size(); ++i)
8598 eiData[j][localColsArray[i]] = TGOT::zero();
8600 globalColsArray.clear();
8601 localColsArray.clear();
8610 std::ostringstream oss;
8612 oss <<
"%%MatrixMarket matrix coordinate ";
8613 if (Teuchos::ScalarTraits<typename operator_type::scalar_type>::isComplex) {
8618 oss <<
" general" << std::endl;
8619 oss <<
"% Tpetra::Operator" << std::endl;
8620 std::time_t now = std::time(NULL);
8621 oss <<
"% time stamp: " << ctime(&now);
8622 oss <<
"% written from " << numProcs <<
" processes" << std::endl;
8623 size_t numRows = rangeMap->getGlobalNumElements();
8624 size_t numCols = domainMap.getGlobalNumElements();
8625 oss << numRows <<
" " << numCols <<
" " << globalNnz << std::endl;
8632 writeColumns(std::ostream& os, mv_type
const &colsA,
size_t const &numCols,
8633 Teuchos::ArrayView<const global_ordinal_type>
const &rowGids,
8634 Teuchos::Array<global_ordinal_type>
const &colsArray,
8638 typedef scalar_type Scalar;
8639 typedef Teuchos::ScalarTraits<Scalar> STS;
8642 const Scalar zero = STS::zero();
8643 const size_t numRows = colsA.getGlobalLength();
8644 for (
size_t j=0; j<numCols; ++j) {
8645 Teuchos::ArrayRCP<const Scalar>
const curCol = colsA.getData(j);
8646 const GO J = colsArray[j];
8647 for (
size_t i=0; i<numRows; ++i) {
8648 const Scalar val = curCol[i];
8650 os << rowGids[i]+indexBase <<
" " << J+indexBase <<
" " << val << std::endl;
8667 #endif // __MatrixMarket_Tpetra_hpp
static Teuchos::RCP< vector_type > readVectorFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read a Vector from the given Matrix Market file.
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Teuchos::RCP< const map_type > getRowMap() const override
Returns the Map that describes the row distribution in this graph.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market input stream.
static void writeSparseGraphFile(const std::string &filename, const Teuchos::RCP< const crs_graph_type > &pGraph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), with no comments...
static void writeOperator(std::ostream &out, const operator_type &A)
Write a Tpetra::Operator to an output stream.
GlobalOrdinal getMinGlobalIndex() const
The minimum global index owned by the calling process.
Teuchos::RCP< const map_type > getColMap() const override
Returns the Map that describes the column distribution in this graph.
static void writeSparseFile(const std::string &filename, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Print the sparse matrix in Matrix Market format, with comments.
SparseMatrixType::node_type node_type
The fourth template parameter of CrsMatrix and MultiVector.
static void writeDense(std::ostream &out, const multivector_type &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file.
bool isContiguous() const
True if this Map is distributed contiguously, else false.
void doImport(const SrcDistObject &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, const CombineMode CM, const bool restrictedMode=false)
Import data into this object using an Import object ("forward mode").
size_t getNumVectors() const
Number of columns in the multivector.
SparseMatrixType::local_ordinal_type local_ordinal_type
SparseMatrixType::global_ordinal_type global_ordinal_type
One or more distributed dense vectors.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream.
Teuchos::RCP< const map_type > getDomainMap() const override
Returns the Map associated with the domain of this graph.
SparseMatrixType::local_ordinal_type local_ordinal_type
Type of the local indices of the sparse matrix.
void getLocalRowView(const local_ordinal_type lclRow, Teuchos::ArrayView< const local_ordinal_type > &lclColInds) const override
Get a const, non-persisting view of the given local row's local column indices, as a Teuchos::ArrayVi...
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file.
static void writeDenseFile(const std::string &filename, const Teuchos::RCP< const multivector_type > &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
SparseMatrixType::scalar_type scalar_type
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file, with provided Maps.
MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > multivector_type
Specialization of Tpetra::MultiVector that matches SparseMatrixType.
static Teuchos::RCP< const map_type > readMapFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given Matrix Market file.
bool isGloballyIndexed() const override
Whether the graph's column indices are stored as global indices.
static void writeMap(std::ostream &out, const map_type &map, const bool debug=false)
Print the Map to the given output stream.
static void writeDenseFile(const std::string &filename, const multivector_type &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static void writeDenseFile(const std::string &filename, const Teuchos::RCP< const multivector_type > &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file.
SparseMatrixType::global_ordinal_type global_ordinal_type
Type of indices as read from the Matrix Market file.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream, with provided Maps. ...
static Teuchos::RCP< multivector_type > readDense(std::istream &in, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read dense matrix (as a MultiVector) from the given Matrix Market input stream.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator, in rank order.
static void writeSparseGraph(std::ostream &out, const crs_graph_type &graph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given output stream, with no comments...
size_t global_size_t
Global size_t object.
static Teuchos::RCP< vector_type > readVector(std::istream &in, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read Vector from the given Matrix Market input stream.
SparseMatrixType::node_type node_type
The Kokkos Node type; fourth template parameter of Tpetra::CrsMatrix.
static void writeOperator(const std::string &fileName, const operator_type &A, const Teuchos::ParameterList ¶ms)
Write a Tpetra::Operator to a file, with options.
static Teuchos::RCP< const map_type > readMap(std::istream &in, const Teuchos::RCP< const comm_type > &comm, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given input stream.
Map< local_ordinal_type, global_ordinal_type, node_type > map_type
Specialization of Tpetra::Map that matches SparseMatrixType.
Insert new values that don't currently exist.
SparseMatrixType::scalar_type scalar_type
Type of the entries of the sparse matrix.
static void writeMapFile(const std::string &filename, const map_type &map)
Write the Map to the given file.
Abstract interface for operators (e.g., matrices and preconditioners).
static void writeSparseGraphFile(const std::string &filename, const Teuchos::RCP< const crs_graph_type > &pGraph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), taking the graph by T...
CrsGraph< local_ordinal_type, global_ordinal_type, node_type > crs_graph_type
Specialization of Tpetra::CrsGraph that matches SparseMatrixType.
static void writeDense(std::ostream &out, const Teuchos::RCP< const multivector_type > &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static Teuchos::RCP< sparse_matrix_type > readSparseFile(const std::string &filename, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market file, with provided Maps.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
From a distributed map build a map with all GIDs on the root node.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const =0
The Map associated with the domain of this operator, which must be compatible with X...
static Teuchos::RCP< sparse_graph_type > readSparseGraphFile(const std::string &filename, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market file.
static void writeMap(std::ostream &out, const map_type &map, const Teuchos::RCP< Teuchos::FancyOStream > &err, const bool debug=false)
Print the Map to the given output stream out.
static Teuchos::RCP< const map_type > readMap(std::istream &in, const Teuchos::RCP< const comm_type > &comm, const Teuchos::RCP< Teuchos::FancyOStream > &err, const bool tolerant=false, const bool debug=false)
Read Map (as a MultiVector) from the given input stream, with optional debugging output stream...
static Teuchos::RCP< multivector_type > readDenseFile(const std::string &filename, const Teuchos::RCP< const comm_type > &comm, Teuchos::RCP< const map_type > &map, const bool tolerant=false, const bool debug=false)
Read dense matrix (as a MultiVector) from the given Matrix Market file.
static void writeSparseGraphFile(const std::string &filename, const crs_graph_type &graph, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename), with no comments...
Teuchos::RCP< const map_type > getRangeMap() const override
Returns the Map associated with the domain of this graph.
static void writeDense(std::ostream &out, const multivector_type &X, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with no matrix name or description.
static void writeSparse(std::ostream &out, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const std::string &matrixName, const std::string &matrixDescription, const bool debug=false)
Print the sparse matrix in Matrix Market format, with comments.
void getGlobalRowView(const global_ordinal_type gblRow, Teuchos::ArrayView< const global_ordinal_type > &gblColInds) const override
Get a const, non-persisting view of the given global row's global column indices, as a Teuchos::Array...
static void writeSparseFile(const std::string &filename, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const bool debug=false)
Print the sparse matrix in Matrix Market format.
Teuchos::RCP< const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVector(const size_t j) const
Return a Vector which is a const view of column j.
MultiVector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > multivector_type
The MultiVector specialization associated with SparseMatrixType.
static void writeSparseGraphFile(const std::string &filename, const crs_graph_type &graph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given file (by filename).
Teuchos::ArrayView< const GlobalOrdinal > getNodeElementList() const
Return a NONOWNING view of the global indices owned by this process.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
Vector< scalar_type, local_ordinal_type, global_ordinal_type, node_type > vector_type
The Vector specialization associated with SparseMatrixType.
size_t getNodeNumElements() const
The number of elements belonging to the calling process.
A parallel distribution of indices over processes.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse graph from the given Matrix Market input stream.
void fillComplete(const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const Teuchos::RCP< Teuchos::ParameterList > ¶ms=Teuchos::null)
Tell the graph that you are done changing its structure.
static Teuchos::RCP< sparse_matrix_type > readSparse(std::istream &in, const Teuchos::RCP< const Teuchos::Comm< int > > &pComm, const Teuchos::RCP< Teuchos::ParameterList > &constructorParams, const Teuchos::RCP< Teuchos::ParameterList > &fillCompleteParams, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream.
A distributed dense vector.
static void writeSparseGraph(std::ostream &out, const crs_graph_type &graph, const std::string &graphName, const std::string &graphDescription, const bool debug=false)
Print the sparse graph in Matrix Market format to the given output stream.
CrsGraph< local_ordinal_type, global_ordinal_type, node_type > sparse_graph_type
The CrsGraph specialization associated with SparseMatrixType.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
Returns the communicator.
SparseMatrixType sparse_matrix_type
This class' template parameter; a specialization of CrsMatrix.
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
SparseMatrixType sparse_matrix_type
Template parameter of this class; specialization of CrsMatrix.
static Teuchos::RCP< sparse_graph_type > readSparseGraph(std::istream &in, const Teuchos::RCP< const map_type > &rowMap, Teuchos::RCP< const map_type > &colMap, const Teuchos::RCP< const map_type > &domainMap, const Teuchos::RCP< const map_type > &rangeMap, const bool callFillComplete=true, const bool tolerant=false, const bool debug=false)
Read sparse matrix from the given Matrix Market input stream, with provided Maps. ...
static void writeDenseFile(const std::string &filename, const multivector_type &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and description.
static void writeOperator(std::ostream &out, const operator_type &A, const Teuchos::ParameterList ¶ms)
Write a Tpetra::Operator to an output stream, with options.
global_size_t getGlobalNumEntries() const override
Returns the global number of entries in the graph.
Matrix Market file reader for CrsMatrix and MultiVector.
Matrix Market file writer for CrsMatrix and MultiVector.
static void writeSparse(std::ostream &out, const Teuchos::RCP< const sparse_matrix_type > &pMatrix, const bool debug=false)
Print the sparse matrix in Matrix Market format.
static void writeDense(std::ostream &out, const Teuchos::RCP< const multivector_type > &X, const std::string &matrixName, const std::string &matrixDescription, const Teuchos::RCP< Teuchos::FancyOStream > &err=Teuchos::null, const Teuchos::RCP< Teuchos::FancyOStream > &dbg=Teuchos::null)
Print the multivector in Matrix Market format, with matrix name and or description.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
static void writeOperator(const std::string &fileName, operator_type const &A)
Write a Tpetra::Operator to a file.