| 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_UnitTestRepository.hpp" |
| 12 |
#include "BelosSolverFactory.hpp" |
| 13 |
#include "BelosTpetraAdapter.hpp" |
| 14 |
#include "Tpetra_Core.hpp" |
| 15 |
#include "Tpetra_Map.hpp" |
| 16 |
#include "Tpetra_Vector.hpp" |
| 17 |
#include "KokkosBlas1_mult.hpp" |
| 18 |
#include "Teuchos_ParameterList.hpp" |
| 19 |
#include <iostream> |
| 20 |
|
| 21 |
namespace { // (anonymous) |
| 22 |
|
| 23 |
struct CommandLineOptions { |
| 24 |
std::string solverName {"TPETRA CG"}; |
| 25 |
// mfh 14 Aug 2018: Most of these CG solvers take 29 or 30 iterations |
| 26 |
// on this problem. We add five iterations to allow for rounding |
| 27 |
// error. |
| 28 |
int maxAllowedNumIters {35}; |
| 29 |
bool verbose {true}; |
| 30 |
}; |
| 31 |
CommandLineOptions commandLineOptions; |
| 32 |
|
| 33 |
TEUCHOS_STATIC_SETUP() |
| 34 |
{ |
| 35 |
Teuchos::CommandLineProcessor& clp = Teuchos::UnitTestRepository::getCLP(); |
| 36 |
clp.addOutputSetupOptions (true); |
| 37 |
clp.setOption ("solver", &commandLineOptions.solverName, |
| 38 |
"Name of the solver to test. Belos::SolverFactory::create " |
| 39 |
"must accept this string. Protect with double quotes if it " |
| 40 |
"has spaces: e.g., \"TPETRA CG PIPELINE\"."); |
| 41 |
clp.setOption ("maxNumIters", &commandLineOptions.maxAllowedNumIters, |
| 42 |
"Maximum number of iterations"); |
| 43 |
clp.setOption ("verbose", "quiet", &commandLineOptions.verbose, |
| 44 |
"Whether to print verbose output"); |
| 45 |
} |
| 46 |
|
| 47 |
template<class TpetraOperatorType = Tpetra::Operator<>> |
| 48 |
class TestDiagonalOperator : public TpetraOperatorType { |
| 49 |
public: |
| 50 |
using multivector_type = Tpetra::MultiVector< |
| 51 |
typename TpetraOperatorType::scalar_type, |
| 52 |
typename TpetraOperatorType::local_ordinal_type, |
| 53 |
typename TpetraOperatorType::global_ordinal_type, |
| 54 |
typename TpetraOperatorType::node_type>; |
| 55 |
using map_type = Tpetra::Map< |
| 56 |
typename TpetraOperatorType::local_ordinal_type, |
| 57 |
typename TpetraOperatorType::global_ordinal_type, |
| 58 |
typename TpetraOperatorType::node_type>; |
| 59 |
|
| 60 |
private: |
| 61 |
using device_type = typename map_type::device_type; |
| 62 |
using val_type = typename TpetraOperatorType::scalar_type; |
| 63 |
using STS = Teuchos::ScalarTraits<val_type>; |
| 64 |
|
| 65 |
public: |
| 66 |
using mag_type = typename Teuchos::ScalarTraits<val_type>::magnitudeType; |
| 67 |
|
| 68 |
TestDiagonalOperator (const Teuchos::RCP<const map_type>& map, |
| 69 |
const mag_type minSingularValue) : |
| 70 |
map_ (map) |
| 71 |
{ |
| 72 |
using dev_view_type = Kokkos::View<mag_type*, device_type>; |
| 73 |
using host_view_type = typename dev_view_type::HostMirror; |
| 74 |
using SC = typename TpetraOperatorType::scalar_type; |
| 75 |
using LO = typename TpetraOperatorType::local_ordinal_type; |
| 76 |
|
| 77 |
const LO lclNumRows = map_.is_null () ? |
| 78 |
static_cast<LO> (0) : |
| 79 |
static_cast<LO> (map_->getLocalNumElements ()); |
| 80 |
dev_view_type diag_d ("diag", lclNumRows); |
| 81 |
host_view_type diag_h = Kokkos::create_mirror_view (diag_d); |
| 82 |
|
| 83 |
if (lclNumRows != 0) { |
| 84 |
const SC ONE = STS::one (); |
| 85 |
const SC exponent = ONE / static_cast<mag_type> (lclNumRows - 1); |
| 86 |
const SC scalingFactor = ::pow (minSingularValue, exponent); |
| 87 |
diag_h(0) = ONE; |
| 88 |
for (LO lclRow = 1; lclRow < lclNumRows; ++lclRow) { |
| 89 |
diag_h(lclRow) = diag_h(lclRow-1) * scalingFactor; |
| 90 |
} |
| 91 |
} |
| 92 |
Kokkos::deep_copy (diag_d, diag_h); |
| 93 |
diag_ = diag_d; |
| 94 |
} |
| 95 |
|
| 96 |
Teuchos::RCP<const map_type> getDomainMap () const { |
| 97 |
return map_; |
| 98 |
} |
| 99 |
|
| 100 |
Teuchos::RCP<const map_type> getRangeMap () const { |
| 101 |
return map_; |
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
apply (const multivector_type& X, |
| 106 |
multivector_type& Y, |
| 107 |
Teuchos::ETransp mode = Teuchos::NO_TRANS, |
| 108 |
val_type alpha = Teuchos::ScalarTraits<val_type>::one (), |
| 109 |
val_type beta = Teuchos::ScalarTraits<val_type>::zero ()) const |
| 110 |
{ |
| 111 |
using ISC = typename multivector_type::impl_scalar_type; |
| 112 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 113 |
(mode == Teuchos::CONJ_TRANS && STS::isComplex, |
| 114 |
std::logic_error, "Conjugate transpose case not implemented."); |
| 115 |
|
| 116 |
const size_t numVecs = X.getNumVectors (); |
| 117 |
for (size_t j = 0; j < numVecs; ++j) { |
| 118 |
auto X_j = X.getVector (j); |
| 119 |
auto Y_j = Y.getVectorNonConst (j); |
| 120 |
|
| 121 |
auto X_j_lcl_2d = X_j->getLocalViewDevice (Tpetra::Access::ReadOnly); |
| 122 |
auto X_j_lcl = Kokkos::subview (X_j_lcl_2d, Kokkos::ALL (), 0); |
| 123 |
auto Y_j_lcl_2d = Y_j->getLocalViewDevice (Tpetra::Access::ReadWrite); |
| 124 |
auto Y_j_lcl = Kokkos::subview (Y_j_lcl_2d, Kokkos::ALL (), 0); |
| 125 |
|
| 126 |
KokkosBlas::mult (static_cast<ISC> (beta), Y_j_lcl, |
| 127 |
static_cast<ISC> (alpha), X_j_lcl, diag_); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
private: |
| 132 |
Teuchos::RCP<const map_type> map_; |
| 133 |
Kokkos::View<const mag_type*, device_type> diag_; |
| 134 |
}; |
| 135 |
|
| 136 |
void |
| 137 |
testSolver (Teuchos::FancyOStream& out, |
| 138 |
bool& success, |
| 139 |
const std::string& solverName, |
| 140 |
const int maxAllowedNumIters, |
| 141 |
const bool verbose) |
| 142 |
{ |
| 143 |
using Teuchos::FancyOStream; |
| 144 |
using Teuchos::getFancyOStream; |
| 145 |
using Teuchos::ParameterList; |
| 146 |
using Teuchos::parameterList; |
| 147 |
using Teuchos::RCP; |
| 148 |
using Teuchos::rcp; |
| 149 |
using Teuchos::rcpFromRef; |
| 150 |
using std::endl; |
| 151 |
using map_type = Tpetra::Map<>; |
| 152 |
using MV = Tpetra::MultiVector<>; |
| 153 |
using OP = Tpetra::Operator<>; |
| 154 |
using SC = MV::scalar_type; |
| 155 |
using GO = map_type::global_ordinal_type; |
| 156 |
using mag_type = MV::mag_type; |
| 157 |
using STS = Teuchos::ScalarTraits<SC>; |
| 158 |
using STM = Teuchos::ScalarTraits<mag_type>; |
| 159 |
// The Teuchos unit test framework likes to capture output to 'out', |
| 160 |
// and not print anything until the test is done. This can hinder |
| 161 |
// debugging. If the test crashes without useful output, try |
| 162 |
// setting this to 'true'. That will change 'myOut' from an alias |
| 163 |
// to 'out', into a wrapper for std::cerr. |
| 164 |
constexpr bool debug = false; |
| 165 |
|
| 166 |
const SC ZERO = STS::zero (); |
| 167 |
const SC ONE = STS::one (); |
| 168 |
|
| 169 |
RCP<FancyOStream> myOutPtr = |
| 170 |
debug ? getFancyOStream (rcpFromRef (std::cerr)) : rcpFromRef (out); |
| 171 |
Teuchos::FancyOStream& myOut = *myOutPtr; |
| 172 |
|
| 173 |
myOut << "Test \"native\" Tpetra version of solver \"" << solverName << "\"" |
| 174 |
<< endl; |
| 175 |
Teuchos::OSTab tab1 (out); |
| 176 |
|
| 177 |
myOut << "Create the linear system to solve" << endl; |
| 178 |
|
| 179 |
auto comm = Tpetra::getDefaultComm (); |
| 180 |
const GO gblNumRows = 10000; |
| 181 |
const GO indexBase = 0; |
| 182 |
RCP<const map_type> map (new map_type (gblNumRows, indexBase, comm)); |
| 183 |
const mag_type minSingVal = 0.1; |
| 184 |
RCP<TestDiagonalOperator<OP> > A = |
| 185 |
rcp (new TestDiagonalOperator<> (map, minSingVal)); |
| 186 |
|
| 187 |
MV X (A->getDomainMap (), 1); |
| 188 |
MV B (A->getRangeMap (), 1); |
| 189 |
B.randomize (); |
| 190 |
// Get ready for next solve by resetting initial guess to zero. |
| 191 |
X.putScalar (ZERO); |
| 192 |
|
| 193 |
myOut << "Create solver instance using Belos::SolverFactory" << endl; |
| 194 |
|
| 195 |
RCP<Belos::SolverManager<SC, MV, OP> > solver; |
| 196 |
try { |
| 197 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 198 |
solver = factory.create (solverName, Teuchos::null); |
| 199 |
} |
| 200 |
catch (std::exception& e) { |
| 201 |
myOut << "*** FAILED: Belos::SolverFactory::create threw an exception: " |
| 202 |
<< e.what () << endl; |
| 203 |
success = false; |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
TEST_ASSERT( solver.get () != nullptr ); |
| 208 |
if (solver.get () == nullptr) { |
| 209 |
myOut << "Belos::SolverFactory returned a null solver." << endl; |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
myOut << "Set parameters" << endl; |
| 214 |
RCP<ParameterList> params = parameterList ("Belos"); |
| 215 |
params->set ("Verbosity", verbose ? 1 : 0); |
| 216 |
try { |
| 217 |
solver->setParameters (params); |
| 218 |
} |
| 219 |
catch (std::exception& e) { |
| 220 |
myOut << "*** FAILED: setParameters threw an exception: " |
| 221 |
<< e.what () << endl; |
| 222 |
success = false; |
| 223 |
return; |
| 224 |
} |
| 225 |
catch (...) { |
| 226 |
myOut << "*** FAILED: setParameters threw an exception " |
| 227 |
"not a subclass of std::exception." << endl; |
| 228 |
success = false; |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
myOut << "Set up the linear system to solve" << endl; |
| 233 |
auto lp = rcp (new Belos::LinearProblem<SC, MV, OP> (A, rcpFromRef (X), |
| 234 |
rcpFromRef (B))); |
| 235 |
lp->setProblem (); |
| 236 |
|
| 237 |
myOut << "Solve the linear system" << endl; |
| 238 |
solver->setProblem (lp); |
| 239 |
const Belos::ReturnType belosResult = solver->solve (); |
| 240 |
|
| 241 |
myOut << "Belos solver wrapper result: " |
| 242 |
<< (belosResult == Belos::Converged ? "Converged" : "Unconverged") |
| 243 |
<< endl |
| 244 |
<< "Number of iterations: " << solver->getNumIters () |
| 245 |
<< endl; |
| 246 |
|
| 247 |
TEST_ASSERT( solver->getNumIters () <= maxAllowedNumIters ); |
| 248 |
TEST_ASSERT( belosResult == Belos::Converged ); |
| 249 |
|
| 250 |
myOut << "Check the explicit residual norm(s)" << endl; |
| 251 |
|
| 252 |
// Get the tolerance that the solver actually used. |
| 253 |
const mag_type tol = [&] () { |
| 254 |
const char tolParamName[] = "Convergence Tolerance"; |
| 255 |
auto pl = solver->getCurrentParameters (); |
| 256 |
if (! pl->isType<mag_type> (tolParamName)) { |
| 257 |
pl = solver->getValidParameters (); |
| 258 |
} |
| 259 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 260 |
(! pl->isType<mag_type> (tolParamName), std::logic_error, |
| 261 |
"Solver lacks \"" << tolParamName << "\" parameter, in either " |
| 262 |
"getCurrentParameters() or getValidParameters()."); |
| 263 |
return pl->get<mag_type> (tolParamName); |
| 264 |
} (); |
| 265 |
|
| 266 |
MV X_copy (X, Teuchos::Copy); |
| 267 |
MV R (B.getMap (), B.getNumVectors ()); |
| 268 |
A->apply (X_copy, R); |
| 269 |
R.update (ONE, B, -ONE); |
| 270 |
Teuchos::Array<mag_type> R_norms (R.getNumVectors ()); |
| 271 |
R.norm2 (R_norms ()); |
| 272 |
Teuchos::Array<mag_type> B_norms (B.getNumVectors ()); |
| 273 |
B.norm2 (B_norms ()); |
| 274 |
|
| 275 |
for (size_t j = 0; j < R.getNumVectors (); ++j) { |
| 276 |
const mag_type relResNorm = (B_norms[j] == STM::zero ()) ? |
| 277 |
R_norms[j] : |
| 278 |
R_norms[j] / B_norms[j]; |
| 279 |
myOut << "Column " << (j+1) << " of " << R.getNumVectors () |
| 280 |
<< ": Absolute residual norm: " << R_norms[j] |
| 281 |
<< ", Relative residual norm: " << relResNorm |
| 282 |
<< endl; |
| 283 |
TEST_ASSERT( relResNorm <= tol ); |
| 284 |
} |
| 285 |
myOut << endl; |
| 286 |
} |
| 287 |
|
| 288 |
TEUCHOS_UNIT_TEST( TpetraNativeSolvers, Diagonal ) |
| 289 |
{ |
| 290 |
testSolver (out, success, commandLineOptions.solverName, |
| 291 |
commandLineOptions.maxAllowedNumIters, |
| 292 |
commandLineOptions.verbose); |
| 293 |
} |
| 294 |
|
| 295 |
} // namespace (anonymous) |
| 296 |
|
| 297 |
int main (int argc, char* argv[]) |
| 298 |
{ |
| 299 |
Tpetra::ScopeGuard tpetraScope (&argc, &argv); |
| 300 |
return Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv); |
| 301 |
} |