| 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 |
// This test is for testing the deflation in the pseudo-block Gmres solver. |
| 15 |
// One set of linear systems is solved and then augmented with additional |
| 16 |
// linear systems and resolved. The already solved linear systems should be |
| 17 |
// deflated immediately, leaving only the augmented systems to be solved. |
| 18 |
// |
| 19 |
// |
| 20 |
#include "BelosConfigDefs.hpp" |
| 21 |
#include "BelosLinearProblem.hpp" |
| 22 |
#include "BelosEpetraAdapter.hpp" |
| 23 |
#include "BelosPseudoBlockGmresSolMgr.hpp" |
| 24 |
#include "BelosEpetraUtils.h" |
| 25 |
#include "Epetra_CrsMatrix.h" |
| 26 |
#include "Epetra_Map.h" |
| 27 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 28 |
#include "Teuchos_ParameterList.hpp" |
| 29 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 30 |
|
| 31 |
int main(int argc, char *argv[]) { |
| 32 |
// |
| 33 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 34 |
// |
| 35 |
typedef double ST; |
| 36 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 37 |
typedef SCT::magnitudeType MT; |
| 38 |
typedef Epetra_MultiVector MV; |
| 39 |
typedef Epetra_Operator OP; |
| 40 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 41 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 42 |
|
| 43 |
using Teuchos::ParameterList; |
| 44 |
using Teuchos::RCP; |
| 45 |
using Teuchos::rcp; |
| 46 |
|
| 47 |
bool verbose = false; |
| 48 |
bool success = true; |
| 49 |
try { |
| 50 |
bool proc_verbose = false; |
| 51 |
int frequency = -1; // how often residuals are printed by solver |
| 52 |
int init_numrhs = 5; // how many right-hand sides get solved first |
| 53 |
int aug_numrhs = 10; // how many right-hand sides are augmented to the first group |
| 54 |
int maxrestarts = 15; // number of restarts allowed |
| 55 |
int length = 100; |
| 56 |
int init_blocksize = 5;// blocksize used for the initial pseudo-block GMRES solve |
| 57 |
int aug_blocksize = 3; // blocksize used for the augmented pseudo-block GMRES solve |
| 58 |
int maxiters = -1; // maximum iterations allowed |
| 59 |
std::string filename("orsirr1.hb"); |
| 60 |
MT tol = 1.0e-5; // relative residual tolerance |
| 61 |
MT aug_tol = 1.0e-5; // relative residual tolerance for augmented system |
| 62 |
|
| 63 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 64 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 65 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 66 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 67 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver."); |
| 68 |
cmdp.setOption("aug-tol",&aug_tol,"Relative residual tolerance used by GMRES solver for augmented systems."); |
| 69 |
cmdp.setOption("init-num-rhs",&init_numrhs,"Number of right-hand sides to be initially solved for."); |
| 70 |
cmdp.setOption("aug-num-rhs",&aug_numrhs,"Number of right-hand sides augmenting the initial solve."); |
| 71 |
cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for GMRES solver."); |
| 72 |
cmdp.setOption("block-size",&init_blocksize,"Block size used by GMRES for the initial solve."); |
| 73 |
cmdp.setOption("aug-block-size",&aug_blocksize,"Block size used by GMRES for the augmented solve."); |
| 74 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 75 |
cmdp.setOption("subspace-size",&length,"Dimension of Krylov subspace used by GMRES."); |
| 76 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 77 |
return -1; |
| 78 |
} |
| 79 |
if (!verbose) |
| 80 |
frequency = -1; // reset frequency if test is not verbose |
| 81 |
// |
| 82 |
// Get the problem |
| 83 |
// |
| 84 |
int MyPID; |
| 85 |
RCP<Epetra_CrsMatrix> A; |
| 86 |
int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,NULL,NULL,&MyPID); |
| 87 |
const Epetra_Map &Map = A->RowMap(); |
| 88 |
if(return_val != 0) return return_val; |
| 89 |
proc_verbose = verbose && (MyPID==0); /* Only print on zero processor */ |
| 90 |
// |
| 91 |
// ********Other information used by block solver*********** |
| 92 |
// *****************(can be user specified)****************** |
| 93 |
// |
| 94 |
const int NumGlobalElements = Map.NumGlobalElements(); |
| 95 |
if (maxiters == -1) |
| 96 |
maxiters = NumGlobalElements - 1; // maximum number of iterations to run |
| 97 |
// |
| 98 |
ParameterList belosList; |
| 99 |
belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization |
| 100 |
belosList.set( "Block Size", init_blocksize ); // Blocksize to be used by iterative solver |
| 101 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 102 |
belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed |
| 103 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 104 |
belosList.set( "Deflation Quorum", init_blocksize ); // Number of converged linear systems before deflation |
| 105 |
belosList.set( "Timer Label", "Belos Init" ); // Label used by the timers in this solver |
| 106 |
if (verbose) { |
| 107 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 108 |
Belos::TimingDetails + Belos::StatusTestDetails ); |
| 109 |
if (frequency > 0) |
| 110 |
belosList.set( "Output Frequency", frequency ); |
| 111 |
} |
| 112 |
else |
| 113 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 114 |
// |
| 115 |
// *****Construct solution std::vector and random right-hand-sides ***** |
| 116 |
// |
| 117 |
RCP<Epetra_MultiVector> initX = rcp( new Epetra_MultiVector(Map, init_numrhs) ); |
| 118 |
RCP<Epetra_MultiVector> initB = rcp( new Epetra_MultiVector(Map, init_numrhs) ); |
| 119 |
initX->Random(); |
| 120 |
OPT::Apply( *A, *initX, *initB ); |
| 121 |
initX->PutScalar( 0.0 ); |
| 122 |
Belos::LinearProblem<double,MV,OP> initProblem( A, initX, initB ); |
| 123 |
initProblem.setLabel("Belos Init"); |
| 124 |
|
| 125 |
bool set = initProblem.setProblem(); |
| 126 |
if (set == false) { |
| 127 |
if (proc_verbose) |
| 128 |
std::cout << std::endl << "ERROR: Initial Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 129 |
return -1; |
| 130 |
} |
| 131 |
// |
| 132 |
// ******************************************************************* |
| 133 |
// *********************Perform initial solve************************* |
| 134 |
// ******************************************************************* |
| 135 |
// |
| 136 |
Teuchos::RCP< Belos::SolverManager<double,MV,OP> > initSolver |
| 137 |
= Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>( rcp(&initProblem,false), rcp(&belosList,false) ) ); |
| 138 |
// |
| 139 |
// Perform solve |
| 140 |
// |
| 141 |
Belos::ReturnType ret = initSolver->solve(); |
| 142 |
|
| 143 |
// |
| 144 |
// Compute actual residuals. |
| 145 |
// |
| 146 |
bool badRes = false; |
| 147 |
std::vector<double> actual_resids( init_numrhs ); |
| 148 |
std::vector<double> rhs_norm( init_numrhs ); |
| 149 |
Epetra_MultiVector initR( Map, init_numrhs ); |
| 150 |
OPT::Apply( *A, *initX, initR ); |
| 151 |
MVT::MvAddMv( -1.0, initR, 1.0, *initB, initR ); |
| 152 |
MVT::MvNorm( initR, actual_resids ); |
| 153 |
MVT::MvNorm( *initB, rhs_norm ); |
| 154 |
if (proc_verbose) { |
| 155 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 156 |
for (int i=0; i<init_numrhs; i++) { |
| 157 |
double actRes = actual_resids[i]/rhs_norm[i]; |
| 158 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 159 |
if (actRes > tol) badRes = true; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (ret != Belos::Converged || badRes==true) { |
| 164 |
if (proc_verbose) |
| 165 |
std::cout << std::endl << "ERROR: Initial solve did not converge to solution!" << std::endl; |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
// |
| 170 |
// ***************Construct augmented linear system**************** |
| 171 |
// |
| 172 |
RCP<Epetra_MultiVector> augX = rcp( new Epetra_MultiVector(Map, init_numrhs+aug_numrhs) ); |
| 173 |
RCP<Epetra_MultiVector> augB = rcp( new Epetra_MultiVector(Map, init_numrhs+aug_numrhs) ); |
| 174 |
if (aug_numrhs) { |
| 175 |
augX->Random(); |
| 176 |
OPT::Apply( *A, *augX, *augB ); |
| 177 |
augX->PutScalar( 0.0 ); |
| 178 |
} |
| 179 |
|
| 180 |
// Copy previous linear system into |
| 181 |
RCP<Epetra_MultiVector> tmpX = rcp( new Epetra_MultiVector( Epetra_DataAccess::View, *augX, 0, init_numrhs ) ); |
| 182 |
RCP<Epetra_MultiVector> tmpB = rcp( new Epetra_MultiVector( Epetra_DataAccess::View, *augB, 0, init_numrhs ) ); |
| 183 |
tmpX->Scale( 1.0, *initX ); |
| 184 |
tmpB->Scale( 1.0, *initB ); |
| 185 |
|
| 186 |
Belos::LinearProblem<double,MV,OP> augProblem( A, augX, augB ); |
| 187 |
augProblem.setLabel("Belos Aug"); |
| 188 |
|
| 189 |
set = augProblem.setProblem(); |
| 190 |
if (set == false) { |
| 191 |
if (proc_verbose) |
| 192 |
std::cout << std::endl << "ERROR: Augmented Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 193 |
return -1; |
| 194 |
} |
| 195 |
// |
| 196 |
// ******************************************************************* |
| 197 |
// *******************Perform augmented solve************************* |
| 198 |
// ******************************************************************* |
| 199 |
// |
| 200 |
belosList.set( "Block Size", aug_blocksize ); // Blocksize to be used by iterative solver |
| 201 |
belosList.set( "Convergence Tolerance", aug_tol ); // Relative convergence tolerance requested |
| 202 |
belosList.set( "Deflation Quorum", aug_blocksize ); // Number of converged linear systems before deflation |
| 203 |
belosList.set( "Timer Label", "Belos Aug" ); // Label used by the timers in this solver |
| 204 |
belosList.set( "Implicit Residual Scaling", "Norm of RHS" ); // Implicit residual scaling for convergence |
| 205 |
belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Explicit residual scaling for convergence |
| 206 |
Teuchos::RCP< Belos::SolverManager<double,MV,OP> > augSolver |
| 207 |
= Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>( rcp(&augProblem,false), rcp(&belosList,false) ) ); |
| 208 |
// |
| 209 |
// Perform solve |
| 210 |
// |
| 211 |
ret = augSolver->solve(); |
| 212 |
|
| 213 |
if (ret != Belos::Converged) { |
| 214 |
if (proc_verbose) |
| 215 |
std::cout << std::endl << "ERROR: Augmented solver did not converge to solution!" << std::endl; |
| 216 |
return -1; |
| 217 |
} |
| 218 |
// |
| 219 |
// **********Print out information about problem******************* |
| 220 |
// |
| 221 |
if (proc_verbose) { |
| 222 |
std::cout << std::endl << std::endl; |
| 223 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 224 |
std::cout << "Number of initial right-hand sides: " << init_numrhs << std::endl; |
| 225 |
std::cout << "Number of augmented right-hand sides: " << aug_numrhs << std::endl; |
| 226 |
std::cout << "Number of restarts allowed: " << maxrestarts << std::endl; |
| 227 |
std::cout << "Length of block Arnoldi factorization: " << length <<std::endl; |
| 228 |
std::cout << "Max number of Gmres iterations: " << maxiters << std::endl; |
| 229 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 230 |
if (aug_tol != tol) |
| 231 |
std::cout << "Relative residual tolerance for augmented systems: " << aug_tol << std::endl; |
| 232 |
std::cout << std::endl; |
| 233 |
} |
| 234 |
// |
| 235 |
// Compute actual residuals. |
| 236 |
// |
| 237 |
badRes = false; |
| 238 |
int total_numrhs = init_numrhs + aug_numrhs; |
| 239 |
actual_resids.resize( total_numrhs ); |
| 240 |
rhs_norm.resize( total_numrhs ); |
| 241 |
Epetra_MultiVector augR( Map, total_numrhs ); |
| 242 |
OPT::Apply( *A, *augX, augR ); |
| 243 |
MVT::MvAddMv( -1.0, augR, 1.0, *augB, augR ); |
| 244 |
MVT::MvNorm( augR, actual_resids ); |
| 245 |
MVT::MvNorm( *augB, rhs_norm ); |
| 246 |
if (proc_verbose) { |
| 247 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 248 |
for ( int i=0; i<total_numrhs; i++) { |
| 249 |
double actRes = actual_resids[i]/rhs_norm[i]; |
| 250 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 251 |
if (actRes > tol ) badRes = true; |
| 252 |
} |
| 253 |
} |
| 254 |
if (ret!=Belos::Converged || badRes==true) { |
| 255 |
success = false; |
| 256 |
if (proc_verbose) |
| 257 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 258 |
} else { |
| 259 |
success = true; |
| 260 |
if (proc_verbose) |
| 261 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 262 |
} |
| 263 |
} |
| 264 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success); |
| 265 |
|
| 266 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 267 |
} // end test_pseudo_gmres_hb.cpp |