Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_MatrixMarket_Raw_Writer.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef __Teuchos_MatrixMarket_Raw_Writer_hpp
43 #define __Teuchos_MatrixMarket_Raw_Writer_hpp
44 
45 #include <Teuchos_SetScientific.hpp>
46 #include "Teuchos_ArrayView.hpp"
48 #include <fstream>
49 #include <iostream>
50 
51 namespace Teuchos {
52  namespace MatrixMarket {
53  namespace Raw {
66  template<class ScalarType, class OrdinalType>
67  class Writer {
68  public:
90  void
91  writeFile (const std::string& filename,
92  const ArrayView<const OrdinalType>& rowptr,
93  const ArrayView<const OrdinalType>& colind,
94  const ArrayView<const ScalarType>& values,
95  const OrdinalType numRows,
96  const OrdinalType numCols)
97  {
98  std::ofstream out (filename.c_str ());
99  TEUCHOS_TEST_FOR_EXCEPTION(! out, std::runtime_error,
100  "Failed to open file \"" << filename << "\" for writing.");
101  write (out, rowptr, colind, values, numRows, numCols);
102  }
103 
129  void
130  write (std::ostream& out,
131  const ArrayView<const OrdinalType>& rowptr,
132  const ArrayView<const OrdinalType>& colind,
133  const ArrayView<const ScalarType>& values,
134  const OrdinalType numRows,
135  const OrdinalType numCols)
136  {
137  using std::endl;
138  typedef ScalarTraits<ScalarType> STS;
139  typedef typename ArrayView<const OrdinalType>::size_type size_type;
140 
141  // Make the output stream write floating-point numbers in
142  // scientific notation. It will politely put the output
143  // stream back to its state on input, when this scope
144  // terminates.
146 
147  // Data type string for ScalarType.
148  std::string dataType;
149  if (STS::isComplex) {
150  dataType = "complex";
151  } else if (STS::isOrdinal) {
152  dataType = "integer";
153  } else {
154  dataType = "real";
155  }
156 
157  // Print the Matrix Market banner line. We assume
158  // nonsymmetric storage ("general").
159  out << "%%MatrixMarket matrix coordinate " << dataType << " general"
160  << endl;
161 
162  // // Print comments (the matrix name and / or description).
163  // if (matrixName != "") {
164  // printAsComment (out, matrixName);
165  // }
166  // if (matrixDescription != "") {
167  // printAsComment (out, matrixDescription);
168  // }
169 
170  // Write the dimensions of the sparse matrix: (# rows, #
171  // columns, # matrix entries (counting duplicates as
172  // separate entries)).
173  out << numRows << " " << numCols << " " << rowptr[numRows] << endl;
174 
175  for (size_type i = 0; i < numRows; ++i) {
176  for (OrdinalType k = rowptr[i]; k < rowptr[i+1]; ++k) {
177  const OrdinalType j = colind[k];
178  const ScalarType& A_ij = values[k];
179 
180  // Matrix Market files use 1-based row and column indices.
181  out << (i+1) << " " << (j+1) << " ";
182  if (STS::isComplex) {
183  out << STS::real (A_ij) << " " << STS::imag (A_ij);
184  } else {
185  out << A_ij;
186  }
187  out << endl;
188  }
189  }
190  }
191  }; // end of class Writer
192  } // namespace Raw
193  } // namespace MatrixMarket
194 } // namespace Teuchos
195 
196 #endif // __Teuchos_MatrixMarket_Raw_Writer_hpp
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Ordinal size_type
Type representing the number of elements in an ArrayRCP or view thereof.
Temporarily make an output stream use scientific notation with sufficient precision.
Templated Parameter List class.
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.
void writeFile(const std::string &filename, 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 file.
Nonowning array view.
Write a sparse matrix from raw CSR (compressed sparse row) storage to a Matrix Market file...