Debian Codegraph @

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