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