| 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. |
| 14 |
// |
| 15 |
#include "BelosConfigDefs.hpp" |
| 16 |
#include "BelosLinearProblem.hpp" |
| 17 |
#include "BelosEpetraAdapter.hpp" |
| 18 |
#include "BelosBiCGStabSolMgr.hpp" |
| 19 |
|
| 20 |
#include "EpetraExt_readEpetraLinearSystem.h" |
| 21 |
#include "Epetra_Map.h" |
| 22 |
#ifdef EPETRA_MPI |
| 23 |
# include "Epetra_MpiComm.h" |
| 24 |
#else |
| 25 |
# include "Epetra_SerialComm.h" |
| 26 |
#endif |
| 27 |
#include "Epetra_CrsMatrix.h" |
| 28 |
|
| 29 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 30 |
#include "Teuchos_ParameterList.hpp" |
| 31 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 32 |
|
| 33 |
int main (int argc, char *argv[]) |
| 34 |
{ |
| 35 |
using Teuchos::ParameterList; |
| 36 |
using Teuchos::RCP; |
| 37 |
using Teuchos::rcp; |
| 38 |
using std::cout; |
| 39 |
using std::endl; |
| 40 |
typedef double ST; |
| 41 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 42 |
typedef SCT::magnitudeType MT; |
| 43 |
typedef Epetra_MultiVector MV; |
| 44 |
typedef Epetra_Operator OP; |
| 45 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 46 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 47 |
|
| 48 |
int MyPID = 0; |
| 49 |
#ifdef EPETRA_MPI |
| 50 |
MPI_Init (&argc, &argv); |
| 51 |
Epetra_MpiComm Comm (MPI_COMM_WORLD); |
| 52 |
MyPID = Comm.MyPID (); |
| 53 |
#else |
| 54 |
Epetra_SerialComm Comm; |
| 55 |
#endif |
| 56 |
|
| 57 |
bool verbose = false; |
| 58 |
bool success = true; |
| 59 |
|
| 60 |
// This "try" relates to TEUCHOS_STANDARD_CATCH_STATEMENTS near the |
| 61 |
// bottom of main(). That macro has the corresponding "catch". |
| 62 |
try { |
| 63 |
bool proc_verbose = false; |
| 64 |
int frequency = -1; // frequency of status test output. |
| 65 |
int numrhs = 1; // number of right-hand sides to solve for |
| 66 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 67 |
std::string filename ("cage4.hb"); |
| 68 |
MT tol = 1.0e-5; // relative residual tolerance |
| 69 |
|
| 70 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 71 |
cmdp.setOption ("verbose", "quiet", &verbose, "Whether to print messages " |
| 72 |
"and results."); |
| 73 |
cmdp.setOption ("frequency", &frequency, "Frequency of solver output " |
| 74 |
"(1 means every iteration; -1 means never)."); |
| 75 |
cmdp.setOption ("filename", &filename, "Test matrix filename. " |
| 76 |
"Allowed file extensions: *.hb, *.mtx, *.triU, *.triS"); |
| 77 |
cmdp.setOption ("tol", &tol, "Relative residual tolerance for solver."); |
| 78 |
cmdp.setOption ("num-rhs", &numrhs, "Number of right-hand sides to solve."); |
| 79 |
cmdp.setOption ("max-iters", &maxiters, "Maximum number of iterations per " |
| 80 |
"linear system (-1 = adapted to problem/block size)."); |
| 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 |
// |
| 89 |
// *************Get the problem********************* |
| 90 |
// |
| 91 |
RCP<Epetra_Map> Map; |
| 92 |
RCP<Epetra_CrsMatrix> A; |
| 93 |
RCP<Epetra_MultiVector> B, X; |
| 94 |
RCP<Epetra_Vector> vecB, vecX; |
| 95 |
EpetraExt::readEpetraLinearSystem (filename, Comm, &A, &Map, &vecX, &vecB); |
| 96 |
A->OptimizeStorage (); |
| 97 |
proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 98 |
|
| 99 |
// Check to see if the number of right-hand sides is the same as requested. |
| 100 |
if (numrhs > 1) { |
| 101 |
X = rcp (new Epetra_MultiVector (*Map, numrhs)); |
| 102 |
B = rcp (new Epetra_MultiVector (*Map, numrhs)); |
| 103 |
X->Random (); |
| 104 |
OPT::Apply (*A, *X, *B); |
| 105 |
X->PutScalar (0.0); |
| 106 |
} |
| 107 |
else { |
| 108 |
X = Teuchos::rcp_implicit_cast<Epetra_MultiVector> (vecX); |
| 109 |
B = Teuchos::rcp_implicit_cast<Epetra_MultiVector> (vecB); |
| 110 |
} |
| 111 |
|
| 112 |
// |
| 113 |
// Create parameter list for the Belos solver |
| 114 |
// |
| 115 |
const int NumGlobalElements = B->GlobalLength (); |
| 116 |
if (maxiters == -1) { |
| 117 |
maxiters = NumGlobalElements - 1; // maximum number of iterations to run |
| 118 |
} |
| 119 |
RCP<ParameterList> belosList (new ParameterList ("Belos")); |
| 120 |
belosList->set ("Maximum Iterations", maxiters); |
| 121 |
belosList->set ("Convergence Tolerance", tol); |
| 122 |
if (verbose) { |
| 123 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings + |
| 124 |
Belos::TimingDetails + Belos::StatusTestDetails); |
| 125 |
if (frequency > 0) { |
| 126 |
belosList->set ("Output Frequency", frequency); |
| 127 |
} |
| 128 |
} |
| 129 |
else { |
| 130 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings + |
| 131 |
Belos::FinalSummary); |
| 132 |
} |
| 133 |
// |
| 134 |
// Construct a preconditioned linear problem |
| 135 |
// |
| 136 |
RCP<Belos::LinearProblem<double,MV,OP> > problem |
| 137 |
= rcp (new Belos::LinearProblem<double,MV,OP> (A, X, B)); |
| 138 |
bool set = problem->setProblem (); |
| 139 |
if (! set) { |
| 140 |
if (proc_verbose) { |
| 141 |
cout << endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << endl; |
| 142 |
} |
| 143 |
return -1; |
| 144 |
} |
| 145 |
|
| 146 |
// Create a Belos solver. |
| 147 |
RCP<Belos::SolverManager<double,MV,OP> > solver |
| 148 |
= rcp (new Belos::BiCGStabSolMgr<double,MV,OP> (problem, belosList)); |
| 149 |
|
| 150 |
if (proc_verbose) { |
| 151 |
cout << endl << endl; |
| 152 |
cout << "Dimension of matrix: " << NumGlobalElements << endl; |
| 153 |
cout << "Number of right-hand sides: " << numrhs << endl; |
| 154 |
cout << "Max number of CG iterations: " << maxiters << endl; |
| 155 |
cout << "Relative residual tolerance: " << tol << endl; |
| 156 |
cout << endl; |
| 157 |
} |
| 158 |
|
| 159 |
// Ask Belos to solve the linear system. |
| 160 |
Belos::ReturnType ret = solver->solve(); |
| 161 |
|
| 162 |
// |
| 163 |
// Compute actual residuals. |
| 164 |
// |
| 165 |
bool badRes = false; |
| 166 |
std::vector<double> actual_resids (numrhs); |
| 167 |
std::vector<double> rhs_norm (numrhs); |
| 168 |
Epetra_MultiVector resid (*Map, numrhs); |
| 169 |
OPT::Apply (*A, *X, resid); |
| 170 |
MVT::MvAddMv (-1.0, resid, 1.0, *B, resid); |
| 171 |
MVT::MvNorm (resid, actual_resids); |
| 172 |
MVT::MvNorm (*B, rhs_norm); |
| 173 |
if (proc_verbose) { |
| 174 |
cout << "---------- Actual Residuals (normalized) ----------" << endl << endl; |
| 175 |
for (int i = 0; i < numrhs; ++i) { |
| 176 |
double actRes = actual_resids[i] / rhs_norm[i]; |
| 177 |
cout <<"Problem " << i << " : \t" << actRes << endl; |
| 178 |
if (actRes > tol) { |
| 179 |
badRes = true; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (ret != Belos::Converged || badRes) { |
| 185 |
success = false; |
| 186 |
if (proc_verbose) { |
| 187 |
cout << endl << "ERROR: Belos did not converge!" << endl |
| 188 |
<< "End Result: TEST FAILED" << endl; |
| 189 |
} |
| 190 |
} else { |
| 191 |
success = true; |
| 192 |
if (proc_verbose) { |
| 193 |
cout << endl << "SUCCESS: Belos converged!" << endl |
| 194 |
<< "End Result: TEST PASSED" << endl; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 199 |
|
| 200 |
#ifdef EPETRA_MPI |
| 201 |
MPI_Finalize(); |
| 202 |
#endif |
| 203 |
|
| 204 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 205 |
} |