Debian Codegraph @

packages/belos/epetra/test/SolverFactory/test_solver_factory.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 test_solver_factory.cpp
11 /// \brief Test Belos::SolverFactory with Epetra
12 /// \author Mark Hoemmen
13 ///
14 #include "BelosConfigDefs.hpp"
15 #include "BelosOutputManager.hpp"
16 #include "BelosSolverFactory.hpp"
17 #include "BelosEpetraAdapter.hpp"
18
19 #include <Galeri_Maps.h>
20 #include <Galeri_CrsMatrices.h>
21
22 #include <Teuchos_CommandLineProcessor.hpp>
23 #include <Teuchos_GlobalMPISession.hpp>
24 #include <Teuchos_oblackholestream.hpp>
25 #include <Teuchos_TypeNameTraits.hpp>
26
27 #ifdef EPETRA_MPI
28 # include <mpi.h>
29 # include <Epetra_MpiComm.h>
30 #else
31 # include <Epetra_SerialComm.h>
32 #endif
33 #include <Epetra_CrsMatrix.h>
34
35 #include <algorithm>
36 #include <iostream>
37 #include <sstream>
38 #include <stdexcept>
39
40 // These are added to so we can preserve the type checking of the manager.
41 // TODO: With the new DII system we can consider adding a second test like
42 // this which does not have these included and does not do the type check.
43 // However the test should still work.
44 #include "BelosBlockGmresSolMgr.hpp"
45 #include "BelosPseudoBlockGmresSolMgr.hpp"
46 #include "BelosBlockCGSolMgr.hpp"
47 #include "BelosPseudoBlockCGSolMgr.hpp"
48 #include "BelosGCRODRSolMgr.hpp"
49 #include "BelosMinresSolMgr.hpp"
50 #include "BelosLSQRSolMgr.hpp"
51 #include "BelosTFQMRSolMgr.hpp"
52 #include "BelosRCGSolMgr.hpp"
53 #include "BelosPseudoBlockTFQMRSolMgr.hpp"
54
55 using std::cout;
56 using std::endl;
57 using std::vector;
58
59 namespace {
60
61 // Test creation of a Belos solver, using Belos::SolverFactory.
62 //
63 // Template parameters:
64 //
65 // SolverFactoryType: the Belos::SolverFactory specialization
66 // SolverBaseType: the Belos::SolverManager specialization
67 // SolverImplType: the Belos::SolverManager subclass which the
68 // factory is supposed to produce.
69 //
70 // Function parameters:
71 //
72 // factory: A Belos::SolverFactory instance. Passing it in as a
73 // reference lets us reuse the same factory instance for multiple
74 // tests. This saves the (small) overhead of creating the factory
75 // each time, and also (more importantly) ensures that the
76 // factory's create() method gets tested for multiple calls to the
77 // same factory instance. The reference is nonconst because the
78 // create() method is nonconst.
79 //
80 // solverName: Name of the solver to create.
81 //
82 // out: An output stream reference to which to write verbose output
83 // (verbose == true). When building with MPI, this should be a
84 // black-hole stream (Teuchos::oblackholestream) reference on
85 // every MPI process except the process with Rank 0.
86 //
87 // verbose: Whether to write verbose output.
88 template<class SolverFactoryType, class SolverBaseType, class SolverImplType>
89 void
90 testCreatingSolver (SolverFactoryType& factory,
91 const std::string& solverName,
92 std::ostream& out,
93 const bool verbose)
94 {
95 using Teuchos::ParameterList;
96 using Teuchos::parameterList;
97 using Teuchos::RCP;
98 using Teuchos::rcp_dynamic_cast;
99 using Teuchos::TypeNameTraits;
100 using std::endl;
101
102 if (verbose) {
103 out << "Creating solver named: \"" << solverName << "\"" << endl;
104 }
105 RCP<ParameterList> solverParams = parameterList ();
106 RCP<SolverBaseType> solver = factory.create (solverName, solverParams);
107 TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null(), std::logic_error,
108 "Failed to create solver with valid name \""
109 << solverName << "\".");
110 if (verbose) {
111 out << "Solver description: " << solver->description() << endl;
112 }
113 TEUCHOS_TEST_FOR_EXCEPTION(rcp_dynamic_cast<SolverImplType> (solver).is_null(),
114 std::logic_error, "Solver does not have the correct type. Type should be "
115 << TypeNameTraits<SolverImplType>::name() << ".");
116 }
117
118 int
119 selectVerbosity (const bool verbose, const bool debug)
120 {
121 // NOTE Calling this a "MsgType" (its correct type) or even an
122 // "enum MsgType" confuses the compiler.
123 int theType = Belos::Errors; // default (always print errors)
124 if (verbose)
125 {
126 // "Verbose" also means printing out Debug messages (as well
127 // as everything else).
128 theType = theType |
129 Belos::Warnings |
130 Belos::IterationDetails |
131 Belos::OrthoDetails |
132 Belos::FinalSummary |
133 Belos::TimingDetails |
134 Belos::StatusTestDetails |
135 Belos::Debug;
136 }
137 if (debug)
138 // "Debug" doesn't necessarily mean the same thing as
139 // "Verbose". We interpret "Debug" to mean printing out
140 // messages marked as Debug (as well as Error messages).
141 theType = theType | Belos::Debug;
142 return theType;
143 }
144
145 /// The problem is defined on a 2D grid; global size is nx * nx.
146 Teuchos::RCP<Epetra_Operator>
147 makeMatrix (const Teuchos::RCP<Epetra_Comm>& comm,
148 Teuchos::RCP<const Epetra_Map>& domainMap,
149 Teuchos::RCP<const Epetra_Map>& rangeMap,
150 const int nx = 30)
151 {
152 using Teuchos::ParameterList;
153 using Teuchos::RCP;
154 using Teuchos::rcp;
155 using Teuchos::rcp_implicit_cast;
156
157 ParameterList GaleriList;
158 GaleriList.set("n", nx * nx);
159 GaleriList.set("nx", nx);
160 GaleriList.set("ny", nx);
161 RCP<const Epetra_Map> rowMap = rcp (Galeri::CreateMap("Linear", *comm, GaleriList));
162 // "&*rowMap" turns an RCP<Epetra_Map> into a raw pointer
163 // (Epetra_Map*), which is what Galeri::CreateCrsMatrix() wants.
164 RCP<Epetra_RowMatrix> A =
165 rcp (Galeri::CreateCrsMatrix("Laplace2D", &*rowMap, GaleriList));
166 TEUCHOS_TEST_FOR_EXCEPTION(A.is_null(), std::runtime_error,
167 "Galeri returned a null operator A.");
168 domainMap = rowMap;
169 rangeMap = rowMap;
170 return rcp_implicit_cast<Epetra_Operator> (A);
171 }
172
173 } // namespace (anonymous)
174
175
176 int
177 main (int argc, char *argv[])
178 {
179 using Belos::OutputManager;
180 using Belos::SolverFactory;
181 using Teuchos::CommandLineProcessor;
182 using Teuchos::null;
183 using Teuchos::ParameterList;
184 using Teuchos::parameterList;
185 using Teuchos::RCP;
186 using Teuchos::rcp;
187 using Teuchos::rcp_dynamic_cast;
188 using Teuchos::rcp_implicit_cast;
189 using std::cerr;
190 using std::cout;
191 using std::endl;
192 typedef double scalar_type;
193 typedef Epetra_MultiVector MV;
194 typedef Epetra_Operator OP;
195 typedef Belos::SolverManager<scalar_type, MV, OP> solver_base_type;
196 typedef Belos::SolverFactory<scalar_type, MV, OP> factory_type;
197
198 Teuchos::oblackholestream blackHole;
199 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackHole);
200
201 RCP<Epetra_Comm> comm;
202 {
203 #ifdef EPETRA_MPI
204 RCP<Epetra_MpiComm> commSpecific (new Epetra_MpiComm (MPI_COMM_WORLD));
205 #else
206 RCP<Epetra_SerialComm> commSpecific (new Epetra_SerialComm);
207 #endif // EPETRA_MPI
208 comm = rcp_implicit_cast<Epetra_Comm> (commSpecific);
209 }
210 std::ostream& out = (comm->MyPID() == 0) ? std::cout : blackHole;
211
212 bool success = false;
213 bool verbose = false;
214 try {
215 int numRHS = 1;
216 bool debug = false;
217
218 // Define command-line arguments.
219 CommandLineProcessor cmdp (false, true);
220 cmdp.setOption ("numRHS", &numRHS, "Number of right-hand sides in the linear "
221 "system to solve.");
222 cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results.");
223 cmdp.setOption ("debug", "nodebug", &debug, "Print debugging information.");
224
225 // Parse the command-line arguments.
226 {
227 const CommandLineProcessor::EParseCommandLineReturn parseResult =
228 cmdp.parse (argc,argv);
229 if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) {
230 if (comm->MyPID() == 0)
231 std::cout << "End Result: TEST PASSED" << endl;
232 return EXIT_SUCCESS;
233 }
234 TEUCHOS_TEST_FOR_EXCEPTION(parseResult != CommandLineProcessor::PARSE_SUCCESSFUL,
235 std::invalid_argument, "Failed to parse command-line arguments.");
236 }
237
238 // Declare an output manager for handling local output. Initialize,
239 // using the caller's desired verbosity level.
240 RCP<OutputManager<scalar_type> > outMan =
241 rcp (new OutputManager<scalar_type> (selectVerbosity (verbose, debug)));
242
243 // Stream for debug output. If debug output is not enabled, then
244 // this stream doesn't print anything sent to it (it's a "black
245 // hole" stream).
246 //std::ostream& debugOut = outMan->stream (Belos::Debug);
247
248 // Create the operator to test, with domain and range maps.
249 RCP<const Epetra_Map> domainMap, rangeMap;
250 RCP<Epetra_Operator> A = makeMatrix (comm, domainMap, rangeMap);
251 // "Solution" input/output multivector.
252 RCP<MV> X_exact = rcp (new MV (*domainMap, numRHS));
253 X_exact->Random ();
254 RCP<MV> X = rcp (new MV (*domainMap, numRHS));
255 X->PutScalar (0.0);
256 // "Right-hand side" input multivector.
257 RCP<MV> B = rcp (new MV (*rangeMap, numRHS));
258 A->Apply (*X_exact, *B);
259
260 //
261 // Test creating solver instances using the solver factory.
262 //
263 factory_type factory;
264 //
265 // Test the canonical solver names.
266 //
267 {
268 typedef Belos::BlockGmresSolMgr<scalar_type, MV, OP> solver_impl_type;
269 testCreatingSolver<factory_type, solver_base_type,
270 solver_impl_type> (factory, "Block GMRES", out, verbose);
271 }
272 {
273 typedef Belos::PseudoBlockGmresSolMgr<scalar_type, MV, OP> solver_impl_type;
274 testCreatingSolver<factory_type, solver_base_type,
275 solver_impl_type> (factory, "Pseudoblock GMRES", out, verbose);
276 }
277 {
278 typedef Belos::BlockCGSolMgr<scalar_type, MV, OP> solver_impl_type;
279 testCreatingSolver<factory_type, solver_base_type,
280 solver_impl_type> (factory, "Block CG", out, verbose);
281 }
282 {
283 typedef Belos::PseudoBlockCGSolMgr<scalar_type, MV, OP> solver_impl_type;
284 testCreatingSolver<factory_type, solver_base_type,
285 solver_impl_type> (factory, "Pseudoblock CG", out, verbose);
286 }
287 {
288 typedef Belos::GCRODRSolMgr<scalar_type, MV, OP> solver_impl_type;
289 testCreatingSolver<factory_type, solver_base_type,
290 solver_impl_type> (factory, "GCRODR", out, verbose);
291 }
292 {
293 typedef Belos::RCGSolMgr<scalar_type, MV, OP> solver_impl_type;
294 testCreatingSolver<factory_type, solver_base_type,
295 solver_impl_type> (factory, "RCG", out, verbose);
296 }
297 {
298 typedef Belos::MinresSolMgr<scalar_type, MV, OP> solver_impl_type;
299 testCreatingSolver<factory_type, solver_base_type,
300 solver_impl_type> (factory, "MINRES", out, verbose);
301 }
302 {
303 typedef Belos::LSQRSolMgr<scalar_type, MV, OP> solver_impl_type;
304 testCreatingSolver<factory_type, solver_base_type,
305 solver_impl_type> (factory, "LSQR", out, verbose);
306 }
307 {
308 typedef Belos::TFQMRSolMgr<scalar_type, MV, OP> solver_impl_type;
309 testCreatingSolver<factory_type, solver_base_type,
310 solver_impl_type> (factory, "TFQMR", out, verbose);
311 }
312 {
313 typedef Belos::PseudoBlockTFQMRSolMgr<scalar_type, MV, OP> solver_impl_type;
314 testCreatingSolver<factory_type, solver_base_type,
315 solver_impl_type> (factory, "Pseudoblock TFQMR", out, verbose);
316 }
317 //
318 // Test aliases.
319 //
320 {
321 typedef Belos::BlockGmresSolMgr<scalar_type, MV, OP> solver_impl_type;
322 testCreatingSolver<factory_type, solver_base_type,
323 solver_impl_type> (factory, "Flexible GMRES", out, verbose);
324 }
325 {
326 typedef Belos::PseudoBlockCGSolMgr<scalar_type, MV, OP> solver_impl_type;
327 testCreatingSolver<factory_type, solver_base_type,
328 solver_impl_type> (factory, "CG", out, verbose);
329 }
330 {
331 typedef Belos::RCGSolMgr<scalar_type, MV, OP> solver_impl_type;
332 testCreatingSolver<factory_type, solver_base_type,
333 solver_impl_type> (factory, "Recycling CG", out, verbose);
334 }
335 {
336 typedef Belos::GCRODRSolMgr<scalar_type, MV, OP> solver_impl_type;
337 testCreatingSolver<factory_type, solver_base_type,
338 solver_impl_type> (factory, "Recycling GMRES", out, verbose);
339 }
340 {
341 typedef Belos::PseudoBlockGmresSolMgr<scalar_type, MV, OP> solver_impl_type;
342 testCreatingSolver<factory_type, solver_base_type,
343 solver_impl_type> (factory, "Pseudo Block GMRES", out, verbose);
344 }
345 {
346 typedef Belos::PseudoBlockCGSolMgr<scalar_type, MV, OP> solver_impl_type;
347 testCreatingSolver<factory_type, solver_base_type,
348 solver_impl_type> (factory, "Pseudo Block CG", out, verbose);
349 }
350 {
351 typedef Belos::PseudoBlockTFQMRSolMgr<scalar_type, MV, OP> solver_impl_type;
352 testCreatingSolver<factory_type, solver_base_type,
353 solver_impl_type> (factory, "Pseudo Block TFQMR", out, verbose);
354 }
355
356 success = true;
357 if (comm->MyPID() == 0) {
358 cout << "End Result: TEST PASSED" << endl;
359 }
360 }
361 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
362
363 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
364 }