| 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 |
#include "BelosConfigDefs.hpp" |
| 15 |
#include "BelosLinearProblem.hpp" |
| 16 |
#include "BelosEpetraAdapter.hpp" |
| 17 |
#include "BelosPseudoBlockTFQMRSolMgr.hpp" |
| 18 |
#include "BelosEpetraUtils.h" |
| 19 |
#include "Ifpack_IlukGraph.h" |
| 20 |
#include "Ifpack_CrsRiluk.h" |
| 21 |
#include "Epetra_CrsMatrix.h" |
| 22 |
#include "Epetra_Map.h" |
| 23 |
#include "Teuchos_CommandLineProcessor.hpp" |
| 24 |
#include "Teuchos_ParameterList.hpp" |
| 25 |
#include "Teuchos_StandardCatchMacros.hpp" |
| 26 |
#include "Teuchos_Assert.hpp" |
| 27 |
|
| 28 |
int main(int argc, char *argv[]) { |
| 29 |
// |
| 30 |
Teuchos::GlobalMPISession session(&argc, &argv, NULL); |
| 31 |
// |
| 32 |
typedef double ST; |
| 33 |
typedef Teuchos::ScalarTraits<ST> SCT; |
| 34 |
typedef SCT::magnitudeType MT; |
| 35 |
typedef Epetra_MultiVector MV; |
| 36 |
typedef Epetra_Operator OP; |
| 37 |
typedef Belos::MultiVecTraits<ST,MV> MVT; |
| 38 |
typedef Belos::OperatorTraits<ST,MV,OP> OPT; |
| 39 |
|
| 40 |
using Teuchos::ParameterList; |
| 41 |
using Teuchos::RCP; |
| 42 |
using Teuchos::rcp; |
| 43 |
|
| 44 |
bool verbose = false; |
| 45 |
bool success = true; |
| 46 |
try { |
| 47 |
bool proc_verbose = false; |
| 48 |
bool leftprec = true; // use left preconditioning to solve these linear systems |
| 49 |
int frequency = -1; // how often residuals are printed by solver |
| 50 |
int numrhs = 1; |
| 51 |
int maxiters = -1; // maximum iterations allowed |
| 52 |
std::string filename("orsirr1.hb"); |
| 53 |
MT tol = 1.0e-5; // relative residual tolerance |
| 54 |
|
| 55 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 56 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 57 |
cmdp.setOption("left-prec","right-prec",&leftprec,"Left preconditioning or right."); |
| 58 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 59 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 60 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by GMRES solver."); |
| 61 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 62 |
cmdp.setOption("maxiters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)."); |
| 63 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 64 |
return -1; |
| 65 |
} |
| 66 |
if (!verbose) |
| 67 |
frequency = -1; // reset frequency if test is not verbose |
| 68 |
// |
| 69 |
// Get the problem |
| 70 |
// |
| 71 |
int MyPID; |
| 72 |
RCP<Epetra_CrsMatrix> A; |
| 73 |
int return_val =Belos::Util::createEpetraProblem(filename,NULL,&A,NULL,NULL,&MyPID); |
| 74 |
const Epetra_Map &Map = A->RowMap(); |
| 75 |
if(return_val != 0) return return_val; |
| 76 |
proc_verbose = verbose && (MyPID==0); /* Only print on zero processor */ |
| 77 |
// |
| 78 |
// *****Construct the Preconditioner***** |
| 79 |
// |
| 80 |
if (proc_verbose) std::cout << std::endl << std::endl; |
| 81 |
if (proc_verbose) std::cout << "Constructing ILU preconditioner" << std::endl; |
| 82 |
int Lfill = 2; |
| 83 |
// if (argc > 2) Lfill = atoi(argv[2]); |
| 84 |
if (proc_verbose) std::cout << "Using Lfill = " << Lfill << std::endl; |
| 85 |
int Overlap = 2; |
| 86 |
// if (argc > 3) Overlap = atoi(argv[3]); |
| 87 |
if (proc_verbose) std::cout << "Using Level Overlap = " << Overlap << std::endl; |
| 88 |
double Athresh = 0.0; |
| 89 |
// if (argc > 4) Athresh = atof(argv[4]); |
| 90 |
if (proc_verbose) std::cout << "Using Absolute Threshold Value of " << Athresh << std::endl; |
| 91 |
double Rthresh = 1.0; |
| 92 |
// if (argc >5) Rthresh = atof(argv[5]); |
| 93 |
if (proc_verbose) std::cout << "Using Relative Threshold Value of " << Rthresh << std::endl; |
| 94 |
// |
| 95 |
Teuchos::RCP<Ifpack_IlukGraph> ilukGraph; |
| 96 |
Teuchos::RCP<Ifpack_CrsRiluk> ilukFactors; |
| 97 |
// |
| 98 |
if (Lfill > -1) { |
| 99 |
ilukGraph = Teuchos::rcp(new Ifpack_IlukGraph(A->Graph(), Lfill, Overlap)); |
| 100 |
int info = ilukGraph->ConstructFilledGraph(); |
| 101 |
TEUCHOS_ASSERT( info == 0 ); |
| 102 |
ilukFactors = Teuchos::rcp(new Ifpack_CrsRiluk(*ilukGraph)); |
| 103 |
int initerr = ilukFactors->InitValues(*A); |
| 104 |
if (initerr != 0) std::cout << "InitValues error = " << initerr; |
| 105 |
info = ilukFactors->Factor(); |
| 106 |
TEUCHOS_ASSERT( info == 0 ); |
| 107 |
} |
| 108 |
// |
| 109 |
bool transA = false; |
| 110 |
double Cond_Est; |
| 111 |
ilukFactors->Condest(transA, Cond_Est); |
| 112 |
if (proc_verbose) { |
| 113 |
std::cout << "Condition number estimate for this preconditoner = " << Cond_Est << std::endl; |
| 114 |
std::cout << std::endl; |
| 115 |
} |
| 116 |
|
| 117 |
// |
| 118 |
// Create the Belos preconditioned operator from the Ifpack preconditioner. |
| 119 |
// NOTE: This is necessary because Belos expects an operator to apply the |
| 120 |
// preconditioner with Apply() NOT ApplyInverse(). |
| 121 |
RCP<Belos::EpetraPrecOp> Prec = rcp( new Belos::EpetraPrecOp( ilukFactors ) ); |
| 122 |
|
| 123 |
// |
| 124 |
// ********Other information used by block solver*********** |
| 125 |
// *****************(can be user specified)****************** |
| 126 |
// |
| 127 |
const int NumGlobalElements = Map.NumGlobalElements(); |
| 128 |
if (maxiters == -1) |
| 129 |
maxiters = NumGlobalElements - 1; // maximum number of iterations to run |
| 130 |
// |
| 131 |
ParameterList belosList; |
| 132 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 133 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 134 |
if (leftprec) |
| 135 |
belosList.set( "Explicit Residual Test", true ); // Need to check for the explicit residual before returning |
| 136 |
if (verbose) { |
| 137 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 138 |
Belos::TimingDetails + Belos::StatusTestDetails ); |
| 139 |
if (frequency > 0) |
| 140 |
belosList.set( "Output Frequency", frequency ); |
| 141 |
} |
| 142 |
else |
| 143 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings ); |
| 144 |
// |
| 145 |
// *****Construct solution std::vector and random right-hand-sides ***** |
| 146 |
// |
| 147 |
RCP<Epetra_MultiVector> X = rcp( new Epetra_MultiVector(Map, numrhs) ); |
| 148 |
X->PutScalar( 0.0 ); |
| 149 |
RCP<Epetra_MultiVector> B = rcp( new Epetra_MultiVector(Map, numrhs) ); |
| 150 |
B->Random(); |
| 151 |
Belos::LinearProblem<double,MV,OP> problem( A, X, B ); |
| 152 |
if (leftprec) |
| 153 |
problem.setLeftPrec( Prec ); |
| 154 |
else |
| 155 |
problem.setRightPrec( Prec ); |
| 156 |
|
| 157 |
bool set = problem.setProblem(); |
| 158 |
if (set == false) { |
| 159 |
if (proc_verbose) |
| 160 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 161 |
return -1; |
| 162 |
} |
| 163 |
// |
| 164 |
// ******************************************************************* |
| 165 |
// ************Start the Pseudo Block TFQMR iteration***************** |
| 166 |
// ******************************************************************* |
| 167 |
// |
| 168 |
Teuchos::RCP< Belos::PseudoBlockTFQMRSolMgr<double,MV,OP> > solver = |
| 169 |
Teuchos::rcp( new Belos::PseudoBlockTFQMRSolMgr<double,MV,OP>( rcp(&problem,false), rcp(&belosList,false) ) ); |
| 170 |
// |
| 171 |
// **********Print out information about problem******************* |
| 172 |
// |
| 173 |
if (proc_verbose) { |
| 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 |
// |
| 184 |
Belos::ReturnType ret = solver->solve(); |
| 185 |
|
| 186 |
// |
| 187 |
// Compute actual residuals. |
| 188 |
// |
| 189 |
bool badRes = false; |
| 190 |
std::vector<double> actual_resids( numrhs ); |
| 191 |
std::vector<double> rhs_norm( numrhs ); |
| 192 |
Epetra_MultiVector R(Map, numrhs); |
| 193 |
OPT::Apply( *A, *X, R ); |
| 194 |
MVT::MvAddMv( -1.0, R, 1.0, *B, R ); |
| 195 |
MVT::MvNorm( R, actual_resids ); |
| 196 |
MVT::MvNorm( *B, rhs_norm ); |
| 197 |
if (proc_verbose) { |
| 198 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 199 |
for ( int i=0; i<numrhs; i++) { |
| 200 |
double actRes = actual_resids[i]/rhs_norm[i]; |
| 201 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 202 |
if (actRes > tol ) badRes = true; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (ret!=Belos::Converged || badRes) { |
| 207 |
success = false; |
| 208 |
if (proc_verbose) |
| 209 |
std::cout << std::endl << "ERROR: Belos did not converge!" << std::endl; |
| 210 |
} else { |
| 211 |
success = true; |
| 212 |
if (proc_verbose) |
| 213 |
std::cout << std::endl << "SUCCESS: Belos converged!" << std::endl; |
| 214 |
} |
| 215 |
} |
| 216 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 217 |
|
| 218 |
return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 219 |
} |