| 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 |
/* Originally convert test here: belos/epetra/test/BlockCG/test_pseudo_cg_hb.cpp */ |
| 15 |
|
| 16 |
#include "BelosConfigDefs.hpp" |
| 17 |
#include "BelosLinearProblem.hpp" |
| 18 |
#include "BelosTpetraAdapter.hpp" |
| 19 |
#include "BelosPseudoBlockCGSolMgr.hpp" |
| 20 |
|
| 21 |
#include <Trilinos_Util.h> |
| 22 |
|
| 23 |
#include <Tpetra_Core.hpp> |
| 24 |
#include <Tpetra_CrsMatrix.hpp> |
| 25 |
#include <Tpetra_Map.hpp> |
| 26 |
#include <Tpetra_MatrixIO.hpp> |
| 27 |
|
| 28 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 29 |
#include <Teuchos_ParameterList.hpp> |
| 30 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 31 |
|
| 32 |
template <typename ScalarType> |
| 33 |
int run(int argc, char *argv[]) |
| 34 |
{ |
| 35 |
using ST = ScalarType; |
| 36 |
using OP = typename Tpetra::Operator<ST>; |
| 37 |
using MV = typename Tpetra::MultiVector<ST>; |
| 38 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST>; |
| 39 |
|
| 40 |
using SCT = typename Teuchos::ScalarTraits<ST>; |
| 41 |
using MT = typename SCT::magnitudeType; |
| 42 |
|
| 43 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 44 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 45 |
|
| 46 |
using Teuchos::ParameterList; |
| 47 |
using Teuchos::Comm; |
| 48 |
using Teuchos::RCP; |
| 49 |
using Teuchos::rcp; |
| 50 |
|
| 51 |
Teuchos::GlobalMPISession session(&argc, &argv, &std::cout); |
| 52 |
RCP<const Comm<int>> comm = Tpetra::getDefaultComm(); |
| 53 |
int MyPID = rank(*comm); |
| 54 |
|
| 55 |
bool success = false; |
| 56 |
bool verbose = false; |
| 57 |
|
| 58 |
try |
| 59 |
{ |
| 60 |
// Get test parameters from command-line processor |
| 61 |
bool proc_verbose = false; |
| 62 |
bool leftprec = true; // left preconditioning or right. |
| 63 |
int frequency = -1; // how often residuals are printed by solver |
| 64 |
int numrhs = 1; // total number of right-hand sides to solve for |
| 65 |
int maxiters = -1; // maximum number of iterations for the solver to use |
| 66 |
std::string filename("bcsstk14.hb"); |
| 67 |
MT tol = 1.0e-5; // relative residual tolerance |
| 68 |
|
| 69 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 70 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 71 |
cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right."); |
| 72 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 73 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 74 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver."); |
| 75 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 76 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem size)."); |
| 77 |
|
| 78 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 79 |
return -1; |
| 80 |
} |
| 81 |
if (!verbose) { |
| 82 |
frequency = -1; // Reset frequency if verbosity is off |
| 83 |
} |
| 84 |
|
| 85 |
proc_verbose = ( verbose && (MyPID==0) ); |
| 86 |
if (proc_verbose) { |
| 87 |
std::cout << Belos::Belos_Version() << std::endl << std::endl; |
| 88 |
} |
| 89 |
|
| 90 |
// Get the problem |
| 91 |
RCP<tcrsmatrix_t> A; |
| 92 |
Tpetra::Utils::readHBMatrix(filename,comm,A); |
| 93 |
RCP<const Tpetra::Map<> > map = A->getDomainMap(); |
| 94 |
|
| 95 |
// Construct initial guess and random right-hand-sides |
| 96 |
RCP<MV> B, X; |
| 97 |
X = rcp(new MV(map, numrhs)); |
| 98 |
MVT::MvRandom(*X); |
| 99 |
B = rcp(new MV(map, numrhs)); |
| 100 |
OPT::Apply(*A, *X, *B); |
| 101 |
MVT::MvInit(*X, 0.0); |
| 102 |
|
| 103 |
// Create parameter list for the pseudo block CG solver manager |
| 104 |
const int NumGlobalElements = B->getGlobalLength(); |
| 105 |
if (maxiters == -1) |
| 106 |
maxiters = NumGlobalElements; // maximum number of iterations to run |
| 107 |
|
| 108 |
// |
| 109 |
ParameterList belosList; |
| 110 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 111 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 112 |
belosList.set( "Estimate Condition Number", true ); |
| 113 |
if (verbose) { |
| 114 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 115 |
Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails ); |
| 116 |
if (frequency > 0) |
| 117 |
belosList.set( "Output Frequency", frequency ); |
| 118 |
} |
| 119 |
else |
| 120 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 121 |
|
| 122 |
// Construct a linear problem |
| 123 |
Belos::LinearProblem<ST,MV,OP> problem(A, X, B); |
| 124 |
bool set = problem.setProblem(); |
| 125 |
if (set == false) { |
| 126 |
if (proc_verbose) |
| 127 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
// Create an iterative solver manager. |
| 132 |
Belos::PseudoBlockCGSolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(belosList)); |
| 133 |
|
| 134 |
// Start the block CG iteration |
| 135 |
if (proc_verbose) { |
| 136 |
std::cout << std::endl << std::endl; |
| 137 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 138 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 139 |
std::cout << "Max number of pseudo block CG iterations: " << maxiters << std::endl; |
| 140 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 141 |
std::cout << std::endl; |
| 142 |
} |
| 143 |
|
| 144 |
// Perform solve |
| 145 |
Belos::ReturnType ret = solver.solve(); |
| 146 |
|
| 147 |
// Compute actual residuals. |
| 148 |
bool badRes = false; |
| 149 |
std::vector<MT> actual_resids( numrhs ); |
| 150 |
std::vector<MT> rhs_norm( numrhs ); |
| 151 |
MV resid(map, numrhs); |
| 152 |
OPT::Apply( *A, *X, resid ); |
| 153 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 154 |
MVT::MvNorm( resid, actual_resids ); |
| 155 |
MVT::MvNorm( *B, rhs_norm ); |
| 156 |
if (proc_verbose) { |
| 157 |
std::cout << "---------- Actual Residuals (normalized) ----------" << std::endl<<std::endl; |
| 158 |
for ( int i=0; i<numrhs; i++) { |
| 159 |
ST actRes = actual_resids[i]/rhs_norm[i]; |
| 160 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 161 |
if (actRes > tol) badRes = true; |
| 162 |
} |
| 163 |
std::cout << std::endl << "Condition Estimate: " << solver.getConditionEstimate() << std::endl; |
| 164 |
} |
| 165 |
|
| 166 |
success = ret==Belos::Converged && !badRes; |
| 167 |
|
| 168 |
if (success) { |
| 169 |
if (proc_verbose) |
| 170 |
std::cout << std::endl << "End Result: TEST PASSED" << std::endl; |
| 171 |
} else { |
| 172 |
if (proc_verbose) |
| 173 |
std::cout << std::endl << "End Result: TEST FAILED" << std::endl; |
| 174 |
} |
| 175 |
} |
| 176 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 177 |
|
| 178 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 179 |
} // end test_pseudo_cg_hb.cpp |
| 180 |
|
| 181 |
int main(int argc, char *argv[]) { |
| 182 |
return run<double>(argc,argv); |
| 183 |
|
| 184 |
// wrapped with a check: CMake option Trilinos_ENABLE_FLOAT=ON |
| 185 |
// return run<float>(argc,argv); |
| 186 |
} |
| 187 |
|