Debian Codegraph @

packages/belos/epetra/example/BlockCG/BlockPrecCGEpetraExFile.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 file, which can be in Harwell-Boeing (*.hb),
11 // Matrix Market (*.mtx), or triplet format (*.triU, *.triS). The right-hand side
12 // from the problem, if it exists, will be used instead of multiple random
13 // right-hand-sides. The initial guesses are all set to zero. An ICT preconditioner
14 // is constructed using the Ifpack factory.
15 //
16 #include "BelosConfigDefs.hpp"
17 #include "BelosLinearProblem.hpp"
18 #include "BelosEpetraAdapter.hpp"
19 #include "BelosBlockCGSolMgr.hpp"
20
21 #include "EpetraExt_readEpetraLinearSystem.h"
22 #include "Epetra_Map.h"
23 #ifdef EPETRA_MPI
24 #include "Epetra_MpiComm.h"
25 #else
26 #include "Epetra_SerialComm.h"
27 #endif
28 #include "Epetra_CrsMatrix.h"
29
30 #include "Ifpack.h"
31
32 #include "Teuchos_CommandLineProcessor.hpp"
33 #include "Teuchos_ParameterList.hpp"
34 #include "Teuchos_StandardCatchMacros.hpp"
35
36 int main(int argc, char *argv[]) {
37 //
38 int MyPID = 0;
39 #ifdef EPETRA_MPI
40 // Initialize MPI
41 MPI_Init(&argc,&argv);
42 Epetra_MpiComm Comm(MPI_COMM_WORLD);
43 MyPID = Comm.MyPID();
44 #else
45 Epetra_SerialComm Comm;
46 #endif
47 //
48 typedef double ST;
49 typedef Teuchos::ScalarTraits<ST> SCT;
50 typedef SCT::magnitudeType MT;
51 typedef Epetra_MultiVector MV;
52 typedef Epetra_Operator OP;
53 typedef Belos::MultiVecTraits<ST,MV> MVT;
54 typedef Belos::OperatorTraits<ST,MV,OP> OPT;
55
56 using Teuchos::ParameterList;
57 using Teuchos::RCP;
58 using Teuchos::rcp;
59
60 bool verbose = false;
61 bool success = true;
62 try {
63 bool proc_verbose = false;
64 bool leftprec = true; // left preconditioning or right.
65 int frequency = -1; // frequency of status test output.
66 int blocksize = 1; // blocksize
67 int numrhs = 1; // number of right-hand sides to solve for
68 int maxiters = -1; // maximum number of iterations allowed per linear system
69 std::string filename("bcsstk14.hb");
70 MT tol = 1.0e-5; // relative residual tolerance
71
72 Teuchos::CommandLineProcessor cmdp(false,true);
73 cmdp.setOption("verbose","quiet",&verbose,"Print messages and results.");
74 cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters).");
75 cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS");
76 cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver.");
77 cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for.");
78 cmdp.setOption("block-size",&blocksize,"Block size used by CG.");
79 cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size).");
80 cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right.");
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 //
88 // *************Get the problem*********************
89 //
90 RCP<Epetra_Map> Map;
91 RCP<Epetra_CrsMatrix> A;
92 RCP<Epetra_MultiVector> B, X;
93 RCP<Epetra_Vector> vecB, vecX;
94 EpetraExt::readEpetraLinearSystem(filename, Comm, &A, &Map, &vecX, &vecB);
95 A->OptimizeStorage();
96 proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */
97
98 // Check to see if the number of right-hand sides is the same as requested.
99 if (numrhs>1) {
100 X = rcp( new Epetra_MultiVector( *Map, numrhs ) );
101 B = rcp( new Epetra_MultiVector( *Map, numrhs ) );
102 X->Random();
103 OPT::Apply( *A, *X, *B );
104 X->PutScalar( 0.0 );
105 }
106 else {
107 X = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecX);
108 B = Teuchos::rcp_implicit_cast<Epetra_MultiVector>(vecB);
109 }
110 //
111 // ************Construct preconditioner*************
112 //
113 ParameterList ifpackList;
114
115 // allocates an IFPACK factory. No data is associated
116 // to this object (only method Create()).
117 Ifpack Factory;
118
119 // create the preconditioner. For valid PrecType values,
120 // please check the documentation
121 std::string PrecType = "ICT"; // incomplete Cholesky
122 int OverlapLevel = 0; // must be >= 0. If Comm.NumProc() == 1,
123 // it is ignored.
124
125 RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*A, OverlapLevel) );
126 assert(Prec != Teuchos::null);
127
128 // specify parameters for ICT
129 ifpackList.set("fact: drop tolerance", 1e-9);
130 ifpackList.set("fact: ict level-of-fill", 1.0);
131 // the combine mode is on the following:
132 // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax"
133 // Their meaning is as defined in file Epetra_CombineMode.h
134 ifpackList.set("schwarz: combine mode", "Add");
135 // sets the parameters
136 IFPACK_CHK_ERR(Prec->SetParameters(ifpackList));
137
138 // initialize the preconditioner. At this point the matrix must
139 // have been FillComplete()'d, but actual values are ignored.
140 IFPACK_CHK_ERR(Prec->Initialize());
141
142 // Builds the preconditioners, by looking for the values of
143 // the matrix.
144 IFPACK_CHK_ERR(Prec->Compute());
145
146 // Create the Belos preconditioned operator from the Ifpack preconditioner.
147 // NOTE: This is necessary because Belos expects an operator to apply the
148 // preconditioner with Apply() NOT ApplyInverse().
149 RCP<Belos::EpetraPrecOp> belosPrec = rcp( new Belos::EpetraPrecOp( Prec ) );
150
151 //
152 // *****Create parameter list for the block CG solver manager*****
153 //
154 const int NumGlobalElements = B->GlobalLength();
155 if (maxiters == -1)
156 maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run
157 //
158 ParameterList belosList;
159 belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver
160 belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed
161 belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested
162 if (numrhs > 1) {
163 belosList.set( "Show Maximum Residual Norm Only", true ); // Show only the maximum residual norm
164 }
165 if (verbose) {
166 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings +
167 Belos::TimingDetails + Belos::StatusTestDetails );
168 if (frequency > 0)
169 belosList.set( "Output Frequency", frequency );
170 }
171 else
172 belosList.set( "Verbosity", Belos::Errors + Belos::Warnings );
173 //
174 // *******Construct a preconditioned linear problem********
175 //
176 RCP<Belos::LinearProblem<double,MV,OP> > problem
177 = rcp( new Belos::LinearProblem<double,MV,OP>( A, X, B ) );
178 if (leftprec)
179 problem->setLeftPrec( belosPrec );
180 else
181 problem->setRightPrec( belosPrec );
182
183 bool set = problem->setProblem();
184 if (set == false) {
185 if (proc_verbose)
186 std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
187 return -1;
188 }
189
190 // Create an iterative solver manager.
191 RCP< Belos::SolverManager<double,MV,OP> > solver
192 = rcp( new Belos::BlockCGSolMgr<double,MV,OP>(problem, rcp(&belosList,false)) );
193
194 //
195 // *******************************************************************
196 // *************Start the block CG iteration*************************
197 // *******************************************************************
198 //
199 if (proc_verbose) {
200 std::cout << std::endl << std::endl;
201 std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl;
202 std::cout << "Number of right-hand sides: " << numrhs << std::endl;
203 std::cout << "Block size used by solver: " << blocksize << std::endl;
204 std::cout << "Maximum number of iterations allowed: " << maxiters << std::endl;
205 std::cout << "Relative residual tolerance: " << tol << std::endl;
206 std::cout << std::endl;
207 }
208 //
209 // Perform solve
210 //
211 Belos::ReturnType ret = solver->solve();
212 //
213 // Get the number of iterations for this solve.
214 //
215 int numIters = solver->getNumIters();
216 if (proc_verbose)
217 std::cout << "Number of iterations performed for this solve: " << numIters << std::endl;
218 //
219 // Compute actual residuals.
220 //
221 bool badRes = false;
222 std::vector<double> actual_resids( numrhs );
223 std::vector<double> rhs_norm( numrhs );
224 Epetra_MultiVector resid(*Map, numrhs);
225 OPT::Apply( *A, *X, resid );
226 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
227 MVT::MvNorm( resid, actual_resids );
228 MVT::MvNorm( *B, rhs_norm );
229 if (proc_verbose) {
230 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
231 for ( int i=0; i<numrhs; i++) {
232 double actRes = actual_resids[i]/rhs_norm[i];
233 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
234 if (actRes > tol) badRes = true;
235 }
236 }
237
238 if (ret!=Belos::Converged || badRes) {
239 success = false;
240 if (proc_verbose)
241 std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl;
242 } else {
243 success = true;
244 if (proc_verbose)
245 std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl;
246 }
247 }
248 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
249
250 #ifdef EPETRA_MPI
251 MPI_Finalize();
252 #endif
253
254 return success ? EXIT_SUCCESS : EXIT_FAILURE;
255 }