Debian Codegraph @

packages/belos/tpetra/test/BlockGmres/test_pseudo_gmres_hb.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 // This test is for testing the deflation in the pseudo-block Gmres solver.
15 // One set of linear systems is solved and then augmented with additional
16 // linear systems and resolved. The already solved linear systems should be
17 // deflated immediately, leaving only the augmented systems to be solved.
18 //
19 //
20
21 // Tpetra
22 #include <Tpetra_Map.hpp>
23 #include <Tpetra_Core.hpp>
24 #include <Tpetra_CrsMatrix.hpp>
25 #include <Tpetra_MultiVector.hpp>
26
27 // Belos
28 #include "BelosConfigDefs.hpp"
29 #include "BelosLinearProblem.hpp"
30 #include "BelosTpetraTestFramework.hpp"
31 #include "BelosPseudoBlockGmresSolMgr.hpp"
32
33
34 template <typename ScalarType>
35 int run(int argc, char *argv[]) {
36
37 using ST = typename Tpetra::CrsMatrix<ScalarType>::scalar_type;
38 using LO = typename Tpetra::CrsMatrix<>::local_ordinal_type;
39 using GO = typename Tpetra::CrsMatrix<>::global_ordinal_type;
40 using NT = typename Tpetra::CrsMatrix<>::node_type;
41
42 using OP = typename Tpetra::Operator<ST,LO,GO,NT>;
43 using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>;
44 using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType;
45
46 using tmap_t = Tpetra::Map<LO,GO,NT>;
47 using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>;
48
49 using MVT = typename Belos::MultiVecTraits<ST,MV>;
50 using OPT = typename Belos::OperatorTraits<ST,MV,OP>;
51
52 using Teuchos::RCP;
53 using Teuchos::rcp;
54 using Teuchos::ParameterList;
55
56 Teuchos::GlobalMPISession session(&argc, &argv, nullptr);
57
58 bool verbose = false;
59 bool success = true;
60
61 try {
62
63 const auto comm = Tpetra::getDefaultComm();
64 const int myPID = comm->getRank();
65
66 bool procVerbose = false;
67 int frequency = -1; // how often residuals are printed by solver
68 int initNumRHS = 5; // how many right-hand sides get solved first
69 int augNumRHS = 10; // how many right-hand sides are augmented to the first group
70 int maxRestarts = 15; // number of restarts allowed
71 int length = 100;
72 int initBlockSize = 5;// blocksize used for the initial pseudo-block GMRES solve
73 int augBlockSize = 3; // blocksize used for the augmented pseudo-block GMRES solve
74 int maxIters = -1; // maximum iterations allowed
75 std::string filename("orsirr1.hb");
76 MT tol = 1.0e-5; // relative residual tolerance
77 MT aug_tol = 1.0e-5; // relative residual tolerance for augmented system
78
79 Teuchos::CommandLineProcessor cmdp(false,true);
80 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
81 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
82 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
83 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
84 cmdp.setOption("aug-tol",&aug_tol,"Relative residual tolerance used by GMRES solver for augmented systems.");
85 cmdp.setOption("init-num-rhs",&initNumRHS,"Number of right-hand sides to be initially solved for.");
86 cmdp.setOption("aug-num-rhs",&augNumRHS,"Number of right-hand sides augmenting the initial solve.");
87 cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver.");
88 cmdp.setOption("block-size",&initBlockSize,"Block size used by GMRES for the initial solve.");
89 cmdp.setOption("aug-block-size",&augBlockSize,"Block size used by GMRES for the augmented solve.");
90 cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
91 cmdp.setOption("subspace-size",&length,"Dimension of Krylov subspace used by GMRES.");
92 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
93 return -1;
94 }
95 if (!verbose)
96 frequency = -1; // reset frequency if test is not verbose
97
98 procVerbose = verbose && (myPID==0); /* Only print on zero processor */
99
100 // Get the problem
101 Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm );
102 RCP<tcrsmatrix_t> A = reader.readFromFile( filename );
103 RCP<const tmap_t> Map = A->getDomainMap();
104
105 // ********Other information used by block solver***********
106 // *****************(can be user specified)******************
107
108 const int numGlobalElements = Map->getGlobalNumElements();
109 if (maxIters == -1)
110 maxIters = numGlobalElements - 1; // maximum number of iterations to run
111
112 ParameterList belosList;
113 belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization
114 belosList.set( "Block Size", initBlockSize ); // Blocksize to be used by iterative solver
115 belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed
116 belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed
117 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
118 belosList.set( "Deflation Quorum", initBlockSize ); // Number of converged linear systems before deflation
119 belosList.set( "Timer Label", "Belos Init" ); // Label used by the timers in this solver
120 if (verbose) {
121 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
122 Belos::TimingDetails + Belos::StatusTestDetails );
123 if (frequency > 0)
124 belosList.set( "Output Frequency", frequency );
125 }
126 else
127 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
128
129 // *****Construct solution std::vector and random right-hand-sides *****
130
131 RCP<MV> initX = rcp( new MV(Map, initNumRHS) );
132 RCP<MV> initB = rcp( new MV(Map, initNumRHS) );
133 MVT::MvRandom( *initX );
134 OPT::Apply( *A, *initX, *initB );
135 initX->putScalar( 0.0 );
136 Belos::LinearProblem<ST,MV,OP> initProblem( A, initX, initB );
137 initProblem.setLabel("Belos Init");
138
139 bool set = initProblem.setProblem();
140
141 if (set == false) {
142 if (procVerbose)
143 std::cout << std::endl << "ERROR: Initial Belos::LinearProblem failed to set up correctly!" << std::endl;
144 return -1;
145 }
146
147 // *******************************************************************
148 // *********************Perform initial solve*************************
149 // *******************************************************************
150
151 RCP< Belos::SolverManager<ST,MV,OP> > initSolver
152 = rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>( rcp(&initProblem,false), rcp(&belosList,false) ) );
153
154 // Perform solve
155 Belos::ReturnType ret = initSolver->solve();
156
157 // Compute actual residuals
158 bool badRes = false;
159 std::vector<ST> actualResids( initNumRHS );
160 std::vector<ST> rhsNorm( initNumRHS );
161 MV initR( Map, initNumRHS );
162 OPT::Apply( *A, *initX, initR );
163 MVT::MvAddMv( -1.0, initR, 1.0, *initB, initR );
164 MVT::MvNorm( initR, actualResids );
165 MVT::MvNorm( *initB, rhsNorm );
166 if (procVerbose) {
167 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
168 for (int i=0; i<initNumRHS; i++) {
169 ST actRes = actualResids[i]/rhsNorm[i];
170 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
171 if (actRes > tol) badRes = true;
172 }
173 }
174
175 if (ret != Belos::Converged || badRes==true) {
176 if (procVerbose)
177 std::cout << std::endl << "ERROR: Initial solve did not converge to solution!" << std::endl;
178 return -1;
179 }
180
181 // ***************Construct augmented linear system****************
182
183 RCP<MV> augX = rcp( new MV(Map, initNumRHS+augNumRHS) );
184 RCP<MV> augB = rcp( new MV(Map, initNumRHS+augNumRHS) );
185 if (augNumRHS) {
186 MVT::MvRandom( *augX );
187 OPT::Apply( *A, *augX, *augB );
188 augX->putScalar( 0.0 );
189 }
190
191 // Copy previous linear system into
192 RCP<MV> tmpX = rcp( new MV( *augX ) );
193 RCP<MV> tmpB = rcp( new MV( *augB ) );
194 tmpX->scale( 1.0, *augX );
195 tmpB->scale( 1.0, *augB );
196
197 Belos::LinearProblem<ST,MV,OP> augProblem( A, augX, augB );
198 augProblem.setLabel("Belos Aug");
199
200 set = augProblem.setProblem();
201 if (set == false) {
202 if (procVerbose)
203 std::cout << std::endl << "ERROR: Augmented Belos::LinearProblem failed to set up correctly!" << std::endl;
204 return -1;
205 }
206
207 // *******************************************************************
208 // *******************Perform augmented solve*************************
209 // *******************************************************************
210
211 belosList.set( "Block Size", augBlockSize ); // Blocksize to be used by iterative solver
212 belosList.set( "Convergence Tolerance", aug_tol ); // Relative convergence tolerance requested
213 belosList.set( "Deflation Quorum", augBlockSize ); // Number of converged linear systems before deflation
214 belosList.set( "Timer Label", "Belos Aug" ); // Label used by the timers in this solver
215 belosList.set( "Implicit Residual Scaling", "Norm of RHS" ); // Implicit residual scaling for convergence
216 belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Explicit residual scaling for convergence
217 RCP< Belos::SolverManager<ST,MV,OP> > augSolver
218 = rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>( rcp(&augProblem,false), rcp(&belosList,false) ) );
219
220 // Perform solve
221 ret = augSolver->solve();
222
223 if (ret != Belos::Converged) {
224 if (procVerbose)
225 std::cout << std::endl << "ERROR: Augmented solver did not converge to solution!" << std::endl;
226 return -1;
227 }
228
229 // **********Print out information about problem*******************
230
231 if (procVerbose) {
232 std::cout << std::endl << std::endl;
233 std::cout << "Dimension of matrix: " << numGlobalElements << std::endl;
234 std::cout << "Number of initial right-hand sides: " << initNumRHS << std::endl;
235 std::cout << "Number of augmented right-hand sides: " << augNumRHS << std::endl;
236 std::cout << "Number of restarts allowed: " << maxRestarts << std::endl;
237 std::cout << "Length of block Arnoldi factorization: " << length <<std::endl;
238 std::cout << "Max number of Gmres iterations: " << maxIters << std::endl;
239 std::cout << "Relative residual tolerance: " << tol << std::endl;
240 if (aug_tol != tol)
241 std::cout << "Relative residual tolerance for augmented systems: " << aug_tol << std::endl;
242 std::cout << std::endl;
243 }
244
245 // Compute actual residuals.
246 badRes = false;
247 int total_numrhs = initNumRHS + augNumRHS;
248 actualResids.resize( total_numrhs );
249 rhsNorm.resize( total_numrhs );
250 MV augR( Map, total_numrhs );
251 OPT::Apply( *A, *augX, augR );
252 MVT::MvAddMv( -1.0, augR, 1.0, *augB, augR );
253 MVT::MvNorm( augR, actualResids );
254 MVT::MvNorm( *augB, rhsNorm );
255 if (procVerbose) {
256 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
257 for ( int i=0; i<total_numrhs; i++) {
258 ST actRes = actualResids[i]/rhsNorm[i];
259 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
260 if (actRes > tol ) badRes = true;
261 }
262 }
263 if (ret!=Belos::Converged || badRes==true) {
264 success = false;
265 if (procVerbose)
266 std::cout << "End Result: TEST FAILED" << std::endl;
267 } else {
268 success = true;
269 if (procVerbose)
270 std::cout << "End Result: TEST PASSED" << std::endl;
271 }
272 }
273 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
274
275 return success ? EXIT_SUCCESS : EXIT_FAILURE;
276 } // run
277
278 int main(int argc, char *argv[]) {
279 // run with different ST
280 return run<double>(argc,argv);
281 // run<float>(argc,argv); // FAILS
282 }