| 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 |
// The right-hand-side corresponds to a randomly generated solution. |
| 12 |
// The initial guesses are all set to zero. |
| 13 |
// |
| 14 |
// NOTE: No preconditioner is used in this case. |
| 15 |
// |
| 16 |
#include "BelosConfigDefs.hpp" |
| 17 |
#include "BelosLinearProblem.hpp" |
| 18 |
#include "BelosTpetraAdapter.hpp" |
| 19 |
#include "BelosFixedPointSolMgr.hpp" |
| 20 |
|
| 21 |
// I/O for Harwell-Boeing files |
| 22 |
#define HIDE_TPETRA_INOUT_IMPLEMENTATIONS |
| 23 |
#include <Tpetra_MatrixIO.hpp> |
| 24 |
|
| 25 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 26 |
#include <Teuchos_ParameterList.hpp> |
| 27 |
#include <Teuchos_GlobalMPISession.hpp> |
| 28 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 29 |
#include <Tpetra_Core.hpp> |
| 30 |
#include <Tpetra_CrsMatrix.hpp> |
| 31 |
|
| 32 |
using namespace Teuchos; |
| 33 |
using Tpetra::Operator; |
| 34 |
using Tpetra::CrsMatrix; |
| 35 |
using Tpetra::MultiVector; |
| 36 |
using std::endl; |
| 37 |
using std::cout; |
| 38 |
using std::vector; |
| 39 |
using Teuchos::tuple; |
| 40 |
|
| 41 |
int main(int argc, char *argv[]) { |
| 42 |
typedef Tpetra::MultiVector<>::scalar_type ST; |
| 43 |
typedef ScalarTraits<ST> SCT; |
| 44 |
typedef SCT::magnitudeType MT; |
| 45 |
typedef Tpetra::Operator<ST> OP; |
| 46 |
typedef Tpetra::MultiVector<ST> MV; |
| 47 |
typedef Tpetra::Vector<ST> VV; |
| 48 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 49 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 50 |
using GO = MV::global_ordinal_type; |
| 51 |
|
| 52 |
GlobalMPISession mpisess(&argc,&argv,&cout); |
| 53 |
|
| 54 |
bool success = false; |
| 55 |
bool verbose = false; |
| 56 |
try { |
| 57 |
const ST one = SCT::one(); |
| 58 |
|
| 59 |
int MyPID = 0; |
| 60 |
|
| 61 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm(); |
| 62 |
|
| 63 |
// |
| 64 |
// Get test parameters from command-line processor |
| 65 |
// |
| 66 |
bool proc_verbose = false; |
| 67 |
bool debug = false; |
| 68 |
int frequency = -1; // how often residuals are printed by solver |
| 69 |
int numrhs = 1; // total number of right-hand sides to solve for |
| 70 |
int maxiters = -1; // maximum number of iterations for solver to use |
| 71 |
std::string filename("bcsstk14.hb"); |
| 72 |
MT tol = 1.0e-5; // relative residual tolerance |
| 73 |
bool precond = false; // use diagonal preconditioner |
| 74 |
bool leftPrecond = false; // if preconditioner is used, left or right? |
| 75 |
|
| 76 |
CommandLineProcessor cmdp(false,true); |
| 77 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 78 |
cmdp.setOption("debug","nodebug",&debug,"Run debugging checks."); |
| 79 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 80 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by fixed point solver."); |
| 81 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 82 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 83 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size)."); |
| 84 |
cmdp.setOption("use-precond","no-precond",&precond,"Use a diagonal preconditioner."); |
| 85 |
cmdp.setOption("left","right",&leftPrecond,"Use a left/right preconditioner."); |
| 86 |
if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 87 |
return -1; |
| 88 |
} |
| 89 |
if (debug) { |
| 90 |
verbose = true; |
| 91 |
} |
| 92 |
if (!verbose) { |
| 93 |
frequency = -1; // reset frequency if test is not verbose |
| 94 |
} |
| 95 |
|
| 96 |
MyPID = rank(*comm); |
| 97 |
proc_verbose = ( verbose && (MyPID==0) ); |
| 98 |
|
| 99 |
if (proc_verbose) { |
| 100 |
std::cout << Belos::Belos_Version() << std::endl << std::endl; |
| 101 |
} |
| 102 |
|
| 103 |
// |
| 104 |
// Get the data from the HB file and build the Map,Matrix |
| 105 |
// |
| 106 |
RCP<CrsMatrix<ST> > A; |
| 107 |
Tpetra::Utils::readHBMatrix(filename,comm,A); |
| 108 |
RCP<const Tpetra::Map<> > map = A->getDomainMap(); |
| 109 |
|
| 110 |
// Create initial vectors |
| 111 |
RCP<MV> B, X; |
| 112 |
X = rcp( new MV(map,numrhs) ); |
| 113 |
MVT::MvRandom( *X ); |
| 114 |
B = rcp( new MV(map,numrhs) ); |
| 115 |
OPT::Apply( *A, *X, *B ); |
| 116 |
MVT::MvInit( *X, 0.0 ); |
| 117 |
|
| 118 |
|
| 119 |
// Scale down the rhs |
| 120 |
std::vector<ST> norm( MVT::GetNumberVecs(*B)); |
| 121 |
MVT::MvNorm(*B,norm); |
| 122 |
for(int i=0; i< MVT::GetNumberVecs(*B); i++) |
| 123 |
norm[i] = 1.0 / norm[i]; |
| 124 |
MVT::MvScale(*B,norm); |
| 125 |
|
| 126 |
// Drastically bump the diagonal and then rescale so FP can converge |
| 127 |
{ |
| 128 |
VV diag(A->getRowMap()); |
| 129 |
A->getLocalDiagCopy(diag); |
| 130 |
{ |
| 131 |
Teuchos::ArrayRCP<ST> dd=diag.getDataNonConst(); |
| 132 |
|
| 133 |
auto GlobalElements = A->getRowMap()->getLocalElementList(); |
| 134 |
A->resumeFill(); |
| 135 |
for(int i=0; i<(int)dd.size(); i++) { |
| 136 |
dd[i]=dd[i]*1e4; |
| 137 |
A->replaceGlobalValues(GlobalElements[i], |
| 138 |
Teuchos::tuple<GO>(GlobalElements[i]), |
| 139 |
Teuchos::tuple<ST>(dd[i]) ); |
| 140 |
} |
| 141 |
A->fillComplete(); |
| 142 |
|
| 143 |
for(int i=0; i<(int)dd.size(); i++) |
| 144 |
dd[i] = 1.0/sqrt(dd[i]); |
| 145 |
} |
| 146 |
A->leftScale(diag); |
| 147 |
A->rightScale(diag); |
| 148 |
} |
| 149 |
|
| 150 |
// |
| 151 |
// ********Other information used by block solver*********** |
| 152 |
// *****************(can be user specified)****************** |
| 153 |
// |
| 154 |
const int NumGlobalElements = B->getGlobalLength(); |
| 155 |
if (maxiters == -1) { |
| 156 |
maxiters = NumGlobalElements- 1; // maximum number of iterations to run |
| 157 |
} |
| 158 |
// |
| 159 |
ParameterList belosList; |
| 160 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 161 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 162 |
int verbLevel = Belos::Errors + Belos::Warnings; |
| 163 |
if (debug) { |
| 164 |
verbLevel += Belos::Debug; |
| 165 |
} |
| 166 |
if (verbose) { |
| 167 |
verbLevel += Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails; |
| 168 |
} |
| 169 |
belosList.set( "Verbosity", verbLevel ); |
| 170 |
if (verbose) { |
| 171 |
if (frequency > 0) { |
| 172 |
belosList.set( "Output Frequency", frequency ); |
| 173 |
} |
| 174 |
} |
| 175 |
// |
| 176 |
// Construct an linear problem instance. |
| 177 |
// |
| 178 |
Belos::LinearProblem<ST,MV,OP> problem( A, X, B ); |
| 179 |
// diagonal preconditioner |
| 180 |
if (precond) { |
| 181 |
VV diagonal(A->getRowMap()); |
| 182 |
A->getLocalDiagCopy(diagonal); |
| 183 |
|
| 184 |
int NumMyElements = diagonal.getMap()->getLocalNumElements(); |
| 185 |
auto MyGlobalElements = diagonal.getMap()->getLocalElementList(); |
| 186 |
Teuchos::ArrayRCP<ST> dd=diagonal.getDataNonConst(); |
| 187 |
RCP<CrsMatrix<ST> > invDiagMatrix = Teuchos::rcp(new CrsMatrix<ST>(A->getRowMap(), 1)); |
| 188 |
|
| 189 |
for (Teuchos_Ordinal i=0; i<NumMyElements; ++i) { |
| 190 |
invDiagMatrix->insertGlobalValues(MyGlobalElements[i], |
| 191 |
Teuchos::tuple<GO>(MyGlobalElements[i]), |
| 192 |
Teuchos::tuple<ST>(SCT::one() / dd[i]) ); |
| 193 |
} |
| 194 |
invDiagMatrix->fillComplete(); |
| 195 |
|
| 196 |
if (leftPrecond) |
| 197 |
problem.setLeftPrec(invDiagMatrix); |
| 198 |
else |
| 199 |
problem.setRightPrec(invDiagMatrix); |
| 200 |
} |
| 201 |
bool set = problem.setProblem(); |
| 202 |
if (set == false) { |
| 203 |
if (proc_verbose) |
| 204 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 205 |
return -1; |
| 206 |
} |
| 207 |
// |
| 208 |
// ******************************************************************* |
| 209 |
// *************Start the fixed point iteration*********************** |
| 210 |
// ******************************************************************* |
| 211 |
// |
| 212 |
Belos::FixedPointSolMgr<ST,MV,OP> solver( rcpFromRef(problem), rcpFromRef(belosList) ); |
| 213 |
|
| 214 |
// |
| 215 |
// **********Print out information about problem******************* |
| 216 |
// |
| 217 |
if (proc_verbose) { |
| 218 |
std::cout << std::endl << std::endl; |
| 219 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 220 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 221 |
std::cout << "Max number of fixed point iterations: " << maxiters << std::endl; |
| 222 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 223 |
std::cout << std::endl; |
| 224 |
} |
| 225 |
// |
| 226 |
// Perform solve |
| 227 |
// |
| 228 |
Belos::ReturnType ret = solver.solve(); |
| 229 |
// |
| 230 |
// Compute actual residuals. |
| 231 |
// |
| 232 |
bool badRes = false; |
| 233 |
std::vector<MT> actual_resids( numrhs ); |
| 234 |
std::vector<MT> rhs_norm( numrhs ); |
| 235 |
MV resid(map, numrhs); |
| 236 |
OPT::Apply( *A, *X, resid ); |
| 237 |
MVT::MvAddMv( -one, resid, one, *B, resid ); |
| 238 |
MVT::MvNorm( resid, actual_resids ); |
| 239 |
MVT::MvNorm( *B, rhs_norm ); |
| 240 |
if (proc_verbose) { |
| 241 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 242 |
} |
| 243 |
for ( int i=0; i<numrhs; i++) { |
| 244 |
MT actRes = actual_resids[i]/rhs_norm[i]; |
| 245 |
if (proc_verbose) { |
| 246 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 247 |
} |
| 248 |
if (actRes > tol) badRes = true; |
| 249 |
} |
| 250 |
|
| 251 |
success = (ret==Belos::Converged && !badRes); |
| 252 |
|
| 253 |
if (success) { |
| 254 |
if (proc_verbose) |
| 255 |
std::cout << "\nEnd Result: TEST PASSED" << std::endl; |
| 256 |
} else { |
| 257 |
if (proc_verbose) |
| 258 |
std::cout << "\nEnd Result: TEST FAILED" << std::endl; |
| 259 |
} |
| 260 |
} |
| 261 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 262 |
|
| 263 |
return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); |
| 264 |
} // end test_fp_hb.cpp |