10#ifndef TPETRA_DETAILS_READTRIPLES_HPP
11#define TPETRA_DETAILS_READTRIPLES_HPP
22#include "TpetraCore_config.h"
23#include "Tpetra_Details_PackTriples.hpp"
24#include "KokkosKernels_ArithTraits.hpp"
25#include "Teuchos_MatrixMarket_generic.hpp"
26#include "Teuchos_CommHelpers.hpp"
78template <
class OrdinalType,
class RealType>
86 using ::Teuchos::MatrixMarket::readRealData;
93 std::ostringstream
os;
94 os <<
"Failed to read pattern data and/or real value from line "
96 throw std::invalid_argument(
os.str());
103 std::ostringstream
os;
104 os <<
"No more data after real value on line "
106 throw std::invalid_argument(
os.str());
114 std::ostringstream
os;
115 os <<
"Failed to get imaginary value from line "
117 throw std::invalid_argument(
os.str());
136 const bool isComplex = ::KokkosKernels::ArithTraits<SC>::is_complex>
160 const std::string&
line,
164 const bool debug =
false);
174template <
class SC,
class GO>
198 const std::string&
line,
202 const bool debug =
false) {
203 using ::Teuchos::MatrixMarket::checkCommentLine;
204 typedef typename ::KokkosKernels::ArithTraits<SC>::mag_type
real_type;
217 }
catch (std::exception&
e) {
220 std::ostringstream
os;
221 os <<
"readLine: readComplexData threw an exception: " <<
e.what()
239 std::ostringstream
os;
240 os <<
"readLine: processTriple returned " <<
errCode <<
" != 0."
258template <
class SC,
class GO>
282 const std::string&
line,
286 const bool debug =
false) {
288 using ::Teuchos::MatrixMarket::checkCommentLine;
289 using ::Teuchos::MatrixMarket::readRealData;
298 }
catch (std::exception&
e) {
301 std::ostringstream
os;
302 os <<
"readLine: readRealData threw an exception: " <<
e.what()
310 std::ostringstream
os;
312 <<
", val=" <<
val << std::endl;
318 std::ostringstream
os;
319 os <<
"readLine: processTriple returned " <<
errCode <<
" != 0."
355template <
class SC,
class GO>
357 const std::string&
line,
361 const bool debug =
false) {
396template <
class SC,
class GO>
404 const bool debug =
false) {
407 using Teuchos::MatrixMarket::checkCommentLine;
414 *
errStrm <<
"Input stream reports a failure (not the same as "
478 *
errStrm <<
"The input stream is not at end-of-file, "
479 "but is in a bad state."
512template <
class SC,
class GO>
516 ::Teuchos::ArrayRCP<int>&
sizeBuf,
517 ::Teuchos::ArrayRCP<char>&
msgBuf,
520 std::vector<SC>&
vals,
523 const ::Teuchos::Comm<int>& comm,
526 const bool debug =
false) {
528 using ::Teuchos::isend;
529 using ::Tpetra::Details::countPackTriples;
530 using ::Tpetra::Details::countPackTriplesCount;
531 using ::Tpetra::Details::packTriples;
532 using ::Tpetra::Details::packTriplesCount;
534 using ::KokkosKernels::ArithTraits;
538 constexpr int msgTag = 43;
563 std::ostringstream
os;
564 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
565 <<
", GO=" <<
typeid(GO).name() <<
": "
566 <<
"readAndSendOneBatchOfTriples: readTriples read "
567 <<
numEntRead <<
" matrix entries, and returned errCode="
568 <<
errCode <<
"." << std::endl;
575 *
errStrm <<
"readTriples size results are not consistent. "
577 <<
", rowInds.size() = " <<
rowInds.size()
578 <<
", colInds.size() = " <<
colInds.size()
579 <<
", and vals.size() = " <<
vals.size() <<
"."
600 std::ostringstream
os;
601 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
602 <<
", GO=" <<
typeid(GO).name() <<
": "
603 <<
"Post send (size=0, errCode=" <<
errCode <<
") "
625 std::ostringstream
os;
626 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
627 <<
", GO=" <<
typeid(GO).name() <<
": "
628 <<
"Post send (size=0, error case) to " <<
destRank
641 std::ostringstream
os;
642 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
643 <<
", GO=" <<
typeid(GO).name() <<
": "
644 <<
"Post send (size=0, error case) to " <<
destRank
656 std::ostringstream
os;
657 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
658 <<
", GO=" <<
typeid(GO).name() <<
": "
680 std::ostringstream
os;
681 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
682 <<
", GO=" <<
typeid(GO).name() <<
": "
683 <<
"Post isend (packed data) to " <<
destRank
694 std::ostringstream
os;
695 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
696 <<
", GO=" <<
typeid(GO).name() <<
": "
697 <<
"Wait on isend (size)" <<
endl;
702 std::ostringstream
os;
703 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
704 <<
", GO=" <<
typeid(GO).name() <<
": "
705 <<
"Wait on isend (packed data)" <<
endl;
756template <
class SC,
class GO,
class CommRequestPtr>
759 std::vector<SC>&
vals,
761 ::Teuchos::ArrayRCP<int>&
sizeBuf,
762 ::Teuchos::ArrayRCP<char>&
msgBuf,
765 const ::Teuchos::Comm<int>& comm,
768 const bool debug =
false) {
769 using ::KokkosKernels::ArithTraits;
770 using ::Tpetra::Details::unpackTriples;
771 using ::Tpetra::Details::unpackTriplesCount;
776 constexpr int msgTag = 43;
782 std::ostringstream
os;
783 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
784 <<
", GO=" <<
typeid(GO).name() <<
": "
785 <<
"Wait on irecv (size)" << std::endl;
792 std::ostringstream
os;
793 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
794 <<
", GO=" <<
typeid(GO).name() <<
": "
795 <<
"Received size: sizeBuf[0]=" <<
sizeBuf[0] << std::endl;
809 std::ostringstream
os;
810 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
811 <<
", GO=" <<
typeid(GO).name() <<
": "
812 <<
"Post irecv (packed data) "
814 <<
" with tag " <<
msgTag << std::endl;
819 std::ostringstream
os;
820 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
821 <<
", GO=" <<
typeid(GO).name() <<
": "
822 <<
"Wait on irecv (packed data)" << std::endl;
883template <
class SC,
class GO>
889 const ::Teuchos::Comm<int>& comm,
892 const bool debug =
false) {
893 using KokkosKernels::ArithTraits;
902 const int myRank = comm.getRank();
903 const int numProcs = comm.getSize();
906 ::Teuchos::ArrayRCP<int>
sizeBuf(1);
907 ::Teuchos::ArrayRCP<char>
msgBuf;
913 std::vector<SC>
vals;
937 std::ostringstream
os;
938 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
939 <<
", GO=" <<
typeid(GO).name() <<
": "
940 <<
"(dest=src) readTriples returned curNumEntRead="
947 std::ostringstream
os;
948 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
949 <<
", GO=" <<
typeid(GO).name() <<
": "
950 <<
"Calling readAndSend... with destRank=" <<
destRank <<
endl;
964 std::ostringstream
os;
965 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
966 <<
", GO=" <<
typeid(GO).name() <<
": "
967 <<
"readAndSend... with destRank=" <<
destRank
976 std::ostringstream
os;
977 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
978 <<
", GO=" <<
typeid(GO).name() <<
": "
980 <<
" was legit zero, counts as termination" <<
endl;
1002 std::ostringstream
os;
1003 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1004 <<
", GO=" <<
typeid(GO).name() <<
": "
1005 <<
"Post send (size, termination msg) to " <<
outRank
1006 <<
" with tag " <<
sizeTag <<
"(was last message legit zero? "
1020 std::ostringstream
os;
1021 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1022 <<
", GO=" <<
typeid(GO).name() <<
": "
1023 <<
"Post irecv (size) from " <<
srcRank
1024 <<
" with tag " <<
sizeTag << std::endl;
1035 std::ostringstream
os;
1036 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1037 <<
", GO=" <<
typeid(GO).name() <<
": "
1038 <<
"recvOneBatchOfTriples returned numEnt=" <<
numEnt
1049 *
errStrm <<
"recvOneBatchOfTriples produced inconsistent data sizes. "
1051 <<
", rowInds.size() = " <<
rowInds.size()
1052 <<
", colInds.size() = " <<
colInds.size()
1053 <<
", vals.size() = " <<
vals.size() <<
"."
1072 std::ostringstream
os;
1073 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1074 <<
", GO=" <<
typeid(GO).name() <<
": "
1075 <<
"Done with send/recv loop" <<
endl;
1080 using ::Teuchos::outArg;
1081 using ::Teuchos::REDUCE_BOR;
1082 using ::Teuchos::reduceAll;
bool readComplexData(std::istream &istr, OrdinalType &rowIndex, OrdinalType &colIndex, RealType &realPart, RealType &imagPart, const std::size_t lineNumber, const bool tolerant)
Read "<rowIndex> <colIndex> <realPart> <imagPart>" from a line.
int readTriples(std::istream &inputStream, std::size_t &curLineNum, std::size_t &numTriplesRead, std::function< int(const GO, const GO, const SC &)> processTriple, const std::size_t maxNumTriplesToRead, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Read at most numTriplesToRead triples from the given Matrix Market input stream, and pass along any r...
int recvOneBatchOfTriples(std::vector< GO > &rowInds, std::vector< GO > &colInds, std::vector< SC > &vals, int &numEnt, ::Teuchos::ArrayRCP< int > &sizeBuf, ::Teuchos::ArrayRCP< char > &msgBuf, CommRequestPtr &sizeReq, const int srcRank, const ::Teuchos::Comm< int > &comm, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Read at most maxNumEntPerMsg sparse matrix entries from the input stream, and send them to the proces...
int readAndSendOneBatchOfTriples(std::istream &inputStream, std::size_t &curLineNum, std::size_t &numEntRead, ::Teuchos::ArrayRCP< int > &sizeBuf, ::Teuchos::ArrayRCP< char > &msgBuf, std::vector< GO > &rowInds, std::vector< GO > &colInds, std::vector< SC > &vals, const std::size_t maxNumEntPerMsg, const int destRank, const ::Teuchos::Comm< int > &comm, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Read at most maxNumEntPerMsg sparse matrix entries from the input stream, and send them to the proces...
int readLine(std::function< int(const GO, const GO, const SC &)> processTriple, const std::string &line, const std::size_t lineNumber, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Take a line from the Matrix Market file or input stream, and process the sparse matrix entry in that ...
Struct that holds views of the contents of a CrsMatrix.
Implementation details of Tpetra.
int packTriplesCount(const int, char[], const int, int &, const ::Teuchos::Comm< int > &, std::ostream *errStrm)
Pack the count (number) of matrix triples.
int countPackTriplesCount(const ::Teuchos::Comm< int > &, int &size, std::ostream *errStrm)
Compute the buffer size required by packTriples for packing the number of matrix entries ("triples").
int unpackTriplesCount(const char[], const int, int &, int &, const ::Teuchos::Comm< int > &, std::ostream *errStrm)
Unpack just the count of triples from the given input buffer.
int readAndDealOutTriples(std::istream &inputStream, std::size_t &curLineNum, std::size_t &totalNumEntRead, std::function< int(const GO, const GO, const SC &)> processTriple, const std::size_t maxNumEntPerMsg, const ::Teuchos::Comm< int > &comm, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
On Process 0 in the given communicator, read sparse matrix entries (in chunks of at most maxNumEntPer...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
static int readLine(std::function< int(const GO, const GO, const SC &)> processTriple, const std::string &line, const std::size_t lineNumber, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Take a line from the Matrix Market file or input stream, and process the sparse matrix entry in that ...
static int readLine(std::function< int(const GO, const GO, const SC &)> processTriple, const std::string &line, const std::size_t lineNumber, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Take a line from the Matrix Market file or input stream, and process the sparse matrix entry in that ...
Implementation of the readLine stand-alone function in this namespace (see below).
static int readLine(std::function< int(const GO, const GO, const SC &)> processTriple, const std::string &line, const std::size_t lineNumber, const bool tolerant=false, std::ostream *errStrm=NULL, const bool debug=false)
Take a line from the Matrix Market file or input stream, and process the sparse matrix entry in that ...