42#ifndef __Teuchos_MatrixMarket_Raw_Checker_hpp
43#define __Teuchos_MatrixMarket_Raw_Checker_hpp
45#include "Teuchos_MatrixMarket_Raw_Adder.hpp"
46#include "Teuchos_MatrixMarket_SymmetrizingAdder.hpp"
47#include "Teuchos_MatrixMarket_CoordDataReader.hpp"
67 template<
class Scalar,
class Ordinal>
79 Checker (
const bool echo,
const bool tolerant,
const bool debug) :
80 echo_ (echo), tolerant_ (tolerant), debug_ (debug)
85 echo_ (false), tolerant_ (false), debug_ (false)
99 echo_ (false), tolerant_ (false), debug_ (false)
112 bool tolerant =
false;
116 echo = params->
get (
"Echo to stdout", echo);
117 tolerant = params->
get (
"Parse tolerantly", tolerant);
118 debug = params->
get (
"Debug mode", debug);
123 tolerant_ = tolerant;
128 getValidParameters ()
const
131 const bool echo =
false;
132 const bool tolerant =
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");
139 params->set (
"Parse tolerantly", tolerant,
"Whether to tolerate "
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");
144 return rcp_const_cast<const ParameterList> (params);
159 const std::string& filename)
164 const int myRank = comm.
getRank ();
172 cerr <<
"Attempting to open file \"" << filename
173 <<
"\" on Rank 0...";
175 in =
rcp (
new std::ifstream (filename.c_str()));
179 cerr <<
"failed." << endl;
185 cerr <<
"succeeded." << endl;
189 Teuchos::broadcast (comm, 0, &didReadFile);
192 "Failed to open input file \"" + filename +
"\".");
194 return read (comm, in);
214 const int myRank = comm.
getRank ();
215 std::pair<bool, std::string> result;
219 result.first =
false;
220 result.second =
"Input stream is null on Rank 0";
224 cerr <<
"About to read from input stream on Rank 0" << endl;
226 result = readOnRank0 (*in);
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;
242 msgSize = result.second.size();
245 int success = result.first ? 1 : 0;
246 Teuchos::broadcast (comm, 0, &success);
252 Teuchos::broadcast (comm, 0, &msgSize);
255 std::string errMsg (msgSize,
' ');
257 std::copy (result.second.begin(), result.second.end(),
260 Teuchos::broadcast (comm, 0,
static_cast<int> (msgSize), &errMsg[0]);
265 "Unknown error when reading Matrix Market sparse matrix file; "
266 "the error is \"unknown\" because the error message has length 0.");
269 else if (myRank == 0) {
272 cerr <<
"The following error occurred when reading the "
273 "sparse matrix: " << result.second << endl;
302 readBanner (std::istream& in,
size_t& lineNumber)
314 const bool maybeBannerLine =
true;
315 size_t numLinesRead = 0;
316 bool commentLine =
false;
319 const bool readFailed = ! getline (in, line);
321 "Failed to get Matrix Market banner line from input, after reading "
322 << numLinesRead <<
"line" << (numLinesRead != 1 ?
"s." :
"."));
327 commentLine = checkCommentLine (line, start, size, lineNumber,
328 tolerant_, maybeBannerLine);
329 }
while (commentLine);
332 const bool readFailed = ! getline (in, line);
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;
346 banner =
rcp (
new Banner (line, tolerant_));
347 }
catch (std::exception& e) {
349 "Matrix Market file's banner line contains syntax error(s): "
352 return rcp_const_cast<const Banner> (banner);
364 std::pair<bool, std::string>
365 readOnRank0 (std::istream& in)
370 typedef ScalarTraits<Scalar> STS;
375 typedef Adder<Scalar, Ordinal> raw_adder_type;
380 typedef SymmetrizingAdder<raw_adder_type> adder_type;
383 size_t lineNumber = 1;
387 std::ostringstream err;
388 RCP<const Banner> pBanner;
390 pBanner = readBanner (in, lineNumber);
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;
424 typedef CoordDataReader<adder_type, Ordinal, Scalar,
425 STS::isComplex> reader_type;
433 std::pair<Tuple<Ordinal, 3>,
bool> dims =
434 reader.readDimensions (in, lineNumber, tolerant_);
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;
456 RCP<raw_adder_type> rawAdder =
457 rcp (
new raw_adder_type (numRows, numCols, numEntries,
460 RCP<adder_type> adder =
461 rcp (
new adder_type (rawAdder, pBanner->symmType ()));
464 reader.setAdder (adder);
469 std::pair<bool, std::vector<size_t> > results =
470 reader.read (in, lineNumber, tolerant_, debug_);
473 cerr <<
"Matrix Market file successfully read" << endl;
476 cerr <<
"Failed to read Matrix Market file" << endl;
481 if (! results.first) {
483 err <<
"The Matrix Market input stream had syntax error(s)."
484 " Here is the error report." << endl;
485 reportBadness (err, results);
487 return std::make_pair (
false, err.str ());
491 reportBadness (cerr, results);
499 const bool doMerge =
false;
500 const bool replace =
false;
501 rawAdder->print (cout, doMerge, replace);
504 return std::make_pair (
true, err.str());
509 reportBadness (std::ostream& out,
510 const std::pair<
bool, std::vector<size_t> >& results)
513 const size_t numErrors = results.second.size();
514 const size_t maxNumErrorsToReport = 20;
515 out << numErrors <<
" errors when reading Matrix Market sparse "
516 "matrix file." << endl;
517 if (numErrors > maxNumErrorsToReport) {
518 out <<
"-- We do not report individual errors when there "
519 "are more than " << maxNumErrorsToReport <<
".";
521 else if (numErrors == 1) {
522 out <<
"Error on line " << results.second[0] << endl;
524 else if (numErrors > 1) {
525 out <<
"Errors on lines {";
526 for (
size_t k = 0; k < numErrors-1; ++k) {
527 out << results.second[k] <<
", ";
529 out << results.second[numErrors-1] <<
"}" << endl;
Abstract interface for distributed-memory communication.
virtual int getRank() const =0
Returns the rank of this process.
Tool for debugging the syntax of a Matrix Market file containing a sparse matrix.
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.
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.
Smart reference counting pointer class for automatic garbage collection.
bool is_null() const
Returns true if the underlying pointer is null.
T * get() const
Get the raw C++ pointer to the underlying object.
#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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.