Debian Codegraph @

packages/belos/tpetra/src/solvers/Belos_Tpetra_GmresS.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_GMRES_S_HPP
11 #define BELOS_TPETRA_GMRES_S_HPP
12
13 #include "Belos_Tpetra_Gmres.hpp"
14 #include "Belos_Tpetra_UpdateNewton.hpp"
15
16 namespace BelosTpetra {
17 namespace Impl {
18
19 template<class SC = Tpetra::Operator<>::scalar_type,
20 class MV = Tpetra::MultiVector<SC>,
21 class OP = Tpetra::Operator<SC>>
22 class GmresS : public Gmres<SC, MV, OP> {
23 private:
24 using base_type = Gmres<SC, MV, OP>;
25 using MVT = Belos::MultiVecTraits<SC, MV>;
26 using LO = typename MV::local_ordinal_type;
27 using STS = Teuchos::ScalarTraits<SC>;
28 using mag_type = typename STS::magnitudeType;
29 using STM = Teuchos::ScalarTraits<mag_type>;
30 using complex_type = std::complex<mag_type>;
31 using dense_matrix_type = Teuchos::SerialDenseMatrix<LO, SC>;
32 using dense_vector_type = Teuchos::SerialDenseVector<LO, SC>;
33 using vec_type = typename Krylov<SC, MV, OP>::vec_type;
34
35 public:
36 GmresS () = default;
37
38 GmresS (const Teuchos::RCP<const OP>& A) :
39 base_type::Gmres (A)
40 {}
41
42 virtual ~GmresS () = default;
43
44 virtual void
45 setParameters (Teuchos::ParameterList& params) {
46 base_type::setParameters (params);
47
48 int stepSize = this->input_.stepSize;
49 if (params.isParameter ("Step Size")) {
50 stepSize = params.get<int> ("Step Size");
51 }
52 this->input_.stepSize = stepSize;
53 }
54
55 private:
56 // If no preconditioning: AP := A*P.
57 // If right preconditioning: MP := M(P), AP = A*MP (= A*M(P)).
58 // if left preconditioning: MP := A*P, AP = M(MP) (= M(A*P)).
59 void
60 applyPreconditionedOperator (MV& AP,
61 MV& MP,
62 const OP& A,
63 const OP& M,
64 const MV& P,
65 const SolverInput<SC>& input) const
66 {
67 if (input.precoSide == "none") {
68 A.apply (P, AP);
69 }
70 else if (input.precoSide == "right") {
71 M.apply (P, MP);
72 A.apply (MP, AP);
73 }
74 else { // left
75 A.apply (P, MP);
76 M.apply (MP, AP);
77 }
78 }
79
80 SolverOutput<SC>
81 solveOneVec (Teuchos::FancyOStream* outPtr,
82 vec_type& X, // in X/out X
83 vec_type& B, // in B
84 const OP& A,
85 const OP& M,
86 const SolverInput<SC>& input)
87 {
88 using std::endl;
89 const int s = input.stepSize;
90 const SC zero = STS::zero ();
91 const SC one = STS::one ();
92 SolverOutput<SC> output {};
93
94 if (outPtr != nullptr) {
95 *outPtr << "GMRES(s)" << endl;
96 }
97 Indent indent1 (outPtr);
98 if (outPtr != nullptr) {
99 *outPtr << "Solver input:" << endl;
100 Indent indentInner (outPtr);
101 *outPtr << input;
102 }
103
104 // Initial unpreconditioned residual norm.
105 // This is only useful to help us judge convergence.
106 //
107 // FIXME (mfh 18 Sep 2018) It's a waste to recompute this, since
108 // standard GMRES already does.
109 mag_type b0_norm = STM::zero ();
110 { // FIXME (could get this from standard GMRES)
111 vec_type R (B, Teuchos::Copy);
112 A.apply (X, R, Teuchos::NO_TRANS, STS::one (), -STS::one ());
113 b0_norm = R.norm2 ();
114 }
115
116 // Invoke standard Gmres for the first restart cycle, to compute
117 // Ritz values for use as Newton shifts
118 vec_type R (B, Teuchos::Copy);
119 {
120 if (outPtr != nullptr) {
121 *outPtr << "Run standard GMRES for first restart cycle" << endl;
122 }
123 SolverInput<SC> input_gmres = input;
124 input_gmres.maxNumIters = s;
125 input_gmres.computeRitzValues = true;
126 output = Gmres<SC, MV, OP>::solveOneVec (outPtr, X, R, A, M,
127 input_gmres);
128 if (output.converged) {
129 return output; // standard GMRES converged
130 }
131 output.numRests++;
132 }
133
134 // Standard GMRES did some work for us.
135 // output.absResid is _our_ initial residual norm.
136 mag_type b_norm = output.absResid;
137
138 vec_type MP (B.getMap ());
139 MV Q (B.getMap (), s+1);
140 vec_type P0 = * (Q.getVectorNonConst (0));
141
142 mag_type r_norm = STM::zero ();
143 if (input.precoSide == "left") {
144 M.apply (R, P0);
145 r_norm = P0.norm2 (); // residual norm
146 }
147 else {
148 Tpetra::deep_copy (P0, R);
149 r_norm = output.absResid;
150 }
151 mag_type metric = this->getConvergenceMetric (r_norm, b0_norm, input);
152
153 Teuchos::BLAS<LO, SC> blas;
154 Teuchos::LAPACK<LO, SC> lapack;
155 dense_matrix_type H (s+1, s, true); // upper Hessenberg matrix
156 dense_matrix_type G (s+1, s+1, true); // Upper-tri matrix from ortho
157 dense_matrix_type BB (s, s, true); // Change-of-basis matrix
158 dense_vector_type y (s+1, true);
159 std::vector<mag_type> cs (s);
160 std::vector<SC> sn (s);
161
162 // Construct s x s upper part of change-of-basis matrix.
163 if (outPtr != nullptr) {
164 *outPtr << "Construct B" << endl;
165 }
166 for (int j = 0; j < s; ++j) {
167 const auto newtonShift = output.ritzValues[j];
168 BB(j, j) = SC {newtonShift.real ()};
169 // Correction for real-arithmetic Newton basis
170 if (j != 0) {
171 BB(j-1, j) = -SC {newtonShift.imag () * newtonShift.imag ()};
172 }
173 if (j + 1 < s) {
174 BB(j+1, j) = STS::one ();
175 }
176 }
177
178 // initialize starting vector
179 P0.scale (one / b_norm);
180 y[0] = SC {b_norm};
181
182 if (outPtr != nullptr) {
183 *outPtr << "Main loop" << endl;
184 }
185 for (int k = 0; k < s; ++k) {
186 vec_type P = * (Q.getVectorNonConst (k));
187 vec_type AP = * (Q.getVectorNonConst (k+1));
188
189 applyPreconditionedOperator (AP, MP, A, M, P, input);
190 const auto newtonShift = output.ritzValues[k];
191 // AP := AP - real(theta_k) * P
192 AP.update (-newtonShift.real (), P, STS::one ());
193 // Correction for real-arithmetic Newton basis
194 if (k > 0 && newtonShift.imag () != STM::zero ()) {
195 vec_type P_prev = * (Q.getVectorNonConst (k-1));
196 AP.update (newtonShift.imag () * newtonShift.imag (),
197 P_prev, STS::one ());
198 }
199 }
200 const int rank = this->normalizeBelosOrthoManager (Q, G);
201 if (outPtr != nullptr) {
202 *outPtr << "Rank of s-step basis: " << rank << endl;
203 }
204
205 // For notation, see Equation (3.6) in Hoemmen's dissertation.
206 // Replace R in the dissertation with G here.
207
208 const SC rho = G(s,s);
209 const SC rho_tilde = G(s-1,s-1);
210 if (outPtr != nullptr) {
211 *outPtr << "G(s-1,s-1)=" << rho_tilde << ", G(s,s)=" << rho << endl;
212 }
213 TEUCHOS_TEST_FOR_EXCEPTION
214 (rho_tilde == STS::zero () || rho == STS::zero (),
215 std::runtime_error, "Basis is rank deficient");
216
217 // G * B * G^{-1}
218 dense_matrix_type H_sxs (Teuchos::View, H.values (), H.stride (), s, s);
219 dense_matrix_type G_sxs (Teuchos::View, G.values (), G.stride (), s, s);
220 blas.GEMM (Teuchos::NO_TRANS, Teuchos::NO_TRANS, s, s, s, STS::one (),
221 G.values (), G.stride (),
222 BB.values (), BB.stride (),
223 STS::one (), H_sxs.values (), H_sxs.stride ());
224 blas.TRSM (Teuchos::RIGHT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
225 Teuchos::NON_UNIT_DIAG, s, s, STS::one (),
226 G_sxs.values (), G_sxs.stride (),
227 H_sxs.values (), H_sxs.stride ());
228
229 dense_vector_type z (Teuchos::View, &G(0, s), s);
230 const SC b = STS::one (); // for Newton basis
231 const SC z_scalingFactor = b / rho_tilde;
232
233 for (int i = 0; i < s; ++i) {
234 H_sxs(i, s-1) += z_scalingFactor * z(i);
235 }
236 H(s, s-1) = z_scalingFactor * rho;
237
238 if (outPtr != nullptr) {
239 *outPtr << "Finished computing H; H(s,s-1)=" << H(s,s-1) << endl;
240 }
241
242 // Convergence check
243 if (H(s, s-1) != STS::zero ()) {
244 // Reduce H to upper tri, and apply rotations to y
245 for (int j = 0; j < s; ++j) {
246 this->reduceHessenburgToTriangular (j, H, cs, sn, y);
247 }
248 if (outPtr != nullptr) {
249 *outPtr << "y(s)=" << y(s) << ", b0_norm=" << b0_norm << endl;
250 }
251 metric = this->getConvergenceMetric (STS::magnitude (y(s)),
252 b0_norm, input);
253 }
254 else {
255 metric = STM::zero ();
256 }
257
258 dense_vector_type y_s (Teuchos::View, y.values (), s);
259 // y_s := H(1:s,1:s)^{-1} y_s
260 blas.TRSM (Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI,
261 Teuchos::NO_TRANS, Teuchos::NON_UNIT_DIAG,
262 s, 1, one, H_sxs.values (), H_sxs.stride (),
263 y_s.values (), y_s.stride ());
264 Teuchos::Range1D cols (0, s-1);
265 Teuchos::RCP<const MV> Qj = Q.subView (cols);
266
267 if (input.precoSide == "right") {
268 MVT::MvTimesMatAddMv (one, *Qj, y_s, zero, R);
269 M.apply (R, MP);
270 X.update (one, MP, one);
271 }
272 else {
273 MVT::MvTimesMatAddMv (one, *Qj, y_s, one, X);
274 }
275
276 // Compute explicit unpreconditioned residual
277 P0 = * (Q.getVectorNonConst (0));
278 A.apply (X, P0);
279 P0.update (one, B, -one);
280 r_norm = P0.norm2 (); // residual norm
281
282 output.numIters += s;
283 output.numRests++;
284 output.absResid = r_norm;
285 output.relResid = r_norm / b0_norm;
286 metric = this->getConvergenceMetric (r_norm, b0_norm, input);
287 output.converged = (metric <= input.tol);
288
289 Tpetra::deep_copy (B, P0); // return residual norm as B
290
291 if (outPtr != nullptr) {
292 *outPtr << "At end of GMRES(s) cycle:" << endl;
293 Indent indentInner (outPtr);
294 *outPtr << output;
295 }
296 return output;
297 }
298 };
299
300 template<class SC, class MV, class OP,
301 template<class, class, class> class KrylovSubclassType>
302 class SolverManager;
303
304 // This is the Belos::SolverManager subclass that gets registered with
305 // Belos::SolverFactory.
306 template<class SC, class MV, class OP>
307 using GmresSSolverManager = SolverManager<SC, MV, OP, GmresS>;
308
309 /// \brief Register GmresSSolverManager for all enabled Tpetra
310 /// template parameter combinations.
311 void register_GmresS (const bool verbose);
312
313 } // namespace Impl
314 } // namespace BelosTpetra
315
316 #endif // BELOS_TPETRA_GMRES_S_HPP