Debian Codegraph @

packages/belos/epetra/test/Epetra64Driver/cxx_main.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 program computes the linear solution using Belos and a laplacian matrix.
11 // NOTE: The laplacian matrix generated has row and column ids larger than INT_MAX
12 // to test Epetra64 functionality with Belos solvers.
13 //
14 // Karen Devine, April 2012
15 // Erik Boman, May 2012
16 // K. Devine & H. Thornquist, January 2013
17 //
18
19 #define TINYMATRIX 26
20 #include "Epetra_ConfigDefs.h"
21 #ifdef EPETRA_MPI
22 #include <mpi.h>
23 #include "Epetra_MpiComm.h"
24 #define FINALIZE MPI_Finalize()
25 #else
26 #include "Epetra_SerialComm.h"
27 #define FINALIZE
28 #endif
29 #include "Epetra_Map.h"
30 #include "Epetra_CrsMatrix.h"
31 #include "Epetra_Import.h"
32 #include "Epetra_InvOperator.h"
33
34 #include "BelosBlockCGSolMgr.hpp"
35 #include "BelosLinearProblem.hpp"
36 #include "BelosConfigDefs.hpp"
37 #include "BelosEpetraAdapter.hpp"
38
39 #include "Epetra_Time.h"
40 #include "Teuchos_LAPACK.hpp"
41 #include "Teuchos_CommandLineProcessor.hpp"
42 #include "Teuchos_RCP.hpp"
43 #include "Teuchos_as.hpp"
44 #include "Teuchos_StandardCatchMacros.hpp"
45
46 #include "build_simple_matrices.hpp"
47
48 #include <stdio.h>
49
50 ////////////////////////////////////////////////////////////////////////////
51
52 int main(int narg, char *arg[])
53 {
54 using std::cout;
55
56 #ifdef EPETRA_MPI
57 // Initialize MPI
58 MPI_Init(&narg,&arg);
59 Epetra_MpiComm Comm( MPI_COMM_WORLD );
60 #else
61 Epetra_SerialComm Comm;
62 #endif
63
64 bool verbose = true;
65 bool success = true;
66 try {
67 int MyPID = Comm.MyPID();
68
69 int verbosity = 1;
70
71 bool testEpetra64 = true;
72
73 // Matrix properties
74 bool isHermitian = true;
75
76 // Linear solver properties
77
78 std::string method = "BlockCG";
79 std::string ortho = "DGKS";
80 int blockSize = 1;
81 int numrhs = 1;
82 int numblocks = -1;
83 int maxrestarts = 0;
84 int maxiterations = -1;
85 int gensize = 25; // Needs to be long long to test with > INT_MAX rows
86 double tol = 1.0e-5;
87
88 // Echo the command line
89 if (MyPID == 0) {
90 for (int i = 0; i < narg; i++)
91 std::cout << arg[i] << " ";
92 std::cout << std::endl;
93 }
94
95 // Command-line processing
96
97 Teuchos::CommandLineProcessor cmdp(false,true);
98 cmdp.setOption("Epetra64", "no-Epetra64", &testEpetra64,
99 "Force code to use Epetra64, even if the problem size does "
100 "not require it. (Epetra64 will be used automatically for "
101 "sufficiently large problems, or not used if Epetra does not have built in support.)");
102 cmdp.setOption("gen",&gensize,
103 "Generate a simple Laplacian matrix of size n.");
104 cmdp.setOption("verbosity", &verbosity, "0=quiet, 1=low, 2=medium, 3=high.");
105 cmdp.setOption("method",&method,
106 "Solver method to use: BlockCG.");
107 cmdp.setOption("tol",&tol,"Solver convergence tolerance.");
108 cmdp.setOption("blocksize",&blockSize,"Block size to use in solver.");
109 cmdp.setOption("numrhs",&numrhs,"Number of right-hand sides.");
110 cmdp.setOption("numblocks",&numblocks,"Number of blocks to allocate.");
111 cmdp.setOption("maxrestarts",&maxrestarts,
112 "Maximum number of restarts.");
113 cmdp.setOption("maxiterations",&maxiterations,
114 "Maximum number of iterations.");
115 cmdp.setOption("ortho", &ortho,
116 "Orthogonalization method (DGKS, ICGS, IMGS).");
117 if (cmdp.parse(narg,arg)!=Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) {
118 FINALIZE;
119 return -1;
120 }
121
122 // Print the most essential options (not in the MyPL parameters later)
123 verbose = (verbosity>0);
124 if (verbose && MyPID==0){
125 std::cout << "verbosity = " << verbosity << std::endl;
126 std::cout << "method = " << method << std::endl;
127 std::cout << "numrhs = " << numrhs << std::endl;
128 }
129
130 // Make sure Epetra was built with 64-bit global indices enabled.
131 #ifdef EPETRA_NO_64BIT_GLOBAL_INDICES
132 if (testEpetra64)
133 testEpetra64 = false;
134 #endif
135
136 Epetra_CrsMatrix *A = NULL;
137
138 // Read matrix from file or generate a matrix
139 if ((gensize > 0 && testEpetra64)) {
140 // Generate the matrix using long long for global indices
141 build_simple_matrix<long long>(Comm, A, (long long)gensize, true, verbose);
142 }
143 else if (gensize) {
144 // Generate the matrix using int for global indices
145 build_simple_matrix<int>(Comm, A, gensize, false, verbose);
146 }
147 else {
148 printf("YOU SHOULDN'T BE HERE \n");
149 exit(-1);
150 }
151
152 if (verbose && (A->NumGlobalRows64() < TINYMATRIX)) {
153 if (MyPID == 0) std::cout << "Input matrix: " << std::endl;
154 A->Print(std::cout);
155 }
156 Teuchos::RCP<Epetra_CrsMatrix> rcpA = Teuchos::rcp( A );
157
158 // Set Belos verbosity level
159 if (MyPID == 0) std::cout << "Setting up the problem..." << std::endl;
160
161 int belos_verbosity = Belos::Errors + Belos::Warnings;
162 if (verbosity >= 1) // low
163 belos_verbosity += Belos::FinalSummary + Belos::TimingDetails;
164 if (verbosity >= 2) // medium
165 belos_verbosity += Belos::IterationDetails;
166 if (verbosity >= 3) // high
167 belos_verbosity += Belos::StatusTestDetails
168 + Belos::OrthoDetails
169 + Belos::Debug;
170
171 // Create parameter list to pass into solver
172
173 Teuchos::ParameterList MyPL;
174 MyPL.set("Verbosity", belos_verbosity);
175 MyPL.set("Convergence Tolerance", tol);
176 MyPL.set("Orthogonalization", ortho);
177
178 // For the following, use Belos's defaults unless explicitly specified.
179 if (numblocks > 0) MyPL.set( "Num Blocks", numblocks);
180 if (maxrestarts > 0) MyPL.set( "Maximum Restarts", maxrestarts);
181 if (maxiterations <= 0)
182 maxiterations = Teuchos::asSafe<int>( gensize );
183 MyPL.set( "Maximum Iterations", maxiterations);
184 if (blockSize > 0) MyPL.set( "Block Size", blockSize );
185
186 typedef Epetra_MultiVector MV;
187 typedef Epetra_Operator OP;
188 typedef Belos::MultiVecTraits<double, MV> MVT;
189 typedef Belos::OperatorTraits<double, MV, OP> OPT;
190
191
192 // Create the linear problem to be solved.
193
194 Teuchos::RCP<Epetra_MultiVector> X =
195 Teuchos::rcp(new Epetra_MultiVector(rcpA->Map(), numrhs));
196 Teuchos::RCP<Epetra_MultiVector> B =
197 Teuchos::rcp(new Epetra_MultiVector(rcpA->Map(), numrhs));
198
199 Teuchos::RCP<Belos::LinearProblem<double, MV, OP> > MyProblem;
200 MyProblem =
201 Teuchos::rcp(new Belos::LinearProblem<double, MV, OP>(Teuchos::rcp(A,false), X, B) );
202
203 // Inform the linear problem whether A is Hermitian
204
205 MyProblem->setHermitian( isHermitian );
206
207 int iter = 0;
208
209 // Set random seed to have consistent initial vectors between experiments.
210 X->SetSeed(2*(MyPID) +1); // Odd seed
211 MVT::MvRandom( *X );
212 OPT::Apply( *rcpA, *X, *B );
213 MVT::MvInit( *X, 0. );
214
215 // Inform the linear problem that you are finished passing it information
216 bool boolret = MyProblem->setProblem();
217 if (boolret != true) {
218 if (verbose && MyPID == 0) {
219 std::cout << "Belos::LinearProblem::setProblem() returned with error."
220 << std::endl;
221 }
222 FINALIZE;
223 return -1;
224 }
225
226 Teuchos::RCP<Belos::SolverManager<double, MV, OP> > MySolverMgr;
227
228 if (method == "BlockCG") {
229 // Initialize the Block CG solver
230 MySolverMgr = Teuchos::rcp( new Belos::BlockCGSolMgr<double, MV, OP>(MyProblem,Teuchos::rcp(&MyPL, false)) );
231 }
232 else
233 std::cout << "Unknown solver method!" << std::endl;
234
235 if (verbose && MyPID==0) MyPL.print(std::cout);
236
237 // Solve the problem to the specified tolerances or length
238 if (MyPID == 0) std::cout << "Beginning the " << method << " solve..." << std::endl;
239
240 int numfailed = 0;
241 Belos::ReturnType returnCode = MySolverMgr->solve();
242 if (returnCode != Belos::Converged && MyPID==0) {
243 ++numfailed;
244 std::cout << "Belos::SolverManager::solve() returned unconverged." << std::endl;
245 }
246 iter = MySolverMgr->getNumIters();
247
248 if (MyPID == 0) {
249 std::cout << "Iterations in this solve: " << iter << std::endl;
250 std::cout << "Solve complete; beginning post-processing..."<< std::endl;
251 }
252
253 // Compute residuals.
254 bool badRes = false;
255 std::vector<double> actual_resids( numrhs );
256 std::vector<double> rhs_norm( numrhs );
257 Epetra_MultiVector resid(rcpA->Map(), numrhs);
258 OPT::Apply( *rcpA, *X, resid );
259 MVT::MvAddMv( -1.0, resid, 1.0, *B, resid );
260 MVT::MvNorm( resid, actual_resids );
261 MVT::MvNorm( *B, rhs_norm );
262 if (MyPID==0)
263 std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl;
264 for ( int i=0; i<numrhs; i++) {
265 double actRes = actual_resids[i]/rhs_norm[i];
266 if (MyPID==0)
267 std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl;
268 if (actRes > tol) badRes = true;
269 }
270
271 if (badRes || numfailed) {
272 success = false;
273 if (MyPID == 0)
274 std::cout << "End Result: TEST FAILED" << std::endl;
275 } else {
276 success = true;
277 if (MyPID == 0)
278 std::cout << "End Result: TEST PASSED" << std::endl;
279 }
280 }
281 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
282
283 FINALIZE;
284
285 return success ? EXIT_SUCCESS : EXIT_FAILURE;
286 }