61 const char sampleMatrixMarketFile[] =
62 "%%MatrixMarket matrix coordinate real general\n" 78 const char symmetricMatrixMarketFile[] =
79 "%%MatrixMarket matrix coordinate real symmetric\n" 96 main (
int argc,
char *argv[])
107 using Teuchos::rcpFromRef;
111 typedef double scalar_type;
112 typedef int ordinal_type;
114 bool success =
false;
123 bool checkOnly =
false;
128 bool tolerant =
false;
132 CommandLineProcessor cmdp (
false,
true);
133 cmdp.setOption (
"filename", &
filename,
134 "Name of the Matrix Market sparse matrix file to read.");
135 cmdp.setOption (
"checkOnly",
"fullTest", &checkOnly,
136 "If true, just check the syntax of the input file. " 137 "Otherwise, do a full test.");
138 cmdp.setOption (
"echo",
"noecho", &echo,
139 "Whether to echo the sparse matrix contents to stdout " 140 "after reading it successfully.");
141 cmdp.setOption (
"tolerant",
"strict", &tolerant,
142 "Whether to tolerate syntax errors in the Matrix Market file.");
143 cmdp.setOption (
"verbose",
"quiet", &verbose,
144 "Print status output to stdout.");
145 cmdp.setOption (
"debug",
"nodebug", &debug,
146 "Print possibly copious debugging output to stderr.");
149 const CommandLineProcessor::EParseCommandLineReturn parseResult =
150 cmdp.parse (argc,argv);
154 if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) {
155 std::cout <<
"End Result: TEST PASSED" << endl;
159 parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
160 std::invalid_argument,
"Failed to parse command-line arguments.");
167 typedef Checker<scalar_type, ordinal_type> checker_type;
168 checker_type checker (echo, tolerant, debug);
170 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
173 cout <<
"Checking syntax of the Matrix Market file \"" <<
filename 176 success = success && checker.readFile (*comm,
filename);
179 cout <<
"The given file is a valid Matrix Market file." << endl;
182 cout <<
"The given file has syntax errors." << endl;
188 cout <<
"Checking syntax of the first built-in Matrix Market example" << endl
192 cerr <<
"First built-in Matrix Market example: " << endl
193 << sampleMatrixMarketFile << endl;
195 std::istringstream in (sampleMatrixMarketFile);
197 success = success && checker.read (*comm, inStream);
200 cout <<
"The example has valid Matrix Market syntax." << endl;
203 cout <<
"The example has syntax errors." << endl;
209 typedef Reader<scalar_type, ordinal_type> reader_type;
210 reader_type reader (tolerant, debug);
211 ArrayRCP<ordinal_type>
ptr, ind;
212 ArrayRCP<scalar_type> val;
213 ordinal_type numRows, numCols;
220 cout <<
"Reading the Matrix Market file \"" <<
filename <<
"\"" << endl;
222 success = success && reader.readFile (
ptr, ind, val,
227 cout <<
"Reading the first built-in Matrix Market example" << endl;
230 cerr <<
"First built-in Matrix Market example:" << endl
231 << sampleMatrixMarketFile << endl;
233 std::istringstream inStr (sampleMatrixMarketFile);
234 success = success && reader.read (
ptr, ind, val, numRows, numCols, inStr);
237 "reader failed to read the given file or input stream.");
238 if (success && verbose) {
239 cout <<
"Returned from reading the Matrix Market data" << endl
243 cerr <<
"CSR output info:" << endl
244 <<
" ptr.size() = " <<
ptr.size()
245 <<
", ind.size() = " << ind.size()
246 <<
", val.size() = " << val.size()
247 <<
", numRows = " << numRows
248 <<
", numCols = " << numCols
257 std::ostringstream outStr;
258 if (success && verbose) {
259 cout <<
"Printing the CSR arrays to a Matrix Market output stream" 260 << endl << std::flush;
263 writer.
write (outStr,
ptr (), ind (), val (), numRows, numCols);
266 cerr <<
"CSR data:" << endl
268 for (ordinal_type i = 0; i <
ptr.size(); ++i) {
270 if (i+1 !=
ptr.size()) {
276 for (ordinal_type i = 0; i < ind.size(); ++i) {
278 if (i+1 != ind.size()) {
284 for (ordinal_type i = 0; i < val.size(); ++i) {
286 if (i+1 != val.size()) {
292 cerr <<
"CSR data, converted back to Matrix Market format" << endl;
293 writer.
write (cerr,
ptr (), ind (), val (), numRows, numCols);
297 ArrayRCP<ordinal_type> newptr, newind;
298 ArrayRCP<scalar_type> newval;
299 ordinal_type newNumRows, newNumCols;
300 if (success && verbose) {
301 cout <<
"Reading the Matrix Market output back into CSR arrays" << endl;
304 std::istringstream inStr (outStr.str ());
305 success = success && reader.read (newptr, newind, newval,
306 newNumRows, newNumCols, inStr);
309 "reader failed to read the output back into CSR arrays.");
310 if (success && verbose) {
311 cout <<
"Successfully read the Matrix Market output back into CSR arrays" 312 << endl << std::flush;
315 cerr <<
"CSR output info:" << endl
316 <<
" newptr.size() = " << newptr.size()
317 <<
", newind.size() = " << newind.size()
318 <<
", newval.size() = " << newval.size()
319 <<
", newNumRows = " << newNumRows
320 <<
", newNumCols = " << newNumCols
326 "New ptr array has a different length than old ptr array");
328 "New ind array has a different length than old ind array");
330 "New val array has a different length than old val array");
332 std::logic_error,
"New dimensions differ from old dimensions");
334 "ptr.size() != numRows+1");
336 "newptr.size() != newNumRows+1");
338 for (ordinal_type rowIndex = 0; rowIndex < numRows; ++rowIndex) {
340 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex] = " 341 <<
ptr[rowIndex] <<
" != newptr[rowIndex] = " << newptr[rowIndex]
344 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex+1] = " 345 <<
ptr[rowIndex+1] <<
" != newptr[rowIndex+1] = " << newptr[rowIndex+1]
347 for (ordinal_type k =
ptr[rowIndex]; k <
ptr[rowIndex+1]; ++k) {
349 "At row index " << rowIndex <<
", ind[k=" << k <<
"] = " 350 << ind[k] <<
" != newind[k] = " << newind[k] <<
".");
356 "At row index " << rowIndex <<
", val[k=" << k <<
"] = " 357 << val[k] <<
" != newval[k] = " << newval[k] <<
".");
363 std::istringstream inStr (symmetricMatrixMarketFile);
364 success = success && reader.read (
ptr, ind, val, numRows, numCols, inStr);
366 "Matrix Market reader failed to read the given example string.");
367 if (success && verbose) {
368 cout <<
"Returned from reading the Matrix Market data" << endl
372 cerr <<
"CSR output info:" << endl
373 <<
" ptr.size() = " <<
ptr.size()
374 <<
", ind.size() = " << ind.size()
375 <<
", val.size() = " << val.size()
376 <<
", numRows = " << numRows
377 <<
", numCols = " << numCols
384 const ordinal_type correctNumEntries = 15;
386 val.size() != correctNumEntries,
388 "Incorrect number of entries after symmetrization: There should be " 389 << correctNumEntries <<
", but there are " << val.size() <<
" entries " 395 std::cout <<
"End Result: TEST PASSED" << endl;
397 std::cout <<
"End Result: TEST FAILED" << endl;
401 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.
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.
int main(int argc, char *argv[])
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.