Debian Codegraph @

packages/belos/epetra/test/BlockCG/test_bl_pcg_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 #include "Ifpack.h"
37 //
38 int main(int argc, char *argv[]) {
39 //
40 Teuchos::GlobalMPISession session(&argc, &argv, NULL);
41 //
42 using Teuchos::ParameterList;
43 using Teuchos::RCP;
44 using Teuchos::rcp;
45
46 bool success = false;
47 bool verbose = false;
48 try {
49 //
50 // Get test parameters from command-line processor
51 //
52 bool proc_verbose = false;
53 bool leftprec = true; // left preconditioning or right.
54 int frequency = -1; // how often residuals are printed by solver
55 int numrhs = 15; // total number of right-hand sides to solve for
56 int blocksize = 10; // blocksize used by solver
57 int maxiters = -1; // maximum number of iterations for the solver to use
58 std::string filename("bcsstk14.hb");
59 double tol = 1.0e-5; // relative residual tolerance
60
61 Teuchos::CommandLineProcessor cmdp(false,true);
62 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
63 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
64 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
65 cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix.");
66 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
67 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
68 cmdp.setOption("block-size",&blocksize,"Block size to be used by CG solver.");
69 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size).");
70
71
72 if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
73 return -1;
74 }
75 if (!verbose)
76 frequency = -1; // Reset frequency if verbosity is off
77
78 //
79 // Get the problem
80 //
81 int MyPID;
82 RCP<Epetra_CrsMatrix> A;
83 RCP<Epetra_MultiVector> X, B, R, Z;
84 int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID);
85 if(return_val != 0) return return_val;
86 proc_verbose = ( verbose && (MyPID==0) );
87
88 //
89 // Solve using Belos
90 //
91 typedef double ST;
92 typedef Epetra_Operator OP;
93 typedef Epetra_MultiVector MV;
94 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
95 typedef Belos::MultiVecTraits<ST,MV> MVT;
96
97 //
98 // *****Construct initial guess and random right-hand-sides *****
99 //
100 if (numrhs != 1) {
101 X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
102 MVT::MvRandom( *X );
103 B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) );
104 OPT::Apply( *A, *X, *B );
105 MVT::MvRandom( *X );
106 }
107
108 //
109 // ***************Construct residual****************
110 //
111 R = MVT::Clone( *B, numrhs );
112 OPT::Apply( *A, *X, *R );
113 R->Update( 1, *B, -1 );
114
115 //
116 // ************Construct preconditioner*************
117 //
118 ParameterList ifpackList;
119
120 // allocates an IFPACK factory. No data is associated
121 // to this object (only method Create()).
122 Ifpack Factory;
123
124 // create the preconditioner. For valid PrecType values,
125 // please check the documentation
126 std::string PrecType = "ICT"; // incomplete Cholesky
127 int OverlapLevel = 0; // must be >= 0. If Comm.NumProc() == 1,
128 // it is ignored.
129
130 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
131 assert(Prec != Teuchos::null);
132
133 // specify parameters for ICT
134 ifpackList.set("fact: drop tolerance", 1e-9);
135 ifpackList.set("fact: ict level-of-fill", 1.0);
136 // the combine mode is on the following:
137 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
138 // Their meaning is as defined in file Epetra_CombineMode.h
139 ifpackList.set("schwarz: combine mode", "Add");
140 // sets the parameters
141 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
142
143 // initialize the preconditioner. At this point the matrix must
144 // have been FillComplete()'d, but actual values are ignored.
145 IFPACK_CHK_ERR(Prec->Initialize());
146
147 // Builds the preconditioners, by looking for the values of
148 // the matrix.
149 IFPACK_CHK_ERR(Prec->Compute());
150
151 // Create the Belos preconditioned operator from the Ifpack preconditioner.
152 // NOTE: This is necessary because Belos expects an operator to apply the
153 // preconditioner with Apply() NOT ApplyInverse().
154 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
155
156 //
157 // ***************Construct preconditioned residual***************
158 //
159 Z = MVT::Clone( *B, numrhs );
160 OPT::Apply( *belosPrec, *R, *Z );
161
162 //
163 // *****Create parameter list for the block CG solver manager*****
164 //
165 const int NumGlobalElements = B->GlobalLength();
166 if (maxiters == -1)
167 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
168 //
169 ParameterList belosList;
170 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
171 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
172 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
173 if (verbose) {
174 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
175 Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails );
176 if (frequency > 0)
177 belosList.set( "Output Frequency", frequency );
178 }
179 else
180 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
181
182 //
183 // *******Construct a preconditioned linear problem********
184 //
185 RCP<Belos::LinearProblem<double,MV,OP> > problem
186 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
187 if (leftprec) {
188 problem->setLeftPrec( belosPrec );
189 }
190 else {
191 problem->setRightPrec( belosPrec );
192 }
193 problem->setInitResVec(R);
194 problem->setInitPrecResVec(Z);
195
196 bool set = problem->setProblem();
197 if (set == false) {
198 if (proc_verbose)
199 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
200 return -1;
201 }
202
203 // Create an iterative solver manager.
204 RCP< Belos::SolverManager<double,MV,OP> > solver
205 = rcp( new Belos::BlockCGSolMgr<double,MV,OP>(problem, rcp(&belosList,false)) );
206
207 //
208 // *******************************************************************
209 // *************Start the block CG iteration*************************
210 // *******************************************************************
211 if (proc_verbose) {
212 std::cout << std::endl << std::endl;
213 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
214 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
215 std::cout << "Block size used by solver: " << blocksize << std::endl;
216 std::cout << "Max number of CG iterations: " << maxiters << std::endl;
217 std::cout << "Relative residual tolerance: " << tol << std::endl;
218 std::cout << std::endl;
219 }
220 //
221 // Perform solve
222 //
223 Belos::ReturnType ret = solver->solve();
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 resid(A->Map(), numrhs);
231 OPT::Apply( *A, *X, resid );
232 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
233 MVT::MvNorm( resid, actual_resids );
234 MVT::MvNorm( *R, 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 << std::endl << "End Result: TEST PASSED" << std::endl;
249 } else {
250 if (proc_verbose)
251 std::cout << std::endl << "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_pcg_hb.cpp
258