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