42#ifndef __Teuchos_MatrixMarket_Raw_Checker_hpp
43#define __Teuchos_MatrixMarket_Raw_Checker_hpp
67 template<
class Scalar,
class Ordinal>
131 const bool echo =
false;
133 const bool debug =
false;
137 params->set (
"Echo to stdout",
echo,
"Whether to echo the sparse "
138 "matrix to stdout after reading it");
140 "syntax errors when parsing the Matrix Market file");
141 params->set (
"Debug mode",
debug,
"Whether to print debugging output "
142 "to stderr, on all participating MPI processes");
173 <<
"\" on Rank 0...";
179 cerr <<
"failed." << endl;
185 cerr <<
"succeeded." << endl;
192 "Failed to open input file \"" +
filename +
"\".");
215 std::pair<bool, std::string>
result;
220 result.second =
"Input stream is null on Rank 0";
224 cerr <<
"About to read from input stream on Rank 0" << endl;
229 cerr <<
"Successfully read sparse matrix from "
230 "input stream on Rank 0" << endl;
233 cerr <<
"Failed to read sparse matrix from input "
234 "stream on Rank 0" << endl;
265 "Unknown error when reading Matrix Market sparse matrix file; "
266 "the error is \"unknown\" because the error message has length 0.");
272 cerr <<
"The following error occurred when reading the "
273 "sparse matrix: " <<
result.second << endl;
321 "Failed to get Matrix Market banner line from input, after reading "
334 "Failed to get Matrix Market banner line from input. This "
335 "probably means that the file is empty (contains zero lines).");
339 cerr <<
"Raw::Checker::readBanner: Here is the presumed banner line:"
340 << endl <<
line << endl;
347 }
catch (std::exception&
e) {
349 "Matrix Market file's banner line contains syntax error(s): "
364 std::pair<bool, std::string>
387 std::ostringstream
err;
392 catch (std::exception&
e) {
393 err <<
"Failed to read Matrix Market file's Banner: " <<
e.what();
394 return std::make_pair (
false,
err.str());
399 if (
pBanner->matrixType () !=
"coordinate") {
400 err <<
"Matrix Market input file must contain a \"coordinate\"-"
401 "format sparse matrix in order to create a sparse matrix object "
403 return std::make_pair (
false,
err.str ());
405 else if (! STS::isComplex &&
pBanner->dataType () ==
"complex") {
406 err <<
"The Matrix Market sparse matrix file contains complex-"
407 "valued data, but you are try to read the data into a sparse "
408 "matrix containing real values (your matrix's Scalar type is "
410 return std::make_pair (
false,
err.str ());
412 else if (
pBanner->dataType () !=
"real" &&
413 pBanner->dataType () !=
"complex") {
414 err <<
"Only real or complex data types (no pattern or integer "
415 "matrices) are currently supported.";
416 return std::make_pair (
false,
err.str ());
419 cerr <<
"Banner line:" << endl << *
pBanner << endl;
433 std::pair<Tuple<Ordinal, 3>,
bool>
dims =
436 err <<
"Error reading Matrix Market sparse matrix "
437 "file: failed to read coordinate dimensions.";
438 return std::make_pair (
false,
err.str ());
445 const Ordinal numRows =
dims.first[0];
446 const Ordinal numCols =
dims.first[1];
447 const Ordinal numEntries =
dims.first[2];
449 cerr <<
"Reported dimensions: " << numRows <<
" x " << numCols
450 <<
", with " << numEntries <<
" entries (counting possible "
451 <<
"duplicates)." << endl;
469 std::pair<bool, std::vector<size_t> >
results =
473 cerr <<
"Matrix Market file successfully read" << endl;
476 cerr <<
"Failed to read Matrix Market file" << endl;
483 err <<
"The Matrix Market input stream had syntax error(s)."
484 " Here is the error report." << endl;
487 return std::make_pair (
false,
err.str ());
504 return std::make_pair (
true,
err.str());
510 const std::pair<
bool, std::vector<size_t> >&
results)
515 out <<
numErrors <<
" errors when reading Matrix Market sparse "
516 "matrix file." << endl;
518 out <<
"-- We do not report individual errors when there "
522 out <<
"Error on line " <<
results.second[0] << endl;
525 out <<
"Errors on lines {";
int size(const Comm< Ordinal > &comm)
Get the number of processes in the communicator.
Parse a Matrix Market banner line.
Coordinate-format sparse matrix data reader.
Tool for debugging the syntax of a Matrix Market file containing a sparse matrix.
std::pair< bool, std::string > readOnRank0(std::istream &in)
Read the sparse matrix on MPI Rank 0.
void reportBadness(std::ostream &out, const std::pair< bool, std::vector< size_t > > &results)
To be called only on MPI Rank 0.
RCP< const Teuchos::MatrixMarket::Banner > readBanner(std::istream &in, size_t &lineNumber)
Read in the Banner line from the given input stream.
bool echo_
Whether to echo the sparse matrix to stdout after reading it.
RCP< const ParameterList > getValidParameters() const
Checker()
Constructor that sets default Boolean parameters.
Checker(const bool echo, const bool tolerant, const bool debug)
Constructor that takes Boolean parameters.
void setParameters(const RCP< ParameterList > ¶ms)
Set parameters from the given ParameterList.
bool tolerant_
Whether to parse the Matrix Market file tolerantly.
Checker(const RCP< ParameterList > ¶ms)
Constructor that takes a ParameterList of parameters.
bool readFile(const Teuchos::Comm< int > &comm, const std::string &filename)
Read the sparse matrix from the given file.
bool read(const Teuchos::Comm< int > &comm, const RCP< std::istream > &in)
Read the sparse matrix from the given input stream.
bool debug_
Whether to print debugging output to stderr.
Concrete serial communicator subclass.
virtual int getRank() const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Matrix Market file utilities.
"Raw" input of sparse matrices from Matrix Market files.
bool checkCommentLine(const std::string &line, size_t &start, size_t &size, const size_t lineNumber, const bool tolerant, const bool maybeBannerLine)
True if the line is a comment line, false otherwise.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.