10 #ifndef __Teuchos_MatrixMarket_Raw_Reader_hpp
11 #define __Teuchos_MatrixMarket_Raw_Reader_hpp
39 namespace MatrixMarket {
68 template<
class Scalar,
class Ordinal>
77 Reader (
const bool tolerant,
const bool debug) :
111 bool tolerant =
false;
115 tolerant = params->
get (
"Parse tolerantly", tolerant);
116 debug = params->
get (
"Debug mode", debug);
129 const bool tolerant =
false;
130 const bool debug =
false;
134 params->set (
"Parse tolerantly", tolerant,
"Whether to tolerate "
135 "syntax errors when parsing the Matrix Market file");
136 params->set (
"Debug mode", debug,
"Whether to print debugging output "
137 "to stderr, on all participating MPI processes");
175 std::ifstream in (filename.c_str ());
177 "Failed to open file \"" << filename <<
"\" for reading.");
178 return read (rowptr, colind, values, numRows, numCols, in);
231 size_t lineNumber = 1;
236 std::ostringstream err;
240 catch (std::exception& e) {
241 err <<
"Failed to read Matrix Market input's Banner: " << e.what();
244 cerr << err.str() << endl;
257 if (banner->matrixType () !=
"coordinate") {
258 err <<
"Matrix Market input file must contain a \"coordinate\"-"
259 "format sparse matrix in order to create a sparse matrix object "
263 else if (! STS::isComplex && banner->dataType () ==
"complex") {
264 err <<
"The Matrix Market sparse matrix file contains complex-"
265 "valued data, but you are try to read the data into a sparse "
266 "matrix containing real values (your matrix's Scalar type is "
270 else if (banner->dataType () !=
"real" &&
271 banner->dataType () !=
"complex") {
272 err <<
"Only real or complex data types (no pattern or integer "
273 "matrices) are currently supported.";
279 cerr <<
"Matrix Market banner is invalid: " << err.str () << endl;
285 "Matrix Market banner is invalid: " << err.str ());
289 cerr <<
"Matrix Market Banner line:" << endl << *banner << endl;
302 std::pair<Tuple<Ordinal, 3>,
bool> dims =
305 err <<
"Error reading Matrix Market sparse matrix file: failed to "
306 "read coordinate dimensions.";
309 cerr << err.str () << endl;
323 numRows = dims.first[0];
324 numCols = dims.first[1];
325 const Ordinal numEntries = dims.first[2];
327 cerr <<
"Reported dimensions: " << numRows <<
" x " << numCols
328 <<
", with " << numEntries <<
" entries (counting possible "
329 <<
"duplicates)." << endl;
335 rcp (
new raw_adder_type (numRows, numCols, numEntries,
341 rcp (
new adder_type (rawAdder, banner->symmType ()));
344 reader.setAdder (adder);
349 std::pair<bool, std::vector<size_t> > results =
353 if (! results.first) {
354 err <<
"The Matrix Market input stream had syntax error(s)."
355 " Here is the error report." << endl;
359 cerr << err.str() << endl;
368 size_t numUnique, numRemoved;
373 rawAdder->mergeAndConvertToCSR (numUnique, numRemoved, ptr, ind, val);
375 catch (std::exception& e) {
376 err <<
"Failed to convert sparse matrix data to CSR (compressed "
377 "sparse row) format. Reported error: " << e.what ();
380 cerr << err.str () << endl;
411 cerr <<
"MatrixMarket::Raw::Reader:" << endl
412 <<
"- Tolerant mode: " <<
tolerant_ << endl
413 <<
"- Debug mode: " <<
debug_ << endl;
442 const bool maybeBannerLine =
true;
443 size_t numLinesRead = 0;
444 bool commentLine =
false;
447 const bool readFailed = ! getline (in, line);
449 "Failed to get Matrix Market banner line from input, after reading "
450 << numLinesRead <<
"line" << (numLinesRead != 1 ?
"s." :
"."));
457 }
while (commentLine);
460 const bool readFailed = ! getline (in, line);
462 "Failed to get Matrix Market banner line from input. This "
463 "probably means that the file is empty (contains zero lines).");
467 cerr <<
"Raw::Reader::readBanner: Here is the presumed banner line:"
468 << endl << line << endl;
475 }
catch (std::exception& e) {
477 "Matrix Market file's banner line contains syntax error(s): "
480 return rcp_const_cast<
const Banner> (banner);
489 const std::pair<
bool, std::vector<size_t> >& results)
492 const size_t numErrors = results.second.size();
493 const size_t maxNumErrorsToReport = 20;
494 out << numErrors <<
" errors when reading Matrix Market sparse "
495 "matrix file." << endl;
496 if (numErrors > maxNumErrorsToReport) {
497 out <<
"-- We do not report individual errors when there "
498 "are more than " << maxNumErrorsToReport <<
".";
500 else if (numErrors == 1) {
501 out <<
"Error on line " << results.second[0] << endl;
503 else if (numErrors > 1) {
504 out <<
"Errors on lines {";
505 for (
size_t k = 0; k < numErrors-1; ++k) {
506 out << results.second[k] <<
", ";
508 out << results.second[numErrors-1] <<
"}" << endl;
516 #endif // __Teuchos_MatrixMarket_Raw_Reader_hpp
Read a sparse matrix from a Matrix Market file into raw CSR (compressed sparse row) storage...
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.
void setParameters(const RCP< ParameterList > ¶ms)
Set parameters from the given ParameterList.
RCP< const ParameterList > getValidParameters() const
Get a list of valid default parameters, with documentation.
void init()
"Initialize" the Reader.
Adds entries with optional symmetry to a sparse matrix.
bool debug_
Whether to print debugging output to stderr.
std::pair< Teuchos::Tuple< Ordinal, 3 >, bool > readDimensions(std::istream &in, size_t &lineNumber, const bool tolerant=false)
Read (numRows, numCols, numNonzeros).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
T * get() const
Get the raw C++ pointer to the underlying object.
Reader(const RCP< ParameterList > ¶ms)
Constructor that takes a ParameterList of parameters.
Coordinate-format sparse matrix data reader.
This structure defines some basic traits for a scalar field type.
bool read(ArrayRCP< Ordinal > &rowptr, ArrayRCP< Ordinal > &colind, ArrayRCP< Scalar > &values, Ordinal &numRows, Ordinal &numCols, std::istream &in)
Read the sparse matrix from the given input stream into CSR storage.
bool tolerant_
Whether to parse the Matrix Market file tolerantly.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
bool readFile(ArrayRCP< Ordinal > &rowptr, ArrayRCP< Ordinal > &colind, ArrayRCP< Scalar > &values, Ordinal &numRows, Ordinal &numCols, const std::string &filename)
Read the sparse matrix from the given file into CSR storage.
RCP< const Banner > readBanner(std::istream &in, size_t &lineNumber)
Read in the Banner line from the given input stream.
A list of parameters of arbitrary type.
Reader()
Constructor that sets default Boolean parameters.
Smart reference counting pointer class for automatic garbage collection.
Reader(const bool tolerant, const bool debug)
Constructor that takes Boolean parameters.
void reportBadness(std::ostream &out, const std::pair< bool, std::vector< size_t > > &results)
Parse a Matrix Market banner line.
Reference-counted smart pointer for managing arrays.
To be used with Checker for "raw" sparse matrix input.