| 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 |
// The right-hand-side from the HB file is used instead of random vectors. |
| 12 |
// The initial guesses are all set to zero. |
| 13 |
// |
| 14 |
// NOTE: No preconditioner is used in this case. |
| 15 |
// |
| 16 |
#include "BelosConfigDefs.hpp" |
| 17 |
#include "BelosLinearProblem.hpp" |
| 18 |
#include "BelosEpetraAdapter.hpp" |
| 19 |
#include "BelosRCGSolMgr.hpp" |
| 20 |
#include "BelosEpetraUtils.h" |
| 21 |
#include "Trilinos_Util.h" |
| 22 |
#include "Epetra_CrsMatrix.h" |
| 23 |
#include "Epetra_Map.h" |
| 24 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 25 |
#include "Teuchos_ParameterList.hpp" |
| 26 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 27 |
|
| 28 |
int main(int argc, char *argv[]) { |
| 29 |
// |
| 30 |
#ifdef EPETRA_MPI |
| 31 |
// Initialize MPI |
| 32 |
MPI_Init(&argc,&argv); |
| 33 |
#endif |
| 34 |
// |
| 35 |
using Teuchos::ParameterList; |
| 36 |
using Teuchos::RCP; |
| 37 |
using Teuchos::rcp; |
| 38 |
|
| 39 |
bool success = false; |
| 40 |
bool verbose = false; |
| 41 |
try { |
| 42 |
// |
| 43 |
// Get test parameters from command-line processor |
| 44 |
// |
| 45 |
bool proc_verbose = false; |
| 46 |
int frequency = -1; // frequency of status test output. |
| 47 |
std::string filename("gr_30_30.hb"); // default input filename |
| 48 |
double tol = 1.0e-10; // relative residual tolerance |
| 49 |
int numBlocks = 30; // maximum number of blocks the solver can use for the Krylov subspace |
| 50 |
int recycleBlocks = 3; // maximum number of blocks the solver can use for the recycle space |
| 51 |
int numrhs = 1; // number of right-hand sides to solve for |
| 52 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 53 |
|
| 54 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 55 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 56 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 57 |
cmdp.setOption("filename",&filename,"Filename for test matrix."); |
| 58 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by the RCG solver."); |
| 59 |
cmdp.setOption("max-subspace",&numBlocks,"Maximum number of vectors in search space (not including recycle space)."); |
| 60 |
cmdp.setOption("recycle",&recycleBlocks,"Number of vectors in recycle space."); |
| 61 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 62 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 63 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
if (!verbose) |
| 67 |
frequency = -1; // reset frequency if test is not verbose |
| 68 |
// |
| 69 |
// Get the problem |
| 70 |
// |
| 71 |
int MyPID; |
| 72 |
RCP<Epetra_CrsMatrix> A; |
| 73 |
RCP<Epetra_MultiVector> B, X; |
| 74 |
int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID); |
| 75 |
if(return_val != 0) return return_val; |
| 76 |
proc_verbose = ( verbose && (MyPID==0) ); |
| 77 |
// |
| 78 |
// Solve using Belos |
| 79 |
// |
| 80 |
typedef double ST; |
| 81 |
typedef Epetra_Operator OP; |
| 82 |
typedef Epetra_MultiVector MV; |
| 83 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 84 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 85 |
// |
| 86 |
// *****Construct initial guess and right-hand sides ***** |
| 87 |
// |
| 88 |
if (numrhs != 1) { |
| 89 |
X = rcp( new Epetra_MultiVector( A->Map(), numrhs ) ); |
| 90 |
X->Random(); |
| 91 |
B = rcp( new Epetra_MultiVector( A->Map(), numrhs ) ); |
| 92 |
OPT::Apply( *A, *X, *B ); |
| 93 |
MVT::MvInit( *X, 0.0 ); |
| 94 |
} |
| 95 |
else { // initialize exact solution to be vector of ones |
| 96 |
MVT::MvInit( *X, 1.0 ); |
| 97 |
OPT::Apply( *A, *X, *B ); |
| 98 |
MVT::MvInit( *X, 0.0 ); |
| 99 |
} |
| 100 |
// |
| 101 |
// ********Other information used by block solver*********** |
| 102 |
// *****************(can be user specified)****************** |
| 103 |
// |
| 104 |
const int NumGlobalElements = B->GlobalLength(); |
| 105 |
if (maxiters == -1) |
| 106 |
maxiters = NumGlobalElements - 1; // maximum number of iterations to run |
| 107 |
// |
| 108 |
ParameterList belosList; |
| 109 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 110 |
belosList.set( "Num Blocks", numBlocks); // Maximum number of blocks in Krylov space |
| 111 |
belosList.set( "Num Recycled Blocks", recycleBlocks ); // Number of vectors in recycle space |
| 112 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 113 |
if (verbose) { |
| 114 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 115 |
Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails ); |
| 116 |
if (frequency > 0) |
| 117 |
belosList.set( "Output Frequency", frequency ); |
| 118 |
} |
| 119 |
else |
| 120 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 121 |
// |
| 122 |
// Construct an unpreconditioned linear problem instance. |
| 123 |
// |
| 124 |
Belos::LinearProblem<double,MV,OP> problem( A, X, B ); |
| 125 |
bool set = problem.setProblem(); |
| 126 |
if (set == false) { |
| 127 |
if (proc_verbose) |
| 128 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 129 |
return -1; |
| 130 |
} |
| 131 |
// |
| 132 |
// Create an iterative solver manager. |
| 133 |
// |
| 134 |
RCP< Belos::SolverManager<double,MV,OP> > newSolver |
| 135 |
= rcp( new Belos::RCGSolMgr<double,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) ); |
| 136 |
// |
| 137 |
// **********Print out information about problem******************* |
| 138 |
// |
| 139 |
if (proc_verbose) { |
| 140 |
std::cout << std::endl << std::endl; |
| 141 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 142 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 143 |
std::cout << "Max number of RCG iterations: " << maxiters << std::endl; |
| 144 |
std::cout << "Max number of vectors in Krylov space: " << numBlocks << std::endl; |
| 145 |
std::cout << "Number of vectors in recycle space: " << recycleBlocks << std::endl; |
| 146 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 147 |
std::cout << std::endl; |
| 148 |
} |
| 149 |
// |
| 150 |
// Perform solve |
| 151 |
// |
| 152 |
Belos::ReturnType ret = newSolver->solve(); |
| 153 |
// |
| 154 |
// Compute actual residuals. |
| 155 |
// |
| 156 |
bool badRes = false; |
| 157 |
std::vector<double> actual_resids( numrhs ); |
| 158 |
std::vector<double> rhs_norm( numrhs ); |
| 159 |
Epetra_MultiVector resid(A->Map(), numrhs); |
| 160 |
OPT::Apply( *A, *X, resid ); |
| 161 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 162 |
MVT::MvNorm( resid, actual_resids ); |
| 163 |
MVT::MvNorm( *B, rhs_norm ); |
| 164 |
if (proc_verbose) { |
| 165 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 166 |
for ( int i=0; i<numrhs; i++) { |
| 167 |
double actRes = actual_resids[i]/rhs_norm[i]; |
| 168 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 169 |
if (actRes > tol) badRes = true; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
success = ret==Belos::Converged && !badRes; |
| 174 |
if (success) { |
| 175 |
if (proc_verbose) |
| 176 |
std::cout << std::endl << "End Result: TEST PASSED" << std::endl; |
| 177 |
} else { |
| 178 |
if (proc_verbose) |
| 179 |
std::cout << std::endl << "End Result: TEST FAILED" << std::endl; |
| 180 |
} |
| 181 |
} |
| 182 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 183 |
|
| 184 |
#ifdef EPETRA_MPI |
| 185 |
MPI_Finalize(); |
| 186 |
#endif |
| 187 |
|
| 188 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 189 |
} // end test_rcg_hb.cpp |