| 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 |
/* Originally convert test here: belos/epetra/test/MINRES/test_minres_hb.cpp */ |
| 11 |
|
| 12 |
// Tpetra |
| 13 |
#include <Tpetra_Core.hpp> |
| 14 |
#include <Tpetra_CrsMatrix.hpp> |
| 15 |
#include <Tpetra_Map.hpp> |
| 16 |
#include <Tpetra_Vector.hpp> |
| 17 |
#include <Tpetra_MatrixIO.hpp> // I/O for Harwell-Boeing files |
| 18 |
|
| 19 |
// Teuchos |
| 20 |
#include <Teuchos_Comm.hpp> |
| 21 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 22 |
#include <Teuchos_ParameterList.hpp> |
| 23 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 24 |
|
| 25 |
// Belos |
| 26 |
#include "BelosConfigDefs.hpp" |
| 27 |
#include "BelosLinearProblem.hpp" |
| 28 |
#include "BelosTpetraAdapter.hpp" |
| 29 |
#include "BelosSolverFactory.hpp" |
| 30 |
|
| 31 |
template<class ScalarType> |
| 32 |
int run(int argc, char *argv[]) { |
| 33 |
using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type; |
| 34 |
using LO = typename Tpetra::Vector<>::local_ordinal_type; |
| 35 |
using GO = typename Tpetra::Vector<>::global_ordinal_type; |
| 36 |
using NT = typename Tpetra::Vector<>::node_type; |
| 37 |
|
| 38 |
using OP = typename Tpetra::Operator<ST,LO,GO,NT>; |
| 39 |
using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>; |
| 40 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 41 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 42 |
|
| 43 |
using tcrsmatrix_t = typename Tpetra::CrsMatrix<ST,LO,GO,NT>; |
| 44 |
|
| 45 |
using Teuchos::inOutArg; |
| 46 |
using Teuchos::ParameterList; |
| 47 |
using Teuchos::RCP; |
| 48 |
using Teuchos::rcp; |
| 49 |
|
| 50 |
// This calls MPI_Init and MPI_Finalize as necessary. |
| 51 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 52 |
RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm(); |
| 53 |
|
| 54 |
bool success = false; |
| 55 |
bool verbose = false; |
| 56 |
|
| 57 |
try { |
| 58 |
int MyPID = rank(*comm); |
| 59 |
|
| 60 |
// |
| 61 |
// Parameters to read from command-line processor |
| 62 |
// |
| 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 = 13000; // maximum number of iterations for solver to use |
| 66 |
std::string filename ("bcsstk14.hb"); |
| 67 |
ST tol = sqrt(std::numeric_limits<ST>::epsilon()); // relative residual tolerance |
| 68 |
|
| 69 |
// |
| 70 |
// Read in command-line arguments |
| 71 |
// |
| 72 |
Teuchos::CommandLineProcessor cmdp (false, true); |
| 73 |
cmdp.setOption ("verbose", "quiet", &verbose, "Print messages and results."); |
| 74 |
cmdp.setOption ("frequency", &frequency, "Solvers frequency for printing " |
| 75 |
"residuals (#iters)."); |
| 76 |
cmdp.setOption ("tol", &tol, "Relative residual tolerance used by MINRES " |
| 77 |
"solver."); |
| 78 |
cmdp.setOption ("filename", &filename, "Filename for Harwell-Boeing test " |
| 79 |
"matrix."); |
| 80 |
cmdp.setOption ("num-rhs", &numRHS, "Number of right-hand sides to solve."); |
| 81 |
cmdp.setOption ("max-iters", &maxIters, "Maximum number of iterations per " |
| 82 |
"linear system (-1 means \"adapt to problem/block size\")."); |
| 83 |
if (cmdp.parse (argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 84 |
return EXIT_FAILURE; |
| 85 |
} |
| 86 |
Teuchos::oblackholestream blackHole; |
| 87 |
std::ostream& verbOut = (verbose && MyPID == 0) ? std::cout : blackHole; |
| 88 |
|
| 89 |
// |
| 90 |
// Generate the linear system(s) to solve. |
| 91 |
// |
| 92 |
verbOut << "Generating the linear system(s) to solve" << std::endl << std::endl; |
| 93 |
RCP<tcrsmatrix_t> A; |
| 94 |
Tpetra::Utils::readHBMatrix(filename, comm, A); |
| 95 |
RCP<const Tpetra::Map<> > rowMap = A->getDomainMap(); |
| 96 |
|
| 97 |
// |
| 98 |
// *****Construct initial guess and random right-hand-sides ***** |
| 99 |
// |
| 100 |
RCP<MV> B, X; |
| 101 |
X = rcp( new MV(rowMap, numRHS) ); |
| 102 |
MVT::MvRandom( *X ); |
| 103 |
B = rcp( new MV(rowMap, numRHS ) ); |
| 104 |
OPT::Apply( *A, *X, *B ); |
| 105 |
MVT::MvInit( *X, 0.0 ); |
| 106 |
|
| 107 |
// |
| 108 |
// Compute the initial residual norm of the problem, so we can see |
| 109 |
// by how much it improved after the solve. |
| 110 |
// |
| 111 |
std::vector<ST> initialResidualNorms (numRHS); |
| 112 |
std::vector<ST> initialResidualInfNorms (numRHS); |
| 113 |
MV R(rowMap, numRHS); |
| 114 |
OPT::Apply (*A, *X, R); |
| 115 |
MVT::MvAddMv (-1.0, R, 1.0, *B, R); // R := -(A*X) + B. |
| 116 |
MVT::MvNorm (R, initialResidualNorms); |
| 117 |
MVT::MvNorm (R, initialResidualInfNorms, Belos::InfNorm); |
| 118 |
if (verbose) { |
| 119 |
verbOut << "Initial residual 2-norms: \t"; |
| 120 |
for (int i = 0; i < numRHS; ++i) { |
| 121 |
verbOut << initialResidualNorms[i]; |
| 122 |
if (i < numRHS-1) { |
| 123 |
verbOut << ", "; |
| 124 |
} |
| 125 |
} |
| 126 |
verbOut << std::endl << "Initial residual Inf-norms: \t"; |
| 127 |
for (int i = 0; i < numRHS; ++i) { |
| 128 |
verbOut << initialResidualInfNorms[i]; |
| 129 |
if (i < numRHS-1) { |
| 130 |
verbOut << ", "; |
| 131 |
} |
| 132 |
} |
| 133 |
verbOut << std::endl; |
| 134 |
} |
| 135 |
|
| 136 |
std::vector<ST> rhs2Norms (numRHS); |
| 137 |
std::vector<ST> rhsInfNorms (numRHS); |
| 138 |
MVT::MvNorm (*B, rhs2Norms); |
| 139 |
MVT::MvNorm (*B, rhsInfNorms, Belos::InfNorm); |
| 140 |
if (verbose) { |
| 141 |
verbOut << "Right-hand side 2-norms: \t"; |
| 142 |
for (int i = 0; i < numRHS; ++i) { |
| 143 |
verbOut << rhs2Norms[i]; |
| 144 |
if (i < numRHS-1) { |
| 145 |
verbOut << ", "; |
| 146 |
} |
| 147 |
} |
| 148 |
verbOut << std::endl << "Right-hand side Inf-norms: \t"; |
| 149 |
for (int i = 0; i < numRHS; ++i) { |
| 150 |
verbOut << rhsInfNorms[i]; |
| 151 |
if (i < numRHS-1) { |
| 152 |
verbOut << ", "; |
| 153 |
} |
| 154 |
} |
| 155 |
verbOut << std::endl; |
| 156 |
} |
| 157 |
|
| 158 |
std::vector<ST> initialGuess2Norms (numRHS); |
| 159 |
std::vector<ST> initialGuessInfNorms (numRHS); |
| 160 |
MVT::MvNorm (*X, initialGuess2Norms); |
| 161 |
MVT::MvNorm (*X, initialGuessInfNorms, Belos::InfNorm); |
| 162 |
if (verbose) { |
| 163 |
verbOut << "Initial guess 2-norms: \t"; |
| 164 |
for (int i = 0; i < numRHS; ++i) { |
| 165 |
verbOut << initialGuess2Norms[i]; |
| 166 |
if (i < numRHS-1) { |
| 167 |
verbOut << ", "; |
| 168 |
} |
| 169 |
} |
| 170 |
verbOut << std::endl << "Initial guess Inf-norms: \t"; |
| 171 |
for (int i = 0; i < numRHS; ++i) { |
| 172 |
verbOut << initialGuessInfNorms[i]; |
| 173 |
if (i < numRHS-1) { |
| 174 |
verbOut << ", "; |
| 175 |
} |
| 176 |
} |
| 177 |
verbOut << std::endl; |
| 178 |
} |
| 179 |
|
| 180 |
// |
| 181 |
// Compute the infinity-norm of A. |
| 182 |
// |
| 183 |
const ST normOfA = A->getFrobeniusNorm(); |
| 184 |
verbOut << "||A||_inf: \t" << normOfA << std::endl; |
| 185 |
// |
| 186 |
// Compute ||A|| ||X_i|| + ||B_i|| for each right-hand side B_i. |
| 187 |
// |
| 188 |
std::vector<ST> scaleFactors (numRHS); |
| 189 |
for (int i = 0; i < numRHS; ++i) { |
| 190 |
scaleFactors[i] = normOfA * initialGuessInfNorms[i] + rhsInfNorms[i]; |
| 191 |
} |
| 192 |
if (verbose) { |
| 193 |
verbOut << "||A||_inf ||X_i||_inf + ||B_i||_inf: \t"; |
| 194 |
for (int i = 0; i < numRHS; ++i) { |
| 195 |
verbOut << scaleFactors[i]; |
| 196 |
if (i < numRHS-1) { |
| 197 |
verbOut << ", "; |
| 198 |
} |
| 199 |
} |
| 200 |
verbOut << std::endl; |
| 201 |
} |
| 202 |
|
| 203 |
// |
| 204 |
// Solve using Belos |
| 205 |
// |
| 206 |
verbOut << std::endl << "Setting up Belos" << std::endl; |
| 207 |
const int NumGlobalElements = B->getGlobalLength(); |
| 208 |
|
| 209 |
// Set up Belos solver parameters. |
| 210 |
RCP<ParameterList> belosList = Teuchos::parameterList ("MINRES"); |
| 211 |
belosList->set ("Maximum Iterations", maxIters); |
| 212 |
belosList->set ("Convergence Tolerance", tol); |
| 213 |
if (verbose) { |
| 214 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings + |
| 215 |
Belos::IterationDetails + Belos::OrthoDetails + |
| 216 |
Belos::FinalSummary + Belos::TimingDetails + Belos::Debug); |
| 217 |
belosList->set ("Output Frequency", frequency); |
| 218 |
} |
| 219 |
else { |
| 220 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings); |
| 221 |
} |
| 222 |
belosList->set ("Output Stream", Teuchos::rcpFromRef (verbOut)); |
| 223 |
|
| 224 |
// Construct an unpreconditioned linear problem instance. |
| 225 |
typedef Belos::LinearProblem<ST,MV,OP> prob_type; |
| 226 |
RCP<prob_type> problem = rcp (new prob_type (A, X, B)); |
| 227 |
if (! problem->setProblem()) { |
| 228 |
verbOut << std::endl << "ERROR: Failed to set up Belos::LinearProblem!" << std::endl; |
| 229 |
return EXIT_FAILURE; |
| 230 |
} |
| 231 |
|
| 232 |
// Create an iterative solver manager. |
| 233 |
Belos::SolverFactory<ST, MV, OP> factory; |
| 234 |
RCP<Belos::SolverManager<ST,MV,OP> > newSolver = |
| 235 |
factory.create ("MINRES", belosList); |
| 236 |
newSolver->setProblem (problem); |
| 237 |
|
| 238 |
// Print out information about problem. Make sure to use the |
| 239 |
// information as stored in the Belos ParameterList, so that we know |
| 240 |
// what the solver will do. |
| 241 |
verbOut << std::endl |
| 242 |
<< "Dimension of matrix: " << NumGlobalElements << std::endl |
| 243 |
<< "Number of right-hand sides: " << numRHS << std::endl |
| 244 |
<< "Max number of MINRES iterations: " |
| 245 |
<< belosList->get<int> ("Maximum Iterations") << std::endl |
| 246 |
<< "Relative residual tolerance: " |
| 247 |
<< belosList->get<double> ("Convergence Tolerance") << std::endl |
| 248 |
<< "Output frequency: " |
| 249 |
<< belosList->get<int> ("Output Frequency") << std::endl |
| 250 |
<< std::endl; |
| 251 |
|
| 252 |
// Solve the linear system. |
| 253 |
verbOut << "Solving the linear system" << std::endl << std::endl; |
| 254 |
Belos::ReturnType ret = newSolver->solve(); |
| 255 |
verbOut << "Belos results:" << std::endl |
| 256 |
<< "- Number of iterations: " |
| 257 |
<< newSolver->getNumIters () << std::endl |
| 258 |
<< "- " << (ret == Belos::Converged ? "Converged" : "Not converged") |
| 259 |
<< std::endl; |
| 260 |
|
| 261 |
// |
| 262 |
// After the solve, compute residual(s) explicitly. This tests |
| 263 |
// whether the Belos solver did so correctly. |
| 264 |
// |
| 265 |
std::vector<ST> absoluteResidualNorms (numRHS); |
| 266 |
OPT::Apply (*A, *X, R); |
| 267 |
MVT::MvAddMv (-1.0, R, 1.0, *B, R); |
| 268 |
MVT::MvNorm (R, absoluteResidualNorms); |
| 269 |
|
| 270 |
std::vector<ST> relativeResidualNorms (numRHS); |
| 271 |
for (int i = 0; i < numRHS; ++i) { |
| 272 |
relativeResidualNorms[i] = (initialResidualNorms[i] == 0.0) ? |
| 273 |
absoluteResidualNorms[i] : |
| 274 |
absoluteResidualNorms[i] / initialResidualNorms[i]; |
| 275 |
} |
| 276 |
|
| 277 |
verbOut << "---------- Computed relative residual norms ----------" |
| 278 |
<< std::endl << std::endl; |
| 279 |
bool badRes = false; |
| 280 |
if (verbose) { |
| 281 |
for (int i = 0; i < numRHS; ++i) { |
| 282 |
const double actRes = relativeResidualNorms[i]; |
| 283 |
verbOut << "Problem " << i << " : \t" << actRes << std::endl; |
| 284 |
if (actRes > tol) { |
| 285 |
badRes = true; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
success = (ret == Belos::Converged && !badRes); |
| 290 |
|
| 291 |
if (success) { |
| 292 |
verbOut << std::endl << "End Result: TEST PASSED" << std::endl; |
| 293 |
} else { |
| 294 |
verbOut << std::endl << "End Result: TEST FAILED" << std::endl; |
| 295 |
} |
| 296 |
} // try |
| 297 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 298 |
|
| 299 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 300 |
} // end test_minres_hb.cpp |
| 301 |
|
| 302 |
int main(int argc, char *argv[]) { |
| 303 |
// run with different scalar types |
| 304 |
return run<double>(argc, argv); |
| 305 |
// return run<float>(argc, argv); // FAILS |
| 306 |
} |