| 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 |
// Tpetra |
| 11 |
#include <Tpetra_Core.hpp> |
| 12 |
#include <Tpetra_CrsMatrix.hpp> |
| 13 |
#include <Tpetra_MultiVector.hpp> |
| 14 |
|
| 15 |
// Galeri |
| 16 |
#include <Galeri_XpetraMaps.hpp> |
| 17 |
#include <Galeri_XpetraMatrixTypes.hpp> |
| 18 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 19 |
|
| 20 |
// Teuchos |
| 21 |
#include <Teuchos_RCP.hpp> |
| 22 |
#include <Teuchos_Comm.hpp> |
| 23 |
#include <Teuchos_CommHelpers.hpp> |
| 24 |
#include <Teuchos_DefaultComm.hpp> |
| 25 |
#include "Teuchos_ParameterList.hpp" |
| 26 |
#include <Teuchos_TypeNameTraits.hpp> |
| 27 |
#include <Teuchos_oblackholestream.hpp> |
| 28 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 29 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 30 |
|
| 31 |
// Belos |
| 32 |
#include "BelosTpetraAdapter.hpp" |
| 33 |
#include "BelosSolverFactory.hpp" |
| 34 |
|
| 35 |
// **************************************************************************** |
| 36 |
// BEGIN RUN ROUTINE |
| 37 |
// **************************************************************************** |
| 38 |
|
| 39 |
template <typename ScalarType> |
| 40 |
int run(int argc, char *argv[]) { |
| 41 |
|
| 42 |
// Belos solvers have the following template parameters: |
| 43 |
// |
| 44 |
// - Scalar: The type of dot product results. |
| 45 |
// - MV: The type of (multi)vectors. |
| 46 |
// - OP: The type of operators (functions from multivector to |
| 47 |
// multivector). A matrix (like Tpetra::CrsMatrix) is an example |
| 48 |
// of an operator; an Ifpack2 preconditioner is another example. |
| 49 |
// |
| 50 |
// Here, ST is set by the main function, MV is Tpetra::MultiVector, and OP is |
| 51 |
// Tpetra::Operator. |
| 52 |
using ST = typename Tpetra::MultiVector<ScalarType>::scalar_type; |
| 53 |
using LO = typename Tpetra::MultiVector<>::local_ordinal_type; |
| 54 |
using GO = typename Tpetra::MultiVector<>::global_ordinal_type; |
| 55 |
using NT = typename Tpetra::MultiVector<>::node_type; |
| 56 |
|
| 57 |
using OP = typename Tpetra::Operator<ST,LO,GO,NT>; |
| 58 |
using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>; |
| 59 |
using MT = typename Teuchos::ScalarTraits<ST>::magnitudeType; |
| 60 |
|
| 61 |
using tmap_t = Tpetra::Map<LO,GO,NT>; |
| 62 |
using tvector_t = Tpetra::Vector<ST,LO,GO,NT>; |
| 63 |
using trowmatrix_t = Tpetra::RowMatrix<ST,LO,GO,NT>; |
| 64 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>; |
| 65 |
|
| 66 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 67 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 68 |
|
| 69 |
using Teuchos::RCP; |
| 70 |
using Teuchos::rcp; |
| 71 |
using Teuchos::ParameterList; |
| 72 |
|
| 73 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout); |
| 74 |
const auto comm = Tpetra::getDefaultComm(); |
| 75 |
const int myPID = comm->getRank(); |
| 76 |
|
| 77 |
bool verbose = false; |
| 78 |
bool success = true; |
| 79 |
|
| 80 |
try { |
| 81 |
bool procVerbose = false; |
| 82 |
bool debug = false; |
| 83 |
int frequency = -1; // frequency of status test output |
| 84 |
int blockSize = 1; // blockSize |
| 85 |
int numrhs = 1; // number of right-hand sides to solve for |
| 86 |
int maxIters = -1; // maximum number of iterations allowed per linear system |
| 87 |
int maxSubspace = 50; // maximum number of blocks the solver can use for the subspace |
| 88 |
int maxRestarts = 15; // number of restarts allowed |
| 89 |
int nx = 10; // number of discretization points in each direction |
| 90 |
MT tol = 1.0e-5; // relative residual tolerance |
| 91 |
|
| 92 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 93 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 94 |
cmdp.setOption("debug","nondebug",&debug,"Print debugging information from solver."); |
| 95 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 96 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver."); |
| 97 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 98 |
cmdp.setOption("block-size",&blockSize,"Block size used by GMRES."); |
| 99 |
cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 100 |
cmdp.setOption("max-subspace",&maxSubspace,"Maximum number of blocks the solver can use for the subspace."); |
| 101 |
cmdp.setOption("max-restarts",&maxRestarts,"Maximum number of restarts allowed for GMRES solver."); |
| 102 |
cmdp.setOption("nx",&nx,"Number of discretization points in each direction of 3D Laplacian."); |
| 103 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 104 |
return -1; |
| 105 |
} |
| 106 |
if (!verbose) |
| 107 |
frequency = -1; // reset frequency if test is not verbose |
| 108 |
|
| 109 |
procVerbose = ( verbose && (myPID==0) ); // Only print on the zero processor |
| 110 |
|
| 111 |
if (procVerbose) { |
| 112 |
std::cout << Belos::Belos_Version() << std::endl << std::endl; |
| 113 |
} |
| 114 |
|
| 115 |
// Set up the test problem. |
| 116 |
// |
| 117 |
// We use Trilinos' Galeri package to construct a test problem. |
| 118 |
// Here, we use a discretization of the 2-D Laplacian operator. |
| 119 |
// The global mesh size is nx * nx. |
| 120 |
|
| 121 |
Teuchos::ParameterList GaleriList; |
| 122 |
GaleriList.set ("n", nx * nx ); |
| 123 |
GaleriList.set ("nx", nx); |
| 124 |
GaleriList.set ("ny", nx); |
| 125 |
|
| 126 |
auto Map = RCP{Galeri::Xpetra::CreateMap<ST,GO,tmap_t>("Cartesian2D", comm, GaleriList)}; |
| 127 |
auto GaleriProblem = Galeri::Xpetra::BuildProblem<ST,LO,GO,tmap_t,tcrsmatrix_t,MV>("Laplace2D", Map, GaleriList); |
| 128 |
|
| 129 |
// Create matrix from problem |
| 130 |
auto A = GaleriProblem->BuildMatrix(); |
| 131 |
|
| 132 |
// Create RHS using random solution vector |
| 133 |
RCP<MV> B = rcp (new MV (Map, numrhs)); |
| 134 |
RCP<MV> X = rcp (new MV (Map, numrhs)); |
| 135 |
RCP<MV> Xexact = rcp (new MV (Map, numrhs)); |
| 136 |
MVT::MvRandom(*Xexact); |
| 137 |
|
| 138 |
OPT::Apply(*A, *Xexact, *B ); |
| 139 |
|
| 140 |
// ********Other information used by block solver*********** |
| 141 |
// *****************(can be user specified)****************** |
| 142 |
|
| 143 |
const int numGlobalElements = B->getGlobalLength(); |
| 144 |
if (maxIters == -1) |
| 145 |
maxIters = numGlobalElements/blockSize - 1; // maximum number of iterations to run |
| 146 |
|
| 147 |
ParameterList belosList; |
| 148 |
belosList.set( "Num Blocks", maxSubspace); // Maximum number of blocks in Krylov factorization |
| 149 |
belosList.set( "Block Size", blockSize ); // BlockSize to be used by iterative solver |
| 150 |
belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed |
| 151 |
belosList.set( "Maximum Restarts", maxRestarts ); // Maximum number of restarts allowed |
| 152 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 153 |
int verbosity = Belos::Errors + Belos::Warnings; |
| 154 |
if (verbose) { |
| 155 |
verbosity += Belos::TimingDetails + Belos::StatusTestDetails; |
| 156 |
if (frequency > 0) |
| 157 |
belosList.set( "Output Frequency", frequency ); |
| 158 |
} |
| 159 |
if (debug) { |
| 160 |
verbosity += Belos::Debug; |
| 161 |
} |
| 162 |
belosList.set( "Verbosity", verbosity ); |
| 163 |
|
| 164 |
// Construct an unpreconditioned linear problem instance. |
| 165 |
Belos::LinearProblem<ST,MV,OP> problem( A, X, B ); |
| 166 |
bool set = problem.setProblem(); |
| 167 |
if (set == false) { |
| 168 |
if (procVerbose) |
| 169 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
// |
| 174 |
// ******************************************************************* |
| 175 |
// ****************Start the solver iteration************************* |
| 176 |
// ******************************************************************* |
| 177 |
// |
| 178 |
// Create a solver factory |
| 179 |
Belos::SolverFactory<double,MV,OP> factory; |
| 180 |
|
| 181 |
// Create an iterative solver manager |
| 182 |
std::string solverName = "Block GMRES"; |
| 183 |
RCP< Belos::SolverManager<double,MV,OP> > newSolver = factory.create (solverName, rcp(&belosList,false)); |
| 184 |
|
| 185 |
// Set the problem on the solver manager |
| 186 |
newSolver->setProblem( rcp(&problem,false) ); |
| 187 |
|
| 188 |
|
| 189 |
// **********Print out information about problem******************* |
| 190 |
|
| 191 |
if (procVerbose) { |
| 192 |
std::cout << std::endl << std::endl; |
| 193 |
std::cout << "Dimension of matrix: " << numGlobalElements << std::endl; |
| 194 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 195 |
std::cout << "Block size used by solver: " << blockSize << std::endl; |
| 196 |
std::cout << "Max number of restarts allowed: " << maxRestarts << std::endl; |
| 197 |
std::cout << "Max number of Gmres iterations per linear system: " << maxIters << std::endl; |
| 198 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 199 |
std::cout << std::endl; |
| 200 |
} |
| 201 |
|
| 202 |
// Perform solve |
| 203 |
Belos::ReturnType ret = newSolver->solve(); |
| 204 |
|
| 205 |
// Get the number of iterations for this solve. |
| 206 |
int numIters = newSolver->getNumIters(); |
| 207 |
if (procVerbose) |
| 208 |
std::cout << "Number of iterations performed for this solve: " << numIters << std::endl; |
| 209 |
|
| 210 |
// Compute actual residuals. |
| 211 |
bool badRes = false; |
| 212 |
std::vector<ST> actualResids( numrhs ); |
| 213 |
std::vector<ST> rhsNorm( numrhs ); |
| 214 |
MV resid(Map, numrhs); |
| 215 |
OPT::Apply( *A, *X, resid ); |
| 216 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 217 |
MVT::MvNorm( resid, actualResids ); |
| 218 |
MVT::MvNorm( *B, rhsNorm ); |
| 219 |
if (procVerbose) { |
| 220 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 221 |
for ( int i=0; i<numrhs; i++) { |
| 222 |
ST actRes = actualResids[i]/rhsNorm[i]; |
| 223 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 224 |
if (actRes > tol) badRes = true; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if (ret!=Belos::Converged || badRes) { |
| 229 |
success = false; |
| 230 |
if (procVerbose) |
| 231 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 232 |
} else { |
| 233 |
if (procVerbose) |
| 234 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 235 |
} |
| 236 |
} |
| 237 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 238 |
|
| 239 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 240 |
} |
| 241 |
|
| 242 |
int main(int argc, char *argv[]) { |
| 243 |
// run with different ST |
| 244 |
return run<double>(argc,argv); |
| 245 |
// run<float>(argc,argv); // FAILS |
| 246 |
} |