42 #ifndef __Teuchos_MatrixMarket_Raw_Reader_hpp
43 #define __Teuchos_MatrixMarket_Raw_Reader_hpp
45 #include "Teuchos_MatrixMarket_Raw_Adder.hpp"
46 #include "Teuchos_MatrixMarket_SymmetrizingAdder.hpp"
47 #include "Teuchos_MatrixMarket_CoordDataReader.hpp"
100 template<
class Scalar,
class Ordinal>
109 Reader (
const bool tolerant,
const bool debug) :
110 tolerant_ (tolerant), debug_ (debug)
117 tolerant_ (false), debug_ (false)
130 tolerant_ (false), debug_ (false)
143 bool tolerant =
false;
147 tolerant = params->
get (
"Parse tolerantly", tolerant);
148 debug = params->
get (
"Debug mode", debug);
152 tolerant_ = tolerant;
161 const bool tolerant =
false;
162 const bool debug =
false;
166 params->set (
"Parse tolerantly", tolerant,
"Whether to tolerate "
167 "syntax errors when parsing the Matrix Market file");
168 params->set (
"Debug mode", debug,
"Whether to print debugging output "
169 "to stderr, on all participating MPI processes");
171 return rcp_const_cast<const ParameterList> (params);
205 const std::string& filename)
207 std::ifstream in (filename.c_str ());
209 "Failed to open file \"" << filename <<
"\" for reading.");
210 return read (rowptr, colind, values, numRows, numCols, in);
263 size_t lineNumber = 1;
268 std::ostringstream err;
270 banner = readBanner (in, lineNumber);
272 catch (std::exception& e) {
273 err <<
"Failed to read Matrix Market input's Banner: " << e.what();
276 cerr << err.str() << endl;
289 if (banner->matrixType () !=
"coordinate") {
290 err <<
"Matrix Market input file must contain a \"coordinate\"-"
291 "format sparse matrix in order to create a sparse matrix object "
295 else if (! STS::isComplex && banner->dataType () ==
"complex") {
296 err <<
"The Matrix Market sparse matrix file contains complex-"
297 "valued data, but you are try to read the data into a sparse "
298 "matrix containing real values (your matrix's Scalar type is "
302 else if (banner->dataType () !=
"real" &&
303 banner->dataType () !=
"complex") {
304 err <<
"Only real or complex data types (no pattern or integer "
305 "matrices) are currently supported.";
311 cerr <<
"Matrix Market banner is invalid: " << err.str () << endl;
317 "Matrix Market banner is invalid: " << err.str ());
321 cerr <<
"Matrix Market Banner line:" << endl << *banner << endl;
334 std::pair<Tuple<Ordinal, 3>,
bool> dims =
337 err <<
"Error reading Matrix Market sparse matrix file: failed to "
338 "read coordinate dimensions.";
341 cerr << err.str () << endl;
355 numRows = dims.first[0];
356 numCols = dims.first[1];
357 const Ordinal numEntries = dims.first[2];
359 cerr <<
"Reported dimensions: " << numRows <<
" x " << numCols
360 <<
", with " << numEntries <<
" entries (counting possible "
361 <<
"duplicates)." << endl;
367 rcp (
new raw_adder_type (numRows, numCols, numEntries,
373 rcp (
new adder_type (rawAdder, banner->symmType ()));
376 reader.setAdder (adder);
381 std::pair<bool, std::vector<size_t> > results =
382 reader.read (in, lineNumber, tolerant_, debug_);
385 if (! results.first) {
386 err <<
"The Matrix Market input stream had syntax error(s)."
387 " Here is the error report." << endl;
388 reportBadness (err, results);
391 cerr << err.str() << endl;
400 size_t numUnique, numRemoved;
405 rawAdder->mergeAndConvertToCSR (numUnique, numRemoved, ptr, ind, val);
407 catch (std::exception& e) {
408 err <<
"Failed to convert sparse matrix data to CSR (compressed "
409 "sparse row) format. Reported error: " << e.what ();
412 cerr << err.str () << endl;
443 cerr <<
"MatrixMarket::Raw::Reader:" << endl
444 <<
"- Tolerant mode: " << tolerant_ << endl
445 <<
"- Debug mode: " << debug_ << endl;
462 readBanner (std::istream& in,
size_t& lineNumber)
474 const bool maybeBannerLine =
true;
475 size_t numLinesRead = 0;
476 bool commentLine =
false;
479 const bool readFailed = ! getline (in, line);
481 "Failed to get Matrix Market banner line from input, after reading "
482 << numLinesRead <<
"line" << (numLinesRead != 1 ?
"s." :
"."));
487 commentLine = checkCommentLine (line, start, size, lineNumber,
488 tolerant_, maybeBannerLine);
489 }
while (commentLine);
492 const bool readFailed = ! getline (in, line);
494 "Failed to get Matrix Market banner line from input. This "
495 "probably means that the file is empty (contains zero lines).");
499 cerr <<
"Raw::Reader::readBanner: Here is the presumed banner line:"
500 << endl << line << endl;
506 banner =
rcp (
new Banner (line, tolerant_));
507 }
catch (std::exception& e) {
509 "Matrix Market file's banner line contains syntax error(s): "
512 return rcp_const_cast<const Banner> (banner);
520 reportBadness (std::ostream& out,
521 const std::pair<
bool, std::vector<size_t> >& results)
524 const size_t numErrors = results.second.size();
525 const size_t maxNumErrorsToReport = 20;
526 out << numErrors <<
" errors when reading Matrix Market sparse "
527 "matrix file." << endl;
528 if (numErrors > maxNumErrorsToReport) {
529 out <<
"-- We do not report individual errors when there "
530 "are more than " << maxNumErrorsToReport <<
".";
532 else if (numErrors == 1) {
533 out <<
"Error on line " << results.second[0] << endl;
535 else if (numErrors > 1) {
536 out <<
"Errors on lines {";
537 for (
size_t k = 0; k < numErrors-1; ++k) {
538 out << results.second[k] <<
", ";
540 out << results.second[numErrors-1] <<
"}" << endl;
548 #endif // __Teuchos_MatrixMarket_Raw_Reader_hpp