| 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_CrsMatrix.hpp" |
| 16 |
#include "Tpetra_Map.hpp" |
| 17 |
#include "Tpetra_Vector.hpp" |
| 18 |
#include "KokkosBlas1_mult.hpp" |
| 19 |
#include "Teuchos_ParameterList.hpp" |
| 20 |
#include <iostream> |
| 21 |
|
| 22 |
namespace { // (anonymous) |
| 23 |
|
| 24 |
struct CommandLineOptions { |
| 25 |
std::string solverName {"TPETRA GMRES"}; |
| 26 |
Tpetra::MultiVector<>::scalar_type offDiagDiff = 1.0 / 8.0; |
| 27 |
// mfh 14 Aug 2018: GMRES takes 20 iterations on this problem (with |
| 28 |
// offDiagDiff = 1/8). We add 10 iterations to allow for rounding |
| 29 |
// error and differences in the algorithm. |
| 30 |
int maxAllowedNumIters {30}; |
| 31 |
int maxNumIters {100}; |
| 32 |
int restartLength {30}; |
| 33 |
std::string orthoType {"ICGS"}; |
| 34 |
int stepSize {1}; |
| 35 |
bool useCholQR {false}; |
| 36 |
bool useCholQR2 {false}; |
| 37 |
bool computeRitzValues {true}; |
| 38 |
bool zeroInitialGuess {true}; |
| 39 |
bool verbose {true}; |
| 40 |
}; |
| 41 |
CommandLineOptions commandLineOptions; |
| 42 |
|
| 43 |
TEUCHOS_STATIC_SETUP() |
| 44 |
{ |
| 45 |
Teuchos::CommandLineProcessor& clp = Teuchos::UnitTestRepository::getCLP(); |
| 46 |
clp.addOutputSetupOptions (true); |
| 47 |
clp.setOption ("solver", &commandLineOptions.solverName, |
| 48 |
"Name of the solver to test. Belos::SolverFactory::create " |
| 49 |
"must accept this string. Protect with double quotes if it " |
| 50 |
"has spaces: e.g., \"TPETRA CG PIPELINE\"."); |
| 51 |
clp.setOption ("offDiagDiff", &commandLineOptions.offDiagDiff, |
| 52 |
"Value of the term that makes the matrix nonsymmetric"); |
| 53 |
clp.setOption ("maxNumAllowedIters", |
| 54 |
&commandLineOptions.maxAllowedNumIters, |
| 55 |
"Maximum number of iterations that the solver is " |
| 56 |
"allowed to take before converging, in order for " |
| 57 |
"the test to pass."); |
| 58 |
clp.setOption ("maxNumIters", &commandLineOptions.maxNumIters, |
| 59 |
"Maximum number of iterations that the solver is " |
| 60 |
"allowed to take, over all restart cycles. This has " |
| 61 |
"nothing to do with the test passing."); |
| 62 |
clp.setOption ("restartLength", &commandLineOptions.restartLength, |
| 63 |
"Maximum number of iterations per restart cycle. " |
| 64 |
"This corresponds to the standard Belos parameter " |
| 65 |
"\"Num Blocks\"."); |
| 66 |
clp.setOption ("ortho", &commandLineOptions.orthoType, |
| 67 |
"Name of the orthogonalization procedure"); |
| 68 |
clp.setOption ("stepSize", &commandLineOptions.stepSize, |
| 69 |
"Step size; only applies to algorithms that take it."); |
| 70 |
clp.setOption ("useCholQR", "noCholQR", &commandLineOptions.useCholQR, |
| 71 |
"Whether to use CholQR"); |
| 72 |
clp.setOption ("useCholQR2", "noCholQR2", &commandLineOptions.useCholQR2, |
| 73 |
"Whether to use CholQR2"); |
| 74 |
clp.setOption ("computeRitzValues", "noRitzValues", &commandLineOptions.computeRitzValues, |
| 75 |
"Whether to compute Ritz values"); |
| 76 |
clp.setOption ("zeroInitialGuess", "nonzeroInitialGuess", |
| 77 |
&commandLineOptions.zeroInitialGuess, "Whether to test " |
| 78 |
"with a zero, or a nonzero, initial guess vector"); |
| 79 |
clp.setOption ("verbose", "quiet", &commandLineOptions.verbose, |
| 80 |
"Whether to print verbose output"); |
| 81 |
} |
| 82 |
|
| 83 |
// Create a nonsymmetric tridiagonal matrix representing a |
| 84 |
// discretization of a 1-D convection-diffusion operator. |
| 85 |
// Stencil looks like this: |
| 86 |
// |
| 87 |
// [1/4 - offDiagDiff, 1, 1/4 + offDiagDiff] |
| 88 |
// |
| 89 |
// The point is to have the matrix be diagonally dominant, but still |
| 90 |
// nonsymmetric. |
| 91 |
template<class SC> |
| 92 |
Teuchos::RCP<Tpetra::CrsMatrix<SC> > |
| 93 |
createNonsymmTridiagMatrix (const Teuchos::RCP<const Tpetra::Map<> >& rowMap, |
| 94 |
const SC offDiagDiff) |
| 95 |
{ |
| 96 |
using Teuchos::rcp; |
| 97 |
using crs_matrix_type = Tpetra::CrsMatrix<SC>; |
| 98 |
using map_type = Tpetra::Map<>; |
| 99 |
using LO = typename map_type::local_ordinal_type; |
| 100 |
using GO = typename map_type::global_ordinal_type; |
| 101 |
using crs_matrix_type = Tpetra::CrsMatrix<SC>; |
| 102 |
using STS = Teuchos::ScalarTraits<SC>; |
| 103 |
//using mag_type = typename Tpetra::CrsMatrix<SC>::mag_type; |
| 104 |
|
| 105 |
const LO lclNumRows = rowMap.is_null () ? LO (0) : |
| 106 |
LO (rowMap->getLocalNumElements ()); |
| 107 |
const GO gblMinGblInd = rowMap->getMinAllGlobalIndex (); |
| 108 |
const GO gblMaxGblInd = rowMap->getMaxAllGlobalIndex (); |
| 109 |
auto A = rcp (new crs_matrix_type (rowMap, 3)); |
| 110 |
|
| 111 |
const SC ONE = STS::one (); |
| 112 |
const SC TWO = ONE + ONE; |
| 113 |
const SC FOUR = TWO + TWO; |
| 114 |
const SC baseOffDiagEnt = ONE / FOUR; |
| 115 |
const SC subDiagEnt = baseOffDiagEnt - offDiagDiff; |
| 116 |
const SC diagEnt = ONE; |
| 117 |
const SC superDiagEnt = baseOffDiagEnt + offDiagDiff; |
| 118 |
|
| 119 |
Teuchos::Array<GO> gblColIndsBuf (3); |
| 120 |
Teuchos::Array<SC> valsBuf (3); |
| 121 |
for (LO lclRow = 0; lclRow < lclNumRows; ++lclRow) { |
| 122 |
const GO gblRow = rowMap->getGlobalElement (lclRow); |
| 123 |
const GO gblCol = gblRow; |
| 124 |
LO numEnt = 0; // to be set below |
| 125 |
if (gblRow == gblMinGblInd && gblRow == gblMaxGblInd) { |
| 126 |
numEnt = 1; |
| 127 |
valsBuf[0] = diagEnt; |
| 128 |
gblColIndsBuf[0] = gblCol; |
| 129 |
} |
| 130 |
else if (gblRow == gblMinGblInd) { |
| 131 |
numEnt = 2; |
| 132 |
valsBuf[0] = diagEnt; |
| 133 |
valsBuf[1] = superDiagEnt; |
| 134 |
gblColIndsBuf[0] = gblCol; |
| 135 |
gblColIndsBuf[1] = gblCol + GO (1); |
| 136 |
} |
| 137 |
else if (gblRow == gblMaxGblInd) { |
| 138 |
numEnt = 2; |
| 139 |
valsBuf[0] = subDiagEnt; |
| 140 |
valsBuf[1] = diagEnt; |
| 141 |
gblColIndsBuf[0] = gblCol - GO (1); |
| 142 |
gblColIndsBuf[1] = gblCol; |
| 143 |
} |
| 144 |
else { |
| 145 |
numEnt = 3; |
| 146 |
valsBuf[0] = subDiagEnt; |
| 147 |
valsBuf[1] = diagEnt; |
| 148 |
valsBuf[2] = superDiagEnt; |
| 149 |
gblColIndsBuf[0] = gblCol - GO (1); |
| 150 |
gblColIndsBuf[1] = gblCol; |
| 151 |
gblColIndsBuf[2] = gblCol + GO (1); |
| 152 |
} |
| 153 |
Teuchos::ArrayView<GO> gblColInds = gblColIndsBuf.view (0, numEnt); |
| 154 |
Teuchos::ArrayView<SC> vals = valsBuf.view (0, numEnt); |
| 155 |
A->insertGlobalValues (gblRow, gblColInds, vals); |
| 156 |
} |
| 157 |
A->fillComplete (); |
| 158 |
return A; |
| 159 |
} |
| 160 |
|
| 161 |
void |
| 162 |
testSolver (Teuchos::FancyOStream& out, |
| 163 |
bool& success, |
| 164 |
const std::string& solverName, |
| 165 |
const int maxAllowedNumIters, |
| 166 |
const bool verbose) |
| 167 |
{ |
| 168 |
using Teuchos::FancyOStream; |
| 169 |
using Teuchos::getFancyOStream; |
| 170 |
using Teuchos::ParameterList; |
| 171 |
using Teuchos::parameterList; |
| 172 |
using Teuchos::RCP; |
| 173 |
using Teuchos::rcp; |
| 174 |
using Teuchos::rcpFromRef; |
| 175 |
using std::endl; |
| 176 |
using map_type = Tpetra::Map<>; |
| 177 |
using MV = Tpetra::MultiVector<>; |
| 178 |
using OP = Tpetra::Operator<>; |
| 179 |
using SC = MV::scalar_type; |
| 180 |
using GO = map_type::global_ordinal_type; |
| 181 |
using mag_type = MV::mag_type; |
| 182 |
using STS = Teuchos::ScalarTraits<SC>; |
| 183 |
using STM = Teuchos::ScalarTraits<mag_type>; |
| 184 |
// The Teuchos unit test framework likes to capture output to 'out', |
| 185 |
// and not print anything until the test is done. This can hinder |
| 186 |
// debugging. If the test crashes without useful output, try |
| 187 |
// setting this to 'true'. That will change 'myOut' from an alias |
| 188 |
// to 'out', into a wrapper for std::cerr. |
| 189 |
constexpr bool debug = false; |
| 190 |
|
| 191 |
const SC ZERO = STS::zero (); |
| 192 |
const SC ONE = STS::one (); |
| 193 |
|
| 194 |
RCP<FancyOStream> myOutPtr = |
| 195 |
debug ? getFancyOStream (rcpFromRef (std::cerr)) : rcpFromRef (out); |
| 196 |
Teuchos::FancyOStream& myOut = *myOutPtr; |
| 197 |
|
| 198 |
myOut << "Test \"native\" Tpetra version of solver \"" << solverName << "\"" |
| 199 |
<< endl; |
| 200 |
myOut << (STS::isComplex ? "complex type" : "real type") << endl; |
| 201 |
Teuchos::OSTab tab1 (out); |
| 202 |
|
| 203 |
myOut << "Create the linear system to solve" << endl; |
| 204 |
|
| 205 |
auto comm = Tpetra::getDefaultComm (); |
| 206 |
const GO gblNumRows = 10000; |
| 207 |
const GO indexBase = 0; |
| 208 |
RCP<const map_type> map (new map_type (gblNumRows, indexBase, comm)); |
| 209 |
auto A = createNonsymmTridiagMatrix (map, commandLineOptions.offDiagDiff); |
| 210 |
|
| 211 |
MV X_initial (A->getDomainMap (), 1); |
| 212 |
if (commandLineOptions.zeroInitialGuess) { |
| 213 |
X_initial.putScalar (ZERO); // (re)set initial guess to zero |
| 214 |
} |
| 215 |
else { |
| 216 |
X_initial.putScalar (ONE); // just something nonzero, to test |
| 217 |
} |
| 218 |
MV X (X_initial, Teuchos::Copy); |
| 219 |
MV B (A->getRangeMap (), 1); |
| 220 |
B.randomize (); |
| 221 |
|
| 222 |
myOut << "Create solver instance using Belos::SolverFactory" << endl; |
| 223 |
|
| 224 |
RCP<Belos::SolverManager<SC, MV, OP> > solver; |
| 225 |
try { |
| 226 |
Belos::SolverFactory<SC, MV, OP> factory; |
| 227 |
solver = factory.create (solverName, Teuchos::null); |
| 228 |
} |
| 229 |
catch (std::exception& e) { |
| 230 |
myOut << "*** FAILED: Belos::SolverFactory::create threw an exception: " |
| 231 |
<< e.what () << endl; |
| 232 |
success = false; |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
TEST_ASSERT( solver.get () != nullptr ); |
| 237 |
if (solver.get () == nullptr) { |
| 238 |
myOut << "Belos::SolverFactory returned a null solver." << endl; |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
myOut << "Set parameters" << endl; |
| 243 |
RCP<ParameterList> params = parameterList ("Belos"); |
| 244 |
params->set ("Verbosity", verbose ? 1 : 0); |
| 245 |
params->set ("Maximum Iterations", commandLineOptions.maxNumIters); |
| 246 |
params->set ("Num Blocks", commandLineOptions.restartLength); |
| 247 |
params->set ("Orthogonalization", commandLineOptions.orthoType); |
| 248 |
if (solverName == "TPETRA GMRES S-STEP") { |
| 249 |
params->set ("Step Size", commandLineOptions.stepSize); |
| 250 |
params->set ("CholeskyQR", commandLineOptions.useCholQR); |
| 251 |
params->set ("CholeskyQR2", commandLineOptions.useCholQR2); |
| 252 |
} |
| 253 |
if (solverName == "TPETRA GMRES S-STEP" || solverName == "TPETRA GMRES SINGLE REDUCE" || solverName == "TPETRA GMRES PIPELINE") { |
| 254 |
params->set ("Compute Ritz Values", commandLineOptions.computeRitzValues); |
| 255 |
} |
| 256 |
try { |
| 257 |
solver->setParameters (params); |
| 258 |
} |
| 259 |
catch (std::exception& e) { |
| 260 |
myOut << "*** FAILED: setParameters threw an exception: " |
| 261 |
<< e.what () << endl; |
| 262 |
success = false; |
| 263 |
return; |
| 264 |
} |
| 265 |
catch (...) { |
| 266 |
myOut << "*** FAILED: setParameters threw an exception " |
| 267 |
"not a subclass of std::exception." << endl; |
| 268 |
success = false; |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
myOut << "Set up the linear system to solve" << endl; |
| 273 |
auto lp = rcp (new Belos::LinearProblem<SC, MV, OP> (A, rcpFromRef (X), |
| 274 |
rcpFromRef (B))); |
| 275 |
lp->setProblem (); |
| 276 |
|
| 277 |
myOut << "Solve the linear system" << endl; |
| 278 |
solver->setProblem (lp); |
| 279 |
const Belos::ReturnType belosResult = solver->solve (); |
| 280 |
|
| 281 |
myOut << "Belos solver wrapper result: " |
| 282 |
<< (belosResult == Belos::Converged ? "Converged" : "Unconverged") |
| 283 |
<< endl |
| 284 |
<< "Number of iterations: " << solver->getNumIters () |
| 285 |
<< endl; |
| 286 |
|
| 287 |
TEST_ASSERT( solver->getNumIters () <= maxAllowedNumIters ); |
| 288 |
TEST_ASSERT( belosResult == Belos::Converged ); |
| 289 |
|
| 290 |
myOut << "Check the explicit residual norm(s)" << endl; |
| 291 |
|
| 292 |
// Get the tolerance that the solver actually used. |
| 293 |
const mag_type tol = [&] () { |
| 294 |
const char tolParamName[] = "Convergence Tolerance"; |
| 295 |
auto pl = solver->getCurrentParameters (); |
| 296 |
if (! pl->isType<mag_type> (tolParamName)) { |
| 297 |
pl = solver->getValidParameters (); |
| 298 |
} |
| 299 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 300 |
(! pl->isType<mag_type> (tolParamName), std::logic_error, |
| 301 |
"Solver lacks \"" << tolParamName << "\" parameter, in either " |
| 302 |
"getCurrentParameters() or getValidParameters()."); |
| 303 |
return pl->get<mag_type> (tolParamName); |
| 304 |
} (); |
| 305 |
|
| 306 |
MV R_initial (B.getMap (), B.getNumVectors ()); |
| 307 |
A->apply (X_initial, R_initial); |
| 308 |
R_initial.update (ONE, B, -ONE); |
| 309 |
Teuchos::Array<mag_type> R_initial_norms (R_initial.getNumVectors ()); |
| 310 |
R_initial.norm2 (R_initial_norms ()); |
| 311 |
|
| 312 |
MV R_final (B.getMap (), B.getNumVectors ()); |
| 313 |
A->apply (X, R_final); |
| 314 |
R_final.update (ONE, B, -ONE); |
| 315 |
Teuchos::Array<mag_type> R_final_norms (R_final.getNumVectors ()); |
| 316 |
R_final.norm2 (R_final_norms ()); |
| 317 |
|
| 318 |
for (size_t j = 0; j < R_final.getNumVectors (); ++j) { |
| 319 |
const mag_type relResNorm = (R_initial_norms[j] == STM::zero ()) ? |
| 320 |
R_final_norms[j] : |
| 321 |
R_final_norms[j] / R_initial_norms[j]; |
| 322 |
myOut << "Column " << (j+1) << " of " << R_final.getNumVectors () |
| 323 |
<< ": Absolute residual norm: " << R_final_norms[j] |
| 324 |
<< ", Relative residual norm: " << relResNorm |
| 325 |
<< ", Tolerance: " << tol |
| 326 |
<< endl; |
| 327 |
TEST_ASSERT( relResNorm <= tol ); |
| 328 |
} |
| 329 |
myOut << endl; |
| 330 |
|
| 331 |
Teuchos::TimeMonitor::summarize(); |
| 332 |
} |
| 333 |
|
| 334 |
TEUCHOS_UNIT_TEST( TpetraNativeSolvers, Diagonal ) |
| 335 |
{ |
| 336 |
testSolver (out, success, commandLineOptions.solverName, |
| 337 |
commandLineOptions.maxAllowedNumIters, |
| 338 |
commandLineOptions.verbose); |
| 339 |
} |
| 340 |
|
| 341 |
} // namespace (anonymous) |
| 342 |
|
| 343 |
int main (int argc, char* argv[]) |
| 344 |
{ |
| 345 |
Tpetra::ScopeGuard tpetraScope (&argc, &argv); |
| 346 |
return Teuchos::UnitTestRepository::runUnitTestsFromMain (argc, argv); |
| 347 |
} |