| 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 "BelosTpetraAdapter.hpp" |
| 12 |
#include "Tpetra_Core.hpp" |
| 13 |
#include "MatrixMarket_Tpetra.hpp" |
| 14 |
#include "BelosSolverFactory.hpp" |
| 15 |
#include "Trilinos_Details_LinearSolver.hpp" |
| 16 |
#include "Trilinos_Details_LinearSolverFactory.hpp" |
| 17 |
#include "TpetraCore_ETIHelperMacros.h" |
| 18 |
|
| 19 |
// FIXME (mfh 21 Aug 2015) Temporary work-around for Bug 6392. |
| 20 |
#if ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) |
| 21 |
namespace Belos { |
| 22 |
namespace Details { |
| 23 |
//extern void registerLinearSolverFactory (); |
| 24 |
|
| 25 |
// FIXME (mfh 24 Aug 2015) The weak symbol stuff in the main |
| 26 |
// registration function doesn't work. I have to reference the |
| 27 |
// Tpetra registration function for now instead. |
| 28 |
namespace Tpetra { |
| 29 |
extern void registerLinearSolverFactory (); |
| 30 |
} // namespace Tpetra |
| 31 |
|
| 32 |
} // namespace Details |
| 33 |
} // namespace Belos |
| 34 |
#endif // ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) |
| 35 |
|
| 36 |
namespace { // (anonymous) |
| 37 |
|
| 38 |
// FIXME (mfh 21 Aug 2015) Temporary work-around for Bug 6392. |
| 39 |
#if ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) |
| 40 |
TEUCHOS_STATIC_SETUP() |
| 41 |
{ |
| 42 |
// FIXME (mfh 24 Aug 2015) The weak symbol stuff in the main |
| 43 |
// registration function doesn't work. I have to reference the |
| 44 |
// Tpetra registration function for now instead. |
| 45 |
|
| 46 |
// Belos::Details::registerLinearSolverFactory (); |
| 47 |
Belos::Details::Tpetra::registerLinearSolverFactory (); |
| 48 |
} |
| 49 |
#endif // ! defined(HAVE_TEUCHOS_DYNAMIC_LIBS) |
| 50 |
|
| 51 |
// Create a very simple square test matrix. We use the identity |
| 52 |
// matrix here. The point of this test is NOT to exercise the solver; |
| 53 |
// it's just to check that Belos' LinearSolverFactory can create |
| 54 |
// working solvers. Belos has more rigorous tests for its solvers. |
| 55 |
template<class SC, class LO, class GO, class NT> |
| 56 |
Teuchos::RCP<Tpetra::CrsMatrix<SC, LO, GO, NT> > |
| 57 |
createTestMatrix (Teuchos::FancyOStream& out, |
| 58 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 59 |
const Tpetra::global_size_t gblNumRows) |
| 60 |
{ |
| 61 |
using Teuchos::Comm; |
| 62 |
using Teuchos::RCP; |
| 63 |
using Teuchos::rcp; |
| 64 |
using std::endl; |
| 65 |
typedef Tpetra::CrsMatrix<SC,LO,GO,NT> MAT; |
| 66 |
typedef Tpetra::Map<LO,GO,NT> map_type; |
| 67 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 68 |
|
| 69 |
Teuchos::OSTab tab0 (out); |
| 70 |
out << "Create test matrix with " << gblNumRows << " row(s)" << endl; |
| 71 |
Teuchos::OSTab tab1 (out); |
| 72 |
|
| 73 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 74 |
( gblNumRows == 0, std::invalid_argument, "gblNumRows = 0"); |
| 75 |
|
| 76 |
const GO indexBase = 0; |
| 77 |
RCP<const map_type> rowMap (new map_type (gblNumRows, indexBase, comm)); |
| 78 |
// For this particular row matrix, the row Map and the column Map |
| 79 |
// are the same. Giving a column Map to CrsMatrix's constructor |
| 80 |
// lets us use local indices. |
| 81 |
RCP<const map_type> colMap = rowMap; |
| 82 |
const size_t maxNumEntPerRow = 1; |
| 83 |
RCP<MAT> A (new MAT (rowMap, colMap, maxNumEntPerRow)); |
| 84 |
|
| 85 |
if (rowMap->getLocalNumElements () != 0) { |
| 86 |
Teuchos::Array<SC> vals (1); |
| 87 |
Teuchos::Array<LO> inds (1); |
| 88 |
for (LO lclRow = rowMap->getMinLocalIndex (); |
| 89 |
lclRow <= rowMap->getMaxLocalIndex (); ++lclRow) { |
| 90 |
inds[0] = lclRow; |
| 91 |
vals[0] = STS::one (); |
| 92 |
A->insertLocalValues (lclRow, inds (), vals ()); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
RCP<const map_type> domMap = rowMap; |
| 97 |
RCP<const map_type> ranMap = rowMap; |
| 98 |
A->fillComplete (domMap, ranMap); |
| 99 |
return A; |
| 100 |
} |
| 101 |
|
| 102 |
// Create a very simple square test linear system (matrix, right-hand |
| 103 |
// side(s), and exact solution(s). We use the identity matrix here. |
| 104 |
// The point of this test is NOT to exercise the preconditioner; it's |
| 105 |
// just to check that its LinearSolverFactory can create working |
| 106 |
// preconditioners. Belos has more rigorous tests for each of its |
| 107 |
// preconditioners. |
| 108 |
template<class SC, class LO, class GO, class NT> |
| 109 |
void |
| 110 |
createTestProblem (Teuchos::FancyOStream& out, |
| 111 |
Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NT> >& X, |
| 112 |
Teuchos::RCP<Tpetra::CrsMatrix<SC, LO, GO, NT> >& A, |
| 113 |
Teuchos::RCP<Tpetra::MultiVector<SC, LO, GO, NT> >& B, |
| 114 |
const Teuchos::RCP<const Teuchos::Comm<int> >& comm, |
| 115 |
const Tpetra::global_size_t gblNumRows, |
| 116 |
const size_t numVecs) |
| 117 |
{ |
| 118 |
using Teuchos::Comm; |
| 119 |
using Teuchos::RCP; |
| 120 |
using Teuchos::rcp; |
| 121 |
using std::endl; |
| 122 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 123 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 124 |
|
| 125 |
A = createTestMatrix<SC, LO, GO, NT> (out, comm, gblNumRows); |
| 126 |
X = rcp (new MV (A->getDomainMap (), numVecs)); |
| 127 |
B = rcp (new MV (A->getRangeMap (), numVecs)); |
| 128 |
|
| 129 |
X->putScalar (STS::one ()); |
| 130 |
A->apply (*X, *B); |
| 131 |
} |
| 132 |
|
| 133 |
template<class SC, class LO, class GO, class NT> |
| 134 |
void |
| 135 |
testSolver (Teuchos::FancyOStream& out, |
| 136 |
bool& success, |
| 137 |
Tpetra::MultiVector<SC, LO, GO, NT>& X, |
| 138 |
const Teuchos::RCP<const Tpetra::CrsMatrix<SC, LO, GO, NT> >& A, |
| 139 |
const Tpetra::MultiVector<SC, LO, GO, NT>& B, |
| 140 |
const Tpetra::MultiVector<SC, LO, GO, NT>& X_exact, |
| 141 |
const std::string& solverName) |
| 142 |
{ |
| 143 |
using Teuchos::Comm; |
| 144 |
using Teuchos::RCP; |
| 145 |
using Teuchos::rcp; |
| 146 |
using std::endl; |
| 147 |
typedef Tpetra::Operator<SC,LO,GO,NT> OP; |
| 148 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 149 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 150 |
typedef typename MV::mag_type mag_type; |
| 151 |
|
| 152 |
Teuchos::OSTab tab0 (out); |
| 153 |
out << "Test solver \"" << solverName << "\" from Belos package" << endl; |
| 154 |
Teuchos::OSTab tab1 (out); |
| 155 |
|
| 156 |
RCP<Trilinos::Details::LinearSolver<MV, OP, mag_type> > solver; |
| 157 |
try { |
| 158 |
solver = Trilinos::Details::getLinearSolver<MV, OP, mag_type> ("Belos", solverName); |
| 159 |
} catch (std::exception& e) { |
| 160 |
out << "*** FAILED: getLinearSolver threw an exception: " << e.what () << endl; |
| 161 |
success = false; |
| 162 |
return; |
| 163 |
} |
| 164 |
TEST_EQUALITY_CONST( solver.is_null (), false ); |
| 165 |
if (solver.is_null ()) { |
| 166 |
out << "*** FAILED to create solver \"" << solverName |
| 167 |
<< "\" from Belos package" << endl; |
| 168 |
success = false; |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
out << "Set matrix" << endl; |
| 173 |
solver->setMatrix (A); |
| 174 |
|
| 175 |
out << "Compute symbolic and numeric factorization" << endl; |
| 176 |
solver->symbolic (); |
| 177 |
solver->numeric (); |
| 178 |
|
| 179 |
out << "Apply solver to \"solve\" AX=B for X. Belos already has solver " |
| 180 |
"tests; the point is to check that solve() doesn't throw." << endl; |
| 181 |
X.putScalar (STS::zero ()); |
| 182 |
solver->solve (X, B); |
| 183 |
} |
| 184 |
|
| 185 |
// |
| 186 |
// The actual unit test. |
| 187 |
// |
| 188 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL( LinearSolverFactory, Solve, SC, LO, GO, NT ) |
| 189 |
{ |
| 190 |
using Teuchos::Comm; |
| 191 |
using Teuchos::RCP; |
| 192 |
using Teuchos::rcp; |
| 193 |
using Teuchos::TypeNameTraits; |
| 194 |
using std::endl; |
| 195 |
typedef Tpetra::CrsMatrix<SC,LO,GO,NT> MAT; |
| 196 |
typedef Tpetra::MultiVector<SC,LO,GO,NT> MV; |
| 197 |
typedef Tpetra::Operator<SC,LO,GO,NT> OP; |
| 198 |
|
| 199 |
return; |
| 200 |
|
| 201 |
#if ! defined(TRILINOS_HAVE_LINEAR_SOLVER_FACTORY_REGISTRATION) |
| 202 |
out << "LinearSolverFactory run-time registration disabled; " |
| 203 |
"not running test" << endl; |
| 204 |
return; |
| 205 |
#endif // NOT TRILINOS_HAVE_LINEAR_SOLVER_FACTORY_REGISTRATION |
| 206 |
|
| 207 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm (); |
| 208 |
const Tpetra::global_size_t gblNumRows = comm->getSize () * 10; |
| 209 |
const size_t numVecs = 3; |
| 210 |
|
| 211 |
out << "Create test problem" << endl; |
| 212 |
RCP<MV> X_exact, B; |
| 213 |
RCP<MAT> A; |
| 214 |
createTestProblem (out, X_exact, A, B, comm, gblNumRows, numVecs); |
| 215 |
|
| 216 |
RCP<MV> X = rcp (new MV (X_exact->getMap (), numVecs)); |
| 217 |
|
| 218 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 219 |
// FIXME (mfh 23 Aug 2015) Not all Belos solvers can handle solves |
| 220 |
// with the identity matrix. BiCGSTAB might need a bit of work, for |
| 221 |
// example. I'm not so worried about that for now but we should go |
| 222 |
// back and revisit this at some point. |
| 223 |
// |
| 224 |
// Teuchos::Array<std::string> solverNames = factory.supportedSolverNames (); |
| 225 |
// const int numSolvers = static_cast<int> (solverNames.size ()); |
| 226 |
const char* solverNames[2] = {"CG", "GMRES"}; |
| 227 |
const int numSolvers = 2; |
| 228 |
|
| 229 |
int numSolversTested = 0; |
| 230 |
for (int k = 0; k < numSolvers; ++k) { |
| 231 |
const std::string solverName (solverNames[k]); |
| 232 |
|
| 233 |
// Use Belos' factory to tell us whether the factory supports the |
| 234 |
// given combination of template parameters. If create() throws, |
| 235 |
// the combination is not supported. |
| 236 |
bool skip = false; |
| 237 |
try { |
| 238 |
(void) factory.create (solverName, Teuchos::null); |
| 239 |
} |
| 240 |
catch (...) { |
| 241 |
skip = true; |
| 242 |
} |
| 243 |
if (! skip) { |
| 244 |
testSolver<SC, LO, GO, NT> (out, success, *X, A, *B, *X_exact, solverName); |
| 245 |
++numSolversTested; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
out << "Tested " << numSolversTested << " solver(s) of " << numSolvers |
| 250 |
<< endl; |
| 251 |
if (numSolversTested == 0) { |
| 252 |
out << "*** ERROR: Tested no solvers for template parameters" |
| 253 |
<< "SC = " << TypeNameTraits<SC>::name () |
| 254 |
<< ", LO = " << TypeNameTraits<LO>::name () |
| 255 |
<< ", GO = " << TypeNameTraits<GO>::name () |
| 256 |
<< ", NT = " << TypeNameTraits<NT>::name () << endl; |
| 257 |
success = false; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
// Define typedefs that make the Tpetra macros work. |
| 262 |
TPETRA_ETI_MANGLING_TYPEDEFS() |
| 263 |
|
| 264 |
// Macro that instantiates the unit test |
| 265 |
#define LCLINST( SC, LO, GO, NT ) \ |
| 266 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT( LinearSolverFactory, Solve, SC, LO, GO, NT ) |
| 267 |
|
| 268 |
// Tpetra's ETI will instantiate the unit test for all enabled type |
| 269 |
// combinations. |
| 270 |
TPETRA_INSTANTIATE_SLGN_NO_ORDINAL_SCALAR( LCLINST ) |
| 271 |
|
| 272 |
} // namespace (anonymous) |