60 const char sampleMatrixMarketFile[] =
61 "%%MatrixMarket matrix coordinate real general\n"
77 const char symmetricMatrixMarketFile[] =
78 "%%MatrixMarket matrix coordinate real symmetric\n"
95 main (
int argc,
char *argv[])
106 using Teuchos::rcpFromRef;
110 typedef double scalar_type;
111 typedef int ordinal_type;
113 bool success =
false;
122 bool checkOnly =
false;
127 bool tolerant =
false;
131 CommandLineProcessor cmdp (
false,
true);
132 cmdp.setOption (
"filename", &filename,
133 "Name of the Matrix Market sparse matrix file to read.");
134 cmdp.setOption (
"checkOnly",
"fullTest", &checkOnly,
135 "If true, just check the syntax of the input file. "
136 "Otherwise, do a full test.");
137 cmdp.setOption (
"echo",
"noecho", &echo,
138 "Whether to echo the sparse matrix contents to stdout "
139 "after reading it successfully.");
140 cmdp.setOption (
"tolerant",
"strict", &tolerant,
141 "Whether to tolerate syntax errors in the Matrix Market file.");
142 cmdp.setOption (
"verbose",
"quiet", &verbose,
143 "Print status output to stdout.");
144 cmdp.setOption (
"debug",
"nodebug", &debug,
145 "Print possibly copious debugging output to stderr.");
148 const CommandLineProcessor::EParseCommandLineReturn parseResult =
149 cmdp.parse (argc,argv);
153 if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) {
154 std::cout <<
"End Result: TEST PASSED" << endl;
158 parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
159 std::invalid_argument,
"Failed to parse command-line arguments.");
172 cout <<
"Checking MatrixMarket banner parsing\n";
176 if (verbose) cout <<
"Reading first banner\n";
177 typedef Checker<scalar_type, ordinal_type> checker_type;
178 checker_type checker (echo,
false,
false);
179 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
180 std::stringstream in;
181 in.str(
"%%MatrixMarket matrix coordinate real symmetric\n0 0 0\n");
183 xs = checker.read (*comm, inStream);
185 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
187 success = success && xs;
192 if (verbose) cout <<
"Reading second banner\n";
193 typedef Checker<scalar_type, ordinal_type> checker_type;
194 checker_type checker (echo,
false,
false);
195 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
196 std::stringstream in;
197 in.str(
"%%MatrixMarket\tmatrix\t\tcoordinate real symmetric\n0 0 0\n");
199 xs = checker.read (*comm, inStream);
201 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
203 success = success && xs;
208 if (verbose) cout <<
"Reading third banner\n";
209 typedef Checker<scalar_type, ordinal_type> checker_type;
210 checker_type checker (echo,
false,
false);
211 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
212 std::stringstream in;
214 in.str(
"%%MatrixMarket matrix coordinate real xyz\n0 0 0\n");
216 checker.read (*comm, inStream);
221 catch (
const std::runtime_error& e) {
224 std::string es(e.what());
225 xs = es.find(
"xyz") != std::string::npos;
228 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
230 success = success && xs;
235 cout <<
"Banners parsed successfully\n";
237 cout <<
"Banners not parsed successfully\n";
243 typedef Checker<scalar_type, ordinal_type> checker_type;
244 checker_type checker (echo, tolerant, debug);
246 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
247 if (filename !=
"") {
249 cout <<
"Checking syntax of the Matrix Market file \"" << filename
252 success = success && checker.readFile (*comm, filename);
255 cout <<
"The given file is a valid Matrix Market file." << endl;
258 cout <<
"The given file has syntax errors." << endl;
264 cout <<
"Checking syntax of the first built-in Matrix Market example" << endl
268 cerr <<
"First built-in Matrix Market example: " << endl
269 << sampleMatrixMarketFile << endl;
271 std::istringstream in (sampleMatrixMarketFile);
273 success = success && checker.read (*comm, inStream);
276 cout <<
"The example has valid Matrix Market syntax." << endl;
279 cout <<
"The example has syntax errors." << endl;
285 typedef Reader<scalar_type, ordinal_type> reader_type;
286 reader_type reader (tolerant, debug);
287 ArrayRCP<ordinal_type>
ptr, ind;
288 ArrayRCP<scalar_type> val;
289 ordinal_type numRows, numCols;
294 if (filename !=
"") {
296 cout <<
"Reading the Matrix Market file \"" << filename <<
"\"" << endl;
298 success = success && reader.readFile (ptr, ind, val,
299 numRows, numCols, filename);
303 cout <<
"Reading the first built-in Matrix Market example" << endl;
306 cerr <<
"First built-in Matrix Market example:" << endl
307 << sampleMatrixMarketFile << endl;
309 std::istringstream inStr (sampleMatrixMarketFile);
310 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
313 "reader failed to read the given file or input stream.");
314 if (success && verbose) {
315 cout <<
"Returned from reading the Matrix Market data" << endl
319 cerr <<
"CSR output info:" << endl
320 <<
" ptr.size() = " << ptr.size()
321 <<
", ind.size() = " << ind.size()
322 <<
", val.size() = " << val.size()
323 <<
", numRows = " << numRows
324 <<
", numCols = " << numCols
333 std::ostringstream outStr;
334 if (success && verbose) {
335 cout <<
"Printing the CSR arrays to a Matrix Market output stream"
336 << endl << std::flush;
339 writer.
write (outStr, ptr (), ind (), val (), numRows, numCols);
342 cerr <<
"CSR data:" << endl
344 for (ordinal_type i = 0; i < ptr.size(); ++i) {
346 if (i+1 != ptr.size()) {
352 for (ordinal_type i = 0; i < ind.size(); ++i) {
354 if (i+1 != ind.size()) {
360 for (ordinal_type i = 0; i < val.size(); ++i) {
362 if (i+1 != val.size()) {
368 cerr <<
"CSR data, converted back to Matrix Market format" << endl;
369 writer.
write (cerr, ptr (), ind (), val (), numRows, numCols);
373 ArrayRCP<ordinal_type> newptr, newind;
374 ArrayRCP<scalar_type> newval;
375 ordinal_type newNumRows, newNumCols;
376 if (success && verbose) {
377 cout <<
"Reading the Matrix Market output back into CSR arrays" << endl;
380 std::istringstream inStr (outStr.str ());
381 success = success && reader.read (newptr, newind, newval,
382 newNumRows, newNumCols, inStr);
385 "reader failed to read the output back into CSR arrays.");
386 if (success && verbose) {
387 cout <<
"Successfully read the Matrix Market output back into CSR arrays"
388 << endl << std::flush;
391 cerr <<
"CSR output info:" << endl
392 <<
" newptr.size() = " << newptr.size()
393 <<
", newind.size() = " << newind.size()
394 <<
", newval.size() = " << newval.size()
395 <<
", newNumRows = " << newNumRows
396 <<
", newNumCols = " << newNumCols
402 "New ptr array has a different length than old ptr array");
404 "New ind array has a different length than old ind array");
406 "New val array has a different length than old val array");
408 std::logic_error,
"New dimensions differ from old dimensions");
410 "ptr.size() != numRows+1");
412 "newptr.size() != newNumRows+1");
414 for (ordinal_type rowIndex = 0; rowIndex < numRows; ++rowIndex) {
416 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex] = "
417 << ptr[rowIndex] <<
" != newptr[rowIndex] = " << newptr[rowIndex]
420 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex+1] = "
421 << ptr[rowIndex+1] <<
" != newptr[rowIndex+1] = " << newptr[rowIndex+1]
423 for (ordinal_type k = ptr[rowIndex]; k < ptr[rowIndex+1]; ++k) {
425 "At row index " << rowIndex <<
", ind[k=" << k <<
"] = "
426 << ind[k] <<
" != newind[k] = " << newind[k] <<
".");
432 "At row index " << rowIndex <<
", val[k=" << k <<
"] = "
433 << val[k] <<
" != newval[k] = " << newval[k] <<
".");
438 if (filename ==
"") {
439 std::istringstream inStr (symmetricMatrixMarketFile);
440 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
442 "Matrix Market reader failed to read the given example string.");
443 if (success && verbose) {
444 cout <<
"Returned from reading the Matrix Market data" << endl
448 cerr <<
"CSR output info:" << endl
449 <<
" ptr.size() = " << ptr.size()
450 <<
", ind.size() = " << ind.size()
451 <<
", val.size() = " << val.size()
452 <<
", numRows = " << numRows
453 <<
", numCols = " << numCols
460 const ordinal_type correctNumEntries = 15;
462 val.size() != correctNumEntries,
464 "Incorrect number of entries after symmetrization: There should be "
465 << correctNumEntries <<
", but there are " << val.size() <<
" entries "
471 std::cout <<
"End Result: TEST PASSED" << endl;
473 std::cout <<
"End Result: TEST FAILED" << endl;
477 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
Read a sparse matrix from a Matrix Market file into raw CSR (compressed sparse row) storage...
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Concrete serial communicator subclass.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void write(std::ostream &out, const ArrayView< const OrdinalType > &rowptr, const ArrayView< const OrdinalType > &colind, const ArrayView< const ScalarType > &values, const OrdinalType numRows, const OrdinalType numCols)
Write the sparse matrix to the given output stream.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
A list of parameters of arbitrary type.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
int main(int argc, char *argv[])
Abstract interface for distributed-memory communication.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
RCP< T > rcpFromRef(T &r)
Return a non-owning weak RCP object from a raw object reference for a defined type.
Smart reference counting pointer class for automatic garbage collection.
Tool for debugging the syntax of a Matrix Market file containing a sparse matrix. ...
Write a sparse matrix from raw CSR (compressed sparse row) storage to a Matrix Market file...
Class that helps parse command line input arguments from (argc,argv[]) and set options.
Reference-counted smart pointer for managing arrays.