| 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 |
// The problem is solver for multiple scalar types, and timings are reported. |
| 14 |
// |
| 15 |
// NOTE: No preconditioner is used in this case. |
| 16 |
|
| 17 |
#include "BelosConfigDefs.hpp" |
| 18 |
#include "BelosLinearProblem.hpp" |
| 19 |
#include "BelosTpetraAdapter.hpp" |
| 20 |
#include "BelosPseudoBlockCGSolMgr.hpp" |
| 21 |
|
| 22 |
#include <Tpetra_Core.hpp> |
| 23 |
#include <Tpetra_CrsMatrix.hpp> |
| 24 |
#include <Tpetra_DiagPrecond.hpp> |
| 25 |
|
| 26 |
// I/O for Harwell-Boeing files |
| 27 |
#include <Trilinos_Util_iohb.h> |
| 28 |
|
| 29 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 30 |
#include <Teuchos_ScalarTraits.hpp> |
| 31 |
#include <Teuchos_TypeNameTraits.hpp> |
| 32 |
|
| 33 |
using namespace Teuchos; |
| 34 |
using namespace Belos; |
| 35 |
|
| 36 |
using Tpetra::Operator; |
| 37 |
using Tpetra::CrsMatrix; |
| 38 |
using Tpetra::DiagPrecond; |
| 39 |
using Tpetra::MultiVector; |
| 40 |
using Tpetra::Vector; |
| 41 |
using Tpetra::Map; |
| 42 |
|
| 43 |
using std::endl; |
| 44 |
using std::cout; |
| 45 |
using std::string; |
| 46 |
using std::setw; |
| 47 |
using std::vector; |
| 48 |
|
| 49 |
using Teuchos::tuple; |
| 50 |
|
| 51 |
bool proc_verbose = false, reduce_tol, precond = true, dumpdata = false; |
| 52 |
RCP<Map<int> > vmap; |
| 53 |
RCP<MultiVector<float,int> > actX; |
| 54 |
ParameterList mptestpl; |
| 55 |
int rnnzmax; |
| 56 |
int dim, numrhs; |
| 57 |
float *fvals; |
| 58 |
int *colptr, *rowind; |
| 59 |
int mptestmypid = 0; |
| 60 |
|
| 61 |
/////////////////////////////////////////////////////////////////////////// |
| 62 |
template <class Scalar> |
| 63 |
RCP<LinearProblem<Scalar,MultiVector<Scalar,int>,Operator<Scalar,int> > > buildProblem() |
| 64 |
{ |
| 65 |
using SCT = typename ScalarTraits<Scalar>; |
| 66 |
using MT = typename SCT::magnitudeType; |
| 67 |
using OP = typename Operator<Scalar,int>; |
| 68 |
using MV = typename MultiVector<Scalar,int>; |
| 69 |
using OPT = typename OperatorTraits<Scalar,MV,OP>; |
| 70 |
using MVT = typename MultiVecTraits<Scalar,MV>; |
| 71 |
|
| 72 |
RCP<CrsMatrix<Scalar,int> > A = rcp(new CrsMatrix<Scalar,int>(*vmap,rnnzmax)); |
| 73 |
if (mptestmypid == 0) { |
| 74 |
// HB format is compressed column. CrsMatrix is compressed row. |
| 75 |
const MT *fptr = fvals; |
| 76 |
const int *rptr = rowind; |
| 77 |
for (int c=0; c<dim; ++c) { |
| 78 |
for (int colnnz=0; colnnz < colptr[c+1]-colptr[c]; ++colnnz) { |
| 79 |
A->insertGlobalValues(*rptr-1,tuple(c),tuple<Scalar>(*fptr)); |
| 80 |
A->insertGlobalValues(c,tuple(*rptr-1),tuple<Scalar>(*fptr)); |
| 81 |
++rptr; |
| 82 |
++fptr; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
// Distribute matrix data to other nodes |
| 87 |
A->fillComplete(); |
| 88 |
// Create initial MV and solution MV |
| 89 |
RCP<MV> B, X; |
| 90 |
X = rcp( new MV(*vmap,numrhs) ); |
| 91 |
// Set LHS to actX |
| 92 |
{ |
| 93 |
typename MultiVector<Scalar,int>::double_pointer Xvals = X->extractView2D(); |
| 94 |
typename MultiVector<Scalar,int>::const_double_pointer actXvals = actX->extractConstView2D(); |
| 95 |
for (Teuchos_Ordinal j=0; j<numrhs; ++j) { |
| 96 |
std::copy(actXvals[j], actXvals[j]+actX->myLength(), Xvals[j]); |
| 97 |
} |
| 98 |
} |
| 99 |
B = rcp( new MV(*vmap,numrhs) ); |
| 100 |
OPT::Apply( *A, *X, *B ); |
| 101 |
MVT::MvInit( *X, 0.0 ); |
| 102 |
|
| 103 |
// Construct a linear problem instance with zero initial MV |
| 104 |
RCP<LinearProblem<Scalar,MV,OP> > problem = rcp( new LinearProblem<Scalar,MV,OP>(A,X,B) ); |
| 105 |
problem->setLabel(Teuchos::typeName(SCT::one())); |
| 106 |
// Diagonal preconditioner |
| 107 |
if (precond) { |
| 108 |
Vector<Scalar,int> diags(A->getRowMap()); |
| 109 |
A->getLocalDiagCopy(diags); |
| 110 |
for (Teuchos_Ordinal i=0; i<vmap->getNumMyEntries(); ++i) { |
| 111 |
TEUCHOS_TEST_FOR_EXCEPTION(diags[i] <= SCT::zero(), std::runtime_error,"Matrix is not positive-definite: " << diags[i]); |
| 112 |
diags[i] = SCT::one() / diags[i]; |
| 113 |
} |
| 114 |
RCP<Operator<Scalar,int> > P = rcp(new DiagPrecond<Scalar,int>(diags)); |
| 115 |
problem->setRightPrec(P); |
| 116 |
} |
| 117 |
TEUCHOS_TEST_FOR_EXCEPT(problem->setProblem() == false); |
| 118 |
return problem; |
| 119 |
} |
| 120 |
|
| 121 |
/////////////////////////////////////////////////////////////////////////// |
| 122 |
template <class Scalar> |
| 123 |
bool runTest(double ltol, double times[], int &numIters, RCP<MultiVector<Scalar,int> > &X) |
| 124 |
{ |
| 125 |
|
| 126 |
using SCT = typename ScalarTraits<Scalar>; |
| 127 |
using MT = typename SCT::magnitudeType; |
| 128 |
using OP = typename Operator<Scalar,int>; |
| 129 |
using MV = typename MultiVector<Scalar,int>; |
| 130 |
using OPT = typename OperatorTraits<Scalar,MV,OP>; |
| 131 |
using MVT = typename MultiVecTraits<Scalar,MV>; |
| 132 |
|
| 133 |
const Scalar ONE = SCT::one(); |
| 134 |
mptestpl.set<MT>( "Convergence Tolerance", ltol ); // Relative convergence tolerance requested |
| 135 |
mptestpl.set( "Timer Label", typeName(ONE) ); // Set timer label to discern between the two solvers. |
| 136 |
|
| 137 |
if (mptestmypid==0) cout << "Testing Scalar == " << typeName(ONE) << endl; |
| 138 |
|
| 139 |
RCP<LinearProblem<Scalar,MV,OP> > problem; |
| 140 |
Time btimer("Build Timer"), ctimer("Construct Timer"), stimer("Solve Timer"); |
| 141 |
ReturnType ret; |
| 142 |
if (mptestmypid==0) cout << "Building problem..." << endl; |
| 143 |
{ |
| 144 |
TimeMonitor localtimer(btimer); |
| 145 |
problem = buildProblem<Scalar>(); |
| 146 |
} |
| 147 |
RCP<SolverManager<Scalar,MV,OP> > solver; |
| 148 |
if (mptestmypid==0) cout << "Constructing solver..." << endl; |
| 149 |
{ |
| 150 |
TimeMonitor localtimer(ctimer); |
| 151 |
solver = rcp(new PseudoBlockCGSolMgr<Scalar,MV,OP>( problem, rcpFromRef(mptestpl) )); |
| 152 |
} |
| 153 |
if (mptestmypid==0) cout << "Solving problem..." << endl; |
| 154 |
{ |
| 155 |
TimeMonitor localtimer(stimer); |
| 156 |
try { |
| 157 |
ret = solver->solve(); |
| 158 |
} |
| 159 |
catch (std::exception &e) { |
| 160 |
cout << "Caught exception: " << endl << e.what() << endl; |
| 161 |
ret = Unconverged; |
| 162 |
} |
| 163 |
} |
| 164 |
numIters = solver->getNumIters(); |
| 165 |
times[0] = btimer.totalElapsedTime(); |
| 166 |
times[1] = ctimer.totalElapsedTime(); |
| 167 |
times[2] = stimer.totalElapsedTime(); |
| 168 |
|
| 169 |
bool badRes = false; |
| 170 |
if (ret == Converged) { |
| 171 |
if (proc_verbose) cout << endl; |
| 172 |
if (mptestmypid==0) cout << "Computing residuals..." << endl; |
| 173 |
// Compute actual residuals. |
| 174 |
RCP<const OP> A = problem->getOperator(); |
| 175 |
X = problem->getLHS(); |
| 176 |
RCP<const MV> B = problem->getRHS(); |
| 177 |
std::vector<MT> actual_resids( numrhs ); |
| 178 |
std::vector<MT> rhs_norm( numrhs ); |
| 179 |
MV resid(*vmap, numrhs); |
| 180 |
OPT::Apply( *A, *X, resid ); |
| 181 |
MVT::MvAddMv( -ONE, resid, ONE, *B, resid ); |
| 182 |
MVT::MvNorm( resid, actual_resids ); |
| 183 |
MVT::MvNorm( *B, rhs_norm ); |
| 184 |
if (proc_verbose) cout << "Actual Residuals (normalized)" << endl; |
| 185 |
for ( int i=0; i<numrhs; i++) { |
| 186 |
MT actRes = actual_resids[i]/rhs_norm[i]; |
| 187 |
if (proc_verbose) cout << "Problem "<<i<<" : \t"<< actRes <<endl; |
| 188 |
if (actRes > ltol) badRes = true; |
| 189 |
} |
| 190 |
if (proc_verbose) cout << endl; |
| 191 |
} |
| 192 |
if (ret != Converged || badRes) return false; |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
/////////////////////////////////////////////////////////////////////////// |
| 197 |
int main(int argc, char *argv[]) |
| 198 |
{ |
| 199 |
Tpetra::ScopeGuard tpetraScope(&argc,&argv); |
| 200 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm(); |
| 201 |
|
| 202 |
// Get test parameters from command-line processor |
| 203 |
bool verbose = false, debug = false; |
| 204 |
bool printX = false; |
| 205 |
int frequency = -1; // how often residuals are printed by solver |
| 206 |
numrhs = 1; // total number of right-hand sides to solve for |
| 207 |
int blocksize = 1; // blocksize used by solver |
| 208 |
int maxiters = -1; // maximum number of iterations for solver to use |
| 209 |
string filename("bcsstk14.hb"); |
| 210 |
double tol = 1.0e-5; // relative residual tolerance |
| 211 |
|
| 212 |
CommandLineProcessor cmdp(false,true); |
| 213 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); |
| 214 |
cmdp.setOption("debug","nodebug",&debug,"Run debugging checks."); |
| 215 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)."); |
| 216 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by CG solver."); |
| 217 |
cmdp.setOption("filename",&filename,"Filename for Harwell-Boeing test matrix."); |
| 218 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for."); |
| 219 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 := adapted to problem/block size)."); |
| 220 |
cmdp.setOption("block-size",&blocksize,"Block size to be used by the CG solver."); |
| 221 |
cmdp.setOption("reduce-tol","fixed-tol",&reduce_tol,"Require increased accuracy from higher precision scalar types."); |
| 222 |
cmdp.setOption("use-precond","no-precond",&precond,"Use a diagonal preconditioner."); |
| 223 |
cmdp.setOption("dump-data","no-dump-data",&dumpdata,"Dump raw data to data.dat."); |
| 224 |
cmdp.setOption("print-x","no-print-x",&printX,"Print solution multivectors."); |
| 225 |
if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 226 |
return -1; |
| 227 |
} |
| 228 |
if (debug) { |
| 229 |
verbose = true; |
| 230 |
} |
| 231 |
if (!verbose) { |
| 232 |
frequency = -1; // reset frequency if test is not verbose |
| 233 |
} |
| 234 |
|
| 235 |
mptestmypid = rank(*comm); |
| 236 |
proc_verbose = ( verbose && (mptestmypid==0) ); |
| 237 |
if (proc_verbose) cout << Belos_Version() << endl << endl; |
| 238 |
|
| 239 |
// Get the data (double) from the HB file and build the Map,Matrix |
| 240 |
int nnz, info; |
| 241 |
nnz = -1; |
| 242 |
if (mptestmypid == 0) { |
| 243 |
int dim2; |
| 244 |
double *dvals; |
| 245 |
info = readHB_newmat_double(filename.c_str(),&dim,&dim2,&nnz,&colptr,&rowind,&dvals); |
| 246 |
// Truncate data into float |
| 247 |
fvals = new float[nnz]; |
| 248 |
std::copy( dvals, dvals+nnz, fvals ); |
| 249 |
free(dvals); |
| 250 |
// Find maximum NNZ over all rows |
| 251 |
vector<int> rnnz(dim,0); |
| 252 |
for (int *ri=rowind; ri<rowind+nnz; ++ri) { |
| 253 |
++rnnz[*ri-1]; |
| 254 |
} |
| 255 |
for (int c=0; c<dim; ++c) { |
| 256 |
rnnz[c] += colptr[c+1]-colptr[c]; |
| 257 |
} |
| 258 |
rnnzmax = *std::max_element(rnnz.begin(),rnnz.end()); |
| 259 |
} |
| 260 |
else { |
| 261 |
// Address uninitialized data warnings |
| 262 |
fvals = NULL; |
| 263 |
colptr = NULL; |
| 264 |
rowind = NULL; |
| 265 |
} |
| 266 |
broadcast(*comm,0,&info); |
| 267 |
broadcast(*comm,0,&nnz); |
| 268 |
broadcast(*comm,0,&dim); |
| 269 |
broadcast(*comm,0,&rnnzmax); |
| 270 |
if (info == 0 || nnz < 0) { |
| 271 |
if (mptestmypid == 0) { |
| 272 |
cout << "Error reading '" << filename << "'" << endl |
| 273 |
<< "End Result: TEST FAILED" << endl; |
| 274 |
} |
| 275 |
return -1; |
| 276 |
} |
| 277 |
// Create map |
| 278 |
vmap = rcp(new Map<int>(dim,0,comm)); |
| 279 |
// Create a consistent LHS |
| 280 |
actX = rcp(new MultiVector<float,int>(*vmap,numrhs)); |
| 281 |
MultiVecTraits<float,MultiVector<float,int> >::MvRandom(*actX); |
| 282 |
// Create the parameter list |
| 283 |
if (maxiters == -1) { |
| 284 |
maxiters = dim/blocksize - 1; // maximum number of iterations to run |
| 285 |
} |
| 286 |
// |
| 287 |
mptestpl.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver |
| 288 |
mptestpl.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 289 |
int verbLevel = Errors + Warnings; |
| 290 |
if (debug) { |
| 291 |
verbLevel |= Debug; |
| 292 |
} |
| 293 |
if (verbose) { |
| 294 |
verbLevel |= TimingDetails + FinalSummary + StatusTestDetails; |
| 295 |
} |
| 296 |
mptestpl.set( "Verbosity", verbLevel ); |
| 297 |
if (verbose) { |
| 298 |
if (frequency > 0) { |
| 299 |
mptestpl.set( "Output Frequency", frequency ); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// Print out information about problem |
| 304 |
if (mptestmypid==0) { |
| 305 |
cout << "Filename: " << filename << endl; |
| 306 |
cout << "Dimension of matrix: " << dim << endl; |
| 307 |
cout << "Number of nonzeros: " << nnz << endl; |
| 308 |
cout << "Number of right-hand sides: " << numrhs << endl; |
| 309 |
cout << "Block size used by solver: " << blocksize << endl; |
| 310 |
cout << "Max number of CG iterations: " << maxiters << endl; |
| 311 |
cout << "Relative residual tolerance: " << tol << endl; |
| 312 |
cout << endl; |
| 313 |
} |
| 314 |
|
| 315 |
// Run tests for different scalar types |
| 316 |
RCP<MultiVector<float,int> > Xf; |
| 317 |
RCP<MultiVector<double,int> > Xd; |
| 318 |
double ltol = tol; |
| 319 |
double ftime[3]; int fiter; bool fpass = runTest<float>(ltol,ftime,fiter,Xf); |
| 320 |
ltol = (reduce_tol ? ltol*ltol : ltol); |
| 321 |
double dtime[3]; int diter; bool dpass = runTest<double>(ltol,dtime,diter,Xd); |
| 322 |
// #ifdef HAVE_TEUCHOS_QD |
| 323 |
// unsigned int old_cw; fpu_fix_start(&old_cw); // fix floating point rounding for intel processors; required for proper implementation |
| 324 |
// ltol = (reduce_tol ? ltol*ltol : ltol); |
| 325 |
// double ddtime[3]; int dditer; bool ddpass = runTest<dd_real>(ltol,ddtime,dditer); |
| 326 |
// ltol = (reduce_tol ? ltol*ltol : ltol); |
| 327 |
// double qdtime[3]; int qditer; bool qdpass = runTest<qd_real>(ltol,qdtime,qditer); |
| 328 |
// fpu_fix_end(&old_cw); // restore previous floating point rounding |
| 329 |
// #endif |
| 330 |
|
| 331 |
if (printX) { |
| 332 |
// doesn't compie; fix |
| 333 |
// RCP<FancyOStream> fos = Teuchos::getFancyOStream(rcp(&std::cout,false)); |
| 334 |
// if (mptestmypid==0) cout << "X (float): " << endl; |
| 335 |
// Xf->describe(fos,Teuchos::VERB_EXTREME); |
| 336 |
// if (mptestmypid==0) cout << "X (double): " << endl; |
| 337 |
// Xd->describe(fos,Teuchos::VERB_EXTREME); |
| 338 |
} |
| 339 |
|
| 340 |
// Test the relative error between Xf and Xd |
| 341 |
if (mptestmypid==0) { |
| 342 |
cout << "Relative error |xdi - xfi|/|xdi|" << endl; |
| 343 |
cout << "--------------------------------" << endl; |
| 344 |
} |
| 345 |
for (int j=0; j<numrhs; ++j) { |
| 346 |
// Compute |xdj-xfj|/|xdj| |
| 347 |
RCP<Vector<float,int> > xfj = (*Xf)(j); |
| 348 |
RCP<Vector<double,int> > xdj = (*Xd)(j); |
| 349 |
double mag = xdj->norm2(); |
| 350 |
ArrayView<double> dval; |
| 351 |
ArrayView<const float> fval; |
| 352 |
xdj->extractView1D(dval); |
| 353 |
xfj->extractConstView1D(fval); |
| 354 |
for (int i=0; i<xdj->myLength(); ++i) { |
| 355 |
dval[i] -= (double)fval[i]; |
| 356 |
} |
| 357 |
double err = xdj->norm2() / mag; |
| 358 |
if (mptestmypid==0) { |
| 359 |
cout << err << endl; |
| 360 |
} |
| 361 |
} |
| 362 |
Xf = Teuchos::null; |
| 363 |
Xd = Teuchos::null; |
| 364 |
|
| 365 |
// Done with the matrix data now; delete it |
| 366 |
if (mptestmypid == 0) { |
| 367 |
free( fvals ); |
| 368 |
free( colptr ); |
| 369 |
free( rowind ); |
| 370 |
} |
| 371 |
|
| 372 |
if (mptestmypid==0) { |
| 373 |
cout << "Scalar field Build time Init time Solve time Num Iters Test Passsed" << endl; |
| 374 |
cout << "---------------------------------------------------------------------------------------" << endl; |
| 375 |
cout << setw(12) << "float" << " " << setw(10) << ftime[0] << " " << setw(9) << ftime[1] << " " << setw(10) << ftime[2] << " " << setw(9) << fiter << " " << ( fpass ? "pass" : "fail") << endl; |
| 376 |
cout << setw(12) << "double" << " " << setw(10) << dtime[0] << " " << setw(9) << dtime[1] << " " << setw(10) << dtime[2] << " " << setw(9) << diter << " " << ( dpass ? "pass" : "fail") << endl; |
| 377 |
// #ifdef HAVE_TEUCHOS_QD |
| 378 |
// cout << setw(12) << "dd_real" << " " << setw(10) << ddtime[0] << " " << setw(9) << ddtime[1] << " " << setw(10) << ddtime[2] << " " << setw(9) << dditer << " " << (ddpass ? "pass" : "fail") << endl; |
| 379 |
// cout << setw(12) << "qd_real" << " " << setw(10) << qdtime[0] << " " << setw(9) << qdtime[1] << " " << setw(10) << qdtime[2] << " " << setw(9) << qditer << " " << (qdpass ? "pass" : "fail") << endl; |
| 380 |
// #endif |
| 381 |
} |
| 382 |
|
| 383 |
if (dumpdata) { |
| 384 |
std::ofstream fout("data.dat",std::ios_base::app | std::ios_base::out); |
| 385 |
fout << setw(14) << nnz << setw(8) << "float" << setw(12) << ftime[0] << setw(12) << ftime[1] << setw(12) << ftime[2] << setw(12) << fiter << endl; |
| 386 |
fout << setw(14) << nnz << setw(8) << "double" << setw(12) << dtime[0] << setw(12) << dtime[1] << setw(12) << dtime[2] << setw(12) << diter << endl; |
| 387 |
// #ifdef HAVE_TEUCHOS_QD |
| 388 |
// fout << setw(14) << nnz << setw(8) << "dd_real" << setw(12) << ddtime[0] << setw(12) << ddtime[1] << setw(12) << ddtime[2] << setw(12) << dditer << endl; |
| 389 |
// fout << setw(14) << nnz << setw(8) << "qd_real" << setw(12) << qdtime[0] << setw(12) << qdtime[1] << setw(12) << qdtime[2] << setw(12) << qditer << endl; |
| 390 |
// #endif |
| 391 |
fout.close(); |
| 392 |
} |
| 393 |
|
| 394 |
bool allpass = fpass && dpass; |
| 395 |
// #ifdef HAVE_TEUCHOS_QD |
| 396 |
// allpass = allpass && ddpass && qdpass; |
| 397 |
// #endif |
| 398 |
|
| 399 |
if (!allpass) { |
| 400 |
if (mptestmypid==0) cout << "\nEnd Result: TEST FAILED" << endl; |
| 401 |
return -1; |
| 402 |
} |
| 403 |
|
| 404 |
// Default return value |
| 405 |
if (mptestmypid==0) cout << "\nEnd Result: TEST PASSED" << endl; |
| 406 |
return 0; |
| 407 |
} |