Debian Codegraph @

packages/belos/tpetra/test/OrthoManager/belos_orthomanager_tpetra_benchmark.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_benchmark.cpp
11 /// \brief Benchmark (Mat)OrthoManager subclass(es) with Tpetra
12 ///
13 /// Benchmark various subclasses of (Mat)OrthoManager, using
14 /// Tpetra::MultiVector as the multivector implementation, and
15 /// Tpetra::Operator as the operator implementation.
16 ///
17 #include "belos_orthomanager_tpetra_util.hpp"
18 #include "Tpetra_Core.hpp"
19 #include "Teuchos_CommandLineProcessor.hpp"
20 #include "Teuchos_oblackholestream.hpp"
21 #include <algorithm>
22
23 using std::endl;
24
25 //
26 // These typedefs make main() as generic as possible.
27 //
28 typedef Tpetra::MultiVector<>::scalar_type scalar_type;
29 typedef Tpetra::Map<>::local_ordinal_type local_ordinal_type;
30 typedef Tpetra::Map<>::global_ordinal_type global_ordinal_type;
31 typedef Tpetra::Map<>::node_type node_type;
32
33 typedef Teuchos::ScalarTraits<scalar_type> SCT;
34 typedef SCT::magnitudeType magnitude_type;
35 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MV;
36 typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> OP;
37 typedef Belos::MultiVecTraits<scalar_type, MV> MVT;
38 typedef Belos::OperatorTraits<scalar_type, MV, OP> OPT;
39 typedef Teuchos::SerialDenseMatrix<int, scalar_type> serial_matrix_type;
40 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> map_type;
41 typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> sparse_matrix_type;
42
43 /* ******************************************************************* */
44
45 /// \fn main
46 /// \brief Benchmark driver for (Mat)OrthoManager subclasses
47 int
48 main (int argc, char *argv[])
49 {
50 using Belos::OrthoManager;
51 using Belos::OrthoManagerFactory;
52 using Belos::OutputManager;
53 using Teuchos::CommandLineProcessor;
54 using Teuchos::ParameterList;
55 using Teuchos::parameterList;
56 using Teuchos::RCP;
57 using Teuchos::rcp;
58
59 Tpetra::ScopeGuard tpetraScope (&argc, &argv);
60
61 bool success = false;
62 bool verbose = false; // Verbosity of output
63 try {
64 RCP<const Teuchos::Comm<int> > pComm = Tpetra::getDefaultComm();
65 // This factory object knows how to make a (Mat)OrthoManager
66 // subclass, given a name for the subclass. The name is not the
67 // same as the class' syntactic name: e.g., "TSQR" is the name of
68 // TsqrOrthoManager.
69 OrthoManagerFactory<scalar_type, MV, OP> factory;
70
71 // The name of the (Mat)OrthoManager subclass to instantiate.
72 std::string orthoManName (factory.defaultName());
73
74 // For SimpleOrthoManager: the normalization method to use. Valid
75 // values: "MGS", "CGS".
76 std::string normalization ("CGS");
77
78 // Name of the Harwell-Boeing sparse matrix file from which to read
79 // the inner product operator matrix. If name is "" or not provided
80 // at the command line, use the standard Euclidean inner product.
81 std::string filename;
82
83 bool debug = false; // Whether to print debugging-level output
84 // Whether or not to run the benchmark. If false, we let this
85 // "test" pass trivially.
86 bool benchmark = false;
87
88 // Whether to display benchmark results compactly (in a CSV format),
89 // or in a human-readable table.
90 bool displayResultsCompactly = false;
91
92 // Default _local_ (per MPI process) number of rows. This will
93 // change if a sparse matrix is loaded in as an inner product
94 // operator. Regardless, the number of rows per MPI process must be
95 // no less than numCols*numBlocks in order for TSQR to work. To
96 // ensure that the test always passes with default parameters, we
97 // scale by the number of processes. The default value below may be
98 // changed by a command-line parameter with a corresponding name.
99 int numRowsPerProcess = 100;
100
101 // The OrthoManager is benchmarked with numBlocks multivectors of
102 // width numCols each, for numTrials trials. The values below are
103 // defaults and may be changed by the corresponding command-line
104 // arguments.
105 int numCols = 10;
106 int numBlocks = 5;
107 int numTrials = 3;
108
109 CommandLineProcessor cmdp (false, true);
110 cmdp.setOption ("benchmark", "nobenchmark", &benchmark,
111 "Whether to run the benchmark. If not, this \"test\" "
112 "passes trivially.");
113 cmdp.setOption ("verbose", "quiet", &verbose,
114 "Print messages and results.");
115 cmdp.setOption ("debug", "nodebug", &debug,
116 "Print debugging information.");
117 cmdp.setOption ("compact", "human", &displayResultsCompactly,
118 "Whether to display benchmark results compactly (in a "
119 "CSV format), or in a human-readable table.");
120 cmdp.setOption ("filename", &filename,
121 "Filename of a Harwell-Boeing sparse matrix, used as the "
122 "inner product operator by the orthogonalization manager."
123 " If not provided, no matrix is read and the Euclidean "
124 "inner product is used.");
125 {
126 std::ostringstream os;
127 const int numValid = factory.numOrthoManagers();
128 const bool plural = numValid > 1 || numValid == 0;
129
130 os << "OrthoManager subclass to benchmark. There ";
131 os << (plural ? "are " : "is ") << numValid << (plural ? "s: " : ": ");
132 factory.printValidNames (os);
133 os << ". If none is provided, the test trivially passes.";
134 cmdp.setOption ("ortho", &orthoManName, os.str().c_str());
135 }
136 cmdp.setOption ("normalization", &normalization,
137 "For SimpleOrthoManager (--ortho=Simple): the normalization "
138 "method to use. Valid values: \"MGS\", \"CGS\".");
139 cmdp.setOption ("numRowsPerProcess", &numRowsPerProcess,
140 "Number of rows per MPI process in the test multivectors. "
141 "If an input matrix is given, this value is ignored, since "
142 "the vectors must be commensurate with the dimensions of "
143 "the matrix.");
144 cmdp.setOption ("numCols", &numCols,
145 "Number of columns in the input multivector (>= 1).");
146 cmdp.setOption ("numBlocks", &numBlocks,
147 "Number of block(s) to benchmark (>= 1).");
148 cmdp.setOption ("numTrials", &numTrials,
149 "Number of trial(s) per timing run (>= 1).");
150
151 // Parse the command-line arguments.
152 {
153 const CommandLineProcessor::EParseCommandLineReturn parseResult = cmdp.parse (argc,argv);
154 // If the caller asks us to print the documentation, or does not
155 // explicitly say to run the benchmark, we let this "test" pass
156 // trivially.
157 if (! benchmark || parseResult == CommandLineProcessor::PARSE_HELP_PRINTED)
158 {
159 if (Teuchos::rank(*pComm) == 0)
160 std::cout << "End Result: TEST PASSED" << endl;
161 return EXIT_SUCCESS;
162 }
163 TEUCHOS_TEST_FOR_EXCEPTION(parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
164 std::invalid_argument,
165 "Failed to parse command-line arguments");
166 }
167
168 // Total number of rows in the test vector(s).
169 // This may be changed if we load in a sparse matrix.
170 int numRows = numRowsPerProcess * pComm->getSize();
171 //
172 // Validate command-line arguments
173 //
174 TEUCHOS_TEST_FOR_EXCEPTION(numRowsPerProcess <= 0, std::invalid_argument,
175 "numRowsPerProcess <= 0 is not allowed");
176 TEUCHOS_TEST_FOR_EXCEPTION(numCols <= 0, std::invalid_argument,
177 "numCols <= 0 is not allowed");
178 TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0, std::invalid_argument,
179 "numBlocks <= 0 is not allowed");
180
181 // Declare an output manager for handling local output. Initialize,
182 // using the caller's desired verbosity level.
183 RCP<OutputManager<scalar_type> > outMan =
184 Belos::Test::makeOutputManager<scalar_type> (verbose, debug);
185
186 // Stream for debug output. If debug output is not enabled, then
187 // this stream doesn't print anything sent to it (it's a "black
188 // hole" stream).
189 std::ostream& debugOut = outMan->stream(Belos::Debug);
190 Belos::Test::printVersionInfo (debugOut);
191
192 // Load the inner product operator matrix from the given filename.
193 // If filename == "", use the identity matrix as the inner product
194 // operator (the Euclidean inner product), and leave M as
195 // Teuchos::null. Also return an appropriate Map (which will
196 // always be initialized; it should never be Teuchos::null).
197 RCP<map_type> map;
198 RCP<sparse_matrix_type> M;
199 {
200 using Belos::Test::loadSparseMatrix;
201 // If the sparse matrix is loaded successfully, this call will
202 // modify numRows to be the total number of rows in the sparse
203 // matrix. Otherwise, it will leave numRows alone.
204 std::pair<RCP<map_type>, RCP<sparse_matrix_type> > results =
205 loadSparseMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> (pComm, filename, numRows, debugOut);
206 map = results.first;
207 M = results.second;
208 }
209 TEUCHOS_TEST_FOR_EXCEPTION(map.is_null(), std::logic_error,
210 "Error: (Mat)OrthoManager test code failed to "
211 "initialize the Map");
212 if (M.is_null())
213 {
214 // Number of rows per process has to be >= number of rows.
215 TEUCHOS_TEST_FOR_EXCEPTION(numRowsPerProcess <= numCols,
216 std::invalid_argument,
217 "numRowsPerProcess <= numCols is not allowed");
218 }
219 // Loading the sparse matrix may have changed numRows, so check
220 // again that the number of rows per process is >= numCols.
221 // getLocalNumElements() returns a size_t, which is unsigned, and you
222 // shouldn't compare signed and unsigned values.
223 if (map->getLocalNumElements() < static_cast<size_t>(numCols))
224 {
225 std::ostringstream os;
226 os << "The number of elements on this process " << pComm->getRank()
227 << " is too small for the number of columns that you want to test."
228 << " There are " << map->getLocalNumElements() << " elements on "
229 "this process, but the normalize() method of the MatOrthoManager "
230 "subclass will need to process a multivector with " << numCols
231 << " columns. Not all MatOrthoManager subclasses can handle a "
232 "local row block with fewer rows than columns.";
233 // QUESTION (mfh 26 Jan 2011) Should this be a logic error
234 // instead? It's really TSQR's fault that it can't handle a
235 // local number of elements less than the number of columns.
236 throw std::invalid_argument(os.str());
237 }
238
239 // Using the factory object, instantiate the specified OrthoManager
240 // subclass to be tested. Specify "fast" parameters for a fair
241 // benchmark comparison, but override the fast parameters to get the
242 // desired normalization method for SimpleOrthoManaager.
243 RCP<OrthoManager<scalar_type, MV> > orthoMan;
244 {
245 std::string label (orthoManName);
246 RCP<ParameterList> params =
247 parameterList (*(factory.getFastParameters (orthoManName)));
248 if (orthoManName == "Simple") {
249 params->set ("Normalization", normalization);
250 label = label + " (" + normalization + " normalization)";
251 }
252 orthoMan = factory.makeOrthoManager (orthoManName, M, outMan, label, params);
253 }
254
255 // "Prototype" multivector. The test code will use this (via
256 // Belos::MultiVecTraits) to clone other multivectors as necessary.
257 // (This means the test code doesn't need the Map, and it also makes
258 // the test code independent of the idea of a Map.) We only have to
259 // allocate one column, because the entries are S are not even read.
260 // (We could allocate zero columns, if the MV object allows it. We
261 // play it safe and allocate 1 column instead.)
262 RCP<MV> X = rcp (new MV (map, 1));
263
264 // "Compact" mode means that we have to override
265 // TimeMonitor::summarize(), which both handles multiple MPI
266 // processes correctly (only Rank 0 prints to std::cout), and prints
267 // verbosely in a table form. We deal with the former by making an
268 // ostream which is std::cout on Rank 0, and prints nothing (is a
269 // "bit bucket") elsewhere. We deal with the latter inside the
270 // benchmark itself.
271 Teuchos::oblackholestream bitBucket;
272 std::ostream& resultStream =
273 (displayResultsCompactly && Teuchos::rank(*pComm) != 0) ? bitBucket : std::cout;
274
275 // Benchmark the OrthoManager subclass.
276 typedef Belos::Test::OrthoManagerBenchmarker<scalar_type, MV> benchmarker_type;
277 benchmarker_type::benchmark (orthoMan, orthoManName, normalization, X,
278 numCols, numBlocks, numTrials,
279 outMan, resultStream, displayResultsCompactly);
280
281 success = true;
282
283 // Only Rank 0 gets to write to cout.
284 if (Teuchos::rank(*pComm) == 0)
285 std::cout << "End Result: TEST PASSED" << endl;
286 }
287 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
288
289 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
290 }