Debian Codegraph @

packages/belos/tpetra/test/OrthoManager/belos_orthomanager_tpetra_util.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 #ifndef __belos_orthomanager_tpetra_util_hpp
10 #define __belos_orthomanager_tpetra_util_hpp
11
12 #include <BelosTypes.hpp> // includes BelosConfigDefs.hpp
13 #include <BelosOrthoManagerFactory.hpp>
14 #include <BelosOrthoManagerTest.hpp>
15 #include <BelosTpetraTestFramework.hpp>
16 #include <BelosTpetraAdapter.hpp>
17 #include <Tpetra_Core.hpp>
18 #include <Tpetra_CrsMatrix.hpp>
19
20 // I/O for Harwell-Boeing files
21 #include <Trilinos_Util_iohb.h>
22
23 namespace Belos {
24 namespace Test {
25
26 //! Print Belos version information
27 void
28 printVersionInfo (std::ostream& debugOut)
29 {
30 using std::endl;
31
32 debugOut << "Belos version information:" << endl
33 << Belos::Belos_Version() << endl << endl;
34 }
35
36 //! Return a MsgType enum to specify Belos::OutputManager verbosity
37 int
38 selectVerbosity (const bool verbose, const bool debug)
39 {
40 // NOTE Calling this a "MsgType" (its correct type) or even an
41 // "enum MsgType" confuses the compiler.
42 int theType = Belos::Errors; // default (always print errors)
43 if (verbose)
44 {
45 // "Verbose" also means printing out Debug messages (as well
46 // as everything else).
47 theType = theType |
48 Belos::Warnings |
49 Belos::IterationDetails |
50 Belos::OrthoDetails |
51 Belos::FinalSummary |
52 Belos::TimingDetails |
53 Belos::StatusTestDetails |
54 Belos::Debug;
55 }
56 if (debug)
57 // "Debug" doesn't necessarily mean the same thing as
58 // "Verbose". We interpret "Debug" to mean printing out
59 // messages marked as Debug (as well as Error messages).
60 theType = theType | Belos::Debug;
61 return theType;
62 }
63
64 template<class Scalar>
65 Teuchos::RCP<Belos::OutputManager<Scalar> >
66 makeOutputManager (const bool verbose, const bool debug)
67 {
68 return Teuchos::rcp (new Belos::OutputManager<Scalar> (selectVerbosity (verbose, debug)));
69 }
70
71 /// \fn loadSparseMatrix
72 /// \brief Load a sparse matrix from a Harwell-Boeing file
73 ///
74 /// Load a sparse matrix from a Harwell-Boeing file, distribute
75 /// it, and return RCPs to a map_type (the map object describing the
76 /// distribution of the sparse matrix: we distribute in a way such
77 /// that the domain, range, and row maps are the same) and a
78 /// sparse_matrix_type (the sparse matrix itself).
79 template<class SC, class LO, class GO, class NodeType>
80 std::pair<Teuchos::RCP<::Tpetra::Map<LO, GO, NodeType> >, Teuchos::RCP<::Tpetra::CrsMatrix<SC, LO, GO, NodeType> > >
81 loadSparseMatrix (const Teuchos::RCP< const Teuchos::Comm<int> > pComm,
82 const std::string& filename,
83 int& numRows,
84 std::ostream& debugOut)
85 {
86 typedef SC scalar_type;
87 typedef LO local_ordinal_type;
88 typedef GO global_ordinal_type;
89 typedef NodeType node_type;
90
91 typedef ::Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
92 typedef ::Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> sparse_matrix_type;
93
94 using Teuchos::RCP;
95 using Teuchos::rcp;
96 using std::vector;
97
98 // const int myRank = Teuchos::rank (*pComm);
99 RCP<map_type> pMap;
100 RCP<sparse_matrix_type> pMatrix;
101
102 if (filename != "")
103 {
104 debugOut << "Loading sparse matrix file \"" << filename << "\"" << endl;
105 Belos::Tpetra::HarwellBoeingReader<sparse_matrix_type> reader(pComm);
106 pMatrix = reader.readFromFile(filename);
107 pMap = Teuchos::rcp_const_cast<map_type>(pMatrix->getRowMap());
108 #if 0
109 int loadedNumRows = 0;
110 int numCols = 0;
111 int nnz = -1;
112 int rnnzmax = 0;
113 double *dvals = NULL;
114 int *colptr = NULL;
115 int *rowind = NULL;
116
117 // The Harwell-Boeing routines use info == 0 to signal failure.
118 int info = 0;
119
120 if (myRank == 0)
121 {
122 // Proc 0 reads the sparse matrix (stored in Harwell-Boeing
123 // format) from the file into the tuple (loadedNumRows, numCols, nnz,
124 // colptr, rowind, dvals). The routine allocates memory for
125 // colptr, rowind, and dvals using malloc().
126 info = readHB_newmat_double (filename.c_str(), &loadedNumRows,
127 &numCols, &nnz, &colptr, &rowind,
128 &dvals);
129 // Make sure that loadedNumRows has a sensible value,
130 // since we'll need to allocate an std::vector with that
131 // many elements.
132 TEUCHOS_TEST_FOR_EXCEPTION(loadedNumRows < 0, std::runtime_error,
133 "Harwell-Boeing sparse matrix file reports that "
134 "the matrix has # rows = " << loadedNumRows
135 << " < 0.");
136
137 // The Harwell-Boeing routines use info == 0 to signal failure.
138 if (info != 0)
139 {
140 // rnnzmax := maximum number of nonzeros per row, over all
141 // rows of the sparse matrix.
142 std::vector<int> rnnz (loadedNumRows, 0);
143 for (int *ri = rowind; ri < rowind + nnz; ++ri) {
144 ++rnnz[*ri-1];
145 }
146 // This business with the iterator ensures that results
147 // are sensible even if the sequence is empty.
148 std::vector<int>::const_iterator iter =
149 std::max_element (rnnz.begin(),rnnz.end());
150 if (iter != rnnz.end())
151 rnnzmax = *iter;
152 else
153 // The matrix has zero rows, so the max number of
154 // nonzeros per row is trivially zero.
155 rnnzmax = 0;
156 }
157 }
158
159 // Proc 0 now broadcasts the sparse matrix data to the other
160 // process(es). First things broadcast are info and nnz, which
161 // tell the other process(es) whether reading the sparse matrix
162 // succeeded. (info should be nonzero if so. The
163 // Harwell-Boeing routines return "C boolean true" rather than
164 // the POSIX-standard "zero for success.")
165 Teuchos::broadcast (*pComm, 0, &info);
166 Teuchos::broadcast (*pComm, 0, &nnz);
167
168 TEUCHOS_TEST_FOR_EXCEPTION(info == 0, std::runtime_error,
169 "Error reading Harwell-Boeing sparse matrix file \""
170 << filename << "\"" << std::endl);
171
172 TEUCHOS_TEST_FOR_EXCEPTION(nnz < 0, std::runtime_error,
173 "Harwell-Boeing sparse matrix file \""
174 << filename << "\" reports having negative nnz "
175 << "(= " << nnz << ")"
176 << std::endl);
177
178 TEUCHOS_TEST_FOR_EXCEPTION(nnz == 0, std::runtime_error,
179 "Test matrix in Harwell-Boeing sparse matrix file '"
180 << filename << "' " << "has zero nonzero values, which "
181 << "means it does not define a valid inner product."
182 << std::endl);
183
184 Teuchos::broadcast (*pComm, 0, &loadedNumRows);
185 Teuchos::broadcast (*pComm, 0, &numCols);
186 Teuchos::broadcast (*pComm, 0, &rnnzmax);
187
188 TEUCHOS_TEST_FOR_EXCEPTION(loadedNumRows != numCols, std::runtime_error,
189 "Test matrix in Harwell-Boeing sparse matrix file '"
190 << filename << "' " << "is not square: it is "
191 << loadedNumRows << " by " << numCols << std::endl);
192 // We've fully validated the number of rows, so set the
193 // appropriate output parameter.
194 numRows = loadedNumRows;
195
196 // Create Tpetra::Map to represent multivectors in the range of
197 // the sparse matrix.
198 pMap = rcp (new map_type (numRows, 0, pComm,
199 Tpetra::GloballyDistributed));
200 // Second argument: max number of nonzero entries per row.
201 pMatrix = rcp (new sparse_matrix_type (pMap, rnnzmax));
202
203 if (myRank == 0)
204 {
205 // Convert from Harwell-Boeing format (compressed sparse
206 // column, one-indexed) to CrsMatrix format (compressed
207 // sparse row, zero-index). We do this by iterating over
208 // all the columns of the matrix.
209 int curNonzeroIndex = 0;
210 for (global_ordinal_type c = 0; c < global_ordinal_type(numCols); ++c)
211 {
212 for (int colnnz = 0; colnnz < colptr[c+1] - colptr[c]; ++colnnz)
213 {
214 // Row index: *rptr - 1 (1-based -> 0-based indexing)
215 // Column index: c
216 // Value to insert there: *dptr
217 const int curGlobalRowIndex = rowind[curNonzeroIndex] - 1;
218 const scalar_type curValue = dvals[curNonzeroIndex];
219 pMatrix->insertGlobalValues (curGlobalRowIndex,
220 Teuchos::tuple(c),
221 Teuchos::tuple(curValue));
222 curNonzeroIndex++;
223 }
224 }
225 }
226 if (myRank == 0)
227 {
228 // Free memory allocated by the Harwell-Boeing input routine.
229 if (dvals != NULL)
230 {
231 free (dvals);
232 dvals = NULL;
233 }
234 if (colptr != NULL)
235 {
236 free (colptr);
237 colptr = NULL;
238 }
239 if (rowind != NULL)
240 {
241 free (rowind);
242 rowind = NULL;
243 }
244 }
245 // We're done reading in the sparse matrix. Now distribute it
246 // among the processes. The domain, range, and row maps are
247 // the same (the matrix must be square).
248 pMatrix->fillComplete();
249 #endif
250 debugOut << "Completed loading and distributing sparse matrix" << endl;
251 } // else M == null
252 else
253 {
254 debugOut << "Testing with Euclidean inner product" << endl;
255
256 // Let M remain null, and allocate map using the number of rows
257 // (numRows) specified on the command line.
258 pMap = rcp (new map_type (numRows, 0, pComm,
259 ::Tpetra::GloballyDistributed));
260 }
261 return std::make_pair (pMap, pMatrix);
262 }
263
264 } // namespace Test
265 } // namespace Belos
266
267 #endif // __belos_orthomanager_tpetra_util_hpp