Debian Codegraph @

packages/belos/tpetra/test/GCRODR/test_block_gcrodr.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 //
11 // Test for BlockGCRODRSolMgr (Block Recycling GMRES, by Kirk
12 // Soodhalter and Michael Parks). This just tests compilation and
13 // setParameters() for now. Later, we'll test actually solving linear
14 // systems.
15 //
16
17 #include "BelosConfigDefs.hpp"
18 #include "BelosTpetraAdapter.hpp"
19 #include "BelosTpetraTestFramework.hpp"
20 #include "BelosBlockGCRODRSolMgr.hpp"
21
22 #include <Tpetra_Core.hpp>
23
24 #include <Teuchos_CommandLineProcessor.hpp>
25 #include <Teuchos_FancyOStream.hpp>
26 #include <Teuchos_GlobalMPISession.hpp>
27 #include <Teuchos_oblackholestream.hpp>
28
29 template <typename ScalarType>
30 int run (int argc, char *argv[])
31 {
32 // Teuchos usings
33 using Teuchos::Comm;
34 using Teuchos::FancyOStream;
35 using Teuchos::getFancyOStream;
36 using Teuchos::oblackholestream;
37 using Teuchos::OSTab;
38 using Teuchos::ParameterList;
39 using Teuchos::parameterList;
40 using Teuchos::RCP;
41 using Teuchos::rcpFromRef;
42
43 // Typedefs for Tpetra template arguments.
44 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
45 using LO = typename Tpetra::MultiVector<>::local_ordinal_type;
46 using GO = typename Tpetra::MultiVector<>::global_ordinal_type;
47 using NT = typename Tpetra::MultiVector<ST, LO, GO>::node_type;
48
49 // Tpetra objects which are the MV and OP template parameters of the
50 // Belos specialization which we are testing.
51 using MV = typename Tpetra::MultiVector<ST, LO, GO>;
52 using OP = typename Tpetra::Operator<ST, LO, GO>;
53
54 // Other typedefs.
55 using tcrsmatrix_t = Tpetra::CrsMatrix<ST, LO, GO>;
56
57 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout);
58 RCP<const Comm<int> > comm = Tpetra::getDefaultComm();
59 RCP<oblackholestream> blackHole (new oblackholestream);
60 const int myRank = comm->getRank ();
61
62 // Output stream that prints only on Process 0.
63 RCP<FancyOStream> out;
64 if (myRank == 0) {
65 out = Teuchos::getFancyOStream (rcpFromRef (std::cout));
66 } else {
67 out = Teuchos::getFancyOStream (blackHole);
68 }
69
70 // Get test parameters from command-line processor.
71 // CommandLineProcessor always understands int, but may not
72 // understand GO. We convert to the latter below.
73 int numRows = comm->getSize() * 100;
74 bool tolerant = false;
75 bool verbose = false;
76 bool debug = false;
77 Teuchos::CommandLineProcessor cmdp (false, true);
78 cmdp.setOption("numRows", &numRows,
79 "Global number of rows (and columns) in the sparse matrix to generate.");
80 cmdp.setOption("tolerant", "intolerant", &tolerant,
81 "Whether to parse files tolerantly.");
82 cmdp.setOption("verbose", "quiet", &verbose,
83 "Print messages and results.");
84 cmdp.setOption("debug", "release", &debug,
85 "Run debugging checks and print copious debugging output.");
86 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
87 *out << "\nEnd Result: TEST FAILED" << std::endl;
88 return EXIT_FAILURE;
89 }
90 // Output stream for verbose output.
91 RCP<FancyOStream> verbOut = verbose ? out : getFancyOStream (blackHole);
92
93 const bool success = true;
94
95 // Test whether it's possible to instantiate the solver.
96 // This is a minimal compilation test.
97 *verbOut << "Instantiating Block GCRODR solver" << std::endl;
98 Belos::BlockGCRODRSolMgr<ST, MV, OP> solver;
99
100 // Test setting solver parameters. For now, we just use an empty
101 // (but non-null) parameter list, which the solver should fill in
102 // with defaults.
103 *verbOut << "Setting solver parameters" << std::endl;
104 RCP<ParameterList> solverParams = parameterList ();
105 solver.setParameters (solverParams);
106
107 // Create a linear system to solve.
108 *verbOut << "Creating linear system" << std::endl;
109 RCP<tcrsmatrix_t> A;
110 RCP<MV> X_guess, X_exact, B;
111 {
112 Teuchos::RCP<NT> node; // can be null; only for type deduction
113 Belos::Tpetra::ProblemMaker<tcrsmatrix_t> factory (comm, node, out, tolerant, debug);
114
115 RCP<ParameterList> problemParams = parameterList ();
116 problemParams->set ("Global number of rows",
117 static_cast<GO> (numRows));
118 problemParams->set ("Problem type", std::string ("Nonsymmetric"));
119 factory.makeProblem (A, X_guess, X_exact, B, problemParams);
120 }
121
122 // Approximate solution vector is a copy of the guess vector.
123 RCP<MV> X (new MV (*X_guess));
124
125 TEUCHOS_TEST_FOR_EXCEPTION(A.is_null(), std::logic_error,
126 "The sparse matrix is null!");
127 TEUCHOS_TEST_FOR_EXCEPTION(X_guess.is_null(), std::logic_error,
128 "The initial guess X_guess is null!");
129 TEUCHOS_TEST_FOR_EXCEPTION(X_exact.is_null(), std::logic_error,
130 "The exact solution X_exact is null!");
131 TEUCHOS_TEST_FOR_EXCEPTION(B.is_null(), std::logic_error,
132 "The right-hand side B is null!");
133 TEUCHOS_TEST_FOR_EXCEPTION(X.is_null(), std::logic_error,
134 "The approximate solution vector X is null!");
135
136 typedef Belos::LinearProblem<ST, MV, OP> problem_type;
137 RCP<problem_type> problem (new problem_type (A, X, B));
138 problem->setProblem ();
139 solver.setProblem (problem);
140
141 *verbOut << "Solving linear system" << std::endl;
142 Belos::ReturnType result = solver.solve ();
143
144 *verbOut << "Result of solve: "
145 << Belos::convertReturnTypeToString (result)
146 << std::endl;
147
148 // Make sure that all the processes finished.
149 comm->barrier ();
150
151 if (success) {
152 *out << "\nEnd Result: TEST PASSED" << std::endl;
153 return EXIT_SUCCESS;
154 }
155 else {
156 *out << "\nEnd Result: TEST FAILED" << std::endl;
157 return EXIT_FAILURE;
158 }
159 }
160
161 int main(int argc, char *argv[]) {
162 return run<double>(argc, argv);
163 // return run<float>(argc, argv);
164 }
165