Debian Codegraph @

packages/belos/epetra/test/BlockCG/test_resolve_cg_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 // The right-hand-side from the problem is being used instead of multiple
12 // random right-hand-sides. The initial guesses are all set to zero.
13 //
14 // NOTE: No preconditioner is used in this case.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosBlockCGSolMgr.hpp"
20 #include "BelosPseudoBlockCGSolMgr.hpp"
21 #include "BelosEpetraUtils.h"
22 #include "Epetra_CrsMatrix.h"
23 #include "Teuchos_CommandLineProcessor.hpp"
24 #include "Teuchos_ParameterList.hpp"
25 #include "Teuchos_StandardCatchMacros.hpp"
26 #include "Epetra_Map.h"
27
28 int main(int argc, char *argv[]) {
29 //
30 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
31 //
32 typedef double ST;
33 typedef Teuchos::ScalarTraits<ST> SCT;
34 typedef SCT::magnitudeType MT;
35 typedef Epetra_MultiVector MV;
36 typedef Epetra_Operator OP;
37 typedef Belos::MultiVecTraits<ST,MV> MVT;
38 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
39
40 using Teuchos::ParameterList;
41 using Teuchos::RCP;
42 using Teuchos::rcp;
43
44 bool success = false;
45 bool verbose = false;
46 try {
47 bool badRes = false;
48 bool proc_verbose = false;
49 bool pseudo = false; // use pseudo block CG to solve this linear system.
50 int frequency = -1;
51 int blocksize = 1;
52 int numrhs = 1;
53 int maxiters = -1; // maximum number of iterations allowed per linear system
54 std::string filename("bcsstk14.hb");
55 std::string ortho("DGKS");
56 MT tol = 1.0e-5; // relative residual tolerance
57
58 Teuchos::CommandLineProcessor cmdp(false,true);
59 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
60 cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block CG to solve the linear systems.");
61 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
62 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
63 cmdp.setOption("ortho",&ortho,"Orthogonalization routine used by CG solver.");
64 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
65 cmdp.setOption("blocksize",&blocksize,"Block size used by CG.");
66 cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
67 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
68 return -1;
69 }
70 if (!verbose)
71 frequency = -1; // reset frequency if test is not verbose
72 //
73 // Get the problem
74 //
75 int MyPID;
76 RCP<Epetra_CrsMatrix> A;
77 RCP<Epetra_MultiVector> B, X;
78 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
79 if(return_val != 0) return return_val;
80 const Epetra_Map &Map = A->RowMap();
81 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
82 //
83 // ********Other information used by block solver***********
84 // *****************(can be user specified)******************
85 //
86 const int NumGlobalElements = B->GlobalLength();
87 if (maxiters == -1)
88 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
89 //
90 ParameterList belosList;
91 belosList.set( "Num Blocks", maxiters ); // Maximum number of blocks in Krylov factorization
92 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
93 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
94 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
95 belosList.set( "Orthogonalization", ortho ); // Orthogonalization routine
96 if (verbose) {
97 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
98 Belos::TimingDetails + Belos::StatusTestDetails );
99 if (frequency > 0)
100 belosList.set( "Output Frequency", frequency );
101 }
102 else
103 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
104 //
105 //
106 // Construct an unpreconditioned linear problem instance.
107 //
108 Belos::LinearProblem<double,MV,OP> problem( A, X, B );
109 bool set = problem.setProblem();
110 if (set == false) {
111 if (proc_verbose)
112 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
113 return -1;
114 }
115 //
116 // *******************************************************************
117 // *************Start the block CG iteration*************************
118 // *******************************************************************
119 //
120 Teuchos::RCP< Belos::SolverManager<double,MV,OP> > solver;
121 if (pseudo)
122 solver = Teuchos::rcp( new Belos::PseudoBlockCGSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
123 else
124 solver = Teuchos::rcp( new Belos::BlockCGSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) );
125 //
126 // Perform solve
127 //
128 Belos::ReturnType ret = solver->solve();
129
130 if (ret!=Belos::Converged) {
131 if (proc_verbose)
132 std::cout << "End Result: TEST FAILED" << std::endl;
133 return -1;
134 }
135 //
136 //
137 // Get the number of iterations for this solve.
138 //
139 int numIters = solver->getNumIters();
140 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
141 //
142 // Compute actual residuals.
143 //
144 std::vector<double> actual_resids( numrhs );
145 std::vector<double> rhs_norm( numrhs );
146 Epetra_MultiVector resid(Map, numrhs);
147 OPT::Apply( *A, *X, resid );
148 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
149 MVT::MvNorm( resid, actual_resids );
150 MVT::MvNorm( *B, rhs_norm );
151 if (proc_verbose) {
152 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
153 for ( int i=0; i<numrhs; i++) {
154 std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl;
155 }
156 }
157 //
158 // -----------------------------------------------------------------
159 // Resolve the first problem by just resetting the solver manager.
160 // -----------------------------------------------------------------
161 X->PutScalar( 0.0 );
162 solver->reset( Belos::Problem );
163 //
164 // Perform solve (again)
165 //
166 ret = solver->solve();
167 //
168 // Get the number of iterations for this solve.
169 //
170 numIters = solver->getNumIters();
171 std::cout << "Number of iterations performed for this solve (manager reset): " << numIters << std::endl;
172
173 if (ret!=Belos::Converged) {
174 if (proc_verbose)
175 std::cout << "End Result: TEST FAILED" << std::endl;
176 return -1;
177 }
178 //
179 // Compute actual residuals.
180 //
181 std::vector<double> actual_resids2( numrhs );
182 Epetra_MultiVector resid2(Map, numrhs);
183 OPT::Apply( *A, *X, resid2 );
184 MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 );
185 MVT::MvNorm( resid2, actual_resids );
186 MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 );
187 MVT::MvNorm( resid2, actual_resids2 );
188 MVT::MvNorm( *B, rhs_norm );
189 if (proc_verbose) {
190 std::cout<< "---------- Actual Residuals (manager reset) ----------"<<std::endl<<std::endl;
191 for ( int i=0; i<numrhs; i++) {
192 std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl;
193 if ( actual_resids2[i] > SCT::prec() ) {
194 badRes = true;
195 std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl;
196 }
197 }
198 }
199 //
200 // ----------------------------------------------------------------------------------
201 // Resolve the first problem by resetting the solver manager and changing the labels.
202 // ----------------------------------------------------------------------------------
203 ParameterList belosList2;
204 belosList2.set( "Timer Label", "Belos Resolve w/ New Label" ); // Change timer labels.
205 solver->setParameters( Teuchos::rcp( &belosList2, false ) );
206
207 problem.setLabel( "Belos Resolve w/ New Label" );
208 X->PutScalar( 0.0 );
209 solver->reset( Belos::Problem );
210 //
211 // Perform solve (again)
212 //
213 ret = solver->solve();
214 //
215 // Get the number of iterations for this solve.
216 //
217 numIters = solver->getNumIters();
218 std::cout << "Number of iterations performed for this solve (label reset): " << numIters << std::endl;
219
220 if (ret!=Belos::Converged) {
221 if (proc_verbose)
222 std::cout << "End Result: TEST FAILED" << std::endl;
223 return -1;
224 }
225 //
226 // Compute actual residuals.
227 //
228 OPT::Apply( *A, *X, resid2 );
229 MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 );
230 MVT::MvNorm( resid2, actual_resids );
231 MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 );
232 MVT::MvNorm( resid2, actual_resids2 );
233 MVT::MvNorm( *B, rhs_norm );
234 if (proc_verbose) {
235 std::cout<< "---------- Actual Residuals (label reset) ----------"<<std::endl<<std::endl;
236 for ( int i=0; i<numrhs; i++) {
237 std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl;
238 if ( actual_resids2[i] > SCT::prec() ) {
239 badRes = true;
240 std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl;
241 }
242 }
243 }
244 //
245 // -------------------------------------------------------------
246 // Construct a second unpreconditioned linear problem instance.
247 // -------------------------------------------------------------
248 //
249 RCP<Epetra_MultiVector> X2 = MVT::Clone(*X, numrhs);
250 MVT::MvInit( *X2, 0.0 );
251 Belos::LinearProblem<double,MV,OP> problem2( A, X2, B );
252 problem2.setLabel("Belos Resolve");
253 set = problem2.setProblem();
254 if (set == false) {
255 if (proc_verbose)
256 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
257 return -1;
258 }
259 //
260 // *******************************************************************
261 // *************Start the block CG iteration*************************
262 // *******************************************************************
263 //
264 // Create the solver without either the problem or parameter list.
265 if (pseudo)
266 solver = Teuchos::rcp( new Belos::PseudoBlockCGSolMgr<double,MV,OP>() );
267 else
268 solver = Teuchos::rcp( new Belos::BlockCGSolMgr<double,MV,OP>() );
269 //
270 // Set the problem after the solver construction.
271 solver->setProblem( rcp( &problem2, false ) );
272
273 // Get the valid list of parameters from the solver and print it.
274 RCP<const Teuchos::ParameterList> validList = solver->getValidParameters();
275 if (proc_verbose) {
276 if (pseudo)
277 std::cout << std::endl << "Valid parameters from the pseudo-block CG solver manager:" << std::endl;
278 else
279 std::cout << std::endl << "Valid parameters from the block CG solver manager:" << std::endl;
280
281 std::cout << *validList << std::endl;
282 }
283
284 // Set the parameter list after the solver construction.
285 belosList.set( "Timer Label", "Belos Resolve" ); // Set timer label to discern between the two solvers.
286 solver->setParameters( rcp( &belosList, false ) );
287 //
288 // Perform solve
289 //
290 ret = solver->solve();
291 //
292 // Get the number of iterations for this solve.
293 //
294 numIters = solver->getNumIters();
295 std::cout << "Number of iterations performed for this solve (new solver): " << numIters << std::endl;
296 //
297 // Compute actual residuals.
298 //
299 OPT::Apply( *A, *X2, resid2 );
300 MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 );
301 MVT::MvNorm( resid2, actual_resids );
302 MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 );
303 MVT::MvNorm( resid2, actual_resids2 );
304 MVT::MvNorm( *B, rhs_norm );
305 if (proc_verbose) {
306 std::cout<< "---------- Actual Residuals (new solver) ----------"<<std::endl<<std::endl;
307 for ( int i=0; i<numrhs; i++) {
308 std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl;
309 if ( actual_resids2[i] > SCT::prec() ) {
310 badRes = true;
311 std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl;
312 }
313 }
314 }
315 //
316 // **********Print out information about problem*******************
317 //
318 if (proc_verbose) {
319 std::cout << std::endl << std::endl;
320 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
321 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
322 std::cout << "Block size used by solver: " << blocksize << std::endl;
323 std::cout << "Max number of CG iterations: " << maxiters << std::endl;
324 std::cout << "Relative residual tolerance: " << tol << std::endl;
325 std::cout << std::endl;
326 }
327
328 success = ret==Belos::Converged && !badRes;
329
330 if (success) {
331 if (proc_verbose)
332 std::cout << "End Result: TEST PASSED" << std::endl;
333 } else {
334 if (proc_verbose)
335 std::cout << "End Result: TEST FAILED" << std::endl;
336 }
337 }
338 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
339
340 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
341 } // end test_resolve_gmres_hb.cpp