Debian Codegraph @

packages/belos/tpetra/test/OrthoManager/belos_orthomanager_tpetra.cpp

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
10 /// \file belos_orthomanager_tpetra.cpp
11 /// \brief Test (Mat)OrthoManager subclass(es) with Tpetra
12 ///
13 /// Test various subclasses of (Mat)OrthoManager, using
14 /// Tpetra::MultiVector as the multivector implementation,
15 /// and Tpetra::Operator as the operator implementation.
16 ///
17 #include "belos_orthomanager_tpetra_util.hpp"
18 #include "Teuchos_CommandLineProcessor.hpp"
19 #include "Tpetra_Core.hpp"
20
21 using std::endl;
22
23 //////////////////////////////////////////////////////////////////////
24 // Command-line arguments
25 //////////////////////////////////////////////////////////////////////
26
27 // The name of the (Mat)OrthoManager subclass to instantiate.
28 std::string orthoManName ("DGKS");
29
30 // For SimpleOrthoManager: the normalization method to use.
31 std::string normalization ("CGS");
32
33 // Name of the Harwell-Boeing sparse matrix file from which to read
34 // the inner product operator matrix. If name is "" or not provided
35 // at the command line, use the standard Euclidean inner product.
36 std::string filename;
37
38 bool verbose = false;
39 bool debug = false;
40
41 // The OrthoManager is tested with three different multivectors: S
42 // (sizeS columns), X1 (sizeX1 columns), and X2 (sizeX2 columns). The
43 // values below are defaults and may be changed by the corresponding
44 // command-line arguments.
45 int sizeS = 5;
46 int sizeX1 = 11;
47 int sizeX2 = 13;
48
49 // Default _global_ number of rows. The number of rows per MPI
50 // process must be no less than max(sizeS, sizeX1, sizeX2). To ensure
51 // that the test always passes with default parameters, we must scale
52 // below by the number of processes. The default value below may be
53 // changed by a command-line parameter with a corresponding name.
54 int numRows = 100;
55
56 // Set default values of command-line arguments,
57 // and get their actual values from the command line.
58 static void
59 getCmdLineArgs (const Teuchos::Comm<int>& comm, int argc, char* argv[])
60 {
61 using Teuchos::CommandLineProcessor;
62 typedef Tpetra::MultiVector<>::scalar_type ST;
63
64 // Define a OrthoManagerFactory to use to get the names of valid
65 // orthogonalization manager types. We won't use this factory to
66 // create them, so we should be able to pick the Scalar, MV, and
67 // OP template parameters as we wish.
68 typedef Belos::OrthoManagerFactory<double,
69 Tpetra::MultiVector<ST>, Tpetra::Operator<ST> > factory_type;
70 factory_type factory;
71
72 ////////////////////////////////////////////////////////////////////
73 // Set default values of command-line arguments.
74 ////////////////////////////////////////////////////////////////////
75
76 // The name of the (Mat)OrthoManager subclass to instantiate.
77 orthoManName = factory.defaultName ();
78
79 // For SimpleOrthoManager: the normalization method to use. Valid
80 // values: "MGS", "CGS".
81 normalization = "CGS";
82
83 // Name of the Harwell-Boeing sparse matrix file from which to read
84 // the inner product operator matrix. If name is "" or not provided
85 // at the command line, use the standard Euclidean inner product.
86 filename = "";
87
88 verbose = false;
89 debug = false;
90
91 // The OrthoManager is tested with three different multivectors: S
92 // (sizeS columns), X1 (sizeX1 columns), and X2 (sizeX2 columns).
93 // The values below are defaults and may be changed by the
94 // corresponding command-line arguments.
95 sizeS = 5;
96 sizeX1 = 11;
97 sizeX2 = 13;
98
99 // Default _global_ number of rows. The number of rows per MPI
100 // process must be no less than max(sizeS, sizeX1, sizeX2). To
101 // ensure that the test always passes with default parameters, we
102 // scale by the number of processes. The default value below may be
103 // changed by a command-line parameter with a corresponding name.
104 numRows = 100 * comm.getSize ();
105
106 ////////////////////////////////////////////////////////////////////
107 // Define command-line arguments and parse them.
108 ////////////////////////////////////////////////////////////////////
109
110 CommandLineProcessor cmdp (false, true);
111 cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
112 cmdp.setOption ("debug", "nodebug", &debug, "Print debugging information.");
113 cmdp.setOption ("filename", &filename,
114 "Filename of a Harwell-Boeing sparse matrix, used as the "
115 "inner product operator by the orthogonalization manager."
116 " If not provided, no matrix is read and the Euclidean "
117 "inner product is used. This is only valid for Scalar="
118 "double.");
119 {
120 std::ostringstream os;
121 const int numValid = factory.numOrthoManagers ();
122 const bool plural = numValid > 1 || numValid == 0;
123
124 os << "OrthoManager subclass to test. There ";
125 os << (plural ? "are " : "is ") << numValid << (plural ? "s: " : ": ");
126 factory.printValidNames (os);
127 os << ".";
128 cmdp.setOption ("ortho", &orthoManName, os.str ().c_str ());
129 }
130 cmdp.setOption ("normalization", &normalization,
131 "For SimpleOrthoManager (--ortho=Simple): the normalization "
132 "method to use. Valid values: \"MGS\", \"CGS\".");
133 cmdp.setOption ("numRows", &numRows,
134 "Controls the number of rows of the test "
135 "multivectors. If an input matrix is given, this "
136 "parameter\'s value is ignored, since the vectors must "
137 "be commensurate with the dimensions of the matrix.");
138 cmdp.setOption ("sizeS", &sizeS, "Controls the number of columns of the "
139 "input multivector.");
140 cmdp.setOption ("sizeX1", &sizeX1, "Controls the number of columns of the "
141 "first basis.");
142 cmdp.setOption ("sizeX2", &sizeX2, "Controls the number of columns of the "
143 "second basis. We require for simplicity of testing (the "
144 "routines do not require it) that sizeX1 >= sizeX2.");
145 // Parse the command-line arguments.
146 {
147 const CommandLineProcessor::EParseCommandLineReturn parseResult =
148 cmdp.parse (argc, argv);
149 // If the caller asks us to print the documentation, or does not
150 // provide the name of an OrthoManager subclass, we just keep
151 // going. Otherwise, we throw an exception.
152 TEUCHOS_TEST_FOR_EXCEPTION(
153 parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
154 std::invalid_argument,
155 "Failed to parse command-line arguments");
156 }
157 //
158 // Validate command-line arguments
159 //
160 TEUCHOS_TEST_FOR_EXCEPTION(
161 numRows <= 0, std::invalid_argument, "numRows <= 0 is not allowed");
162 TEUCHOS_TEST_FOR_EXCEPTION(
163 numRows <= sizeS + sizeX1 + sizeX2, std::invalid_argument,
164 "numRows <= sizeS + sizeX1 + sizeX2 is not allowed");
165 }
166
167 // C++03 does not allow partial specialization of a template function,
168 // so we have to declare a class to load the sparse matrix and create
169 // the Map.
170
171 // We don't have a generic Harwell-Boeing sparse file reader yet. If
172 // filename == "", then the user doesn't want to read in a sparse
173 // matrix, so we can just create the appropriate Map and be done with
174 // it. Otherwise, raise an exception at run time.
175 template<class ScalarType, class LocalOrdinalType, class GlobalOrdinalType, class NodeType>
176 class SparseMatrixLoader {
177 public:
178 typedef Tpetra::Map<LocalOrdinalType, GlobalOrdinalType, NodeType> map_type;
179 typedef Tpetra::CrsMatrix<ScalarType, LocalOrdinalType, GlobalOrdinalType, NodeType> matrix_type;
180
181 static void
182 load (Teuchos::RCP<map_type>& map,
183 Teuchos::RCP<matrix_type>& M,
184 const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
185 std::ostream& debugOut)
186 {
187 TEUCHOS_TEST_FOR_EXCEPTION(
188 filename != "", std::logic_error, "Sorry, reading in a Harwell-Boeing "
189 "sparse matrix file for ScalarType="
190 << Teuchos::TypeNameTraits<ScalarType>::name () << " is not yet "
191 "implemented. This currently only works for ScalarType=double.");
192 const GlobalOrdinalType indexBase = 0;
193 map = Teuchos::rcp (new map_type (numRows, indexBase, comm, Tpetra::GloballyDistributed));
194 M = Teuchos::null;
195 }
196 };
197
198 // We _do_ know how to read a Harwell-Boeing sparse matrix file with Scalar=double.
199 template<class LocalOrdinalType, class GlobalOrdinalType, class NodeType>
200 class SparseMatrixLoader<double, LocalOrdinalType, GlobalOrdinalType, NodeType> {
201 public:
202 typedef Tpetra::Map<LocalOrdinalType, GlobalOrdinalType, NodeType> map_type;
203 typedef Tpetra::CrsMatrix<double, LocalOrdinalType, GlobalOrdinalType, NodeType> matrix_type;
204
205 static void
206 load (Teuchos::RCP<map_type>& map,
207 Teuchos::RCP<matrix_type>& M,
208 const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
209 std::ostream& debugOut)
210 {
211 // If the sparse matrix is loaded successfully, this call will
212 // modify numRows to be the number of rows in the sparse matrix.
213 // Otherwise, it will leave numRows alone.
214 std::pair<Teuchos::RCP<map_type>, Teuchos::RCP<matrix_type> > results =
215 Belos::Test::loadSparseMatrix<typename matrix_type::scalar_type,LocalOrdinalType,
216 GlobalOrdinalType,
217 NodeType> (comm, filename, numRows, debugOut);
218 map = results.first;
219 M = results.second;
220 }
221 };
222
223
224 // Run the actual test. The test has the same template parameters as
225 // MultiVector. The most interesting template parameters for this
226 // test are ScalarType and NodeType.
227 //
228 // Return true if test passed, else return false.
229 template<class ScalarType, class LocalOrdinalType, class GlobalOrdinalType, class NodeType>
230 bool runTest (const Teuchos::RCP<const Teuchos::Comm<int> >& comm)
231 {
232 using Teuchos::ParameterList;
233 using Teuchos::parameterList;
234 using Teuchos::RCP;
235 using Teuchos::rcp;
236
237 typedef ScalarType scalar_type;
238 typedef LocalOrdinalType local_ordinal_type;
239 typedef GlobalOrdinalType global_ordinal_type;
240 typedef NodeType node_type;
241
242 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
243 typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> OP;
244 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
245 typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> crs_matrix_type;
246
247 // Declare an output manager for handling local output. Initialize,
248 // using the caller's desired verbosity level.
249 //
250 // NOTE In Anasazi, this class is called BasicOutputManager. In
251 // Belos, this class is called OutputManager. We should eventually
252 // resolve the difference.
253 RCP<Belos::OutputManager<scalar_type> > outMan =
254 Belos::Test::makeOutputManager<scalar_type> (verbose, debug);
255
256 // Stream for debug output. If debug output is not enabled, then
257 // this stream doesn't print anything sent to it (it's a "black
258 // hole" stream).
259 std::ostream& debugOut = outMan->stream (Belos::Debug);
260 Belos::Test::printVersionInfo (debugOut);
261
262 // Load the inner product operator matrix from the given filename.
263 // If filename == "", use the identity matrix as the inner product
264 // operator (the Euclidean inner product), and leave M as
265 // Teuchos::null. Also return an appropriate Map (which will always
266 // be initialized, even if filename == ""; it should never be
267 // Teuchos::null).
268 RCP<map_type> map;
269 RCP<crs_matrix_type> M;
270 {
271 typedef SparseMatrixLoader<scalar_type, local_ordinal_type,
272 global_ordinal_type, node_type> loader_type;
273 loader_type::load (map, M, comm, debugOut);
274 }
275 TEUCHOS_TEST_FOR_EXCEPTION(map.is_null (), std::runtime_error,
276 "(Mat)OrthoManager test code failed to initialize the Map.");
277 {
278 // The maximum number of columns that will be passed to a
279 // MatOrthoManager's normalize() routine. Some MatOrthoManager
280 // subclasses (e.g., Tsqr(Mat)OrthoManager) need to have the
281 // number of columns no larger than the number of rows on any
282 // process. We check this _after_ attempting to load any sparse
283 // matrix to be used as the inner product matrix, because if a
284 // sparse matrix is successfully loaded, its number of rows will
285 // override the number of rows specified on the command line (if
286 // specified), and will also override the default number of rows.
287 const size_t maxNormalizeNumCols = std::max (sizeS, std::max (sizeX1, sizeX2));
288 // getLocalNumElements() returns a size_t, which is unsigned, and
289 // you shouldn't compare signed and unsigned values. This is why
290 // we make maxNormalizeNumCols a size_t as well.
291 if (map->getLocalNumElements () < maxNormalizeNumCols) {
292 std::ostringstream os;
293 os << "The number of elements on this process " << comm->getRank()
294 << " is too small for the number of columns that you want to test."
295 << " There are " << map->getLocalNumElements() << " elements on "
296 "this process, but the normalize() method of the MatOrthoManager "
297 "subclass will need to process a multivector with "
298 << maxNormalizeNumCols << " columns. Not all MatOrthoManager "
299 "subclasses can handle a local row block with fewer rows than "
300 "columns.";
301 TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, os.str ());
302 }
303 }
304
305 // This factory object knows how to make a (Mat)OrthoManager
306 // subclass, given a name for the subclass and its parameters.
307 Belos::OrthoManagerFactory<scalar_type, MV, OP> factory;
308
309 // Using the factory object, instantiate the specified OrthoManager
310 // subclass to be tested. Specify "default" parameters (which
311 // should favor accuracy over performance), but override the default
312 // parameters to get the desired normalization method for
313 // SimpleOrthoManaager.
314 RCP<Belos::OrthoManager<scalar_type, MV> > orthoMan;
315 {
316 std::string label (orthoManName);
317 RCP<ParameterList> params =
318 parameterList (*(factory.getDefaultParameters (orthoManName)));
319 if (orthoManName == "Simple") {
320 params->set ("Normalization", normalization);
321 label = label + " (" + normalization + " normalization)";
322 } else if (orthoManName == "TSQR") {
323 params->set ("randomizeNullSpace", false); // for testing the norm of zero-vector is zero
324 }
325 orthoMan = factory.makeOrthoManager (orthoManName, M, outMan, label, params);
326 }
327
328 // Whether the specific OrthoManager subclass promises to compute
329 // rank-revealing orthogonalizations. If yes, then test it on
330 // rank-deficient multivectors, otherwise only test it on full-rank
331 // multivectors.
332 const bool isRankRevealing = factory.isRankRevealing (orthoManName);
333
334 // "Prototype" multivector. The test code will use this (via
335 // Belos::MultiVecTraits) to clone other multivectors as
336 // necessary. (This means the test code doesn't need the Map, and
337 // it also makes the test code independent of the idea of a Map.)
338 RCP<MV> S = rcp (new MV (map, sizeS));
339
340 // Test the OrthoManager subclass. Return the number of tests
341 // that failed. None of the tests should fail (this function
342 // should return zero).
343 int numFailed = 0;
344 {
345 typedef Belos::Test::OrthoManagerTester<scalar_type, MV> tester_type;
346 numFailed = tester_type::runTests (orthoMan, isRankRevealing, S,
347 sizeX1, sizeX2, outMan);
348 }
349
350 if (numFailed != 0) {
351 outMan->stream (Belos::Errors) << numFailed << " errors." << endl;
352 return false;
353 } else {
354 return true;
355 }
356 }
357
358 int
359 main (int argc, char *argv[])
360 {
361 using Teuchos::ParameterList;
362 using Teuchos::parameterList;
363 using Teuchos::RCP;
364 using Teuchos::rcp;
365 bool success = false;
366
367 Tpetra::ScopeGuard tpetraScope (&argc, &argv);
368 try {
369 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm ();
370
371 // Get values of command-line arguments.
372 getCmdLineArgs (*comm, argc, argv);
373
374 typedef Tpetra::Map<>::local_ordinal_type local_ordinal_type;
375 typedef Tpetra::Map<>::global_ordinal_type global_ordinal_type;
376 typedef Tpetra::Map<>::node_type node_type;
377
378 {
379 typedef Tpetra::MultiVector<>::scalar_type scalar_type;
380 success = runTest<scalar_type, local_ordinal_type,
381 global_ordinal_type, node_type> (comm);
382 if (success) {
383 // The Trilinos test framework depends on seeing this message,
384 // so don't rely on the OutputManager to report it correctly.
385 if (comm->getRank () == 0) {
386 std::cout << "End Result: TEST PASSED" << endl;
387 }
388 }
389 else {
390 if (comm->getRank () == 0) {
391 std::cout << "End Result: TEST FAILED" << endl;
392 }
393 }
394 }
395 #if defined(HAVE_TEUCHOS_COMPLEX) && defined(HAVE_TPETRA_COMPLEX)
396 {
397 typedef std::complex<Tpetra::MultiVector<>::scalar_type> scalar_type;
398 success = runTest<scalar_type, local_ordinal_type,
399 global_ordinal_type, node_type> (comm);
400 if (success) {
401 // The Trilinos test framework depends on seeing this message,
402 // so don't rely on the OutputManager to report it correctly.
403 if (comm->getRank () == 0) {
404 std::cout << "End Result: TEST PASSED" << endl;
405 }
406 } else {
407 if (comm->getRank () == 0) {
408 std::cout << "End Result: TEST FAILED" << endl;
409 }
410 }
411 }
412 #endif // defined(HAVE_TEUCHOS_COMPLEX) && defined(HAVE_TPETRA_COMPLEX)
413 }
414 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
415
416 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
417 }