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."
511template <
class SC,
class GO>
515 ::Teuchos::ArrayRCP<int>&
sizeBuf,
516 ::Teuchos::ArrayRCP<char>&
msgBuf,
519 std::vector<SC>&
vals,
522 const ::Teuchos::Comm<int>& comm,
525 const bool debug =
false) {
527 using ::Teuchos::isend;
528 using ::Tpetra::Details::countPackTriples;
529 using ::Tpetra::Details::countPackTriplesCount;
530 using ::Tpetra::Details::packTriples;
531 using ::Tpetra::Details::packTriplesCount;
533 using ::KokkosKernels::ArithTraits;
537 constexpr int msgTag = 43;
562 std::ostringstream
os;
563 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
564 <<
", GO=" <<
typeid(GO).name() <<
": "
565 <<
"readAndSendOneBatchOfTriples: readTriples read "
566 <<
numEntRead <<
" matrix entries, and returned errCode="
567 <<
errCode <<
"." << std::endl;
574 *
errStrm <<
"readTriples size results are not consistent. "
576 <<
", rowInds.size() = " <<
rowInds.size()
577 <<
", colInds.size() = " <<
colInds.size()
578 <<
", and vals.size() = " <<
vals.size() <<
"."
599 std::ostringstream
os;
600 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
601 <<
", GO=" <<
typeid(GO).name() <<
": "
602 <<
"Post send (size=0, errCode=" <<
errCode <<
") "
624 std::ostringstream
os;
625 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
626 <<
", GO=" <<
typeid(GO).name() <<
": "
627 <<
"Post send (size=0, error case) to " <<
destRank
640 std::ostringstream
os;
641 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
642 <<
", GO=" <<
typeid(GO).name() <<
": "
643 <<
"Post send (size=0, error case) to " <<
destRank
655 std::ostringstream
os;
656 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
657 <<
", GO=" <<
typeid(GO).name() <<
": "
679 std::ostringstream
os;
680 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
681 <<
", GO=" <<
typeid(GO).name() <<
": "
682 <<
"Post isend (packed data) to " <<
destRank
693 std::ostringstream
os;
694 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
695 <<
", GO=" <<
typeid(GO).name() <<
": "
696 <<
"Wait on isend (size)" <<
endl;
701 std::ostringstream
os;
702 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
703 <<
", GO=" <<
typeid(GO).name() <<
": "
704 <<
"Wait on isend (packed data)" <<
endl;
754template <
class SC,
class GO,
class CommRequestPtr>
757 std::vector<SC>&
vals,
759 ::Teuchos::ArrayRCP<int>&
sizeBuf,
760 ::Teuchos::ArrayRCP<char>&
msgBuf,
763 const ::Teuchos::Comm<int>& comm,
766 const bool debug =
false) {
767 using ::KokkosKernels::ArithTraits;
768 using ::Tpetra::Details::unpackTriples;
769 using ::Tpetra::Details::unpackTriplesCount;
774 constexpr int msgTag = 43;
780 std::ostringstream
os;
781 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
782 <<
", GO=" <<
typeid(GO).name() <<
": "
783 <<
"Wait on irecv (size)" << std::endl;
790 std::ostringstream
os;
791 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
792 <<
", GO=" <<
typeid(GO).name() <<
": "
793 <<
"Received size: sizeBuf[0]=" <<
sizeBuf[0] << std::endl;
807 std::ostringstream
os;
808 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
809 <<
", GO=" <<
typeid(GO).name() <<
": "
810 <<
"Post irecv (packed data) "
812 <<
" with tag " <<
msgTag << std::endl;
817 std::ostringstream
os;
818 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
819 <<
", GO=" <<
typeid(GO).name() <<
": "
820 <<
"Wait on irecv (packed data)" << std::endl;
879template <
class SC,
class GO>
885 const ::Teuchos::Comm<int>& comm,
888 const bool debug =
false) {
889 using KokkosKernels::ArithTraits;
898 const int myRank = comm.getRank();
899 const int numProcs = comm.getSize();
902 ::Teuchos::ArrayRCP<int>
sizeBuf(1);
903 ::Teuchos::ArrayRCP<char>
msgBuf;
909 std::vector<SC>
vals;
933 std::ostringstream
os;
934 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
935 <<
", GO=" <<
typeid(GO).name() <<
": "
936 <<
"(dest=src) readTriples returned curNumEntRead="
943 std::ostringstream
os;
944 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
945 <<
", GO=" <<
typeid(GO).name() <<
": "
946 <<
"Calling readAndSend... with destRank=" <<
destRank <<
endl;
960 std::ostringstream
os;
961 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
962 <<
", GO=" <<
typeid(GO).name() <<
": "
963 <<
"readAndSend... with destRank=" <<
destRank
972 std::ostringstream
os;
973 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
974 <<
", GO=" <<
typeid(GO).name() <<
": "
976 <<
" was legit zero, counts as termination" <<
endl;
998 std::ostringstream
os;
999 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1000 <<
", GO=" <<
typeid(GO).name() <<
": "
1001 <<
"Post send (size, termination msg) to " <<
outRank
1002 <<
" with tag " <<
sizeTag <<
"(was last message legit zero? "
1016 std::ostringstream
os;
1017 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1018 <<
", GO=" <<
typeid(GO).name() <<
": "
1019 <<
"Post irecv (size) from " <<
srcRank
1020 <<
" with tag " <<
sizeTag << std::endl;
1031 std::ostringstream
os;
1032 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1033 <<
", GO=" <<
typeid(GO).name() <<
": "
1034 <<
"recvOneBatchOfTriples returned numEnt=" <<
numEnt
1045 *
errStrm <<
"recvOneBatchOfTriples produced inconsistent data sizes. "
1047 <<
", rowInds.size() = " <<
rowInds.size()
1048 <<
", colInds.size() = " <<
colInds.size()
1049 <<
", vals.size() = " <<
vals.size() <<
"."
1068 std::ostringstream
os;
1069 os <<
"Proc " << comm.getRank() <<
", SC=" <<
typeid(
SC).name()
1070 <<
", GO=" <<
typeid(GO).name() <<
": "
1071 <<
"Done with send/recv loop" <<
endl;
1076 using ::Teuchos::outArg;
1077 using ::Teuchos::REDUCE_BOR;
1078 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 ...