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