28 const char sampleMatrixMarketFile[] =
29 "%%MatrixMarket matrix coordinate real general\n"
45 const char symmetricMatrixMarketFile[] =
46 "%%MatrixMarket matrix coordinate real symmetric\n"
63 main (
int argc,
char *argv[])
74 using Teuchos::rcpFromRef;
78 typedef double scalar_type;
79 typedef int ordinal_type;
90 bool checkOnly =
false;
95 bool tolerant =
false;
99 CommandLineProcessor cmdp (
false,
true);
100 cmdp.setOption (
"filename", &filename,
101 "Name of the Matrix Market sparse matrix file to read.");
102 cmdp.setOption (
"checkOnly",
"fullTest", &checkOnly,
103 "If true, just check the syntax of the input file. "
104 "Otherwise, do a full test.");
105 cmdp.setOption (
"echo",
"noecho", &echo,
106 "Whether to echo the sparse matrix contents to stdout "
107 "after reading it successfully.");
108 cmdp.setOption (
"tolerant",
"strict", &tolerant,
109 "Whether to tolerate syntax errors in the Matrix Market file.");
110 cmdp.setOption (
"verbose",
"quiet", &verbose,
111 "Print status output to stdout.");
112 cmdp.setOption (
"debug",
"nodebug", &debug,
113 "Print possibly copious debugging output to stderr.");
116 const CommandLineProcessor::EParseCommandLineReturn parseResult =
117 cmdp.parse (argc,argv);
121 if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) {
122 std::cout <<
"End Result: TEST PASSED" << endl;
126 parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
127 std::invalid_argument,
"Failed to parse command-line arguments.");
140 cout <<
"Checking MatrixMarket banner parsing\n";
144 if (verbose) cout <<
"Reading first banner\n";
145 typedef Checker<scalar_type, ordinal_type> checker_type;
146 checker_type checker (echo,
false,
false);
147 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
148 std::stringstream in;
149 in.str(
"%%MatrixMarket matrix coordinate real symmetric\n0 0 0\n");
151 xs = checker.read (*comm, inStream);
153 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
155 success = success && xs;
160 if (verbose) cout <<
"Reading second banner\n";
161 typedef Checker<scalar_type, ordinal_type> checker_type;
162 checker_type checker (echo,
false,
false);
163 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
164 std::stringstream in;
165 in.str(
"%%MatrixMarket\tmatrix\t\tcoordinate real symmetric\n0 0 0\n");
167 xs = checker.read (*comm, inStream);
169 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
171 success = success && xs;
176 if (verbose) cout <<
"Reading third 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;
182 in.str(
"%%MatrixMarket matrix coordinate real xyz\n0 0 0\n");
184 checker.read (*comm, inStream);
189 catch (
const std::runtime_error& e) {
192 std::string es(e.what());
193 xs = es.find(
"xyz") != std::string::npos;
196 cout <<
"Banner read " << (!xs ?
"un" :
"") <<
"successfully\n";
198 success = success && xs;
203 cout <<
"Banners parsed successfully\n";
205 cout <<
"Banners not parsed successfully\n";
211 typedef Checker<scalar_type, ordinal_type> checker_type;
212 checker_type checker (echo, tolerant, debug);
214 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
215 if (filename !=
"") {
217 cout <<
"Checking syntax of the Matrix Market file \"" << filename
220 success = success && checker.readFile (*comm, filename);
223 cout <<
"The given file is a valid Matrix Market file." << endl;
226 cout <<
"The given file has syntax errors." << endl;
232 cout <<
"Checking syntax of the first built-in Matrix Market example" << endl
236 cerr <<
"First built-in Matrix Market example: " << endl
237 << sampleMatrixMarketFile << endl;
239 std::istringstream in (sampleMatrixMarketFile);
241 success = success && checker.read (*comm, inStream);
244 cout <<
"The example has valid Matrix Market syntax." << endl;
247 cout <<
"The example has syntax errors." << endl;
253 typedef Reader<scalar_type, ordinal_type> reader_type;
254 reader_type reader (tolerant, debug);
255 ArrayRCP<ordinal_type>
ptr, ind;
256 ArrayRCP<scalar_type> val;
257 ordinal_type numRows, numCols;
262 if (filename !=
"") {
264 cout <<
"Reading the Matrix Market file \"" << filename <<
"\"" << endl;
266 success = success && reader.readFile (ptr, ind, val,
267 numRows, numCols, filename);
271 cout <<
"Reading the first built-in Matrix Market example" << endl;
274 cerr <<
"First built-in Matrix Market example:" << endl
275 << sampleMatrixMarketFile << endl;
277 std::istringstream inStr (sampleMatrixMarketFile);
278 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
281 "reader failed to read the given file or input stream.");
282 if (success && verbose) {
283 cout <<
"Returned from reading the Matrix Market data" << endl
287 cerr <<
"CSR output info:" << endl
288 <<
" ptr.size() = " << ptr.size()
289 <<
", ind.size() = " << ind.size()
290 <<
", val.size() = " << val.size()
291 <<
", numRows = " << numRows
292 <<
", numCols = " << numCols
301 std::ostringstream outStr;
302 if (success && verbose) {
303 cout <<
"Printing the CSR arrays to a Matrix Market output stream"
304 << endl << std::flush;
307 writer.
write (outStr, ptr (), ind (), val (), numRows, numCols);
310 cerr <<
"CSR data:" << endl
312 for (ordinal_type i = 0; i < ptr.size(); ++i) {
314 if (i+1 != ptr.size()) {
320 for (ordinal_type i = 0; i < ind.size(); ++i) {
322 if (i+1 != ind.size()) {
328 for (ordinal_type i = 0; i < val.size(); ++i) {
330 if (i+1 != val.size()) {
336 cerr <<
"CSR data, converted back to Matrix Market format" << endl;
337 writer.
write (cerr, ptr (), ind (), val (), numRows, numCols);
341 ArrayRCP<ordinal_type> newptr, newind;
342 ArrayRCP<scalar_type> newval;
343 ordinal_type newNumRows, newNumCols;
344 if (success && verbose) {
345 cout <<
"Reading the Matrix Market output back into CSR arrays" << endl;
348 std::istringstream inStr (outStr.str ());
349 success = success && reader.read (newptr, newind, newval,
350 newNumRows, newNumCols, inStr);
353 "reader failed to read the output back into CSR arrays.");
354 if (success && verbose) {
355 cout <<
"Successfully read the Matrix Market output back into CSR arrays"
356 << endl << std::flush;
359 cerr <<
"CSR output info:" << endl
360 <<
" newptr.size() = " << newptr.size()
361 <<
", newind.size() = " << newind.size()
362 <<
", newval.size() = " << newval.size()
363 <<
", newNumRows = " << newNumRows
364 <<
", newNumCols = " << newNumCols
370 "New ptr array has a different length than old ptr array");
372 "New ind array has a different length than old ind array");
374 "New val array has a different length than old val array");
376 std::logic_error,
"New dimensions differ from old dimensions");
378 "ptr.size() != numRows+1");
380 "newptr.size() != newNumRows+1");
382 for (ordinal_type rowIndex = 0; rowIndex < numRows; ++rowIndex) {
384 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex] = "
385 << ptr[rowIndex] <<
" != newptr[rowIndex] = " << newptr[rowIndex]
388 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex+1] = "
389 << ptr[rowIndex+1] <<
" != newptr[rowIndex+1] = " << newptr[rowIndex+1]
391 for (ordinal_type k = ptr[rowIndex]; k < ptr[rowIndex+1]; ++k) {
393 "At row index " << rowIndex <<
", ind[k=" << k <<
"] = "
394 << ind[k] <<
" != newind[k] = " << newind[k] <<
".");
400 "At row index " << rowIndex <<
", val[k=" << k <<
"] = "
401 << val[k] <<
" != newval[k] = " << newval[k] <<
".");
406 if (filename ==
"") {
407 std::istringstream inStr (symmetricMatrixMarketFile);
408 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
410 "Matrix Market reader failed to read the given example string.");
411 if (success && verbose) {
412 cout <<
"Returned from reading the Matrix Market data" << endl
416 cerr <<
"CSR output info:" << endl
417 <<
" ptr.size() = " << ptr.size()
418 <<
", ind.size() = " << ind.size()
419 <<
", val.size() = " << val.size()
420 <<
", numRows = " << numRows
421 <<
", numCols = " << numCols
428 const ordinal_type correctNumEntries = 15;
430 val.size() != correctNumEntries,
432 "Incorrect number of entries after symmetrization: There should be "
433 << correctNumEntries <<
", but there are " << val.size() <<
" entries "
439 std::cout <<
"End Result: TEST PASSED" << endl;
441 std::cout <<
"End Result: TEST FAILED" << endl;
445 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.