Debian Codegraph @

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