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;
118 bool checkOnly =
false;
123 bool tolerant =
false;
125 bool verbose =
false;
129 CommandLineProcessor cmdp (
false,
true);
130 cmdp.setOption (
"filename", &filename,
131 "Name of the Matrix Market sparse matrix file to read.");
132 cmdp.setOption (
"checkOnly",
"fullTest", &checkOnly,
133 "If true, just check the syntax of the input file. " 134 "Otherwise, do a full test.");
135 cmdp.setOption (
"echo",
"noecho", &echo,
136 "Whether to echo the sparse matrix contents to stdout " 137 "after reading it successfully.");
138 cmdp.setOption (
"tolerant",
"strict", &tolerant,
139 "Whether to tolerate syntax errors in the Matrix Market file.");
140 cmdp.setOption (
"verbose",
"quiet", &verbose,
141 "Print status output to stdout.");
142 cmdp.setOption (
"debug",
"nodebug", &debug,
143 "Print possibly copious debugging output to stderr.");
146 const CommandLineProcessor::EParseCommandLineReturn parseResult =
147 cmdp.parse (argc,argv);
151 if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) {
152 std::cout <<
"End Result: TEST PASSED" << endl;
156 parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
157 std::invalid_argument,
"Failed to parse command-line arguments.");
164 typedef Checker<scalar_type, ordinal_type> checker_type;
165 checker_type checker (echo, tolerant, debug);
167 RCP<const Comm<int> > comm =
rcp (
new SerialComm<int>);
168 if (filename !=
"") {
170 cout <<
"Checking syntax of the Matrix Market file \"" << filename
173 success = success && checker.readFile (*comm, filename);
176 cout <<
"The given file is a valid Matrix Market file." << endl;
179 cout <<
"The given file has syntax errors." << endl;
185 cout <<
"Checking syntax of the first built-in Matrix Market example" << endl
189 cerr <<
"First built-in Matrix Market example: " << endl
190 << sampleMatrixMarketFile << endl;
192 std::istringstream in (sampleMatrixMarketFile);
194 success = success && checker.read (*comm, inStream);
197 cout <<
"The example has valid Matrix Market syntax." << endl;
200 cout <<
"The example has syntax errors." << endl;
206 typedef Reader<scalar_type, ordinal_type> reader_type;
207 reader_type reader (tolerant, debug);
208 ArrayRCP<ordinal_type>
ptr, ind;
209 ArrayRCP<scalar_type> val;
210 ordinal_type numRows, numCols;
215 if (filename !=
"") {
217 cout <<
"Reading the Matrix Market file \"" << filename <<
"\"" << endl;
219 success = success && reader.readFile (ptr, ind, val,
220 numRows, numCols, filename);
224 cout <<
"Reading the first built-in Matrix Market example" << endl;
227 cerr <<
"First built-in Matrix Market example:" << endl
228 << sampleMatrixMarketFile << endl;
230 std::istringstream inStr (sampleMatrixMarketFile);
231 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
234 "reader failed to read the given file or input stream.");
235 if (success && verbose) {
236 cout <<
"Returned from reading the Matrix Market data" << endl
240 cerr <<
"CSR output info:" << endl
241 <<
" ptr.size() = " << ptr.size()
242 <<
", ind.size() = " << ind.size()
243 <<
", val.size() = " << val.size()
244 <<
", numRows = " << numRows
245 <<
", numCols = " << numCols
254 std::ostringstream outStr;
255 if (success && verbose) {
256 cout <<
"Printing the CSR arrays to a Matrix Market output stream" 257 << endl << std::flush;
260 writer.
write (outStr, ptr (), ind (), val (), numRows, numCols);
263 cerr <<
"CSR data:" << endl
265 for (ordinal_type i = 0; i < ptr.size(); ++i) {
267 if (i+1 != ptr.size()) {
273 for (ordinal_type i = 0; i < ind.size(); ++i) {
275 if (i+1 != ind.size()) {
281 for (ordinal_type i = 0; i < val.size(); ++i) {
283 if (i+1 != val.size()) {
289 cerr <<
"CSR data, converted back to Matrix Market format" << endl;
290 writer.
write (cerr, ptr (), ind (), val (), numRows, numCols);
294 ArrayRCP<ordinal_type> newptr, newind;
295 ArrayRCP<scalar_type> newval;
296 ordinal_type newNumRows, newNumCols;
297 if (success && verbose) {
298 cout <<
"Reading the Matrix Market output back into CSR arrays" << endl;
301 std::istringstream inStr (outStr.str ());
302 success = success && reader.read (newptr, newind, newval,
303 newNumRows, newNumCols, inStr);
306 "reader failed to read the output back into CSR arrays.");
307 if (success && verbose) {
308 cout <<
"Successfully read the Matrix Market output back into CSR arrays" 309 << endl << std::flush;
312 cerr <<
"CSR output info:" << endl
313 <<
" newptr.size() = " << newptr.size()
314 <<
", newind.size() = " << newind.size()
315 <<
", newval.size() = " << newval.size()
316 <<
", newNumRows = " << newNumRows
317 <<
", newNumCols = " << newNumCols
323 "New ptr array has a different length than old ptr array");
325 "New ind array has a different length than old ind array");
327 "New val array has a different length than old val array");
329 std::logic_error,
"New dimensions differ from old dimensions");
331 "ptr.size() != numRows+1");
333 "newptr.size() != newNumRows+1");
335 for (ordinal_type rowIndex = 0; rowIndex < numRows; ++rowIndex) {
337 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex] = " 338 << ptr[rowIndex] <<
" != newptr[rowIndex] = " << newptr[rowIndex]
341 std::logic_error,
"At row index " << rowIndex <<
", ptr[rowIndex+1] = " 342 << ptr[rowIndex+1] <<
" != newptr[rowIndex+1] = " << newptr[rowIndex+1]
344 for (ordinal_type k = ptr[rowIndex]; k < ptr[rowIndex+1]; ++k) {
346 "At row index " << rowIndex <<
", ind[k=" << k <<
"] = " 347 << ind[k] <<
" != newind[k] = " << newind[k] <<
".");
353 "At row index " << rowIndex <<
", val[k=" << k <<
"] = " 354 << val[k] <<
" != newval[k] = " << newval[k] <<
".");
359 if (filename ==
"") {
360 std::istringstream inStr (symmetricMatrixMarketFile);
361 success = success && reader.read (ptr, ind, val, numRows, numCols, inStr);
363 "Matrix Market reader failed to read the given example string.");
364 if (success && verbose) {
365 cout <<
"Returned from reading the Matrix Market data" << endl
369 cerr <<
"CSR output info:" << endl
370 <<
" ptr.size() = " << ptr.size()
371 <<
", ind.size() = " << ind.size()
372 <<
", val.size() = " << val.size()
373 <<
", numRows = " << numRows
374 <<
", numCols = " << numCols
381 const ordinal_type correctNumEntries = 15;
383 val.size() != correctNumEntries,
385 "Incorrect number of entries after symmetrization: There should be " 386 << correctNumEntries <<
", but there are " << val.size() <<
" entries " 392 std::cout <<
"End Result: TEST PASSED" << endl;
396 std::cout <<
"End Result: TEST FAILED" << endl;
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.
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.