| 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 |
|
| 21 |
// Tpetra |
| 22 |
#include <Tpetra_Map.hpp> |
| 23 |
#include <Tpetra_Core.hpp> |
| 24 |
#include <Tpetra_MultiVector.hpp> |
| 25 |
#include <Tpetra_CrsMatrix.hpp> |
| 26 |
|
| 27 |
// Belos |
| 28 |
#include "BelosConfigDefs.hpp" |
| 29 |
#include "BelosTpetraTestFramework.hpp" |
| 30 |
#include "BelosPseudoBlockGmresSolMgr.hpp" |
| 31 |
#include "BelosLinearMultiShiftProblem.hpp" |
| 32 |
|
| 33 |
template <typename ScalarType> |
| 34 |
int run(int argc, char *argv[]) { |
| 35 |
|
| 36 |
using ST = typename Tpetra::CrsMatrix<ScalarType>::scalar_type; |
| 37 |
using LO = typename Tpetra::CrsMatrix<>::local_ordinal_type; |
| 38 |
using GO = typename Tpetra::CrsMatrix<>::global_ordinal_type; |
| 39 |
using NT = typename Tpetra::CrsMatrix<>::node_type; |
| 40 |
|
| 41 |
using OP = typename Tpetra::Operator<ST,LO,GO,NT>; |
| 42 |
using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>; |
| 43 |
using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType; |
| 44 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 45 |
|
| 46 |
using tmap_t = Tpetra::Map<LO,GO,NT>; |
| 47 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>; |
| 48 |
|
| 49 |
using Teuchos::RCP; |
| 50 |
using Teuchos::rcp; |
| 51 |
using Teuchos::ParameterList; |
| 52 |
|
| 53 |
Teuchos::GlobalMPISession session(&argc, &argv, nullptr); |
| 54 |
|
| 55 |
bool verbose = false; |
| 56 |
bool success = true; |
| 57 |
|
| 58 |
try { |
| 59 |
|
| 60 |
const auto comm = Tpetra::getDefaultComm(); |
| 61 |
const int myPID = comm->getRank(); |
| 62 |
|
| 63 |
bool procVerbose = false; |
| 64 |
bool useShift = true; |
| 65 |
int frequency = -1; // how often residuals are printed by solver |
| 66 |
int initNumRHS = 3; // how many right-hand sides get solved first |
| 67 |
int augNumRHS = 2; // how many right-hand sides are augmented to the first group |
| 68 |
int maxRestarts = 15; // number of restarts allowed |
| 69 |
int length = 100; |
| 70 |
int initBlockSize = 2;// blocksize used for the initial pseudo-block GMRES solve |
| 71 |
int augBlockSize = 1; // blocksize used for the augmented pseudo-block GMRES solve |
| 72 |
int maxIters = -1; // maximum iterations allowed |
| 73 |
std::string filename("orsirr1.hb"); |
| 74 |
MT tol = 1.0e-5; // relative residual tolerance |
| 75 |
MT aug_tol = 1.0e-5; // relative residual tolerance for augmented system |
| 76 |
|
| 77 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 78 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 79 |
cmdp.setOption("use-shift", "no-shift", &useShift, "Whether shift should be used."); |
| 80 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 81 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 82 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver."); |
| 83 |
cmdp.setOption("aug-tol",&aug_tol,"Relative residual tolerance used by GMRES solver for augmented systems."); |
| 84 |
cmdp.setOption("init-num-rhs",&initNumRHS,"Number of right-hand sides to be initially solved for."); |
| 85 |
cmdp.setOption("aug-num-rhs",&augNumRHS,"Number of right-hand sides augmenting the initial solve."); |
| 86 |
cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver."); |
| 87 |
cmdp.setOption("block-size",&initBlockSize,"Block size used by GMRES for the initial solve."); |
| 88 |
cmdp.setOption("aug-block-size",&augBlockSize,"Block size used by GMRES for the augmented solve."); |
| 89 |
cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 90 |
cmdp.setOption("subspace-size",&length,"Dimension of Krylov subspace used by GMRES."); |
| 91 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 92 |
return -1; |
| 93 |
} |
| 94 |
if (!verbose) { |
| 95 |
frequency = -1; // reset frequency if test is not verbose |
| 96 |
} |
| 97 |
procVerbose = verbose && (myPID==0); /* Only print on zero processor */ |
| 98 |
|
| 99 |
// Get the problem |
| 100 |
Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm ); |
| 101 |
RCP<tcrsmatrix_t> A = reader.readFromFile( filename ); |
| 102 |
RCP<const tmap_t> Map = A->getDomainMap(); |
| 103 |
|
| 104 |
// ********Other information used by block solver*********** |
| 105 |
// *****************(can be user specified)****************** |
| 106 |
|
| 107 |
const int numGlobalElements = Map->getGlobalNumElements(); |
| 108 |
if (maxIters == -1) |
| 109 |
maxIters = numGlobalElements - 1; // maximum number of iterations to run |
| 110 |
|
| 111 |
ParameterList belosList; |
| 112 |
belosList.set( "Num Blocks", length ); // Maximum number of blocks in Krylov factorization |
| 113 |
belosList.set( "Block Size", initBlockSize ); // Blocksize to be used by iterative solver |
| 114 |
belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed |
| 115 |
belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed |
| 116 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 117 |
belosList.set( "Deflation Quorum", initBlockSize ); // Number of converged linear systems before deflation |
| 118 |
belosList.set( "Timer Label", "Belos Init" ); // Label used by the timers in this solver |
| 119 |
if (verbose) { |
| 120 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 121 |
Belos::TimingDetails + Belos::StatusTestDetails ); |
| 122 |
if (frequency > 0) |
| 123 |
belosList.set( "Output Frequency", frequency ); |
| 124 |
} |
| 125 |
else |
| 126 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 127 |
|
| 128 |
// *****Construct solution std::vector and random right-hand-sides ***** |
| 129 |
RCP<MV> initX = rcp( new MV(Map, initNumRHS) ); |
| 130 |
RCP<MV> initB = rcp( new MV(Map, initNumRHS) ); |
| 131 |
|
| 132 |
Belos::LinearMultiShiftProblem<ST,MV,OP> initProblem( A, initX, initB ); |
| 133 |
initProblem.setLabel("Belos Init"); |
| 134 |
initProblem.setShift( useShift ); |
| 135 |
bool set = initProblem.setProblem(); |
| 136 |
|
| 137 |
MVT::MvRandom( *initX ); |
| 138 |
initProblem.applyOp( *initX, *initB ); |
| 139 |
initX->putScalar( 0.0 ); |
| 140 |
set = initProblem.setProblem( initX, initB ); |
| 141 |
|
| 142 |
if (set == false) { |
| 143 |
if (procVerbose) |
| 144 |
std::cout << std::endl << "ERROR: Initial Belos::LinearMultiShiftProblem failed to set up correctly!" << std::endl; |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
// ******************************************************************* |
| 149 |
// *********************Perform initial solve************************* |
| 150 |
// ******************************************************************* |
| 151 |
|
| 152 |
RCP< Belos::SolverManager<ST,MV,OP> > initSolver |
| 153 |
= rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>( rcp(&initProblem,false), rcp(&belosList,false) ) ); |
| 154 |
|
| 155 |
// Perform solve |
| 156 |
Belos::ReturnType ret = initSolver->solve(); |
| 157 |
|
| 158 |
// Compute actual residuals. |
| 159 |
std::vector<int> initIndex( initNumRHS ); |
| 160 |
for (int i=0; i<initNumRHS; ++i) |
| 161 |
initIndex[i] = i; |
| 162 |
initProblem.setLSIndex( initIndex ); |
| 163 |
|
| 164 |
bool badRes = false; |
| 165 |
std::vector<ST> actualResids( initNumRHS ); |
| 166 |
std::vector<ST> rhsNorm( initNumRHS ); |
| 167 |
MV initR( Map, initNumRHS ); |
| 168 |
initProblem.applyOp( *initX, initR ); |
| 169 |
MVT::MvAddMv( -1.0, initR, 1.0, *initB, initR ); |
| 170 |
MVT::MvNorm( initR, actualResids ); |
| 171 |
MVT::MvNorm( *initB, rhsNorm ); |
| 172 |
if (procVerbose) { |
| 173 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 174 |
for (int i=0; i<initNumRHS; i++) { |
| 175 |
ST actRes = actualResids[i]/rhsNorm[i]; |
| 176 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 177 |
if (actRes > tol) badRes = true; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
if (ret != Belos::Converged || badRes==true) { |
| 182 |
if (procVerbose) |
| 183 |
std::cout << std::endl << "ERROR: Initial solve did not converge to solution!" << std::endl; |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
// ***************Construct augmented linear system**************** |
| 188 |
|
| 189 |
RCP<MV> augX = rcp( new MV(Map, initNumRHS+augNumRHS) ); |
| 190 |
RCP<MV> augB = rcp( new MV(Map, initNumRHS+augNumRHS) ); |
| 191 |
Belos::LinearMultiShiftProblem<ST,MV,OP> augProblem( A, augX, augB ); |
| 192 |
augProblem.setLabel("Belos Aug"); |
| 193 |
augProblem.setShift( useShift ); |
| 194 |
set = augProblem.setProblem(); |
| 195 |
|
| 196 |
if (augNumRHS) { |
| 197 |
MVT::MvRandom( *augX ); |
| 198 |
augProblem.applyOp( *augX, *augB ); |
| 199 |
augX->putScalar( 0.0 ); |
| 200 |
} |
| 201 |
|
| 202 |
// Copy previous linear system into |
| 203 |
RCP<MV> tmpX = rcp( new MV( *augX ) ); |
| 204 |
RCP<MV> tmpB = rcp( new MV( *augB ) ); |
| 205 |
tmpX->scale( 1.0, *augX ); |
| 206 |
tmpB->scale( 1.0, *augB ); |
| 207 |
|
| 208 |
set = augProblem.setProblem( augX, augB ); |
| 209 |
if (set == false) { |
| 210 |
if (procVerbose) |
| 211 |
std::cout << std::endl << "ERROR: Augmented Belos::LinearMultiShiftProblem failed to set up correctly!" << std::endl; |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
// ******************************************************************* |
| 216 |
// *******************Perform augmented solve************************* |
| 217 |
// ******************************************************************* |
| 218 |
|
| 219 |
belosList.set( "Block Size", augBlockSize ); // Blocksize to be used by iterative solver |
| 220 |
belosList.set( "Convergence Tolerance", aug_tol ); // Relative convergence tolerance requested |
| 221 |
belosList.set( "Deflation Quorum", augBlockSize ); // Number of converged linear systems before deflation |
| 222 |
belosList.set( "Timer Label", "Belos Aug" ); // Label used by the timers in this solver |
| 223 |
belosList.set( "Implicit Residual Scaling", "Norm of RHS" ); // Implicit residual scaling for convergence |
| 224 |
belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Explicit residual scaling for convergence |
| 225 |
RCP< Belos::SolverManager<ST,MV,OP> > augSolver |
| 226 |
= rcp( new Belos::PseudoBlockGmresSolMgr<ST,MV,OP>( rcp(&augProblem,false), rcp(&belosList,false) ) ); |
| 227 |
|
| 228 |
// Perform solve |
| 229 |
ret = augSolver->solve(); |
| 230 |
|
| 231 |
if (ret != Belos::Converged) { |
| 232 |
if (procVerbose) |
| 233 |
std::cout << std::endl << "ERROR: Augmented solver did not converge to solution!" << std::endl; |
| 234 |
return -1; |
| 235 |
} |
| 236 |
|
| 237 |
// **********Print out information about problem******************* |
| 238 |
|
| 239 |
if (procVerbose) { |
| 240 |
std::cout << std::endl << std::endl; |
| 241 |
std::cout << "Dimension of matrix: " << numGlobalElements << std::endl; |
| 242 |
std::cout << "Number of initial right-hand sides: " << initNumRHS << std::endl; |
| 243 |
std::cout << "Number of augmented right-hand sides: " << augNumRHS << std::endl; |
| 244 |
std::cout << "Number of restarts allowed: " << maxRestarts << std::endl; |
| 245 |
std::cout << "Length of Krylov subspace: " << length <<std::endl; |
| 246 |
std::cout << "Max number of Gmres iterations: " << maxIters << std::endl; |
| 247 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 248 |
if (aug_tol != tol) { |
| 249 |
std::cout << "Relative residual tolerance for augmented systems: " << aug_tol << std::endl; |
| 250 |
} |
| 251 |
std::cout << std::endl; |
| 252 |
} |
| 253 |
|
| 254 |
// Compute actual residuals. |
| 255 |
int totalNumRHS = initNumRHS + augNumRHS; |
| 256 |
std::vector<int> totalIndex( totalNumRHS ); |
| 257 |
for (int i=0; i<totalNumRHS; ++i) |
| 258 |
totalIndex[i] = i; |
| 259 |
augProblem.setLSIndex( totalIndex ); |
| 260 |
|
| 261 |
badRes = false; |
| 262 |
actualResids.resize( totalNumRHS ); |
| 263 |
rhsNorm.resize( totalNumRHS ); |
| 264 |
MV augR( Map, totalNumRHS ); |
| 265 |
augProblem.apply( *augX, augR ); |
| 266 |
MVT::MvAddMv( -1.0, augR, 1.0, *augB, augR ); |
| 267 |
MVT::MvNorm( augR, actualResids ); |
| 268 |
MVT::MvNorm( *augB, rhsNorm ); |
| 269 |
if (procVerbose) { |
| 270 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 271 |
for ( int i=0; i<totalNumRHS; i++) { |
| 272 |
ST actRes = actualResids[i]/rhsNorm[i]; |
| 273 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 274 |
if (actRes > tol ) badRes = true; |
| 275 |
} |
| 276 |
} |
| 277 |
if (ret!=Belos::Converged || badRes==true) { |
| 278 |
success = false; |
| 279 |
if (procVerbose) |
| 280 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 281 |
} else { |
| 282 |
success = true; |
| 283 |
if (procVerbose) |
| 284 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 285 |
} |
| 286 |
} |
| 287 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success); |
| 288 |
|
| 289 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 290 |
} // run |
| 291 |
|
| 292 |
int main(int argc, char *argv[]) { |
| 293 |
// run with different ST |
| 294 |
return run<double>(argc,argv); |
| 295 |
// run<float>(argc,argv); // FAILS |
| 296 |
} |