| 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 |
// Adapted from PseudoBlockTFQMREpetraExFile.cpp (with original comments) |
| 15 |
|
| 16 |
// All preconditioning has been commented out |
| 17 |
|
| 18 |
// Ifpack2 |
| 19 |
// #include <Ifpack2_Factory.hpp> |
| 20 |
// #include <Ifpack2_Preconditioner.hpp> |
| 21 |
|
| 22 |
// Teuchos |
| 23 |
#include <Teuchos_Assert.hpp> |
| 24 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 25 |
#include <Teuchos_ParameterList.hpp> |
| 26 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 27 |
|
| 28 |
// Tpetra |
| 29 |
#include <Tpetra_Core.hpp> |
| 30 |
#include <Tpetra_CrsMatrix.hpp> |
| 31 |
#include <Tpetra_Map.hpp> |
| 32 |
#include <Tpetra_MatrixIO.hpp> |
| 33 |
#include <Tpetra_MultiVector.hpp> |
| 34 |
#include <Tpetra_Operator.hpp> |
| 35 |
|
| 36 |
// Belos |
| 37 |
#include "BelosConfigDefs.hpp" |
| 38 |
#include "BelosLinearProblem.hpp" |
| 39 |
#include "BelosPseudoBlockTFQMRSolMgr.hpp" |
| 40 |
#include "BelosTpetraAdapter.hpp" |
| 41 |
#include "BelosTpetraTestFramework.hpp" |
| 42 |
|
| 43 |
template <typename ScalarType> |
| 44 |
int run(int argc, char *argv[]) { |
| 45 |
using Teuchos::ParameterList; |
| 46 |
using Teuchos::RCP; |
| 47 |
using Teuchos::rcp; |
| 48 |
|
| 49 |
using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type; |
| 50 |
using LO = typename Tpetra::Vector<>::local_ordinal_type; |
| 51 |
using GO = typename Tpetra::Vector<>::global_ordinal_type; |
| 52 |
using NT = typename Tpetra::Vector<>::node_type; |
| 53 |
|
| 54 |
using MV = typename Tpetra::MultiVector<ST, LO, GO, NT>; |
| 55 |
using OP = typename Tpetra::Operator<ST, LO, GO, NT>; |
| 56 |
using MAP = typename Tpetra::Map<LO, GO, NT>; |
| 57 |
using MAT = typename Tpetra::CrsMatrix<ST, LO, GO, NT>; |
| 58 |
|
| 59 |
using MVT = typename Belos::MultiVecTraits<ST, MV>; |
| 60 |
using OPT = typename Belos::OperatorTraits<ST, MV, OP>; |
| 61 |
|
| 62 |
using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType; |
| 63 |
using STM = typename Teuchos::ScalarTraits<MT>; |
| 64 |
|
| 65 |
using LinearProblem = typename Belos::LinearProblem<ST, MV, OP>; |
| 66 |
// using Preconditioner = typename Ifpack2::Preconditioner<ST, LO, GO, NT>; |
| 67 |
using PseudoBlockTFQMRSolMgr = ::Belos::PseudoBlockTFQMRSolMgr<ST, MV, OP>; |
| 68 |
|
| 69 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 70 |
RCP<const Teuchos::Comm<int>> comm = Tpetra::getDefaultComm(); |
| 71 |
|
| 72 |
bool verbose = false; |
| 73 |
bool success = true; |
| 74 |
|
| 75 |
try { |
| 76 |
bool procVerbose = false; |
| 77 |
bool leftPrec = true; // use left preconditioning to solve these linear systems |
| 78 |
int frequency = -1; // how often residuals are printed by solver |
| 79 |
int numRhs = 1; |
| 80 |
int maxiters = -1; // maximum iterations allowed |
| 81 |
std::string filename("osrirr1.hb"); |
| 82 |
MT tol = STM::squareroot(STM::eps()); // relative residual tolerance |
| 83 |
|
| 84 |
Teuchos::CommandLineProcessor cmdp(false, true); |
| 85 |
cmdp.setOption("verbose", "quiet", &verbose, "Print messages and results."); |
| 86 |
cmdp.setOption("left-prec", "right-prec", &leftPrec, "Left preconditioning or right."); |
| 87 |
cmdp.setOption("frequency", &frequency, "Solvers frequency for printing residuals (#iters)."); |
| 88 |
cmdp.setOption("filename", &filename, "Filename for Harwell-Boeing test matrix."); |
| 89 |
cmdp.setOption("tol", &tol, "Relative residual tolerance used by GMRES solver."); |
| 90 |
cmdp.setOption("num-rhs", &numRhs, "Number of right-hand sides to be solved for."); |
| 91 |
cmdp.setOption("maxiters", &maxiters, |
| 92 |
"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 93 |
if (cmdp.parse(argc, argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 94 |
return -1; |
| 95 |
} |
| 96 |
if (!verbose) frequency = -1; // reset frequency if test is not verbose |
| 97 |
|
| 98 |
// Get the problem |
| 99 |
RCP<MAT> A; |
| 100 |
Tpetra::Utils::readHBMatrix(filename, comm, A); |
| 101 |
RCP<const MAP> map = A->getRowMap(); |
| 102 |
|
| 103 |
procVerbose = verbose && (comm->getRank() == 0); // Only print on zero processor |
| 104 |
|
| 105 |
// Create the preconditioner. |
| 106 |
// std::string precType = "RILUK"; |
| 107 |
// RCP<Preconditioner> prec = Ifpack2::Factory::create<MAT>(precType, A); |
| 108 |
// assert(prec != Teuchos::null); |
| 109 |
|
| 110 |
// // Specify parameters for the preconditioner |
| 111 |
// int lFill = 2; // if (argc > 2) lFill = atoi(argv[2]); |
| 112 |
// int overlap = 2; // if (argc > 3) overlap = atoi(argv[3]); |
| 113 |
// ST absThres = 0.0; // if (argc > 4) absThres = atof(argv[4]); |
| 114 |
// ST relThresh = 1.0; // if (argc >5) relThresh = atof(argv[5]); |
| 115 |
|
| 116 |
// if (procVerbose) { |
| 117 |
// std::cout << std::endl << std::endl; |
| 118 |
// std::cout << "Constructing RILUK preconditioner" << std::endl; |
| 119 |
// std::cout << "Using Level of fill = " << lFill << std::endl; |
| 120 |
// std::cout << "Using Level Overlap = " << overlap << std::endl; |
| 121 |
// std::cout << "Using Absolute Threshold Value of " << absThres << std::endl; |
| 122 |
// std::cout << "Using Relative Threshold Value of " << relThresh << std::endl; |
| 123 |
// } |
| 124 |
|
| 125 |
// ParameterList precParams; |
| 126 |
// precParams.set("fact: iluk level-of-fill", lFill); |
| 127 |
// precParams.set("fact: iluk level-of-overlap", overlap); |
| 128 |
// precParams.set("fact: absolute threshold", absThres); |
| 129 |
// precParams.set("fact: relative threshold", relThresh); |
| 130 |
// prec->setParameters(precParams); |
| 131 |
|
| 132 |
// // Initialize and build the preconditioner |
| 133 |
// prec->initialize(); |
| 134 |
// prec->compute(); |
| 135 |
|
| 136 |
// Specify parameters for the PseudoBlockTFQMR solver manager |
| 137 |
const int numGlobalElements = map->getGlobalNumElements(); |
| 138 |
if (maxiters == -1) maxiters = numGlobalElements - 1; // maximum number of iterations to run |
| 139 |
RCP<ParameterList> belosList = rcp(new ParameterList()); |
| 140 |
belosList->set("Maximum Iterations", maxiters); // Maximum number of iterations allowed |
| 141 |
belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested |
| 142 |
if (leftPrec) |
| 143 |
belosList->set("Explicit Residual Test", true); // Need to check for the explicit residual before returning |
| 144 |
if (verbose) { |
| 145 |
belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::TimingDetails + Belos::StatusTestDetails); |
| 146 |
if (frequency > 0) belosList->set("Output Frequency", frequency); |
| 147 |
} else |
| 148 |
belosList->set("Verbosity", Belos::Errors + Belos::Warnings); |
| 149 |
|
| 150 |
// Construct solution std::vector and random right-hand-sides |
| 151 |
RCP<MV> X = rcp(new MV(map, numRhs)); |
| 152 |
X->putScalar(0.0); |
| 153 |
RCP<MV> B = rcp(new MV(map, numRhs)); |
| 154 |
B->randomize(); |
| 155 |
|
| 156 |
RCP<LinearProblem> problem = rcp(new LinearProblem(A, X, B)); |
| 157 |
// if (leftPrec) |
| 158 |
// problem->setLeftPrec(prec); |
| 159 |
// else |
| 160 |
// problem->setRightPrec(prec); |
| 161 |
|
| 162 |
bool set = problem->setProblem(); |
| 163 |
if (set == false) { |
| 164 |
if (procVerbose) |
| 165 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
// Create the PseudoBlockTFQMR solver manager |
| 170 |
RCP<PseudoBlockTFQMRSolMgr> solver = rcp(new PseudoBlockTFQMRSolMgr(problem, belosList)); |
| 171 |
|
| 172 |
// Print out information about problem |
| 173 |
if (procVerbose) { |
| 174 |
std::cout << std::endl << std::endl; |
| 175 |
std::cout << "Dimension of matrix: " << numGlobalElements << std::endl; |
| 176 |
std::cout << "Number of right-hand sides: " << numRhs << std::endl; |
| 177 |
std::cout << "Max number of Pseudo Block TFQMR iterations: " << maxiters << std::endl; |
| 178 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 179 |
std::cout << std::endl; |
| 180 |
} |
| 181 |
|
| 182 |
// Perform solve |
| 183 |
Belos::ReturnType ret = solver->solve(); |
| 184 |
|
| 185 |
// Compute actual residuals. |
| 186 |
bool badRes = false; |
| 187 |
std::vector<ST> actualResids(numRhs); |
| 188 |
std::vector<ST> rhsNorm(numRhs); |
| 189 |
MV resid(map, numRhs); |
| 190 |
OPT::Apply(*A, *X, resid); |
| 191 |
MVT::MvAddMv(-1.0, resid, 1.0, *B, resid); |
| 192 |
MVT::MvNorm(resid, actualResids); |
| 193 |
MVT::MvNorm(*B, rhsNorm); |
| 194 |
if (procVerbose) { |
| 195 |
std::cout << "---------- Actual Residuals (normalized) ----------" << std::endl << std::endl; |
| 196 |
for (int i = 0; i < numRhs; i++) { |
| 197 |
ST actRes = actualResids[i] / rhsNorm[i]; |
| 198 |
std::cout << "Problem " << i << " : \t" << actRes << std::endl; |
| 199 |
if (actRes > tol) badRes = true; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (ret != Belos::Converged || badRes) { |
| 204 |
success = false; |
| 205 |
if (procVerbose) std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl; |
| 206 |
} else { |
| 207 |
success = true; |
| 208 |
if (procVerbose) std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl; |
| 209 |
} |
| 210 |
} |
| 211 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 212 |
|
| 213 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 214 |
} |
| 215 |
|
| 216 |
int main(int argc, char *argv[]) { |
| 217 |
return run<double>(argc, argv); |
| 218 |
// return run<float>(argc, argv); |
| 219 |
} |
| 220 |
// end PseudoBlockTFQMRpetraExFile.cpp |