| 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 |
#ifndef BELOS_TPETRA_KRYLOV_PARAMETERS_HPP |
| 11 |
#define BELOS_TPETRA_KRYLOV_PARAMETERS_HPP |
| 12 |
|
| 13 |
#include "Teuchos_FancyOStream.hpp" |
| 14 |
#include "Teuchos_ScalarTraits.hpp" |
| 15 |
#include <algorithm> |
| 16 |
#include <iostream> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
namespace BelosTpetra { |
| 21 |
namespace Impl { |
| 22 |
|
| 23 |
class Indent { |
| 24 |
public: |
| 25 |
Indent (Teuchos::FancyOStream* out) : out_ (out) { |
| 26 |
if (out_ != nullptr) { |
| 27 |
out_->pushTab (); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
Indent () = delete; |
| 32 |
Indent (const Indent&) = delete; |
| 33 |
Indent& operator= (const Indent&) = delete; |
| 34 |
|
| 35 |
~Indent () { |
| 36 |
if (out_ != nullptr) { |
| 37 |
out_->popTab (); |
| 38 |
} |
| 39 |
} |
| 40 |
private: |
| 41 |
Teuchos::FancyOStream* out_; |
| 42 |
}; |
| 43 |
|
| 44 |
template<class ScalarType> |
| 45 |
struct SolverInput { |
| 46 |
public: |
| 47 |
using mag_type = typename Teuchos::ScalarTraits<ScalarType>::magnitudeType; |
| 48 |
|
| 49 |
private: |
| 50 |
using STS = Teuchos::ScalarTraits<ScalarType>; |
| 51 |
using STM = Teuchos::ScalarTraits<mag_type>; |
| 52 |
|
| 53 |
public: |
| 54 |
SolverInput () = default; |
| 55 |
SolverInput (const SolverInput<ScalarType>&) = default; |
| 56 |
SolverInput& operator= (const SolverInput<ScalarType>&) = default; |
| 57 |
|
| 58 |
mag_type r_norm_orig = STM::zero (); |
| 59 |
mag_type tol = STM::squareroot (STS::eps ()); |
| 60 |
int maxNumIters = 1000; |
| 61 |
int resCycle = 30; |
| 62 |
int stepSize = 5; |
| 63 |
bool needToScale = true; |
| 64 |
bool needToReortho = false; |
| 65 |
bool delayedNorm = true; |
| 66 |
int maxOrthoSteps = 0; |
| 67 |
std::string orthoType {"ICGS"}; |
| 68 |
std::string precoSide {"none"}; |
| 69 |
bool computeRitzValues = true; |
| 70 |
bool computeRitzValuesOnFly = false; |
| 71 |
}; |
| 72 |
|
| 73 |
// The default constructor creates output corresponding to "solving |
| 74 |
// a linear system with zero right-hand sides." This means that |
| 75 |
// the solve succeeded trivially (converged == true), in zero |
| 76 |
// iterations, with zero residual norm. |
| 77 |
template<class SC> |
| 78 |
struct SolverOutput { |
| 79 |
using val_type = SC; |
| 80 |
using mag_type = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 81 |
using complex_type = std::complex<mag_type>; |
| 82 |
|
| 83 |
SolverOutput () = default; |
| 84 |
SolverOutput (const SolverOutput<SC>&) = default; |
| 85 |
SolverOutput& operator= (const SolverOutput<SC>&) = default; |
| 86 |
|
| 87 |
//! Absolute residual norm. |
| 88 |
mag_type absResid = Teuchos::ScalarTraits<mag_type>::zero (); |
| 89 |
//! Relative residual norm (if applicable). |
| 90 |
mag_type relResid = Teuchos::ScalarTraits<mag_type>::zero (); |
| 91 |
//! Number of iterations executed. |
| 92 |
int numIters = 0; |
| 93 |
//! Number of restarts. |
| 94 |
int numRests = -1; |
| 95 |
//! Whether the solve converged. |
| 96 |
bool converged = true; |
| 97 |
//! Ritz values if requested |
| 98 |
std::vector<complex_type> ritzValues; |
| 99 |
}; |
| 100 |
|
| 101 |
/// \brief Combine two solver outputs. |
| 102 |
/// |
| 103 |
/// "Combining" two solver outputs means aggregating the results of |
| 104 |
/// solving A x_1 = b_1 and A x_2 = b_2, that is, two solves with the |
| 105 |
/// same matrix, but different right-hand sides. Combining is |
| 106 |
/// associative and commutative. |
| 107 |
template<class SC> |
| 108 |
void |
| 109 |
combineSolverOutput (SolverOutput<SC>& dst, const SolverOutput<SC>& src) |
| 110 |
{ |
| 111 |
// max of the residuals and iteration counts |
| 112 |
dst.relResid = dst.relResid > src.relResid ? dst.relResid : src.relResid; |
| 113 |
dst.absResid = dst.absResid > src.absResid ? dst.absResid : src.absResid; |
| 114 |
dst.numIters = dst.numIters > src.numIters ? dst.numIters : src.numIters; |
| 115 |
dst.numRests = dst.numRests > src.numRests ? dst.numRests : src.numRests; |
| 116 |
// "converged" if all converged |
| 117 |
dst.converged = dst.converged && src.converged; |
| 118 |
// copy ritz values |
| 119 |
if (src.ritzValues.size () > dst.ritzValues.size ()) { |
| 120 |
dst.ritzValues.resize (src.ritzValues.size ()); |
| 121 |
std::copy (std::begin (src.ritzValues), std::end (src.ritzValues), |
| 122 |
std::begin (dst.ritzValues)); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
template<class SC> |
| 127 |
std::ostream& |
| 128 |
operator<< (std::ostream& out, |
| 129 |
const SolverOutput<SC>& so) |
| 130 |
{ |
| 131 |
using std::endl; |
| 132 |
|
| 133 |
out << "Solver output:" << endl |
| 134 |
<< " Absolute residual norm: " << so.absResid << endl |
| 135 |
<< " Relative residual norm: " << so.relResid << endl |
| 136 |
<< " Number of iterations: " << so.numIters << endl; |
| 137 |
if (so.numRests >= 0) { |
| 138 |
out << " Number of restarts: " << so.numRests << endl; |
| 139 |
} |
| 140 |
out << " Converged: " << (so.converged ? "true" : "false") << endl; |
| 141 |
if (so.ritzValues.size () != 0) { |
| 142 |
out << " Ritz values: ["; |
| 143 |
for (std::size_t k = 0; k < so.ritzValues.size (); ++k) { |
| 144 |
out << so.ritzValues[k]; |
| 145 |
if (k + std::size_t (1) < so.ritzValues.size ()) { |
| 146 |
out << ", "; |
| 147 |
} |
| 148 |
} |
| 149 |
out << "]" << endl; |
| 150 |
} |
| 151 |
return out; |
| 152 |
} |
| 153 |
|
| 154 |
template<class SC> |
| 155 |
std::ostream& |
| 156 |
operator<< (std::ostream& out, |
| 157 |
const SolverInput<SC>& si) |
| 158 |
{ |
| 159 |
using std::endl; |
| 160 |
|
| 161 |
out << "Solver input:" << endl; |
| 162 |
out << " Original residual norm: " << si.r_norm_orig << endl |
| 163 |
<< " Residual norm tolerance: " << si.tol << endl |
| 164 |
<< " Max. number of iterations: " << si.maxNumIters << endl; |
| 165 |
if (si.resCycle > 0) { |
| 166 |
out << " Restart cycle: " << si.resCycle << endl; |
| 167 |
out << " Orthogonalization: " << si.orthoType << endl; |
| 168 |
} |
| 169 |
out << " Step size: " << si.stepSize << endl |
| 170 |
<< " Preconditioner: " << si.precoSide << endl; |
| 171 |
return out; |
| 172 |
} |
| 173 |
|
| 174 |
} // namespace Impl |
| 175 |
} // namespace BelosTpetra |
| 176 |
|
| 177 |
#endif // BELOS_TPETRA_KRYLOV_PARAMETERS_HPP |