Debian Codegraph @

packages/belos/epetra/test/BlockGmres/test_bl_fgmres_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 = 4) used by
16 // the solver. Here, 4 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 "BelosEpetraOperator.h"
28 #include "BelosBlockGmresSolMgr.hpp"
29 #include "BelosPseudoBlockGmresSolMgr.hpp"
30 #include "BelosEpetraUtils.h"
31 #include "Ifpack_IlukGraph.h"
32 #include "Ifpack_CrsRiluk.h"
33 #include "Epetra_CrsMatrix.h"
34 #include "Epetra_Map.h"
35 #include "Teuchos_CommandLineProcessor.hpp"
36 #include "Teuchos_ParameterList.hpp"
37 #include "Teuchos_StandardCatchMacros.hpp"
38 #include "Teuchos_Assert.hpp"
39
40 int main(int argc, char *argv[]) {
41 //
42 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
43 //
44 typedef double ST;
45 typedef Teuchos::ScalarTraits<ST> SCT;
46 typedef SCT::magnitudeType MT;
47 typedef Epetra_MultiVector MV;
48 typedef Epetra_Operator OP;
49 typedef Belos::MultiVecTraits<ST,MV> MVT;
50 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
51
52 using Teuchos::ParameterList;
53 using Teuchos::RCP;
54 using Teuchos::rcp;
55
56 bool verbose = false;
57 bool success = true;
58 try {
59 bool proc_verbose = false;
60 bool leftprec = true; // use left preconditioning to solve these linear systems
61 int frequency = -1; // how often residuals are printed by solver
62 int blocksize = 4;
63 int numrhs = 15;
64 int maxrestarts = 15; // number of restarts allowed
65 int length = 25;
66 int maxiters = -1; // maximum iterations allowed
67 std::string filename("orsirr1.hb");
68 MT tol = 1.0e-5; // relative residual tolerance
69
70 Teuchos::CommandLineProcessor cmdp(false,true);
71 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
72 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
73 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
74 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
75 cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver.");
76 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
77 cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver.");
78 cmdp.setOption("blocksize",&blocksize,"Block size used by GMRES.");
79 cmdp.setOption("maxiters",&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 //
128 bool transA = false;
129 double Cond_Est;
130 ilukFactors->Condest(transA, Cond_Est);
131 if (proc_verbose) {
132 std::cout << "Condition number estimate for this preconditoner = " << Cond_Est << std::endl;
133 std::cout << std::endl;
134 }
135
136 //
137 // Create the Belos preconditioned operator from the Ifpack preconditioner.
138 // NOTE: This is necessary because Belos expects an operator to apply the
139 // preconditioner with Apply() NOT ApplyInverse().
140 RCP<Belos::EpetraPrecOp> Prec = rcp( new Belos::EpetraPrecOp( ilukFactors ) );
141
142 //
143 // ********Other information used by block solver***********
144 // **************(can be user specified)********************
145 //
146 const int NumGlobalElements = Map.NumGlobalElements();
147 if (maxiters == -1)
148 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
149 //
150 ParameterList innerBelosList;
151 innerBelosList.set( "Solver", "BlockGmres" ); // Set the inner solver to use block Gmres
152 innerBelosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization
153 innerBelosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
154 innerBelosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
155 innerBelosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed
156 innerBelosList.set( "Convergence Tolerance", 1.0e-2 ); // Relative convergence tolerance requested
157 innerBelosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
158 innerBelosList.set( "Timer Label", "Belos Preconditioner Solve" );// Choose a different label for the inner solve
159 //
160 // *****Construct linear problem using A and Prec*****
161 // ***The solution and RHS vectors will be set later**
162 Belos::LinearProblem<double,MV,OP> innerProblem;
163 innerProblem.setOperator( A );
164 if (leftprec)
165 innerProblem.setLeftPrec( Prec );
166 else
167 innerProblem.setRightPrec( Prec );
168 innerProblem.setLabel( "Belos Preconditioner Solve" );
169 //
170 // *****Create the inner block Gmres iteration********
171 //
172 RCP<Belos::EpetraOperator> innerSolver;
173 innerSolver = rcp( new Belos::EpetraOperator( rcp(&innerProblem,false) , rcp(&innerBelosList,false), true ) );
174 //
175 // *****Construct solution std::vector and random right-hand-sides *****
176 //
177 RCP<Epetra_MultiVector> X = rcp( new Epetra_MultiVector(Map, numrhs) );
178 X->PutScalar( 0.0 );
179 RCP<Epetra_MultiVector> B = rcp( new Epetra_MultiVector(Map, numrhs) );
180 B->Random();
181 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
182 problem.setRightPrec( innerSolver );
183 problem.setLabel( "Belos Flexible Gmres Solve" );
184 bool set = problem.setProblem();
185 if (set == false) {
186 if (proc_verbose)
187 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
188 return -1;
189 }
190 //
191 // Copy the list for the inner solver
192 //
193 Teuchos::ParameterList belosList( innerBelosList );
194 belosList.set( "Flexible Gmres" , true ); // Use flexible Gmres to solve this problem
195 belosList.set( "Timer Label", "Belos Flexible Gmres Solve" );// Choose a different label for the outer solve
196 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
197 if (verbose) {
198 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
199 Belos::TimingDetails + Belos::StatusTestDetails );
200 if (frequency > 0)
201 belosList.set( "Output Frequency", frequency );
202 }
203 //
204 // *******************************************************************
205 // **********Create the flexible, block Gmres iteration***************
206 // *******************************************************************
207 //
208 RCP< Belos::SolverManager<double,MV,OP> > solver;
209 solver = rcp( new Belos::BlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
210 //
211 //
212 // **********Print out information about problem*******************
213 //
214 if (proc_verbose) {
215 std::cout << std::endl << std::endl;
216 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
217 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
218 std::cout << "Block size used by solver: " << blocksize << std::endl;
219 std::cout << "Number of restarts allowed: " << maxrestarts << std::endl;
220 std::cout << "Length of block Arnoldi factorization: " << length*blocksize << " ( "<< length << " blocks ) " <<std::endl;
221 std::cout << "Max number of Gmres iterations: " << maxiters << std::endl;
222 std::cout << "Relative residual tolerance: " << tol << std::endl;
223 std::cout << std::endl;
224 }
225 //
226 // Perform solve
227 //
228 Belos::ReturnType ret = solver->solve();
229 //
230 // Compute actual residuals.
231 //
232 bool badRes = false;
233 std::vector<double> actual_resids( numrhs );
234 std::vector<double> rhs_norm( numrhs );
235 Epetra_MultiVector R(Map, numrhs);
236 OPT::Apply( *A, *X, R );
237 MVT::MvAddMv( -1.0, R, 1.0, *B, R );
238 MVT::MvNorm( R, actual_resids );
239 MVT::MvNorm( *B, rhs_norm );
240 if (proc_verbose) {
241 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
242 for ( int i=0; i<numrhs; i++) {
243 double actRes = actual_resids[i]/rhs_norm[i];
244 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
245 if (actRes > tol ) badRes = true;
246 }
247 }
248
249 success = ret==Belos::Converged && !badRes;
250
251 if (success) {
252 if (proc_verbose)
253 std::cout << "End Result: TEST PASSED" << std::endl;
254 } else {
255 if (proc_verbose)
256 std::cout << "End Result: TEST FAILED" << std::endl;
257 }
258 }
259 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
260
261 return success ? EXIT_SUCCESS : EXIT_FAILURE;
262 } // end test_bl_fgmres_hb.cpp