| 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 |
#include "Teuchos_UnitTestHarness.hpp" |
| 11 |
#include "Teuchos_TypeNameTraits.hpp" |
| 12 |
#include "BelosSolverFactory.hpp" |
| 13 |
#include "BelosTpetraAdapter.hpp" |
| 14 |
#include "Tpetra_CrsMatrix.hpp" |
| 15 |
#include "Tpetra_Core.hpp" |
| 16 |
#include "Tpetra_MultiVector.hpp" |
| 17 |
#include "TpetraCore_ETIHelperMacros.h" |
| 18 |
#include <vector> |
| 19 |
#include <string> |
| 20 |
|
| 21 |
// mfh 12 Oct 2017: Test that the Tpetra specialization of |
| 22 |
// Belos::SolverFactory builds and runs without throwing. |
| 23 |
// See e.g., Trilinos GitHub issue #754. |
| 24 |
|
| 25 |
namespace { // (anonymous) |
| 26 |
|
| 27 |
// Create a very simple square test matrix. We use the identity |
| 28 |
// matrix here. The point of this test is NOT to exercise the solver; |
| 29 |
// it's just to check that Belos' LinearSolverFactory can create |
| 30 |
// working solvers. Belos has more rigorous tests for its solvers. |
| 31 |
template<class SC, class LO, class GO, class NT> |
| 32 |
Teuchos::RCP<Tpetra::CrsMatrix<SC, LO, GO, NT> > |
| 33 |
createTestMatrix (Teuchos::FancyOStream& out, |
| 34 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 35 |
const Tpetra::global_size_t gblNumRows) |
| 36 |
{ |
| 37 |
using Teuchos::Comm; |
| 38 |
using Teuchos::RCP; |
| 39 |
using Teuchos::rcp; |
| 40 |
using std::endl; |
| 41 |
typedef Tpetra::CrsMatrix<SC,LO,GO,NT> MAT; |
| 42 |
typedef Tpetra::Map<LO,GO,NT> map_type; |
| 43 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 44 |
typedef Teuchos::ScalarTraits<typename STS::magnitudeType> MTS; |
| 45 |
|
| 46 |
Teuchos::OSTab tab0 (out); |
| 47 |
out << "Create test matrix with " << gblNumRows << " row(s)" << endl; |
| 48 |
Teuchos::OSTab tab1 (out); |
| 49 |
|
| 50 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 51 |
( gblNumRows == 0, std::invalid_argument, "gblNumRows = 0"); |
| 52 |
|
| 53 |
const GO indexBase = 0; |
| 54 |
RCP<const map_type> rowMap (new map_type (gblNumRows, indexBase, comm)); |
| 55 |
// For this particular row matrix, the row Map and the column Map |
| 56 |
// are the same. Giving a column Map to CrsMatrix's constructor |
| 57 |
// lets us use local indices. |
| 58 |
RCP<const map_type> colMap = rowMap; |
| 59 |
const size_t maxNumEntPerRow = 1; |
| 60 |
RCP<MAT> A (new MAT (rowMap, colMap, maxNumEntPerRow)); |
| 61 |
|
| 62 |
if (rowMap->getLocalNumElements () != 0) { |
| 63 |
Teuchos::Array<SC> vals (1); |
| 64 |
Teuchos::Array<LO> inds (1); |
| 65 |
for (LO lclRow = rowMap->getMinLocalIndex (); |
| 66 |
lclRow <= rowMap->getMaxLocalIndex (); ++lclRow) { |
| 67 |
inds[0] = lclRow; |
| 68 |
vals[0] = MTS::zero () / MTS::zero (); // Fill matrix with NaNs |
| 69 |
A->insertLocalValues (lclRow, inds (), vals ()); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
RCP<const map_type> domMap = rowMap; |
| 74 |
RCP<const map_type> ranMap = rowMap; |
| 75 |
A->fillComplete (domMap, ranMap); |
| 76 |
return A; |
| 77 |
} |
| 78 |
|
| 79 |
// Create a very simple square test linear system (matrix, right-hand |
| 80 |
// side(s), and exact solution(s). We use the identity matrix here. |
| 81 |
// The point of this test is NOT to exercise the preconditioner; it's |
| 82 |
// just to check that its LinearSolverFactory can create working |
| 83 |
// preconditioners. Belos has more rigorous tests for each of its |
| 84 |
// preconditioners. |
| 85 |
template<class SC, class LO, class GO, class NT> |
| 86 |
void |
| 87 |
createTestProblem (Teuchos::FancyOStream& out, |
| 88 |
Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NT> >& X, |
| 89 |
Teuchos::RCP<Tpetra::CrsMatrix<SC, LO, GO, NT> >& A, |
| 90 |
Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NT> >& B, |
| 91 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 92 |
const Tpetra::global_size_t gblNumRows, |
| 93 |
const size_t numVecs) |
| 94 |
{ |
| 95 |
using Teuchos::Comm; |
| 96 |
using Teuchos::RCP; |
| 97 |
using Teuchos::rcp; |
| 98 |
using std::endl; |
| 99 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 100 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 101 |
|
| 102 |
A = createTestMatrix<SC, LO, GO, NT> (out, comm, gblNumRows); |
| 103 |
X = rcp (new MV (A->getDomainMap (), numVecs)); |
| 104 |
B = rcp (new MV (A->getRangeMap (), numVecs)); |
| 105 |
|
| 106 |
B->putScalar (STS::one()); |
| 107 |
X->putScalar (STS::zero()); |
| 108 |
} |
| 109 |
|
| 110 |
template<class SC, class LO, class GO, class NT> |
| 111 |
void |
| 112 |
testSolver (Teuchos::FancyOStream& out, |
| 113 |
bool& success, |
| 114 |
const Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NT> >& X, |
| 115 |
const Teuchos::RCP<const Tpetra::CrsMatrix<SC, LO, GO, NT> >& A, |
| 116 |
const Teuchos::RCP<const Tpetra::MultiVector<SC, LO, GO, NT> >& B, |
| 117 |
Tpetra::MultiVector<SC, LO, GO, NT>& /* X_exact */, |
| 118 |
const std::string& solverName) |
| 119 |
{ |
| 120 |
using Teuchos::Comm; |
| 121 |
using Teuchos::RCP; |
| 122 |
using Teuchos::rcp; |
| 123 |
using std::endl; |
| 124 |
typedef Tpetra::Operator<SC,LO,GO,NT> OP; |
| 125 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 126 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 127 |
typedef Teuchos::ScalarTraits<typename STS::magnitudeType> MTS; |
| 128 |
|
| 129 |
Teuchos::OSTab tab0 (out); |
| 130 |
out << "Test solver \"" << solverName << "\" from Belos package" << endl; |
| 131 |
Teuchos::OSTab tab1 (out); |
| 132 |
|
| 133 |
RCP<Belos::SolverManager<SC, MV, OP> > solver; |
| 134 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 135 |
|
| 136 |
// Set up Belos solver parameters. |
| 137 |
Teuchos::RCP<Teuchos::ParameterList> belosList = Teuchos::parameterList (solverName); |
| 138 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings); |
| 139 |
|
| 140 |
try { |
| 141 |
solver = factory.create (solverName, belosList); |
| 142 |
} catch (std::exception& e) { |
| 143 |
out << "*** FAILED: Belos::SolverFactory::create threw an exception: " |
| 144 |
<< e.what () << endl; |
| 145 |
success = false; |
| 146 |
return; |
| 147 |
} |
| 148 |
TEST_EQUALITY_CONST( solver.is_null (), false ); |
| 149 |
if (solver.is_null ()) { |
| 150 |
out << "*** FAILED to create solver \"" << solverName |
| 151 |
<< "\" from Belos package" << endl; |
| 152 |
success = false; |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
out << "Create the Belos::LinearProblem to solve" << endl; |
| 157 |
typedef Belos::LinearProblem<SC, MV, OP> linear_problem_type; |
| 158 |
X->putScalar (STS::zero ()); |
| 159 |
RCP<linear_problem_type> problem (new linear_problem_type (A, X, B)); |
| 160 |
problem->setProblem (); |
| 161 |
|
| 162 |
out << "Set up the solver" << endl; |
| 163 |
solver->setProblem (problem); |
| 164 |
|
| 165 |
out << "Apply solver to \"solve\" AX=B for X. Belos already has solver tests;" |
| 166 |
" the point is to check that solve() doesn't throw if it encounters a NaN." << endl; |
| 167 |
Belos::ReturnType ret; |
| 168 |
|
| 169 |
try { |
| 170 |
ret = solver->solve (); |
| 171 |
} catch (std::exception& e) { |
| 172 |
out << "*** FAILED: Belos::SolverFactory::solve threw an unexpected exception: " |
| 173 |
<< e.what () << endl; |
| 174 |
success = false; |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Check that the solution vector is zeros, the achieved tolerance is 1, and the solver return Unconverged. |
| 179 |
bool nonZeroX = false; |
| 180 |
std::vector<typename STS::magnitudeType> normX( X->getNumVectors () ); |
| 181 |
Teuchos::ArrayView<typename STS::magnitudeType> normAV( normX ); |
| 182 |
X->norm2 ( normAV( 0, X->getNumVectors ()) ); |
| 183 |
for ( int i=0; i<(int)(X->getNumVectors ()); ++i ) |
| 184 |
{ |
| 185 |
if ( normX[i] != MTS::zero() ) |
| 186 |
nonZeroX = true; |
| 187 |
} |
| 188 |
if ( nonZeroX || (ret != Belos::Unconverged) || ( solver->achievedTol() != MTS::one() ) ) { |
| 189 |
success = false; |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
success = true; |
| 194 |
} |
| 195 |
|
| 196 |
template<class SC, class LO, class GO, class NT> |
| 197 |
void |
| 198 |
testCreatingSolver (Teuchos::FancyOStream& out, |
| 199 |
bool& success, |
| 200 |
const std::string& solverName) |
| 201 |
{ |
| 202 |
using Teuchos::RCP; |
| 203 |
using Teuchos::rcp; |
| 204 |
using Teuchos::TypeNameTraits; |
| 205 |
using std::endl; |
| 206 |
using MV = Tpetra::MultiVector<SC, LO, GO, NT>; |
| 207 |
using OP = Tpetra::Operator<SC, LO, GO, NT>; |
| 208 |
|
| 209 |
Teuchos::OSTab tab0 (out); |
| 210 |
out << "Test Belos solver \"" << solverName |
| 211 |
<< "\" for Tpetra <SC=" << TypeNameTraits<SC>::name () |
| 212 |
<< ", LO=" << TypeNameTraits<LO>::name () |
| 213 |
<< ", GO=" << TypeNameTraits<GO>::name () |
| 214 |
<< ", NT=" << TypeNameTraits<NT>::name () |
| 215 |
<< ">" << endl; |
| 216 |
Teuchos::OSTab tab1 (out); |
| 217 |
|
| 218 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 219 |
RCP<Belos::SolverManager<SC, MV, OP> > solver; |
| 220 |
TEST_NOTHROW( solver = factory.create (solverName, Teuchos::null) ); |
| 221 |
TEST_ASSERT( ! solver.is_null () ); |
| 222 |
} |
| 223 |
|
| 224 |
// |
| 225 |
// The actual unit tests start here. |
| 226 |
// |
| 227 |
|
| 228 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( SolverFactory, CreateSolvers, SC, LO, GO, NT ) |
| 229 |
{ |
| 230 |
using std::endl; |
| 231 |
|
| 232 |
out << "Test Belos::SolverFactory with Tpetra for all solvers" << endl; |
| 233 |
Teuchos::OSTab tab1 (out); |
| 234 |
const std::vector<std::string> solverNames {{ |
| 235 |
"BICGSTAB", |
| 236 |
"BLOCK CG", |
| 237 |
"BLOCK GMRES", |
| 238 |
"TPETRA CG PIPELINE", |
| 239 |
"TPETRA CG SINGLE REDUCE", |
| 240 |
"FIXED POINT", |
| 241 |
"GCRODR", |
| 242 |
"TPETRA GMRES PIPELINE", |
| 243 |
"HYBRID BLOCK GMRES", // GmresPoly |
| 244 |
"TPETRA GMRES SINGLE REDUCE", |
| 245 |
"LSQR", |
| 246 |
"MINRES", |
| 247 |
"PCPG", |
| 248 |
"PSEUDOBLOCK CG", |
| 249 |
"PSEUDOBLOCK GMRES", |
| 250 |
"PSEUDOBLOCK TFQMR", |
| 251 |
"TFQMR" |
| 252 |
}}; |
| 253 |
|
| 254 |
for (const std::string& solverName : solverNames) { |
| 255 |
const bool isComplex = Teuchos::ScalarTraits<SC>::isComplex; |
| 256 |
if (isComplex && (solverName == "LSQR" || solverName == "PCPG")) { |
| 257 |
continue; // solver not implemented for complex Scalar types |
| 258 |
} |
| 259 |
testCreatingSolver<SC, LO, GO, NT> (out, success, solverName); |
| 260 |
if (! success) { |
| 261 |
return; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( SolverFactory, CreateAndSolve, SC, LO, GO, NT ) |
| 267 |
{ |
| 268 |
using Teuchos::Comm; |
| 269 |
using Teuchos::RCP; |
| 270 |
using Teuchos::rcp; |
| 271 |
using Teuchos::TypeNameTraits; |
| 272 |
using std::endl; |
| 273 |
typedef Tpetra::CrsMatrix<SC,LO,GO,NT> MAT; |
| 274 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 275 |
typedef Tpetra::Operator<SC,LO,GO,NT> OP; |
| 276 |
|
| 277 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm (); |
| 278 |
const Tpetra::global_size_t gblNumRows = comm->getSize () * 10; |
| 279 |
const size_t numVecs = 3; |
| 280 |
|
| 281 |
out << "Create test problem" << endl; |
| 282 |
RCP<MV> X_exact, B; |
| 283 |
RCP<MAT> A; |
| 284 |
createTestProblem (out, X_exact, A, B, comm, gblNumRows, numVecs); |
| 285 |
|
| 286 |
RCP<MV> X = rcp (new MV (X_exact->getMap (), numVecs)); |
| 287 |
|
| 288 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 289 |
// FIXME (mfh 23 Aug 2015) Not all Belos solvers can handle solves |
| 290 |
// with the identity matrix. BiCGSTAB might need a bit of work, for |
| 291 |
// example. I'm not so worried about that for now but we should go |
| 292 |
// back and revisit this at some point. |
| 293 |
// |
| 294 |
// Teuchos::Array<std::string> solverNames = factory.supportedSolverNames (); |
| 295 |
// const int numSolvers = static_cast<int> (solverNames.size ()); |
| 296 |
const char* solverNames[10] = { |
| 297 |
"BICGSTAB", |
| 298 |
"BLOCK CG", |
| 299 |
"BLOCK GMRES", |
| 300 |
"FIXED POINT", |
| 301 |
"GCRODR", |
| 302 |
"MINRES", |
| 303 |
"PSEUDOBLOCK CG", |
| 304 |
"PSEUDOBLOCK GMRES", |
| 305 |
"PSEUDOBLOCK TFQMR", |
| 306 |
"TFQMR"}; |
| 307 |
const int numSolvers = 10; |
| 308 |
|
| 309 |
int numSolversTested = 0; |
| 310 |
for (int k = 0; k < numSolvers; ++k) { |
| 311 |
const std::string solverName (solverNames[k]); |
| 312 |
|
| 313 |
// Use Belos' factory to tell us whether the factory supports the |
| 314 |
// given combination of template parameters. If create() throws, |
| 315 |
// the combination is not supported. |
| 316 |
bool skip = false; |
| 317 |
try { |
| 318 |
(void) factory.create (solverName, Teuchos::null); |
| 319 |
} |
| 320 |
catch (...) { |
| 321 |
skip = true; |
| 322 |
} |
| 323 |
if (! skip) { |
| 324 |
testSolver<SC, LO, GO, NT> (out, success, X, A, B, *X_exact, solverName); |
| 325 |
++numSolversTested; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
out << "Tested " << numSolversTested << " solver(s) of " << numSolvers |
| 330 |
<< endl; |
| 331 |
if (numSolversTested == 0) { |
| 332 |
out << "*** ERROR: Tested no solvers for template parameters" |
| 333 |
<< "SC = " << TypeNameTraits<SC>::name () |
| 334 |
<< ", LO = " << TypeNameTraits<LO>::name () |
| 335 |
<< ", GO = " << TypeNameTraits<GO>::name () |
| 336 |
<< ", NT = " << TypeNameTraits<NT>::name () << endl; |
| 337 |
success = false; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// Define typedefs that make the Tpetra macros work. |
| 342 |
TPETRA_ETI_MANGLING_TYPEDEFS() |
| 343 |
|
| 344 |
// Macro that instantiates the unit test |
| 345 |
#define LCLINST( SC, LO, GO, NT ) \ |
| 346 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( SolverFactory, CreateSolvers, SC, LO, GO, NT ) \ |
| 347 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( SolverFactory, CreateAndSolve, SC, LO, GO, NT ) |
| 348 |
|
| 349 |
// Tpetra's ETI will instantiate the unit test for all enabled type |
| 350 |
// combinations. |
| 351 |
TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR( LCLINST ) |
| 352 |
|
| 353 |
} // namespace (anonymous) |