Debian Codegraph @

packages/belos/epetra/test/BlockGmres/test_bl_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 // As currently set up, this driver tests the case when the number of right-hand
15 // sides (numrhs = 15) is greater than the blocksize (block = 10) used by
16 // the solver. Here, 2 passes through the solver are required to solve
17 // for all right-hand sides. This information can be edited (see below - other
18 // information used by block solver - can be user specified) to solve for
19 // other sizes of systems. For example, one could set numrhs = 1 and block = 1,
20 // to solve a single right-hand side system in the traditional way, or, set
21 // numrhs = 1 and block > 1 to solve a single rhs-system with a block implementation.
22 //
23 //
24 #include "BelosConfigDefs.hpp"
25 #include "BelosLinearProblem.hpp"
26 #include "BelosEpetraAdapter.hpp"
27 #include "BelosBlockGmresSolMgr.hpp"
28 #include "BelosPseudoBlockGmresSolMgr.hpp"
29 #include "BelosEpetraUtils.h"
30 #include "Ifpack_IlukGraph.h"
31 #include "Ifpack_CrsRiluk.h"
32 #include "Epetra_CrsMatrix.h"
33 #include "Epetra_Map.h"
34 #include "Teuchos_CommandLineProcessor.hpp"
35 #include "Teuchos_ParameterList.hpp"
36 #include "Teuchos_StandardCatchMacros.hpp"
37 #include "Teuchos_Assert.hpp"
38
39 int main(int argc, char *argv[]) {
40 //
41 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
42 //
43 typedef double ST;
44 typedef Teuchos::ScalarTraits<ST> SCT;
45 typedef SCT::magnitudeType MT;
46 typedef Epetra_MultiVector MV;
47 typedef Epetra_Operator OP;
48 typedef Belos::MultiVecTraits<ST,MV> MVT;
49 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
50
51 using Teuchos::ParameterList;
52 using Teuchos::RCP;
53 using Teuchos::rcp;
54
55 bool verbose = false;
56 bool success = false;
57 try {
58 bool proc_verbose = false;
59 bool debug = false;
60 bool pseudo = false; // use pseudo block GMRES to solve this linear system.
61 bool leftprec = true; // use left preconditioning to solve these linear systems
62 int frequency = -1; // how often residuals are printed by solver
63 int blocksize = 4;
64 int numrhs = 15;
65 int maxrestarts = 15; // number of restarts allowed
66 int length = 25;
67 int maxiters = -1; // maximum iterations allowed
68 std::string filename("orsirr1.hb");
69 std::string ortho("ICGS");
70 MT tol = 1.0e-5; // relative residual tolerance
71
72 Teuchos::CommandLineProcessor cmdp(false,true);
73 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
74 cmdp.setOption("debug","no-debug",&debug,"Print debug messages.");
75 cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block GMRES to solve the linear systems.");
76 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
77 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
78 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
79 cmdp.setOption("ortho",&ortho,"Orthogonalization type.");
80 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
81 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
82 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
83 cmdp.setOption("blocksize",&blocksize,"Block size used by GMRES.");
84 cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
85 cmdp.setOption("subspace-size",&length,"Dimension of Krylov subspace used by GMRES.");
86 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
87 return -1;
88 }
89 if (!verbose)
90 frequency = -1; // reset frequency if test is not verbose
91 //
92 // Get the problem
93 //
94 int MyPID;
95 RCP<Epetra_CrsMatrix> A;
96 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,NULL,NULL,&MyPID);
97 const Epetra_Map &Map = A->RowMap();
98 if(return_val != 0) return return_val;
99 proc_verbose = verbose && (MyPID==0); /* Only print on zero processor */
100 //
101 // *****Construct the Preconditioner*****
102 //
103 if (proc_verbose) std::cout << std::endl << std::endl;
104 if (proc_verbose) std::cout << "Constructing ILU preconditioner" << std::endl;
105 int Lfill = 2;
106 // if (argc > 2) Lfill = atoi(argv[2]);
107 if (proc_verbose) std::cout << "Using Lfill = " << Lfill << std::endl;
108 int Overlap = 2;
109 // if (argc > 3) Overlap = atoi(argv[3]);
110 if (proc_verbose) std::cout << "Using Level Overlap = " << Overlap << std::endl;
111 double Athresh = 0.0;
112 // if (argc > 4) Athresh = atof(argv[4]);
113 if (proc_verbose) std::cout << "Using Absolute Threshold Value of " << Athresh << std::endl;
114 double Rthresh = 1.0;
115 // if (argc >5) Rthresh = atof(argv[5]);
116 if (proc_verbose) std::cout << "Using Relative Threshold Value of " << Rthresh << std::endl;
117 //
118 Teuchos::RCP<Ifpack_IlukGraph> ilukGraph;
119 Teuchos::RCP<Ifpack_CrsRiluk> ilukFactors;
120 //
121 if (Lfill > -1) {
122 ilukGraph = Teuchos::rcp(new Ifpack_IlukGraph(A->Graph(), Lfill, Overlap));
123 int info = ilukGraph->ConstructFilledGraph();
124 TEUCHOS_ASSERT( info == 0 );
125 ilukFactors = Teuchos::rcp(new Ifpack_CrsRiluk(*ilukGraph));
126 int initerr = ilukFactors->InitValues(*A);
127 if (initerr != 0) std::cout << "InitValues error = " << initerr;
128 info = ilukFactors->Factor();
129 TEUCHOS_ASSERT( info == 0 );
130 }
131 //
132 bool transA = false;
133 double Cond_Est;
134 ilukFactors->Condest(transA, Cond_Est);
135 if (proc_verbose) {
136 std::cout << "Condition number estimate for this preconditoner = " << Cond_Est << std::endl;
137 std::cout << std::endl;
138 }
139
140 //
141 // Create the Belos preconditioned operator from the Ifpack preconditioner.
142 // NOTE: This is necessary because Belos expects an operator to apply the
143 // preconditioner with Apply() NOT ApplyInverse().
144 RCP<Belos::EpetraPrecOp> Prec = rcp( new Belos::EpetraPrecOp( ilukFactors ) );
145
146 //
147 // ********Other information used by block solver***********
148 // *****************(can be user specified)******************
149 //
150 const int NumGlobalElements = Map.NumGlobalElements();
151 if (maxiters == -1)
152 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
153 //
154 bool adaptiveBlockSize = true;
155 if (numrhs < blocksize)
156 adaptiveBlockSize = false;
157 ParameterList belosList;
158 belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization
159 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
160 belosList.set( "Adaptive Block Size", adaptiveBlockSize ); // Adapt blocksize to numrhs
161 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
162 belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
163 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
164 belosList.set( "Orthogonalization", ortho );
165 if (verbose) {
166 int verbosity = Belos::Errors + Belos::Warnings +
167 Belos::TimingDetails + Belos::StatusTestDetails;
168 if (debug)
169 verbosity += Belos::Debug;
170 belosList.set( "Verbosity", verbosity );
171 if (frequency > 0)
172 belosList.set( "Output Frequency", frequency );
173 }
174 else
175 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
176 //
177 // *****Construct solution std::vector and random right-hand-sides *****
178 //
179 RCP<Epetra_MultiVector> X = rcp( new Epetra_MultiVector(Map, numrhs) );
180 X->PutScalar( 0.0 );
181 RCP<Epetra_MultiVector> B = rcp( new Epetra_MultiVector(Map, numrhs) );
182 B->Random();
183 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
184 if (leftprec)
185 problem.setLeftPrec( Prec );
186 else
187 problem.setRightPrec( Prec );
188
189 bool set = problem.setProblem();
190 if (set == false) {
191 if (proc_verbose)
192 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
193 return -1;
194 }
195 //
196 // *******************************************************************
197 // *************Start the block Gmres iteration*************************
198 // *******************************************************************
199 //
200 Teuchos::RCP< Belos::SolverManager<double,MV,OP> > solver;
201 if (pseudo)
202 solver = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
203 else
204 solver = Teuchos::rcp( new Belos::BlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
205 //
206 // **********Print out information about problem*******************
207 //
208 if (proc_verbose) {
209 std::cout << std::endl << std::endl;
210 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
211 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
212 std::cout << "Block size used by solver: " << blocksize << std::endl;
213 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
214 std::cout << "Length of block Arnoldi factorization: " << length*blocksize << " ( "<< length << " blocks ) " <<std::endl;
215 std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
216 std::cout << "Relative residual tolerance: " << tol << std::endl;
217 std::cout << std::endl;
218 }
219 //
220 // Perform solve
221 //
222 Belos::ReturnType ret = solver->solve();
223
224 //
225 // Compute actual residuals.
226 //
227 bool badRes = false;
228 std::vector<double> actual_resids( numrhs );
229 std::vector<double> rhs_norm( numrhs );
230 Epetra_MultiVector R(Map, numrhs);
231 OPT::Apply( *A, *X, R );
232 MVT::MvAddMv( -1.0, R, 1.0, *B, R );
233 MVT::MvNorm( R, actual_resids );
234 MVT::MvNorm( *B, rhs_norm );
235 if (proc_verbose) {
236 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
237 for ( int i=0; i<numrhs; i++) {
238 double actRes = actual_resids[i]/rhs_norm[i];
239 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
240 if (actRes > tol ) badRes = true;
241 }
242 }
243
244 success = ret==Belos::Converged && !badRes;
245
246 if (success) {
247 if (proc_verbose)
248 std::cout << "End Result: TEST PASSED" << std::endl;
249 } else {
250 if (proc_verbose)
251 std::cout << "End Result: TEST FAILED" << std::endl;
252 }
253 }
254 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
255
256 return success ? EXIT_SUCCESS : EXIT_FAILURE;
257 } // end test_bl_pgmres_hb.cpp