| 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 problem is being used instead of multiple |
| 12 |
// random right-hand-sides. 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 "BelosBlockGmresSolMgr.hpp" |
| 20 |
#include "BelosPseudoBlockGmresSolMgr.hpp" |
| 21 |
#include "BelosEpetraUtils.h" |
| 22 |
#include "Epetra_CrsMatrix.h" |
| 23 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 24 |
#include "Teuchos_ParameterList.hpp" |
| 25 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 26 |
#include "Epetra_Map.h" |
| 27 |
|
| 28 |
int main(int argc, char *argv[]) { |
| 29 |
// |
| 30 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 31 |
// |
| 32 |
typedef double ST; |
| 33 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 34 |
typedef SCT::magnitudeType MT; |
| 35 |
typedef Epetra_MultiVector MV; |
| 36 |
typedef Epetra_Operator OP; |
| 37 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 38 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 39 |
|
| 40 |
using Teuchos::ParameterList; |
| 41 |
using Teuchos::RCP; |
| 42 |
using Teuchos::rcp; |
| 43 |
|
| 44 |
bool success = false; |
| 45 |
bool verbose = false; |
| 46 |
try { |
| 47 |
bool badRes = false; |
| 48 |
bool proc_verbose = false; |
| 49 |
bool pseudo = false; // use pseudo block Gmres to solve this linear system. |
| 50 |
int frequency = -1; |
| 51 |
int blocksize = 1; |
| 52 |
int numrhs = 1; |
| 53 |
int maxrestarts = 15; // number of restarts allowed |
| 54 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 55 |
std::string filename("orsirr1.hb"); |
| 56 |
std::string ortho("DGKS"); |
| 57 |
MT tol = 1.0e-5; // relative residual tolerance |
| 58 |
|
| 59 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 60 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 61 |
cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block Gmres to solve the linear systems."); |
| 62 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 63 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 64 |
cmdp.setOption("ortho",&ortho,"Orthogonalization routine used by Gmres solver."); |
| 65 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by Gmres solver."); |
| 66 |
cmdp.setOption("max-restarts",&maxrestarts,"Maximum number of restarts allowed for Gmres solver."); |
| 67 |
cmdp.setOption("blocksize",&blocksize,"Block size used by Gmres."); |
| 68 |
cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 69 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 70 |
return -1; |
| 71 |
} |
| 72 |
if (!verbose) |
| 73 |
frequency = -1; // reset frequency if test is not verbose |
| 74 |
// |
| 75 |
// Get the problem |
| 76 |
// |
| 77 |
int MyPID; |
| 78 |
RCP<Epetra_CrsMatrix> A; |
| 79 |
RCP<Epetra_MultiVector> B, X; |
| 80 |
int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,&B,&X,&MyPID); |
| 81 |
if(return_val != 0) return return_val; |
| 82 |
const Epetra_Map &Map = A->RowMap(); |
| 83 |
proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 84 |
// |
| 85 |
// ********Other information used by block solver*********** |
| 86 |
// *****************(can be user specified)****************** |
| 87 |
// |
| 88 |
const int NumGlobalElements = B->GlobalLength(); |
| 89 |
if (maxiters == -1) |
| 90 |
maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run |
| 91 |
// |
| 92 |
ParameterList belosList; |
| 93 |
belosList.set( "Num Blocks", maxiters ); // Maximum number of blocks in Krylov factorization |
| 94 |
belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver |
| 95 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 96 |
belosList.set( "Maximum Restarts", maxrestarts ); // Maximum number of restarts allowed |
| 97 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 98 |
belosList.set( "Orthogonalization", ortho ); // Orthogonalization routine |
| 99 |
if (verbose) { |
| 100 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 101 |
Belos::TimingDetails + Belos::StatusTestDetails ); |
| 102 |
if (frequency > 0) |
| 103 |
belosList.set( "Output Frequency", frequency ); |
| 104 |
} |
| 105 |
else |
| 106 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 107 |
// |
| 108 |
// |
| 109 |
// Construct an unpreconditioned linear problem instance. |
| 110 |
// |
| 111 |
Belos::LinearProblem<double,MV,OP> problem( A, X, B ); |
| 112 |
bool set = problem.setProblem(); |
| 113 |
if (set == false) { |
| 114 |
if (proc_verbose) |
| 115 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 116 |
return -1; |
| 117 |
} |
| 118 |
// |
| 119 |
// ******************************************************************* |
| 120 |
// *************Start the block Gmres iteration************************* |
| 121 |
// ******************************************************************* |
| 122 |
// |
| 123 |
Teuchos::RCP< Belos::SolverManager<double,MV,OP> > solver; |
| 124 |
if (pseudo) |
| 125 |
solver = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) ); |
| 126 |
else |
| 127 |
solver = Teuchos::rcp( new Belos::BlockGmresSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) ); |
| 128 |
// |
| 129 |
// Perform solve |
| 130 |
// |
| 131 |
Belos::ReturnType ret = solver->solve(); |
| 132 |
|
| 133 |
if (ret!=Belos::Converged) { |
| 134 |
if (proc_verbose) |
| 135 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 136 |
return -1; |
| 137 |
} |
| 138 |
// |
| 139 |
// |
| 140 |
// Get the number of iterations for this solve. |
| 141 |
// |
| 142 |
int numIters = solver->getNumIters(); |
| 143 |
std::cout << "Number of iterations performed for this solve: " << numIters << std::endl; |
| 144 |
// |
| 145 |
// Compute actual residuals. |
| 146 |
// |
| 147 |
std::vector<double> actual_resids( numrhs ); |
| 148 |
std::vector<double> rhs_norm( numrhs ); |
| 149 |
Epetra_MultiVector resid(Map, numrhs); |
| 150 |
OPT::Apply( *A, *X, resid ); |
| 151 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 152 |
MVT::MvNorm( resid, actual_resids ); |
| 153 |
MVT::MvNorm( *B, rhs_norm ); |
| 154 |
if (proc_verbose) { |
| 155 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 156 |
for ( int i=0; i<numrhs; i++) { |
| 157 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 158 |
} |
| 159 |
} |
| 160 |
// |
| 161 |
// ----------------------------------------------------------------- |
| 162 |
// Resolve the first problem by just resetting the solver manager. |
| 163 |
// ----------------------------------------------------------------- |
| 164 |
X->PutScalar( 0.0 ); |
| 165 |
solver->reset( Belos::Problem ); |
| 166 |
// |
| 167 |
// Perform solve (again) |
| 168 |
// |
| 169 |
ret = solver->solve(); |
| 170 |
// |
| 171 |
// Get the number of iterations for this solve. |
| 172 |
// |
| 173 |
numIters = solver->getNumIters(); |
| 174 |
std::cout << "Number of iterations performed for this solve (manager reset): " << numIters << std::endl; |
| 175 |
|
| 176 |
if (ret!=Belos::Converged) { |
| 177 |
if (proc_verbose) |
| 178 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 179 |
return -1; |
| 180 |
} |
| 181 |
// |
| 182 |
// Compute actual residuals. |
| 183 |
// |
| 184 |
std::vector<double> actual_resids2( numrhs ); |
| 185 |
Epetra_MultiVector resid2(Map, numrhs); |
| 186 |
OPT::Apply( *A, *X, resid2 ); |
| 187 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 188 |
MVT::MvNorm( resid2, actual_resids ); |
| 189 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 190 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 191 |
MVT::MvNorm( *B, rhs_norm ); |
| 192 |
if (proc_verbose) { |
| 193 |
std::cout<< "---------- Actual Residuals (manager reset) ----------"<<std::endl<<std::endl; |
| 194 |
for ( int i=0; i<numrhs; i++) { |
| 195 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 196 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 197 |
badRes = true; |
| 198 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
// |
| 203 |
// ----------------------------------------------------------------- |
| 204 |
// Resolve the first problem by resetting the linear problem. |
| 205 |
// ----------------------------------------------------------------- |
| 206 |
X->PutScalar( 0.0 ); |
| 207 |
set = problem.setProblem(); |
| 208 |
if (set == false) { |
| 209 |
if (proc_verbose) |
| 210 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 211 |
return -1; |
| 212 |
} |
| 213 |
solver->setProblem( rcp(&problem,false) ); |
| 214 |
// |
| 215 |
// Perform solve (again) |
| 216 |
// |
| 217 |
ret = solver->solve(); |
| 218 |
// |
| 219 |
// Get the number of iterations for this solve. |
| 220 |
// |
| 221 |
numIters = solver->getNumIters(); |
| 222 |
std::cout << "Number of iterations performed for this solve (manager setProblem()): " << numIters << std::endl; |
| 223 |
|
| 224 |
if (ret!=Belos::Converged) { |
| 225 |
if (proc_verbose) |
| 226 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 227 |
return -1; |
| 228 |
} |
| 229 |
// |
| 230 |
// Compute actual residuals. |
| 231 |
// |
| 232 |
OPT::Apply( *A, *X, resid2 ); |
| 233 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 234 |
MVT::MvNorm( resid2, actual_resids ); |
| 235 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 236 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 237 |
MVT::MvNorm( *B, rhs_norm ); |
| 238 |
if (proc_verbose) { |
| 239 |
std::cout<< "---------- Actual Residuals (manager setProblem()) ----------"<<std::endl<<std::endl; |
| 240 |
for ( int i=0; i<numrhs; i++) { |
| 241 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 242 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 243 |
badRes = true; |
| 244 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
// |
| 249 |
// ---------------------------------------------------------------------------------- |
| 250 |
// Resolve the first problem by resetting the solver manager and changing the labels. |
| 251 |
// ---------------------------------------------------------------------------------- |
| 252 |
ParameterList belosList2; |
| 253 |
belosList2.set( "Timer Label", "Belos Resolve w/ New Label" ); // Change timer labels. |
| 254 |
solver->setParameters( Teuchos::rcp( &belosList2, false ) ); |
| 255 |
|
| 256 |
problem.setLabel( "Belos Resolve w/ New Label" ); |
| 257 |
X->PutScalar( 0.0 ); |
| 258 |
solver->reset( Belos::Problem ); |
| 259 |
// |
| 260 |
// Perform solve (again) |
| 261 |
// |
| 262 |
ret = solver->solve(); |
| 263 |
// |
| 264 |
// Get the number of iterations for this solve. |
| 265 |
// |
| 266 |
numIters = solver->getNumIters(); |
| 267 |
std::cout << "Number of iterations performed for this solve (label reset): " << numIters << std::endl; |
| 268 |
|
| 269 |
if (ret!=Belos::Converged) { |
| 270 |
if (proc_verbose) |
| 271 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 272 |
return -1; |
| 273 |
} |
| 274 |
// |
| 275 |
// Compute actual residuals. |
| 276 |
// |
| 277 |
OPT::Apply( *A, *X, resid2 ); |
| 278 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 279 |
MVT::MvNorm( resid2, actual_resids ); |
| 280 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 281 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 282 |
MVT::MvNorm( *B, rhs_norm ); |
| 283 |
if (proc_verbose) { |
| 284 |
std::cout<< "---------- Actual Residuals (label reset) ----------"<<std::endl<<std::endl; |
| 285 |
for ( int i=0; i<numrhs; i++) { |
| 286 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 287 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 288 |
badRes = true; |
| 289 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
// |
| 294 |
// ------------------------------------------------------------- |
| 295 |
// Construct a second unpreconditioned linear problem instance. |
| 296 |
// ------------------------------------------------------------- |
| 297 |
// |
| 298 |
RCP<Epetra_MultiVector> X2 = MVT::Clone(*X, numrhs); |
| 299 |
MVT::MvInit( *X2, 0.0 ); |
| 300 |
Belos::LinearProblem<double,MV,OP> problem2( A, X2, B ); |
| 301 |
problem2.setLabel("Belos Resolve"); |
| 302 |
set = problem2.setProblem(); |
| 303 |
if (set == false) { |
| 304 |
if (proc_verbose) |
| 305 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 306 |
return -1; |
| 307 |
} |
| 308 |
// |
| 309 |
// ******************************************************************* |
| 310 |
// *************Start the block Gmres iteration************************* |
| 311 |
// ******************************************************************* |
| 312 |
// |
| 313 |
// Create the solver without either the problem or parameter list. |
| 314 |
if (pseudo) |
| 315 |
solver = Teuchos::rcp( new Belos::PseudoBlockGmresSolMgr<double,MV,OP>() ); |
| 316 |
else |
| 317 |
solver = Teuchos::rcp( new Belos::BlockGmresSolMgr<double,MV,OP>() ); |
| 318 |
// |
| 319 |
// Set the problem after the solver construction. |
| 320 |
solver->setProblem( rcp( &problem2, false ) ); |
| 321 |
|
| 322 |
// Get the valid list of parameters from the solver and print it. |
| 323 |
RCP<const Teuchos::ParameterList> validList = solver->getValidParameters(); |
| 324 |
if (proc_verbose) { |
| 325 |
if (pseudo) |
| 326 |
std::cout << std::endl << "Valid parameters from the pseudo-block Gmres solver manager:" << std::endl; |
| 327 |
else |
| 328 |
std::cout << std::endl << "Valid parameters from the block Gmres solver manager:" << std::endl; |
| 329 |
|
| 330 |
std::cout << *validList << std::endl; |
| 331 |
} |
| 332 |
|
| 333 |
// Set the parameter list after the solver construction. |
| 334 |
belosList.set( "Timer Label", "Belos Resolve" ); // Set timer label to discern between the two solvers. |
| 335 |
solver->setParameters( rcp( &belosList, false ) ); |
| 336 |
// |
| 337 |
// Perform solve |
| 338 |
// |
| 339 |
ret = solver->solve(); |
| 340 |
// |
| 341 |
// Get the number of iterations for this solve. |
| 342 |
// |
| 343 |
numIters = solver->getNumIters(); |
| 344 |
std::cout << "Number of iterations performed for this solve (new solver): " << numIters << std::endl; |
| 345 |
// |
| 346 |
// Compute actual residuals. |
| 347 |
// |
| 348 |
OPT::Apply( *A, *X2, resid2 ); |
| 349 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 350 |
MVT::MvNorm( resid2, actual_resids ); |
| 351 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 352 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 353 |
MVT::MvNorm( *B, rhs_norm ); |
| 354 |
if (proc_verbose) { |
| 355 |
std::cout<< "---------- Actual Residuals (new solver) ----------"<<std::endl<<std::endl; |
| 356 |
for ( int i=0; i<numrhs; i++) { |
| 357 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 358 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 359 |
badRes = true; |
| 360 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
// |
| 365 |
// **********Print out information about problem******************* |
| 366 |
// |
| 367 |
if (proc_verbose) { |
| 368 |
std::cout << std::endl << std::endl; |
| 369 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 370 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 371 |
std::cout << "Block size used by solver: " << blocksize << std::endl; |
| 372 |
std::cout << "Max number of Gmres iterations: " << maxiters << std::endl; |
| 373 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 374 |
std::cout << std::endl; |
| 375 |
} |
| 376 |
|
| 377 |
success = ret==Belos::Converged && !badRes; |
| 378 |
|
| 379 |
if (success) { |
| 380 |
if (proc_verbose) |
| 381 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 382 |
} else { |
| 383 |
if (proc_verbose) |
| 384 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 385 |
} |
| 386 |
} |
| 387 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 388 |
|
| 389 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 390 |
} // end test_resolve_gmres_hb.cpp |