| 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 HB file is used instead of random vectors. |
| 12 |
// 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_Core.hpp> |
| 19 |
#include <Tpetra_CrsMatrix.hpp> |
| 20 |
#include <Tpetra_Map.hpp> |
| 21 |
|
| 22 |
// Teuchos |
| 23 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 24 |
#include <Teuchos_ParameterList.hpp> |
| 25 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 26 |
|
| 27 |
// Belos |
| 28 |
#include "BelosConfigDefs.hpp" |
| 29 |
#include "BelosLinearProblem.hpp" |
| 30 |
#include "BelosTFQMRSolMgr.hpp" |
| 31 |
#include "BelosPseudoBlockTFQMRSolMgr.hpp" |
| 32 |
#include "BelosTpetraTestFramework.hpp" |
| 33 |
|
| 34 |
template<typename ScalarType> |
| 35 |
int run(int argc, char *argv[]) { |
| 36 |
// Teuchos |
| 37 |
using SCT = typename Teuchos::ScalarTraits<ScalarType>; |
| 38 |
using MT = typename SCT::magnitudeType; |
| 39 |
|
| 40 |
using Teuchos::ParameterList; |
| 41 |
using Teuchos::RCP; |
| 42 |
using Teuchos::rcp; |
| 43 |
|
| 44 |
// Tpetra |
| 45 |
using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type; |
| 46 |
using LO = typename Tpetra::MultiVector<>::local_ordinal_type; |
| 47 |
using GO = typename Tpetra::MultiVector<>::global_ordinal_type; |
| 48 |
using NT = typename Tpetra::MultiVector<>::node_type; |
| 49 |
|
| 50 |
using OP = Tpetra::Operator<ST,LO,GO,NT>; |
| 51 |
using MV = Tpetra::MultiVector<ST,LO,GO,NT>; |
| 52 |
|
| 53 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST>; |
| 54 |
|
| 55 |
// Belos |
| 56 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 57 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 58 |
|
| 59 |
// MPI session |
| 60 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 61 |
RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm(); |
| 62 |
int MyPID = rank(*comm); |
| 63 |
|
| 64 |
const ST one = SCT::one(); |
| 65 |
bool success = false; |
| 66 |
bool verbose = false; |
| 67 |
|
| 68 |
try |
| 69 |
{ |
| 70 |
// Get test parameters from command-line processor |
| 71 |
bool procVerbose = false; |
| 72 |
bool explicitTest = false; |
| 73 |
bool compRecursive = false; |
| 74 |
bool pseudo = false; |
| 75 |
int frequency = -1; // how often residuals are printed by solver |
| 76 |
int numrhs = 1; // total number of right-hand sides to solve for |
| 77 |
int maxiters = -1; // maximum number of iterations for solver to use |
| 78 |
std::string filename("orsirr1.hb"); |
| 79 |
MT tol = 1.0e-5; // relative residual tolerance |
| 80 |
|
| 81 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 82 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 83 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 84 |
cmdp.setOption("explicit","implicit-only",&explicitTest,"Compute explicit residuals."); |
| 85 |
cmdp.setOption("recursive","native",&compRecursive,"Compute recursive residuals."); |
| 86 |
cmdp.setOption("pseudo","not-pseudo",&pseudo,"Use pseudo-block TFQMR solver."); |
| 87 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by TFQMR or pseudo-block TFQMR solver."); |
| 88 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 89 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 90 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size)."); |
| 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 |
// Get the problem |
| 98 |
Belos::Tpetra::HarwellBoeingReader<tcrsmatrix_t> reader( comm ); |
| 99 |
RCP<tcrsmatrix_t> A = reader.readFromFile( filename ); |
| 100 |
RCP<const Tpetra::Map<> > map = A->getDomainMap(); |
| 101 |
|
| 102 |
// Create initial vectors |
| 103 |
RCP<MV> B, X; |
| 104 |
X = rcp( new MV(map,numrhs) ); |
| 105 |
MVT::MvRandom( *X ); |
| 106 |
B = rcp( new MV(map,numrhs) ); |
| 107 |
OPT::Apply( *A, *X, *B ); |
| 108 |
MVT::MvInit( *X, 0.0 ); |
| 109 |
procVerbose = ( verbose && (MyPID==0) ); |
| 110 |
|
| 111 |
// Solve using Belos |
| 112 |
const int NumGlobalElements = B->getGlobalLength(); |
| 113 |
if (maxiters == -1) |
| 114 |
maxiters = NumGlobalElements; // maximum number of iterations to run |
| 115 |
|
| 116 |
// |
| 117 |
ParameterList belosList; |
| 118 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 119 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 120 |
if (explicitTest) |
| 121 |
{ |
| 122 |
belosList.set( "Explicit Residual Test", true ); // Scale by norm of right-hand side vector." |
| 123 |
belosList.set( "Explicit Residual Scaling", "Norm of RHS" ); // Scale by norm of right-hand side vector." |
| 124 |
} |
| 125 |
if (compRecursive) |
| 126 |
{ |
| 127 |
belosList.set( "Compute Recursive Residuals", true ); |
| 128 |
} |
| 129 |
if (verbose) { |
| 130 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 131 |
Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails ); |
| 132 |
if (frequency > 0) |
| 133 |
belosList.set( "Output Frequency", frequency ); |
| 134 |
} |
| 135 |
else |
| 136 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 137 |
|
| 138 |
// Construct an unpreconditioned linear problem instance. |
| 139 |
Belos::LinearProblem<ST,MV,OP> problem( A, X, B ); |
| 140 |
bool set = problem.setProblem(); |
| 141 |
if (set == false) { |
| 142 |
if (procVerbose) |
| 143 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 144 |
return -1; |
| 145 |
} |
| 146 |
|
| 147 |
// Create an iterative solver manager. |
| 148 |
RCP< Belos::SolverManager<ST,MV,OP> > solver; |
| 149 |
if (pseudo) |
| 150 |
solver = rcp( new Belos::PseudoBlockTFQMRSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) ); |
| 151 |
else |
| 152 |
solver = rcp( new Belos::TFQMRSolMgr<ST,MV,OP>(rcp(&problem, false), rcp(&belosList, false))); |
| 153 |
|
| 154 |
// **********Print out information about problem******************* |
| 155 |
if (procVerbose) { |
| 156 |
std::cout << std::endl << std::endl; |
| 157 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 158 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 159 |
std::cout << "Max number of TFQMR iterations: " << maxiters << std::endl; |
| 160 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 161 |
std::cout << std::endl; |
| 162 |
} |
| 163 |
|
| 164 |
// Perform solve |
| 165 |
Belos::ReturnType ret = solver->solve(); |
| 166 |
|
| 167 |
// Compute actual residuals. |
| 168 |
bool badRes = false; |
| 169 |
std::vector<MT> actualResids( numrhs ); |
| 170 |
std::vector<MT> rhsNorm( numrhs ); |
| 171 |
MV resid(map, numrhs); |
| 172 |
OPT::Apply( *A, *X, resid ); |
| 173 |
MVT::MvAddMv( -one, resid, one, *B, resid ); |
| 174 |
MVT::MvNorm( resid, actualResids ); |
| 175 |
MVT::MvNorm( *B, rhsNorm ); |
| 176 |
|
| 177 |
if (procVerbose) { |
| 178 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 179 |
for ( int i=0; i<numrhs; i++) { |
| 180 |
MT actRes = actualResids[i]/rhsNorm[i]; |
| 181 |
if (procVerbose) { |
| 182 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 183 |
} |
| 184 |
if (actRes > tol) badRes = true; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
success = ret==Belos::Converged && !badRes; |
| 189 |
|
| 190 |
if (success) { |
| 191 |
if (procVerbose) |
| 192 |
std::cout << std::endl << "End Result: TEST PASSED" << std::endl; |
| 193 |
} else { |
| 194 |
if (procVerbose) |
| 195 |
std::cout << std::endl << "End Result: TEST FAILED" << std::endl; |
| 196 |
} |
| 197 |
} |
| 198 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 199 |
|
| 200 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 201 |
} |
| 202 |
|
| 203 |
int main(int argc, char *argv[]) { |
| 204 |
return run<double>(argc,argv); |
| 205 |
|
| 206 |
// wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON |
| 207 |
// run<float>(argc,argv); |
| 208 |
} |