Debian Codegraph @

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