Debian Codegraph @

packages/belos/tpetra/example/TFQMR/TFQMRTpetraExFile.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 // This driver reads a problem from a Harwell-Boeing (HB) file.
11 // Multiple right-hand-sides are created randomly.
12 // The initial guesses are all set to zero.
13 //
14 // Adapted from TFQMREpetraExFile.cpp (with original comments)
15
16 // All preconditioning has been commented out
17
18 // Ifpack2
19 // #include <Ifpack2_Factory.hpp>
20 // #include <Ifpack2_Preconditioner.hpp>
21
22 // Teuchos
23 #include <Teuchos_Assert.hpp>
24 #include <Teuchos_CommandLineProcessor.hpp>
25 #include <Teuchos_ParameterList.hpp>
26 #include <Teuchos_StandardCatchMacros.hpp>
27
28 // Tpetra
29 #include <Tpetra_Core.hpp>
30 #include <Tpetra_CrsMatrix.hpp>
31 #include <Tpetra_Map.hpp>
32 #include <Tpetra_MatrixIO.hpp>
33 #include <Tpetra_MultiVector.hpp>
34 #include <Tpetra_Operator.hpp>
35
36 // Belos
37 #include "BelosConfigDefs.hpp"
38 #include "BelosLinearProblem.hpp"
39 #include "BelosTFQMRSolMgr.hpp"
40 #include "BelosTpetraAdapter.hpp"
41
42 template <typename ScalarType>
43 int run(int argc, char *argv[]) {
44 using Teuchos::ParameterList;
45 using Teuchos::RCP;
46 using Teuchos::rcp;
47
48 using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type;
49 using LO = typename Tpetra::Vector<>::local_ordinal_type;
50 using GO = typename Tpetra::Vector<>::global_ordinal_type;
51 using NT = typename Tpetra::Vector<>::node_type;
52
53 using MV = typename Tpetra::MultiVector<ST, LO, GO, NT>;
54 using OP = typename Tpetra::Operator<ST, LO, GO, NT>;
55 using MAP = typename Tpetra::Map<LO, GO, NT>;
56 using MAT = typename Tpetra::CrsMatrix<ST, LO, GO, NT>;
57
58 using MVT = typename Belos::MultiVecTraits<ST, MV>;
59 using OPT = typename Belos::OperatorTraits<ST, MV, OP>;
60
61 using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType;
62 using STM = typename Teuchos::ScalarTraits<MT>;
63
64 using LinearProblem = typename Belos::LinearProblem<ST, MV, OP>;
65 // using Preconditioner = typename Ifpack2::Preconditioner<ST, LO, GO, NT>;
66 using TFQMRSolMgr = ::Belos::TFQMRSolMgr<ST, MV, OP>;
67
68 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
69 RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
70
71 bool verbose = false;
72 bool success = true;
73
74 try {
75 bool procVerbose = true;
76 bool leftPrec = true; // use left preconditioning to solve these linear systems
77 int frequency = -1; // how often residuals are printed by solver
78 int numRhs = 1;
79 int maxiters = -1; // maximum iterations allowed
80 std::string filename("osrirr1.hb");
81 MT tol = STM::squareroot (STM::eps()); // 1.e-5 // relative residual tolerance
82
83 if (procVerbose)
84 std::cout << "Init cli processor" << std::endl;
85
86 Teuchos::CommandLineProcessor cmdp(false, true);
87 cmdp.setOption("verbose", "quiet", &verbose, "Print messages and results.");
88 cmdp.setOption("left-prec", "right-prec", &leftPrec, "Left preconditioning or right.");
89 cmdp.setOption("frequency", &frequency, "Solvers frequency for printing residuals (#iters).");
90 cmdp.setOption("filename", &filename, "Filename for Harwell-Boeing test matrix.");
91 cmdp.setOption("tol", &tol, "Relative residual tolerance used by GMRES solver.");
92 cmdp.setOption("num-rhs", &numRhs, "Number of right-hand sides to be solved for.");
93 cmdp.setOption("maxiters", &maxiters,
94 "Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
95 if (cmdp.parse(argc, argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
96 return -1;
97 }
98 if (!verbose)
99 frequency = -1; // reset frequency if test is not verbose
100
101 // Get the problem
102 if (procVerbose)
103 std::cout << "Read Matrix from HB file" << std::endl;
104 RCP<MAT> A;
105 Tpetra::Utils::readHBMatrix(filename, comm, A);
106 if (procVerbose)
107 std::cout << "Read Matrix from HB file OK" << std::endl;
108 RCP<const MAP> map = A->getRowMap();
109 if (procVerbose)
110 std::cout << "Read Map OK" << std::endl;
111
112 procVerbose = verbose && (comm->getRank() == 0); // Only print on zero processor
113
114 // Create the preconditioner
115 // if (procVerbose)
116 // std::cout << "Create preconditioner" << std::endl;
117 // std::string precType = "RILUK";
118 // RCP<Preconditioner> prec = Ifpack2::Factory::create<MAT>(precType, A);
119 // assert(prec != Teuchos::null);
120
121 // // Specify parameters for the preconditioner
122 // int lFill = 2; // if (argc > 2) lFill = atoi(argv[2]);
123 // int overlap = 2; // if (argc > 3) overlap = atoi(argv[3]);
124 // ST absThresh = 0.0; // if (argc > 4) AThresh = atof(argv[4]);
125 // ST relThresh = 1.0; // if (argc >5) RThresh = atof(argv[5]);
126
127 // if (procVerbose) {
128 // std::cout << std::endl << std::endl;
129 // std::cout << "Constructing ILU preconditioner" << std::endl;
130 // std::cout << "Using Level of fill = " << lFill << std::endl;
131 // std::cout << "Using Level Overlap = " << overlap << std::endl;
132 // std::cout << "Using Absolute Threshold Value of " << absThresh << std::endl;
133 // std::cout << "Using Relative Threshold Value of " << relThresh << std::endl;
134 // }
135
136 // if (procVerbose)
137 // std::cout << "Init preconditioner" << std::endl;
138
139 // ParameterList precParams;
140 // precParams.set("fact: iluk level-of-fill", lFill);
141 // precParams.set("fact: iluk level-of-overlap", overlap);
142 // precParams.set("fact: absolute threshold", absThresh);
143 // precParams.set("fact: relative threshold", relThresh);
144 // prec->setParameters(precParams);
145
146 // // Initialize and build the preconditioner
147 // prec->initialize();
148 // prec->compute();
149
150 // Specify parameters for the TFQMR solver manager
151 const int numGlobalElements = map->getGlobalNumElements();
152 if (maxiters == -1)
153 maxiters = numGlobalElements - 1; // maximum number of iterations to run
154 RCP<ParameterList> belosList = rcp(new ParameterList());
155 belosList->set("Maximum Iterations", maxiters); // Maximum number of iterations allowed
156 belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested
157 if (leftPrec)
158 belosList->set("Explicit Residual Test", true); // Need to check for the explicit residual before returning
159 if (verbose) {
160 belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::StatusTestDetails);
161 if (frequency > 0)
162 belosList->set("Output Frequency", frequency);
163 } else
164 belosList->set("Verbosity", Belos::Errors + Belos::Warnings);
165
166 // Construct solution std::vector and random right-hand-sides
167 RCP<MV> X = rcp(new MV(map, numRhs));
168 X->putScalar(0.0);
169 RCP<MV> B = rcp(new MV(map, numRhs));
170 B->randomize();
171 RCP<LinearProblem> problem = rcp(new LinearProblem(A, X, B));
172 // if (leftPrec)
173 // problem->setLeftPrec(prec);
174 // else
175 // problem->setRightPrec(prec);
176
177 bool set = problem->setProblem();
178 if (set == false) {
179 if (procVerbose)
180 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
181 return -1;
182 }
183
184 // Create the TFQMR solver manager
185 RCP<TFQMRSolMgr> solver = rcp(new TFQMRSolMgr(problem, belosList));
186
187 // Print out information about problem
188 if (procVerbose) {
189 std::cout << std::endl << std::endl;
190 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
191 std::cout << "Number of right-hand sides: " << numRhs << std::endl;
192 std::cout << "Max number of TFQMR iterations: " << maxiters << std::endl;
193 std::cout << "Relative residual tolerance: " << tol << std::endl;
194 std::cout << std::endl;
195 }
196
197 // Perform solve
198 if (procVerbose) {
199 std::cout << "Solving..." << std::endl;
200 }
201 Belos::ReturnType ret = solver->solve();
202 if (procVerbose) {
203 std::cout << "Solve end" << std::endl;
204 }
205
206 // Compute actual residuals.
207 bool badRes = false;
208 std::vector<ST> actualResids(numRhs);
209 std::vector<ST> rhsNorm(numRhs);
210 MV resids(map, numRhs);
211 OPT::Apply(*A, *X, resids);
212 MVT::MvAddMv(-1.0, resids, 1.0, *B, resids);
213 MVT::MvNorm(resids, actualResids);
214 MVT::MvNorm(*B, rhsNorm);
215 if (procVerbose) {
216 std::cout << "---------- Actual Residuals (normalized) ----------" << std::endl << std::endl;
217 for (int i = 0; i < numRhs; i++) {
218 ST actRes = actualResids[i] / rhsNorm[i];
219 std::cout << "Problem " << i << " : \t" << actRes << std::endl;
220 if (actRes > tol)
221 badRes = true;
222 }
223 }
224
225 if (ret != Belos::Converged || badRes) {
226 success = false;
227 if (procVerbose)
228 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
229 } else {
230 success = true;
231 if (procVerbose)
232 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
233 }
234 }
235 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
236
237 return success ? EXIT_SUCCESS : EXIT_FAILURE;
238 }
239
240 int main(int argc, char *argv[]) {
241 return run<double>(argc, argv);
242 // return run<float>(argc, argv);
243 }
244 // end TFQMRpetraExFile.cpp