Debian Codegraph @

packages/belos/tpetra/src/solvers/Belos_Tpetra_CgPipeline.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_CGPIPELINE_HPP
11 #define BELOS_TPETRA_CGPIPELINE_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 CgPipeline : public Krylov<SC, MV, OP> {
23 private:
24 using base_type = Krylov<SC, MV, OP>;
25
26 public:
27 CgPipeline () = default;
28
29 CgPipeline (const Teuchos::RCP<const OP>& A) :
30 base_type::Krylov (A)
31 {}
32
33 virtual ~CgPipeline () = 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/out
41 vec_type& R_in, // in/out
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 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 bool zeroOut = false;
58 MV R_AR (R_in.getMap (), 2, zeroOut); // [R, A*R]
59 vec_type R = * (R_AR.getVectorNonConst (0));
60 vec_type AR = * (R_AR.getVectorNonConst (1));
61 Tpetra::deep_copy (R, R_in);
62
63 // aux matrix containing R and R
64 MV R_R (R_in.getMap (), 2);
65 vec_type R1 = * (R_R.getVectorNonConst (0));
66 vec_type R2 = * (R_R.getVectorNonConst (1));
67
68 // results of [R R]'*[R AR]
69 Kokkos::View<val_type*, dev_type> RR_RAR ("results[numVecs]", 2);
70 auto RR_RAR_host = Kokkos::create_mirror_view (RR_RAR);
71 vec_type P (R, Teuchos::Copy);
72 vec_type AP (P.getMap ());
73 vec_type AAR(R.getMap (), zeroOut);
74 vec_type AW (R.getMap ());
75 vec_type MR (R.getMap (), zeroOut);
76 vec_type U (R.getMap (), zeroOut);
77 vec_type Q (R.getMap ());
78
79 // local vars
80 val_type RAR, PAP;
81 magnitude_type alpha = ATM::zero ();
82 magnitude_type beta = ATM::zero ();
83 magnitude_type r_norm = ATM::zero ();
84 magnitude_type r_norm_orig = ATM::zero ();
85 magnitude_type beta_new = ATM::zero ();
86 magnitude_type beta_old = ATM::zero ();
87
88 // Initial step
89 if (input.precoSide == "none") {
90 // W = A*R
91 A.apply (R, AR);
92 }
93 else {
94 // preconditioner
95 M.apply (R, U);
96 // W = A*R
97 A.apply (U, AR);
98 }
99
100 // main loop
101 for (int iter = 0; iter < input.maxNumIters; ++iter) {
102 if (outPtr != nullptr) {
103 *outPtr << "Iteration " << (iter+1) << " of " << input.maxNumIters << ": r_norm: " << r_norm;
104 }
105
106 // * all-reduce *
107 // [RR,RAR] = [R,RAR]'*R
108 using request_ptr = decltype (Tpetra::idot (RR_RAR, R_AR, R));
109 request_ptr req;
110 {
111 if (input.precoSide == "none") {
112 req = Tpetra::idot (RR_RAR, R_AR, R);
113 } else {
114 req = Tpetra::idot (RR_RAR, R_AR, U);
115 }
116 }
117
118 // * matrix op (moved up to overlap with all-reduce) *
119 if (input.precoSide == "none") {
120 // n = A*w (AR is w, and AAR is n)
121 A.apply (AR, AAR);
122 }
123 else {
124 // preconditioner
125 // m = M^{-1}*w (AR is w, and MR is m)
126 M.apply (AR, MR);
127 // n = A*m (MR is m, and AAR is n)
128 A.apply (MR, AAR);
129 }
130
131 // * check for convergence *
132 req->wait ();
133 Kokkos::deep_copy (RR_RAR_host, RR_RAR);
134 RAR = RR_RAR_host(1);
135 beta_new = ATS::real (RR_RAR_host(0));
136
137 r_norm = std::sqrt( beta_new );
138 if (iter == 0) {
139 r_norm_orig = r_norm;
140 }
141 const magnitude_type metric =
142 this->getConvergenceMetric (r_norm, r_norm_orig, input);
143 if (outPtr != nullptr) {
144 *outPtr << ", RAR: " << RAR << ", r_norm: " << r_norm << ", metric: " << metric;
145 }
146 if (metric <= input.tol) {
147 if (outPtr != nullptr) {
148 *outPtr << endl;
149 }
150 output.absResid = r_norm;
151 output.relResid = r_norm / r_norm_orig;
152 output.numIters = iter + 1;
153 output.converged = true;
154
155 Tpetra::deep_copy (R_in, R);
156 return output;
157 }
158
159 if (iter == 0) {
160 alpha = beta_new / ATS::real (RAR);
161 beta = ATM::zero ();
162 }
163 else {
164 // beta
165 beta = beta_new / beta_old;
166 if (outPtr != nullptr) {
167 *outPtr << ", beta: " << beta;
168 }
169 // PAP
170 PAP = RAR - beta_new * (beta / alpha);
171 TEUCHOS_TEST_FOR_EXCEPTION
172 (ATS::real (RAR) <= ATM::zero (), std::runtime_error,
173 "At iteration " << (iter+1)
174 << " out of " << input.maxNumIters << ", R.dot(AR) = " << RAR <<
175 " <= 0. This usually means that the matrix A is not symmetric "
176 "(Hermitian) positive definite.");
177
178 // alpha
179 alpha = beta_new / ATS::real (PAP);
180 }
181 if (outPtr != nullptr) {
182 *outPtr << ", alpha: " << alpha << endl;
183 }
184 // beta_old
185 beta_old = beta_new;
186
187 // * search direction *
188 if (input.precoSide == "none") {
189 // P = R + beta*P
190 P.update (ONE, R, static_cast<SC> (beta));
191 }
192 else {
193 // p = u + beta*p (P is p, and U is u)
194 P.update (ONE, U, static_cast<SC> (beta));
195 // q = m + beta*q (Q is q, and MR is m)
196 Q.update (ONE, MR, static_cast<SC> (beta));
197 // u = u - alpha*q (U is u, and Q is q)
198 U.update (static_cast<SC> (-alpha), Q, ONE);
199 }
200 // s = w + beta*s (AP is s, and AR is w)
201 AP.update (ONE, AR, static_cast<SC> (beta));
202
203 // * solution update *
204 // x = x + alpha*p (X is x, and P is p)
205 X.update (static_cast<SC> (alpha), P, ONE);
206 // r = r - alpha*s (R is r, and AP is s)
207 R.update (static_cast<SC> (-alpha), AP, ONE);
208
209 // * next search directions update *
210 // z = n + beta*z (AW is z, and AAR is n)
211 AW.update (ONE, AAR, static_cast<SC> (beta));
212 // w = w - alpha*n (AR is w, and AW is n)
213 AR.update (static_cast<SC> (-alpha), AW, ONE);
214 }
215
216 // Reached max iteration count without converging
217 Tpetra::deep_copy (R_in, R);
218 output.absResid = r_norm;
219 output.relResid = r_norm / r_norm_orig;
220 output.numIters = input.maxNumIters;
221 output.converged = false;
222 return output;
223 }
224 };
225
226 template<class SC, class MV, class OP,
227 template<class, class, class> class KrylovSubclassType>
228 class SolverManager;
229
230 // This is the Belos::SolverManager subclass that gets registered with
231 // Belos::SolverFactory.
232 template<class SC, class MV, class OP>
233 using CgPipelineSolverManager = SolverManager<SC, MV, OP, CgPipeline>;
234
235 /// \brief Register CgPipelineSolverManager for all enabled Tpetra
236 /// template parameter combinations.
237 void register_CgPipeline (const bool verbose);
238
239 } // namespace Impl
240 } // namespace BelosTpetra
241
242 #endif // BELOS_TPETRA_CGPIPELINE_HPP