Debian Codegraph @

packages/belos/tpetra/src/solvers/Belos_Tpetra_Cg.hpp

raw
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_HPP
11 #define BELOS_TPETRA_CG_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 Cg: public Krylov <SC, MV, OP> {
23 private:
24 using base_type = Krylov<SC, MV, OP>;
25
26 public:
27 Cg () = default;
28
29 Cg (const Teuchos::RCP<const OP>& A) :
30 base_type::Krylov (A)
31 {}
32
33 virtual ~Cg () = 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 X/out X
41 vec_type& B, // in B/out R
42 const OP& A,
43 const OP& M,
44 const SolverInput<SC>& input)
45 {
46 using std::endl;
47 using dev_type = typename MV::device_type;
48 using val_type = typename MV::dot_type;
49 using ATS = Kokkos::ArithTraits<val_type>;
50 using magnitude_type = typename ATS::mag_type;
51 using ATM = Kokkos::ArithTraits<magnitude_type>;
52
53 const auto ONE = ATS::one ();
54
55 SolverOutput<SC> output {};
56
57 // scalars
58 magnitude_type PAP;
59 magnitude_type alpha;
60
61 vec_type P (B.getMap ());
62 vec_type AP (B.getMap ());
63 MV R_MR (B.getMap (), 2);
64 vec_type R = * (R_MR.getVectorNonConst (0));
65 vec_type MR = * (R_MR.getVectorNonConst (1));
66
67 // compute initial residual
68 Kokkos::View<val_type*, dev_type> r_beta ("results[numVecs]", 2);
69 magnitude_type beta_old = ATM::zero ();
70 magnitude_type r_norm = ATM::zero ();
71 magnitude_type r_norm_orig = ATM::zero ();
72
73 A.apply (X, R);
74 R.update (ONE, B, -ONE);
75
76 if (input.precoSide == "none") { // no preconditioner
77 Tpetra::deep_copy (P, R);
78 beta_old = ATS::real (R.dot (R));
79 r_norm = beta_old;
80 }
81 else {
82 M.apply (R, MR);
83 Tpetra::deep_copy (P, MR);
84
85 // Compute [MR, R]'*[R],
86 // R'*R is used for convergence check
87 //TODO: idot is used for now.
88 auto req = Tpetra::idot (r_beta, R_MR, R);
89 req->wait ();
90 r_norm = ATS::real (r_beta(0));
91 beta_old = ATS::real (r_beta(1));
92 }
93 r_norm = std::sqrt (r_norm);
94 r_norm_orig = r_norm;
95
96 // quick-return
97 magnitude_type metric =
98 this->getConvergenceMetric (r_norm, r_norm_orig, input);
99 if (metric <= input.tol) {
100 if (outPtr != nullptr) {
101 *outPtr << "Initial guess' residual norm " << r_norm
102 << " meets tolerance " << input.tol << endl;
103 }
104 output.absResid = r_norm;
105 output.relResid = ATM::one ();
106 output.numIters = 0;
107 output.converged = true;
108 return output;
109 }
110
111 // main loop
112 magnitude_type beta_new = ATM::zero ();
113 for (int iter = 0; iter < input.maxNumIters; ++iter) {
114 if (outPtr != nullptr) {
115 *outPtr << "Iteration " << (iter+1) << " of " << input.maxNumIters << ":" << endl;
116 outPtr->pushTab ();
117 *outPtr << "r_norm: " << r_norm << endl;
118 }
119
120 A.apply (P, AP); // AP = A*P
121 PAP = ATS::real (P.dot (AP));
122
123 if (outPtr != nullptr) {
124 *outPtr << "PAP: " << PAP << endl;
125 }
126 TEUCHOS_TEST_FOR_EXCEPTION
127 (ATS::real (PAP) <= ATM::zero (), std::runtime_error,
128 "At iteration " << (iter+1) << " out of " << input.maxNumIters
129 << ", P.dot(AP) = " << PAP << " <= 0. This usually means that "
130 "the matrix A is not symmetric (Hermitian) positive definite.");
131
132 alpha = beta_old / PAP;
133 if (outPtr != nullptr) {
134 *outPtr << "alpha: " << alpha << endl;
135 }
136
137 X.update (static_cast<SC> (alpha), P, ONE); // X = X + alpha*P
138 R.update (static_cast<SC> (-alpha), AP, ONE); // R = R - alpha*AP
139
140 if (input.precoSide == "none") { // no preconditioner
141 beta_new = ATS::real (R.dot (R));
142 r_norm = beta_new;
143 }
144 else {
145 M.apply (R, MR);
146 //TODO: idot is used to compute [MR, R]'*[R] for now.
147 auto req = Tpetra::idot (r_beta, R_MR, R);
148 req->wait ();
149 r_norm = ATS::real (r_beta(0));
150 beta_new = ATS::real (r_beta(1));
151 }
152 r_norm = std::sqrt (r_norm);
153 metric = this->getConvergenceMetric (r_norm, r_norm_orig, input);
154 if (outPtr != nullptr) {
155 *outPtr << "r_norm: " << r_norm << endl;
156 *outPtr << "metric: " << metric << endl;
157 }
158
159 // convergence check
160 if (metric <= input.tol) {
161 output.absResid = r_norm;
162 output.relResid = r_norm / r_norm_orig;
163 output.numIters = iter + 1;
164 output.converged = true;
165 return output;
166 }
167 else if (iter + 1 < input.maxNumIters) { // not last iteration
168 const magnitude_type beta = beta_new / beta_old;
169 if (input.precoSide == "none") {
170 P.update (ONE, R, static_cast<SC> (beta)); // P += beta*R
171 }
172 else {
173 P.update (ONE, MR, static_cast<SC> (beta));
174 }
175 beta_old = beta_new;
176 }
177
178 if (outPtr != nullptr) {
179 outPtr->popTab ();
180 }
181 }
182
183 // Reached max iteration count without converging
184 output.absResid = r_norm;
185 output.relResid = r_norm / r_norm_orig;
186 output.numIters = input.maxNumIters;
187 output.converged = false;
188 return output;
189 }
190 };
191
192 template<class SC, class MV, class OP,
193 template<class, class, class> class KrylovSubclassType>
194 class SolverManager;
195
196 // This is the Belos::SolverManager subclass that gets registered with
197 // Belos::SolverFactory.
198 template<class SC, class MV, class OP>
199 using CgSolverManager = SolverManager<SC, MV, OP, Cg>;
200
201 /// \brief Register CgSolverManager for all enabled Tpetra
202 /// template parameter combinations.
203 void register_Cg (const bool verbose);
204
205 } // namespace Impl
206 } // namespace BelosTpetra
207
208 #endif // BELOS_TPETRA_CG_HPP