| 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 |
/* Originally convert test here: belos/epetra/test/BlockCG/test_resolve_cg_hb.cpp */ |
| 17 |
|
| 18 |
#include "BelosConfigDefs.hpp" |
| 19 |
#include "BelosLinearProblem.hpp" |
| 20 |
#include "BelosTpetraAdapter.hpp" |
| 21 |
#include "BelosBlockCGSolMgr.hpp" |
| 22 |
#include "BelosPseudoBlockCGSolMgr.hpp" |
| 23 |
|
| 24 |
#include <Tpetra_Core.hpp> |
| 25 |
#include <Tpetra_CrsMatrix.hpp> |
| 26 |
#include <Tpetra_Map.hpp> |
| 27 |
#include <Tpetra_MatrixIO.hpp> |
| 28 |
|
| 29 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 30 |
#include <Teuchos_ParameterList.hpp> |
| 31 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 32 |
|
| 33 |
template <typename ScalarType> |
| 34 |
int run(int argc, char *argv[]) |
| 35 |
{ |
| 36 |
using ST = ScalarType; |
| 37 |
using OP = typename Tpetra::Operator<ST>; |
| 38 |
using MV = typename Tpetra::MultiVector<ST>; |
| 39 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST>; |
| 40 |
|
| 41 |
using SCT = typename Teuchos::ScalarTraits<ST>; |
| 42 |
using MT = typename SCT::magnitudeType; |
| 43 |
|
| 44 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 45 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 46 |
|
| 47 |
using Teuchos::ParameterList; |
| 48 |
using Teuchos::Comm; |
| 49 |
using Teuchos::RCP; |
| 50 |
using Teuchos::rcp; |
| 51 |
|
| 52 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 53 |
RCP<const Comm<int>> comm = Tpetra::getDefaultComm(); |
| 54 |
int MyPID = rank(*comm); |
| 55 |
|
| 56 |
bool success = false; |
| 57 |
bool verbose = false; |
| 58 |
|
| 59 |
try |
| 60 |
{ |
| 61 |
// Get test parameters from command-line processor |
| 62 |
bool badRes = false; |
| 63 |
bool proc_verbose = false; |
| 64 |
bool pseudo = false; // use pseudo block CG to solve this linear system. |
| 65 |
int frequency = -1; |
| 66 |
int blocksize = 1; |
| 67 |
int numrhs = 1; |
| 68 |
int maxiters = -1; // maximum number of iterations allowed per linear system |
| 69 |
std::string filename("bcsstk14.hb"); |
| 70 |
std::string ortho("DGKS"); |
| 71 |
MT tol = 1.0e-5; // relative residual tolerance |
| 72 |
|
| 73 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 74 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 75 |
cmdp.setOption("pseudo","regular",&pseudo,"Use pseudo-block CG to solve the linear systems."); |
| 76 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 77 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 78 |
cmdp.setOption("ortho",&ortho,"Orthogonalization routine used by CG solver."); |
| 79 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver."); |
| 80 |
cmdp.setOption("blocksize",&blocksize,"Block size used by CG."); |
| 81 |
cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 82 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 83 |
return -1; |
| 84 |
} |
| 85 |
if (!verbose){ |
| 86 |
frequency = -1; // reset frequency if test is not verbose |
| 87 |
} |
| 88 |
|
| 89 |
proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 90 |
if (proc_verbose) { |
| 91 |
std::cout << Belos::Belos_Version() << std::endl << std::endl; |
| 92 |
} |
| 93 |
|
| 94 |
// Get the problem |
| 95 |
RCP<tcrsmatrix_t> A; |
| 96 |
Tpetra::Utils::readHBMatrix(filename,comm,A); |
| 97 |
RCP<const Tpetra::Map<> > map = A->getDomainMap(); |
| 98 |
|
| 99 |
// Construct initial guess and random right-hand-sides |
| 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 (can be user specified) |
| 108 |
const int NumGlobalElements = B->getGlobalLength(); |
| 109 |
if (maxiters == -1) |
| 110 |
maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run |
| 111 |
|
| 112 |
// |
| 113 |
ParameterList belosList; |
| 114 |
belosList.set( "Num Blocks", maxiters ); // Maximum number of blocks in Krylov factorization |
| 115 |
belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver |
| 116 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 117 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 118 |
belosList.set( "Orthogonalization", ortho ); // Orthogonalization routine |
| 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 an unpreconditioned linear problem instance. |
| 129 |
Belos::LinearProblem<ST,MV,OP> problem( A, X, B ); |
| 130 |
bool set = problem.setProblem(); |
| 131 |
if (set == false) { |
| 132 |
if (proc_verbose) |
| 133 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 134 |
return -1; |
| 135 |
} |
| 136 |
|
| 137 |
// Start the block CG iteration |
| 138 |
RCP< Belos::SolverManager<ST,MV,OP> > solver; |
| 139 |
if (pseudo) |
| 140 |
solver = rcp( new Belos::PseudoBlockCGSolMgr<ST,MV,OP>( rcpFromRef(problem), rcpFromRef(belosList) ) ); |
| 141 |
else |
| 142 |
solver = rcp( new Belos::BlockCGSolMgr<ST,MV,OP>( rcpFromRef(problem), rcpFromRef(belosList) ) ); |
| 143 |
|
| 144 |
// Perform solve |
| 145 |
Belos::ReturnType ret = solver->solve(); |
| 146 |
if (ret!=Belos::Converged) { |
| 147 |
if (proc_verbose) |
| 148 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 149 |
return -1; |
| 150 |
} |
| 151 |
|
| 152 |
// Get the number of iterations for this solve. |
| 153 |
int numIters = solver->getNumIters(); |
| 154 |
if (proc_verbose) |
| 155 |
std::cout << "Number of iterations performed for this solve: " << numIters << std::endl; |
| 156 |
|
| 157 |
// Compute actual residuals. |
| 158 |
std::vector<MT> actual_resids( numrhs ); |
| 159 |
std::vector<MT> rhs_norm( numrhs ); |
| 160 |
MV resid(map, numrhs); |
| 161 |
OPT::Apply( *A, *X, resid ); |
| 162 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 163 |
MVT::MvNorm( resid, actual_resids ); |
| 164 |
MVT::MvNorm( *B, rhs_norm ); |
| 165 |
if (proc_verbose) { |
| 166 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 167 |
for ( int i=0; i<numrhs; i++) { |
| 168 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
// Resolve the first problem by just resetting the solver manager. |
| 173 |
X->putScalar( 0.0 ); |
| 174 |
solver->reset( Belos::Problem ); |
| 175 |
|
| 176 |
// Perform solve (again) |
| 177 |
ret = solver->solve(); |
| 178 |
|
| 179 |
// Get the number of iterations for this solve. |
| 180 |
numIters = solver->getNumIters(); |
| 181 |
if (proc_verbose) |
| 182 |
std::cout << "Number of iterations performed for this solve (manager reset): " << numIters << std::endl; |
| 183 |
if (ret!=Belos::Converged) { |
| 184 |
if (proc_verbose) |
| 185 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 186 |
return -1; |
| 187 |
} |
| 188 |
|
| 189 |
// Compute actual residuals. |
| 190 |
std::vector<MT> actual_resids2( numrhs ); |
| 191 |
MV resid2(map, numrhs); |
| 192 |
OPT::Apply( *A, *X, resid2 ); |
| 193 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 194 |
MVT::MvNorm( resid2, actual_resids ); |
| 195 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 196 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 197 |
MVT::MvNorm( *B, rhs_norm ); |
| 198 |
if (proc_verbose) { |
| 199 |
std::cout<< "---------- Actual Residuals (manager reset) ----------"<<std::endl<<std::endl; |
| 200 |
for ( int i=0; i<numrhs; i++) { |
| 201 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 202 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 203 |
badRes = true; |
| 204 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// Resolve the first problem by resetting the solver manager and changing the labels. |
| 210 |
ParameterList belosList2; |
| 211 |
belosList2.set( "Timer Label", "Belos Resolve w/ New Label" ); // Change timer labels. |
| 212 |
solver->setParameters( Teuchos::rcp( &belosList2, false ) ); |
| 213 |
|
| 214 |
problem.setLabel( "Belos Resolve w/ New Label" ); |
| 215 |
X->putScalar( 0.0 ); |
| 216 |
solver->reset( Belos::Problem ); |
| 217 |
|
| 218 |
// Perform solve (again) |
| 219 |
ret = solver->solve(); |
| 220 |
|
| 221 |
// Get the number of iterations for this solve. |
| 222 |
numIters = solver->getNumIters(); |
| 223 |
if (proc_verbose) |
| 224 |
std::cout << "Number of iterations performed for this solve (label reset): " << numIters << std::endl; |
| 225 |
if (ret!=Belos::Converged) { |
| 226 |
if (proc_verbose) |
| 227 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
// Compute actual residuals. |
| 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 (label reset) ----------"<<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 |
// Construct a second unpreconditioned linear problem instance. |
| 250 |
RCP<MV> X2 = MVT::Clone(*X, numrhs); |
| 251 |
MVT::MvInit( *X2, 0.0 ); |
| 252 |
Belos::LinearProblem<ST,MV,OP> problem2( A, X2, B ); |
| 253 |
problem2.setLabel("Belos Resolve"); |
| 254 |
set = problem2.setProblem(); |
| 255 |
if (set == false) { |
| 256 |
if (proc_verbose) |
| 257 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 258 |
return -1; |
| 259 |
} |
| 260 |
|
| 261 |
// Start the block CG iteration |
| 262 |
// Create the solver without either the problem or parameter list. |
| 263 |
if (pseudo) |
| 264 |
solver = rcp( new Belos::PseudoBlockCGSolMgr<ST,MV,OP>() ); |
| 265 |
else |
| 266 |
solver = rcp( new Belos::BlockCGSolMgr<ST,MV,OP>() ); |
| 267 |
|
| 268 |
// Set the problem after the solver construction. |
| 269 |
solver->setProblem( rcpFromRef(problem2) ); |
| 270 |
|
| 271 |
// Get the valid list of parameters from the solver and print it. |
| 272 |
RCP<const ParameterList> validList = solver->getValidParameters(); |
| 273 |
if (proc_verbose) { |
| 274 |
if (pseudo) |
| 275 |
std::cout << std::endl << "Valid parameters from the pseudo-block CG solver manager:" << std::endl; |
| 276 |
else |
| 277 |
std::cout << std::endl << "Valid parameters from the block CG solver manager:" << std::endl; |
| 278 |
|
| 279 |
std::cout << *validList << std::endl; |
| 280 |
} |
| 281 |
|
| 282 |
// Set the parameter list after the solver construction. |
| 283 |
belosList.set( "Timer Label", "Belos Resolve" ); // Set timer label to discern between the two solvers. |
| 284 |
solver->setParameters( rcp( &belosList, false ) ); |
| 285 |
|
| 286 |
// Perform solve |
| 287 |
ret = solver->solve(); |
| 288 |
|
| 289 |
// Get the number of iterations for this solve. |
| 290 |
numIters = solver->getNumIters(); |
| 291 |
if (proc_verbose) |
| 292 |
std::cout << "Number of iterations performed for this solve (new solver): " << numIters << std::endl; |
| 293 |
|
| 294 |
// Compute actual residuals. |
| 295 |
OPT::Apply( *A, *X2, resid2 ); |
| 296 |
MVT::MvAddMv( -1.0, resid2, 1.0, *B, resid2 ); |
| 297 |
MVT::MvNorm( resid2, actual_resids ); |
| 298 |
MVT::MvAddMv( -1.0, resid2, 1.0, resid, resid2 ); |
| 299 |
MVT::MvNorm( resid2, actual_resids2 ); |
| 300 |
MVT::MvNorm( *B, rhs_norm ); |
| 301 |
if (proc_verbose) { |
| 302 |
std::cout<< "---------- Actual Residuals (new solver) ----------"<<std::endl<<std::endl; |
| 303 |
for ( int i=0; i<numrhs; i++) { |
| 304 |
std::cout<<"Problem "<<i<<" : \t"<< actual_resids[i]/rhs_norm[i] <<std::endl; |
| 305 |
if ( actual_resids2[i] > SCT::prec() ) { |
| 306 |
badRes = true; |
| 307 |
std::cout << "Resolve residual vector is too different from first solve residual vector: " << actual_resids2[i] << std::endl; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// Print out information about problem |
| 313 |
if (proc_verbose) { |
| 314 |
std::cout << std::endl << std::endl; |
| 315 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 316 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 317 |
std::cout << "Block size used by solver: " << blocksize << std::endl; |
| 318 |
std::cout << "Max number of CG iterations: " << maxiters << std::endl; |
| 319 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 320 |
std::cout << std::endl; |
| 321 |
} |
| 322 |
|
| 323 |
success = ret==Belos::Converged && !badRes; |
| 324 |
|
| 325 |
if (success) { |
| 326 |
if (proc_verbose) |
| 327 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 328 |
} else { |
| 329 |
if (proc_verbose) |
| 330 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 331 |
} |
| 332 |
} |
| 333 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 334 |
|
| 335 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 336 |
} // end test_resolve_cg_hb.cpp |
| 337 |
|
| 338 |
int main(int argc, char *argv[]) { |
| 339 |
return run<double>(argc,argv); |
| 340 |
|
| 341 |
// wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON |
| 342 |
// return run<float>(argc,argv); |
| 343 |
} |
| 344 |
|