Debian Codegraph @

packages/belos/epetra/test/BlockCG/test_bl_cg_set_res.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 "BelosBlockCGSolMgr.hpp"
28 #include "BelosEpetraUtils.h"
29 #include "Trilinos_Util.h"
30 #include "Epetra_CrsMatrix.h"
31 #include "Epetra_Map.h"
32 #include "Teuchos_CommandLineProcessor.hpp"
33 #include "Teuchos_ParameterList.hpp"
34 #include "Teuchos_StandardCatchMacros.hpp"
35
36 //
37 int main(int argc, char *argv[]) {
38 //
39 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
40 //
41 using Teuchos::ParameterList;
42 using Teuchos::RCP;
43 using Teuchos::rcp;
44
45 bool success = false;
46 bool verbose = false;
47 try {
48 //
49 // Get test parameters from command-line processor
50 //
51 bool proc_verbose = false;
52 int frequency = -1; // how often residuals are printed by solver
53 int numrhs = 1; // total number of right-hand sides to solve for
54 int blocksize = 1; // blocksize used by solver
55 int maxiters = -1; // maximum number of iterations for the solver to use
56 std::string filename("bcsstk14.hb");
57 double tol = 1.0e-5; // relative residual tolerance
58
59 Teuchos::CommandLineProcessor cmdp(false,true);
60 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
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("tol",&tol,"Relative residual tolerance used by CG solver.");
64 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
65 cmdp.setOption("block-size",&blocksize,"Block size to be used by CG solver.");
66 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
67
68
69 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
70 return -1;
71 }
72 if (!verbose)
73 frequency = -1; // Reset frequency if verbosity is off
74
75 //
76 // Get the problem
77 //
78 int MyPID;
79 RCP<Epetra_CrsMatrix> A;
80 RCP<Epetra_MultiVector> X, B, R;
81 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
82 if(return_val != 0) return return_val;
83 proc_verbose = ( verbose && (MyPID==0) );
84
85 //
86 // Solve using Belos
87 //
88 typedef double ST;
89 typedef Epetra_Operator OP;
90 typedef Epetra_MultiVector MV;
91 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
92 typedef Belos::MultiVecTraits<ST,MV> MVT;
93
94 //
95 // *****Construct initial guess and random right-hand-sides *****
96 //
97 if (numrhs != 1) {
98 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
99 MVT::MvRandom( *X );
100 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
101 OPT::Apply( *A, *X, *B );
102 MVT::MvRandom( *X );
103 }
104
105 //
106 // ***************Construct residual****************
107 //
108 R = MVT::Clone( *B, numrhs );
109 OPT::Apply( *A, *X, *R );
110 R->Update( 1, *B, -1 );
111
112 //
113 // *****Create parameter list for the block CG solver manager*****
114 //
115 const int NumGlobalElements = B->GlobalLength();
116 if (maxiters == -1)
117 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
118 //
119 ParameterList belosList;
120 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
121 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
122 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
123 if (verbose) {
124 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
125 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
126 if (frequency > 0)
127 belosList.set( "Output Frequency", frequency );
128 }
129 else
130 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
131
132 //
133 // **************Construct a linear problem***************
134 //
135 RCP<Belos::LinearProblem<double,MV,OP> > problem
136 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
137 problem->setInitResVec(R);
138
139 bool set = problem->setProblem();
140 if (set == false) {
141 if (proc_verbose)
142 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
143 return -1;
144 }
145
146 // Create an iterative solver manager.
147 RCP< Belos::SolverManager<double,MV,OP> > solver
148 = rcp( new Belos::BlockCGSolMgr<double,MV,OP>(problem, rcp(&belosList,false)) );
149
150 //
151 // *******************************************************************
152 // *************Start the block CG iteration*************************
153 // *******************************************************************
154 if (proc_verbose) {
155 std::cout << std::endl << std::endl;
156 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
157 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
158 std::cout << "Block size used by solver: " << blocksize << std::endl;
159 std::cout << "Max number of CG iterations: " << maxiters << std::endl;
160 std::cout << "Relative residual tolerance: " << tol << std::endl;
161 std::cout << std::endl;
162 }
163 //
164 // Perform solve
165 //
166 Belos::ReturnType ret = solver->solve();
167 //
168 // Compute actual residuals.
169 //
170 bool badRes = false;
171 std::vector<double> actual_resids( numrhs );
172 std::vector<double> rhs_norm( numrhs );
173 Epetra_MultiVector resid(A->Map(), numrhs);
174 OPT::Apply( *A, *X, resid );
175 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
176 MVT::MvNorm( resid, actual_resids );
177 MVT::MvNorm( *R, rhs_norm );
178 if (proc_verbose) {
179 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
180 for ( int i=0; i<numrhs; i++) {
181 double actRes = actual_resids[i]/rhs_norm[i];
182 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
183 if (actRes > tol) badRes = true;
184 }
185 }
186
187 success = ret==Belos::Converged && !badRes;
188
189 if (success) {
190 if (proc_verbose)
191 std::cout << std::endl << "End Result: TEST PASSED" << std::endl;
192 } else {
193 if (proc_verbose)
194 std::cout << std::endl << "End Result: TEST FAILED" << std::endl;
195 }
196 }
197 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
198
199 return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
200 } // end test_bl_pcg_hb.cpp
201