10 #ifndef __Teuchos_MatrixMarket_Raw_Checker_hpp
11 #define __Teuchos_MatrixMarket_Raw_Checker_hpp
13 #include "Teuchos_MatrixMarket_Raw_Adder.hpp"
14 #include "Teuchos_MatrixMarket_SymmetrizingAdder.hpp"
15 #include "Teuchos_MatrixMarket_CoordDataReader.hpp"
19 namespace MatrixMarket {
35 template<
class Scalar,
class Ordinal>
47 Checker (
const bool echo,
const bool tolerant,
const bool debug) :
48 echo_ (echo), tolerant_ (tolerant), debug_ (debug)
53 echo_ (false), tolerant_ (false), debug_ (false)
67 echo_ (false), tolerant_ (false), debug_ (false)
80 bool tolerant =
false;
84 echo = params->
get (
"Echo to stdout", echo);
85 tolerant = params->
get (
"Parse tolerantly", tolerant);
86 debug = params->
get (
"Debug mode", debug);
96 getValidParameters ()
const
99 const bool echo =
false;
100 const bool tolerant =
false;
101 const bool debug =
false;
105 params->set (
"Echo to stdout", echo,
"Whether to echo the sparse "
106 "matrix to stdout after reading it");
107 params->set (
"Parse tolerantly", tolerant,
"Whether to tolerate "
108 "syntax errors when parsing the Matrix Market file");
109 params->set (
"Debug mode", debug,
"Whether to print debugging output "
110 "to stderr, on all participating MPI processes");
127 const std::string& filename)
132 const int myRank = comm.
getRank ();
140 cerr <<
"Attempting to open file \"" << filename
141 <<
"\" on Rank 0...";
143 in =
rcp (
new std::ifstream (filename.c_str()));
147 cerr <<
"failed." << endl;
153 cerr <<
"succeeded." << endl;
157 Teuchos::broadcast (comm, 0, &didReadFile);
160 "Failed to open input file \"" + filename +
"\".");
162 return read (comm, in);
182 const int myRank = comm.
getRank ();
183 std::pair<bool, std::string> result;
187 result.first =
false;
188 result.second =
"Input stream is null on Rank 0";
192 cerr <<
"About to read from input stream on Rank 0" << endl;
194 result = readOnRank0 (*in);
197 cerr <<
"Successfully read sparse matrix from "
198 "input stream on Rank 0" << endl;
201 cerr <<
"Failed to read sparse matrix from input "
202 "stream on Rank 0" << endl;
210 msgSize = result.second.size();
213 int success = result.first ? 1 : 0;
214 Teuchos::broadcast (comm, 0, &success);
220 Teuchos::broadcast (comm, 0, &msgSize);
223 std::string errMsg (msgSize,
' ');
225 std::copy (result.second.begin(), result.second.end(),
228 Teuchos::broadcast (comm, 0, static_cast<int> (msgSize), &errMsg[0]);
233 "Unknown error when reading Matrix Market sparse matrix file; "
234 "the error is \"unknown\" because the error message has length 0.");
237 else if (myRank == 0) {
240 cerr <<
"The following error occurred when reading the "
241 "sparse matrix: " << result.second << endl;
270 readBanner (std::istream& in,
size_t& lineNumber)
282 const bool maybeBannerLine =
true;
283 size_t numLinesRead = 0;
284 bool commentLine =
false;
287 const bool readFailed = ! getline (in, line);
289 "Failed to get Matrix Market banner line from input, after reading "
290 << numLinesRead <<
"line" << (numLinesRead != 1 ?
"s." :
"."));
295 commentLine = checkCommentLine (line, start, size, lineNumber,
296 tolerant_, maybeBannerLine);
297 }
while (commentLine);
300 const bool readFailed = ! getline (in, line);
302 "Failed to get Matrix Market banner line from input. This "
303 "probably means that the file is empty (contains zero lines).");
307 cerr <<
"Raw::Checker::readBanner: Here is the presumed banner line:"
308 << endl << line << endl;
314 banner =
rcp (
new Banner (line, tolerant_));
315 }
catch (std::exception& e) {
317 "Matrix Market file's banner line contains syntax error(s): "
320 return rcp_const_cast<
const Banner> (banner);
332 std::pair<bool, std::string>
333 readOnRank0 (std::istream& in)
338 typedef ScalarTraits<Scalar> STS;
343 typedef Adder<Scalar, Ordinal> raw_adder_type;
348 typedef SymmetrizingAdder<raw_adder_type> adder_type;
351 size_t lineNumber = 1;
355 std::ostringstream err;
356 RCP<const Banner> pBanner;
358 pBanner = readBanner (in, lineNumber);
360 catch (std::exception& e) {
361 err <<
"Failed to read Matrix Market file's Banner: " << e.what();
362 return std::make_pair (
false, err.str());
367 if (pBanner->matrixType () !=
"coordinate") {
368 err <<
"Matrix Market input file must contain a \"coordinate\"-"
369 "format sparse matrix in order to create a sparse matrix object "
371 return std::make_pair (
false, err.str ());
373 else if (! STS::isComplex && pBanner->dataType () ==
"complex") {
374 err <<
"The Matrix Market sparse matrix file contains complex-"
375 "valued data, but you are try to read the data into a sparse "
376 "matrix containing real values (your matrix's Scalar type is "
378 return std::make_pair (
false, err.str ());
380 else if (pBanner->dataType () !=
"real" &&
381 pBanner->dataType () !=
"complex") {
382 err <<
"Only real or complex data types (no pattern or integer "
383 "matrices) are currently supported.";
384 return std::make_pair (
false, err.str ());
387 cerr <<
"Banner line:" << endl << *pBanner << endl;
392 typedef CoordDataReader<adder_type, Ordinal, Scalar,
393 STS::isComplex> reader_type;
401 std::pair<Tuple<Ordinal, 3>,
bool> dims =
402 reader.readDimensions (in, lineNumber, tolerant_);
404 err <<
"Error reading Matrix Market sparse matrix "
405 "file: failed to read coordinate dimensions.";
406 return std::make_pair (
false, err.str ());
413 const Ordinal numRows = dims.first[0];
414 const Ordinal numCols = dims.first[1];
415 const Ordinal numEntries = dims.first[2];
417 cerr <<
"Reported dimensions: " << numRows <<
" x " << numCols
418 <<
", with " << numEntries <<
" entries (counting possible "
419 <<
"duplicates)." << endl;
424 RCP<raw_adder_type> rawAdder =
425 rcp (
new raw_adder_type (numRows, numCols, numEntries,
428 RCP<adder_type> adder =
429 rcp (
new adder_type (rawAdder, pBanner->symmType ()));
432 reader.setAdder (adder);
437 std::pair<bool, std::vector<size_t> > results =
438 reader.read (in, lineNumber, tolerant_, debug_);
441 cerr <<
"Matrix Market file successfully read" << endl;
444 cerr <<
"Failed to read Matrix Market file" << endl;
449 if (! results.first) {
451 err <<
"The Matrix Market input stream had syntax error(s)."
452 " Here is the error report." << endl;
453 reportBadness (err, results);
455 return std::make_pair (
false, err.str ());
459 reportBadness (cerr, results);
467 const bool doMerge =
false;
468 const bool replace =
false;
469 rawAdder->print (cout, doMerge, replace);
472 return std::make_pair (
true, err.str());
477 reportBadness (std::ostream& out,
478 const std::pair<
bool, std::vector<size_t> >& results)
481 const size_t numErrors = results.second.size();
482 const size_t maxNumErrorsToReport = 20;
483 out << numErrors <<
" errors when reading Matrix Market sparse "
484 "matrix file." << endl;
485 if (numErrors > maxNumErrorsToReport) {
486 out <<
"-- We do not report individual errors when there "
487 "are more than " << maxNumErrorsToReport <<
".";
489 else if (numErrors == 1) {
490 out <<
"Error on line " << results.second[0] << endl;
492 else if (numErrors > 1) {
493 out <<
"Errors on lines {";
494 for (
size_t k = 0; k < numErrors-1; ++k) {
495 out << results.second[k] <<
", ";
497 out << results.second[numErrors-1] <<
"}" << endl;
505 #endif // __Teuchos_MatrixMarket_Raw_Checker_hpp
virtual int getRank() const =0
Returns the rank of this process.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
void setParameters(const RCP< ParameterList > ¶ms)
Set parameters from the given ParameterList.
T * get() const
Get the raw C++ pointer to the underlying object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Checker()
Constructor that sets default Boolean parameters.
bool readFile(const Teuchos::Comm< int > &comm, const std::string &filename)
Read the sparse matrix from the given file.
A list of parameters of arbitrary type.
Checker(const RCP< ParameterList > ¶ms)
Constructor that takes a ParameterList of parameters.
Smart reference counting pointer class for automatic garbage collection.
Tool for debugging the syntax of a Matrix Market file containing a sparse matrix. ...
Checker(const bool echo, const bool tolerant, const bool debug)
Constructor that takes Boolean parameters.
bool read(const Teuchos::Comm< int > &comm, const RCP< std::istream > &in)
Read the sparse matrix from the given input stream.
bool is_null() const
Returns true if the underlying pointer is null.