| 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_CG_SINGLE_REDUCE_HPP |
| 11 |
#define BELOS_TPETRA_CG_SINGLE_REDUCE_HPP |
| 12 |
|
| 13 |
#include "Belos_Tpetra_Krylov.hpp" |
| 14 |
#include "Tpetra_idot.hpp" |
| 15 |
|
| 16 |
namespace BelosTpetra { |
| 17 |
namespace Impl { |
| 18 |
|
| 19 |
template<class SC = Tpetra::MultiVector<>::scalar_type, |
| 20 |
class MV = Tpetra::MultiVector<SC>, |
| 21 |
class OP = Tpetra::Operator<SC>> |
| 22 |
class CgSingleReduce: public Krylov<SC, MV, OP> { |
| 23 |
private: |
| 24 |
using base_type = Krylov<SC, MV, OP>; |
| 25 |
|
| 26 |
public: |
| 27 |
CgSingleReduce () = default; |
| 28 |
|
| 29 |
CgSingleReduce (const Teuchos::RCP<const OP>& A) : |
| 30 |
base_type::Krylov (A) |
| 31 |
{} |
| 32 |
|
| 33 |
virtual ~CgSingleReduce () = default; |
| 34 |
|
| 35 |
protected: |
| 36 |
using vec_type = typename base_type::vec_type; |
| 37 |
|
| 38 |
SolverOutput<SC> |
| 39 |
solveOneVec (Teuchos::FancyOStream* outPtr, |
| 40 |
vec_type& X, // in X0/out X |
| 41 |
vec_type& B, // in B/out R |
| 42 |
const OP& A, |
| 43 |
const OP& M, |
| 44 |
const SolverInput<SC>& input) override |
| 45 |
{ |
| 46 |
using std::endl; |
| 47 |
using dev_type = typename MV::device_type; |
| 48 |
using ATS = Kokkos::ArithTraits<SC>; |
| 49 |
using magnitude_type = typename ATS::mag_type; |
| 50 |
using ATM = Kokkos::ArithTraits<magnitude_type>; |
| 51 |
using dot_type = typename MV::dot_type; |
| 52 |
|
| 53 |
const SC ONE = ATS::one (); |
| 54 |
SolverOutput<SC> output {}; |
| 55 |
|
| 56 |
// compute initial residual |
| 57 |
vec_type MR (B.getMap ()); |
| 58 |
magnitude_type beta_old = ATM::zero (); |
| 59 |
if (input.precoSide == "none") { |
| 60 |
beta_old = ATS::real (B.dot (B)); |
| 61 |
} |
| 62 |
else { |
| 63 |
M.apply (B, MR); |
| 64 |
beta_old = ATS::real (B.dot (MR)); |
| 65 |
} |
| 66 |
magnitude_type r_norm = std::sqrt (beta_old); |
| 67 |
magnitude_type r_norm_orig = r_norm; |
| 68 |
|
| 69 |
// quick return |
| 70 |
magnitude_type metric = this->getConvergenceMetric (r_norm, r_norm_orig, input); |
| 71 |
if (metric <= input.tol) { |
| 72 |
output.absResid = r_norm; |
| 73 |
output.relResid = ATM::one (); |
| 74 |
output.numIters = 0; |
| 75 |
output.converged = true; |
| 76 |
// R doesn't exist yet, so we don't have to copy R back to B |
| 77 |
// here, as we do below. |
| 78 |
return output; |
| 79 |
} |
| 80 |
|
| 81 |
// local vectors |
| 82 |
// matrix containing R and AR |
| 83 |
MV R_AR (B.getMap (), 2); |
| 84 |
vec_type R = * (R_AR.getVectorNonConst (0)); |
| 85 |
vec_type AR = * (R_AR.getVectorNonConst (1)); |
| 86 |
Tpetra::deep_copy (R, B); |
| 87 |
// results of [R AR]'*R |
| 88 |
magnitude_type RAR; |
| 89 |
Kokkos::View<dot_type*, dev_type> RR_RAR ("results[numVecs]", 2); |
| 90 |
auto RR_RAR_host = Kokkos::create_mirror_view (RR_RAR); |
| 91 |
vec_type P (R, Teuchos::Copy); |
| 92 |
vec_type AP (P.getMap ()); |
| 93 |
|
| 94 |
// Initial step |
| 95 |
// AR = A*R |
| 96 |
magnitude_type PAP; |
| 97 |
if (input.precoSide == "none") { |
| 98 |
A.apply (R, AR); |
| 99 |
PAP = ATS::real (R.dot (AR)); |
| 100 |
} |
| 101 |
else { |
| 102 |
M.apply (R, MR); |
| 103 |
A.apply (MR, AR); |
| 104 |
|
| 105 |
// [beta_old, PAP] = [MR,AR]'*MR |
| 106 |
// TODO: idot is used for now. |
| 107 |
//beta_old = MR.dot( R ); |
| 108 |
//PAP = MR.dot( AR ); |
| 109 |
auto req = Tpetra::idot (RR_RAR, R_AR, MR); |
| 110 |
req->wait (); |
| 111 |
|
| 112 |
Kokkos::deep_copy (RR_RAR_host, RR_RAR); |
| 113 |
beta_old = ATS::real (RR_RAR_host(0)); |
| 114 |
PAP = ATS::real (RR_RAR_host(1)); |
| 115 |
|
| 116 |
r_norm = std::sqrt (beta_old); |
| 117 |
} |
| 118 |
magnitude_type alpha = beta_old / PAP; |
| 119 |
magnitude_type beta = ATM::zero (); |
| 120 |
magnitude_type beta_new = ATM::zero (); |
| 121 |
// main loop |
| 122 |
for (int iter = 0; iter < input.maxNumIters; ++iter) { |
| 123 |
if (outPtr != nullptr) { |
| 124 |
*outPtr << "Iteration " << (iter+1) << " of " << input.maxNumIters |
| 125 |
<< ": r_norm: " << r_norm; |
| 126 |
} |
| 127 |
|
| 128 |
// * search direction * |
| 129 |
// P = R + beta*P |
| 130 |
if (input.precoSide == "none") { |
| 131 |
P.update (ONE, R, static_cast<SC> (beta)); |
| 132 |
} else { |
| 133 |
P.update (ONE, MR, static_cast<SC> (beta)); |
| 134 |
} |
| 135 |
// AP = AR + beta*AP |
| 136 |
AP.update (ONE, AR, static_cast<SC> (beta)); |
| 137 |
|
| 138 |
// * solution update * |
| 139 |
// X = X + alpha*P |
| 140 |
X.update (static_cast<SC> (alpha), P, ONE); |
| 141 |
// R = R - alpha*AP |
| 142 |
R.update (static_cast<SC> (-alpha), AP, ONE); |
| 143 |
|
| 144 |
// * matrix op * |
| 145 |
// AR = A*R |
| 146 |
if (input.precoSide == "none") { |
| 147 |
A.apply (R, AR); |
| 148 |
// [RR,RAR] = [R,AR]'*R |
| 149 |
// TODO: idot is used for now. |
| 150 |
auto req = Tpetra::idot (RR_RAR, R_AR, R); |
| 151 |
req->wait (); |
| 152 |
} |
| 153 |
else { |
| 154 |
M.apply (R, MR); |
| 155 |
A.apply (MR, AR); |
| 156 |
// [RR,RAR] = [R,AR]'*MR |
| 157 |
// TODO: idot is used for now. |
| 158 |
// TODO: need to compute R'*R for convergence check. |
| 159 |
auto req = Tpetra::idot (RR_RAR, R_AR, MR); |
| 160 |
req->wait (); |
| 161 |
} |
| 162 |
// * all-reduce * |
| 163 |
Kokkos::deep_copy (RR_RAR_host, RR_RAR); |
| 164 |
beta_new = ATS::real (RR_RAR_host(0)); |
| 165 |
RAR = ATS::real (RR_RAR_host(1)); |
| 166 |
|
| 167 |
// convergence check |
| 168 |
r_norm = std::sqrt( beta_new ); |
| 169 |
metric = this->getConvergenceMetric (r_norm, r_norm_orig, input); |
| 170 |
if (outPtr != nullptr) { |
| 171 |
*outPtr << ", r_norm: " << r_norm << ", RAR: " << RAR << ", metric: " << metric; |
| 172 |
} |
| 173 |
if (metric <= input.tol) { |
| 174 |
if (outPtr != nullptr) { |
| 175 |
*outPtr << endl; |
| 176 |
} |
| 177 |
output.absResid = r_norm; |
| 178 |
output.relResid = r_norm / r_norm_orig; |
| 179 |
output.numIters = iter + 1; |
| 180 |
output.converged = true; |
| 181 |
|
| 182 |
Tpetra::deep_copy (B, R); |
| 183 |
return output; |
| 184 |
} |
| 185 |
else if (iter + 1 < input.maxNumIters) { // not last iteration |
| 186 |
// beta |
| 187 |
beta = beta_new / beta_old; |
| 188 |
if (outPtr != nullptr) { |
| 189 |
*outPtr << ", beta: " << beta; |
| 190 |
} |
| 191 |
// PAP |
| 192 |
PAP = RAR - beta_new * (beta /alpha); |
| 193 |
TEUCHOS_TEST_FOR_EXCEPTION |
| 194 |
(RAR <= ATM::zero (), std::runtime_error, "At iteration " << (iter+1) |
| 195 |
<< " out of " << input.maxNumIters << ", R.dot(AR) = " << RAR << |
| 196 |
" <= 0. This usually means that the matrix A is not symmetric " |
| 197 |
"(Hermitian) positive definite."); |
| 198 |
|
| 199 |
// alpha = |
| 200 |
alpha = beta_new / PAP; |
| 201 |
if (outPtr != nullptr) { |
| 202 |
*outPtr << ", alpha: " << alpha << endl; |
| 203 |
} |
| 204 |
// beta_old |
| 205 |
beta_old = beta_new; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
// Reached max iteration count without converging |
| 210 |
output.absResid = r_norm; |
| 211 |
output.relResid = r_norm / r_norm_orig; |
| 212 |
output.numIters = input.maxNumIters; |
| 213 |
output.converged = false; |
| 214 |
|
| 215 |
Tpetra::deep_copy (B, R); |
| 216 |
return output; |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
template<class SC, class MV, class OP, |
| 221 |
template<class, class, class> class KrylovSubclassType> |
| 222 |
class SolverManager; |
| 223 |
|
| 224 |
// This is the Belos::SolverManager subclass that gets registered with |
| 225 |
// Belos::SolverFactory. |
| 226 |
template<class SC, class MV, class OP> |
| 227 |
using CgSingleReduceSolverManager = SolverManager<SC, MV, OP, CgSingleReduce>; |
| 228 |
|
| 229 |
/// \brief Register CgSingleReduceSolverManager for all enabled Tpetra |
| 230 |
/// template parameter combinations. |
| 231 |
void register_CgSingleReduce (const bool verbose); |
| 232 |
|
| 233 |
} // namespace Impl |
| 234 |
} // namespace BelosTpetra |
| 235 |
|
| 236 |
#endif // BELOS_TPETRA_CG_SINGLE_REDUCE_HPP |