| 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 |
// Update of belos/epetra/example/BlockCG/PseudoBlockCGEpetraExFile.cpp |
| 11 |
// |
| 12 |
// This driver reads a problem from a file, which can be in Harwell-Boeing (*.hb), |
| 13 |
// Matrix Market (*.mtx), or triplet format (*.triU, *.triS). The right-hand side |
| 14 |
// from the problem, if it exists, will be used instead of multiple |
| 15 |
// random right-hand-sides. The initial guesses are all set to zero. |
| 16 |
// |
| 17 |
// NOTE: No preconditioner is used in this example. |
| 18 |
// |
| 19 |
|
| 20 |
// Tpetra |
| 21 |
#include <Tpetra_Map.hpp> |
| 22 |
#include <Tpetra_Core.hpp> |
| 23 |
#include <Tpetra_Vector.hpp> |
| 24 |
#include <Tpetra_CrsMatrix.hpp> |
| 25 |
// I/O for Harwell-Boeing files |
| 26 |
#define HIDE_TPETRA_INOUT_IMPLEMENTATIONS |
| 27 |
#include <Tpetra_MatrixIO.hpp> |
| 28 |
|
| 29 |
// Teuchos |
| 30 |
#include <Teuchos_RCP.hpp> |
| 31 |
#include <Teuchos_Comm.hpp> |
| 32 |
#include <Teuchos_CommHelpers.hpp> |
| 33 |
#include <Teuchos_DefaultComm.hpp> |
| 34 |
#include "Teuchos_ParameterList.hpp" |
| 35 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 36 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 37 |
|
| 38 |
// Belos |
| 39 |
#include "BelosConfigDefs.hpp" |
| 40 |
#include "BelosLinearProblem.hpp" |
| 41 |
#include "BelosPseudoBlockCGSolMgr.hpp" |
| 42 |
#include "BelosTpetraTestFramework.hpp" |
| 43 |
|
| 44 |
template <typename ScalarType> |
| 45 |
int run(int argc, char *argv[]) { |
| 46 |
// |
| 47 |
using ST = typename Tpetra::Vector<ScalarType>::scalar_type; |
| 48 |
using LO = typename Tpetra::Vector<>::local_ordinal_type; |
| 49 |
using GO = typename Tpetra::Vector<>::global_ordinal_type; |
| 50 |
using NT = typename Tpetra::Vector<>::node_type; |
| 51 |
|
| 52 |
using SCT = typename Teuchos::ScalarTraits<ST>; |
| 53 |
using MT = typename SCT::magnitudeType; |
| 54 |
using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>; |
| 55 |
using OP = typename Tpetra::Operator<ST,LO,GO,NT>; |
| 56 |
|
| 57 |
using tmap_t = Tpetra::Map<LO,GO,NT>; |
| 58 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>; |
| 59 |
|
| 60 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 61 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 62 |
|
| 63 |
using Teuchos::RCP; |
| 64 |
using Teuchos::rcp; |
| 65 |
using Teuchos::ParameterList; |
| 66 |
// |
| 67 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout); |
| 68 |
const auto Comm = Tpetra::getDefaultComm(); |
| 69 |
const int MyPID = Comm->getRank(); |
| 70 |
|
| 71 |
bool verbose = false; |
| 72 |
bool success = true; |
| 73 |
try { |
| 74 |
bool procVerbose = false; |
| 75 |
int frequency = -1; // frequency of status test output. |
| 76 |
int blockSize = 1; // blockSize |
| 77 |
int numrhs = 1; // number of right-hand sides to solve for |
| 78 |
int maxIters = -1; // maximum number of iterations allowed per linear system |
| 79 |
std::string filename("bcsstk14.hb"); |
| 80 |
MT tol = 1.0e-5; // relative residual tolerance |
| 81 |
|
| 82 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 83 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 84 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 85 |
cmdp.setOption("filename",&filename,"Filename for test matrix. Acceptable file extensions: *.hb,*.mtx,*.triU,*.triS"); |
| 86 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver."); |
| 87 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 88 |
cmdp.setOption("block-size",&blockSize,"Block size used by CG."); |
| 89 |
cmdp.setOption("max-iters",&maxIters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 90 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 91 |
return -1; |
| 92 |
} |
| 93 |
if (!verbose) |
| 94 |
frequency = -1; // reset frequency if test is not verbose |
| 95 |
|
| 96 |
procVerbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 97 |
|
| 98 |
// Get the problem |
| 99 |
RCP<tcrsmatrix_t> A; |
| 100 |
Tpetra::Utils::readHBMatrix(filename,Comm,A); |
| 101 |
RCP<const tmap_t> Map = A->getDomainMap(); |
| 102 |
|
| 103 |
|
| 104 |
// Create initial vectors |
| 105 |
RCP<MV> B, X; |
| 106 |
X = rcp( new MV(Map,numrhs) ); |
| 107 |
MVT::MvRandom( *X ); |
| 108 |
B = rcp( new MV(Map,numrhs) ); |
| 109 |
OPT::Apply( *A, *X, *B ); |
| 110 |
MVT::MvInit( *X, 0.0 ); |
| 111 |
|
| 112 |
// ********Other information used by block solver*********** |
| 113 |
// *****************(can be user specified)****************** |
| 114 |
|
| 115 |
const int numGlobalElements = B->getGlobalLength(); |
| 116 |
if (maxIters == -1) |
| 117 |
maxIters = numGlobalElements - 1; // maximum number of iterations to run |
| 118 |
// |
| 119 |
ParameterList belosList; |
| 120 |
belosList.set( "Maximum Iterations", maxIters ); // Maximum number of iterations allowed |
| 121 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 122 |
if (verbose) { |
| 123 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 124 |
Belos::TimingDetails + Belos::StatusTestDetails ); |
| 125 |
if (frequency > 0) |
| 126 |
belosList.set( "Output Frequency", frequency ); |
| 127 |
} |
| 128 |
else |
| 129 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 130 |
|
| 131 |
// Construct an unpreconditioned linear problem instance. |
| 132 |
Belos::LinearProblem<ST,MV,OP> problem( A, X, B ); |
| 133 |
bool set = problem.setProblem(); |
| 134 |
if (set == false) { |
| 135 |
if (procVerbose) |
| 136 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 137 |
return -1; |
| 138 |
} |
| 139 |
|
| 140 |
// ******************************************************************* |
| 141 |
// **************Start the block CG iteration************************* |
| 142 |
// ******************************************************************* |
| 143 |
|
| 144 |
// Create an iterative solver manager. |
| 145 |
RCP< Belos::PseudoBlockCGSolMgr<ST,MV,OP> > newSolver |
| 146 |
= rcp( new Belos::PseudoBlockCGSolMgr<ST,MV,OP>(rcp(&problem,false), rcp(&belosList,false)) ); |
| 147 |
|
| 148 |
|
| 149 |
// Print out information about problem |
| 150 |
if (procVerbose) { |
| 151 |
std::cout << std::endl << std::endl; |
| 152 |
std::cout << "Dimension of matrix: " << numGlobalElements << std::endl; |
| 153 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 154 |
std::cout << "Max number of CG iterations: " << maxIters << std::endl; |
| 155 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 156 |
std::cout << std::endl; |
| 157 |
} |
| 158 |
|
| 159 |
// Perform solve |
| 160 |
Belos::ReturnType ret = newSolver->solve(); |
| 161 |
|
| 162 |
// Compute actual residuals |
| 163 |
bool badRes = false; |
| 164 |
std::vector<ST> actualResids( numrhs ); |
| 165 |
std::vector<ST> rhsNorm( numrhs ); |
| 166 |
MV resid(Map, numrhs); |
| 167 |
OPT::Apply( *A, *X, resid ); |
| 168 |
MVT::MvAddMv( -1.0, resid, 1.0, *B, resid ); |
| 169 |
MVT::MvNorm( resid, actualResids ); |
| 170 |
MVT::MvNorm( *B, rhsNorm ); |
| 171 |
if (procVerbose) { |
| 172 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 173 |
for ( int i=0; i<numrhs; i++) { |
| 174 |
ST actRes = actualResids[i]/rhsNorm[i]; |
| 175 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 176 |
if (actRes > tol) badRes = true; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (ret!=Belos::Converged || badRes) { |
| 181 |
success = false; |
| 182 |
if (procVerbose) |
| 183 |
std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl; |
| 184 |
} else { |
| 185 |
success = true; |
| 186 |
if (procVerbose) |
| 187 |
std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl; |
| 188 |
} |
| 189 |
} |
| 190 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 191 |
|
| 192 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 193 |
} |
| 194 |
|
| 195 |
int main(int argc, char *argv[]) { |
| 196 |
// run with different ST |
| 197 |
return run<double>(argc,argv); |
| 198 |
// run<float>(argc,argv); // FAILS |
| 199 |
} |