Debian Codegraph @

packages/belos/tpetra/src/solvers/Belos_Tpetra_GmresSingleReduce.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_SINGLE_REDUCE_HPP
11 #define BELOS_TPETRA_GMRES_SINGLE_REDUCE_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 GmresSingleReduce : 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 real_type = typename STS::magnitudeType;
29 using STM = Teuchos::ScalarTraits<real_type>;
30 using complex_type = std::complex<real_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 GmresSingleReduce () :
37 base_type::Gmres (),
38 stepSize_ (1)
39 {
40 this->input_.computeRitzValues = false;
41 }
42
43 GmresSingleReduce (const Teuchos::RCP<const OP>& A) :
44 base_type::Gmres (A),
45 stepSize_ (1)
46 {
47 this->input_.computeRitzValues = false;
48 }
49
50 virtual ~GmresSingleReduce () = default;
51
52 virtual void
53 setParameters (Teuchos::ParameterList& params) {
54 Gmres<SC, MV, OP>::setParameters (params);
55
56 int stepSize = params.get<int> ("Step Size", stepSize_);
57 stepSize_ = stepSize;
58 }
59
60 void
61 setStepSize(int stepSize) {
62 stepSize_ = stepSize;
63 }
64
65 int
66 getStepSize() {
67 return stepSize_;
68 }
69
70 protected:
71 virtual void
72 setOrthogonalizer (const std::string& ortho)
73 {
74 if (ortho == "MGS" || ortho == "CGS" || ortho == "CGS2") {
75 this->input_.orthoType = ortho;
76 } else {
77 this->input_.orthoType = "MGS";
78 //base_type::setOrthogonalizer (ortho);
79 }
80 }
81
82 private:
83 //! Cleanup the orthogonalization on the "last" basis vector
84 int
85 projectAndNormalizeSingleReduce_cleanup (Teuchos::FancyOStream* outPtr,
86 int n,
87 const SolverInput<SC>& input,
88 MV& Q,
89 dense_matrix_type& H,
90 dense_matrix_type& WORK) const
91 {
92 const real_type eps = STS::eps ();
93 const real_type tolFactor = static_cast<real_type> (0.0);
94 const real_type tolOrtho = tolFactor * eps;
95 const SC zero = STS::zero ();
96 const SC one = STS::one ();
97
98 Teuchos::RCP< Teuchos::Time > dotsTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::LowSynch::dot-prod");
99
100 int rank = 1;
101 if (input.orthoType != "CGS") {
102 vec_type Qn = * (Q.getVectorNonConst (n));
103 if (input.delayedNorm) {
104 // compute norm
105 real_type newNorm (0.0);
106 {
107 Teuchos::TimeMonitor LocalTimer (*dotsTimer);
108 newNorm = Qn.norm2 ();
109 }
110
111 // scale
112 SC Tnn = zero;
113 real_type oldNorm (1.0);
114 if (newNorm > oldNorm * tolOrtho) {
115 Tnn = SC {newNorm};
116 MVT::MvScale (Qn, one / Tnn);
117 } else {
118 if (outPtr != nullptr) {
119 *outPtr << " + newNorm = " << Tnn << " -> H(" << n+1 << ", " << n << ") = zero"
120 << std::endl;
121 }
122 Tnn = zero;
123 rank = 0;
124 }
125 // update coefficients after reortho
126 if (n > 0) {
127 H (n, n-1) *= Tnn;
128 }
129 }
130 }
131 return rank;
132 }
133
134
135
136 //! Apply the orthogonalization using a single all-reduce
137 int
138 projectAndNormalizeSingleReduce (Teuchos::FancyOStream* outPtr,
139 int n,
140 const SolverInput<SC>& input,
141 MV& Q,
142 dense_matrix_type& H,
143 dense_matrix_type& WORK) const
144 {
145 Teuchos::RCP< Teuchos::Time > orthTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::LowSynch::Ortho");
146 Teuchos::TimeMonitor OrthTimer (*orthTimer);
147
148 int rank = 0;
149 if (input.orthoType == "CGS") {
150 // default, one-synch CGS1, optionally with reortho
151 rank = projectAndNormalizeSingleReduce_CGS1 (outPtr, n, input, Q, H, WORK);
152 } else {
153 // one-synch MGS or CGS2, optionally with delayed renorm
154 rank = projectAndNormalizeSingleReduce_GS (outPtr, n, input, Q, H, WORK);
155 }
156 return rank;
157 }
158
159
160 //! MGS/CGS2 specialization
161 int
162 projectAndNormalizeSingleReduce_GS (Teuchos::FancyOStream* outPtr,
163 int n,
164 const SolverInput<SC>& input,
165 MV& Q,
166 dense_matrix_type& H,
167 dense_matrix_type& T) const
168 {
169 const SC zero = STS::zero ();
170 const SC one = STS::one ();
171 const SC two = one + one;
172 const real_type eps = STS::eps ();
173 const real_type tolFactor = static_cast<real_type> (10.0);
174 const real_type tolOrtho = tolFactor * eps;
175
176 Teuchos::RCP< Teuchos::Time > dotsTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::LowSynch::dot-prod");
177 Teuchos::RCP< Teuchos::Time > projTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::LowSynch::project");
178
179 int rank = 1;
180 // ----------------------------------------------------------
181 // dot-product for single-reduce orthogonalization
182 // ----------------------------------------------------------
183 Teuchos::Range1D index_next (n, n+1);
184 MV Qnext = * (Q.subView (index_next));
185
186 // vectors to be orthogonalized against
187 Teuchos::Range1D index (0, n+1);
188 Teuchos::RCP< const MV > Qi = MVT::CloneView (Q, index);
189
190 // compute coefficient, T(:,n:n+1) = Q(:,0:n+1)'*Q(n:n+1)
191 Teuchos::RCP< dense_matrix_type > tj
192 = Teuchos::rcp (new dense_matrix_type (Teuchos::View, T, n+2, 2, 0, n));
193 {
194 Teuchos::TimeMonitor LocalTimer (*dotsTimer);
195 MVT::MvTransMv (one, *Qi, Qnext, *tj);
196 }
197
198 // ----------------------------------------------------------
199 // lagged/delayed re-orthogonalization
200 // ----------------------------------------------------------
201 Teuchos::Range1D index_new (n+1, n+1);
202 MV Qnew = * (Q.subView (index_new));
203 if (input.delayedNorm) {
204 // check
205 real_type prevNorm = STS::real (T(n, n));
206 TEUCHOS_TEST_FOR_EXCEPTION
207 (prevNorm < STM::zero (), std::runtime_error, "At iteration "
208 << n << ", T(" << n << ", " << n << ") = " << T(n, n) << " < 0.");
209
210 // lagged-normalize Q(:,n)
211 SC Tnn = zero;
212 real_type oldNorm (0.0);
213 if (prevNorm > oldNorm * tolOrtho) {
214 prevNorm = STM::squareroot (prevNorm);
215 Tnn = SC {prevNorm};
216
217 Teuchos::Range1D index_old (n, n);
218 MV Qold = * (Q.subView (index_old));
219 MVT::MvScale (Qold, one / Tnn);
220
221 // --------------------------------------
222 // update coefficients after reortho
223 for (int i = 0; i <= n; i++) {
224 T (i, n) /= Tnn;
225 }
226 T(n, n) /= Tnn;
227 T(n, n+1) /= Tnn;
228
229 // --------------------------------------
230 // lagged-normalize Q(:,n+1) := A*Q(:,n)
231 MVT::MvScale (Qnew, one / Tnn);
232
233 // update coefficients after reortho
234 for (int i = 0; i <= n+1; i++) {
235 T (i, n+1) /= Tnn;
236 }
237 T(n+1, n+1) /= Tnn;
238 } else {
239 if (outPtr != nullptr) {
240 *outPtr << " > prevNorm = " << prevNorm << " -> T(" << n << ", " << n << ") = zero"
241 << ", tol = " << tolOrtho << " x oldNorm = " << oldNorm
242 << std::endl;
243 }
244 }
245 // update coefficients after reortho
246 if (n > 0) {
247 H (n, n-1) *= Tnn;
248 }
249 }
250
251 // ----------------------------------------------------------
252 // comopute new coefficients (one-synch MGS/CGS2)
253 // ----------------------------------------------------------
254 // expand from triangular to full, conjugate (needed only for CGS2)
255 for (int j = 0; j < n; j++) {
256 T(n, j) = STS::conjugate(T(j, n));
257 }
258
259 // extract new coefficients
260 for (int i = 0; i <= n+1; i++) {
261 H (i, n) = T (i, n+1);
262 }
263
264 // update new coefficients
265 Teuchos::BLAS<LO ,SC> blas;
266 dense_matrix_type Hnew (Teuchos::View, H, n+1, 1, 0, n);
267 if (input.orthoType == "MGS") {
268 // H := (I+T)^(-T) H, where T is upper-triangular
269 blas.TRSM (Teuchos::LEFT_SIDE, Teuchos::LOWER_TRI,
270 Teuchos::NO_TRANS, Teuchos::UNIT_DIAG,
271 n+1, 1,
272 one, T.values(), T.stride(),
273 Hnew.values(), Hnew.stride());
274 } else {
275 // H := (2*I-L) H, where T is full symmetrix Q'*Q
276 dense_matrix_type Told (Teuchos::View, T, n+1, 1, 0, n+1);
277 blas.COPY (n+1, Hnew.values(), 1, Told.values(), 1);
278 blas.GEMV(Teuchos::NO_TRANS,
279 n+1, n+1,
280 -one, T.values(), T.stride(),
281 Told.values(), 1,
282 two, Hnew.values(), 1);
283 }
284
285 // ----------------------------------------------------------
286 // orthogonalize the new vectors against the previous columns
287 // ----------------------------------------------------------
288 Teuchos::Range1D index_prev (0, n);
289 Teuchos::RCP< const MV > Qprev = MVT::CloneView (Q, index_prev);
290 {
291 Teuchos::TimeMonitor LocalTimer (*projTimer);
292 MVT::MvTimesMatAddMv (-one, *Qprev, Hnew, one, Qnew);
293 }
294
295 // ----------------------------------------------------------
296 // normalize the new vector
297 // ----------------------------------------------------------
298 // fix the norm
299 real_type oldNorm = STS::real (H(n+1, n));
300 for (int i = 0; i <= n; ++i) {
301 H(n+1, n) -= (H(i, n)*H(i, n));
302 }
303
304 real_type newNorm = STS::real (H(n+1, n));
305 if (newNorm <= oldNorm * tolOrtho) {
306 if (input.delayedNorm) {
307 // something might have gone wrong, and let re-norm take care of
308 if (outPtr != nullptr) {
309 *outPtr << " > newNorm = " << newNorm << " -> H(" << n+1 << ", " << n << ") = one"
310 << ", tol = " << tolOrtho << " x oldNorm = " << oldNorm
311 << std::endl;
312 }
313 H(n+1, n) = STS::one ();
314 rank = 1;
315 } else {
316 if (outPtr != nullptr) {
317 *outPtr << " > newNorm = " << newNorm << " -> H(" << n+1 << ", " << n << ") = zero"
318 << ", tol = " << tolOrtho << " x oldNorm = " << oldNorm
319 << std::endl;
320 }
321 H(n+1, n) = zero;
322 rank = 0;
323 }
324 } else {
325 // check
326 TEUCHOS_TEST_FOR_EXCEPTION
327 (newNorm < STM::zero (), std::runtime_error, "At iteration "
328 << n << ", H(" << n+1 << ", " << n << ") = " << newNorm << " < 0.");
329
330 // compute norm
331 newNorm = STM::squareroot (newNorm);
332 H(n+1, n) = SC {newNorm};
333
334 // scale
335 MVT::MvScale (Qnew, one / H (n+1, n)); // normalize
336 rank = 1;
337 }
338 /*printf( " T = [\n" );
339 for (int i = 0; i <= n; i++) {
340 for (int j = 0; j <= n; j++) {
341 printf( "%e ", T (i, j) );
342 }
343 printf( "\n" );
344 }
345 printf( "];\n" );*/
346 /* printf( " H = [\n" );
347 for (int i = 0; i <= n+1; i++) {
348 for (int j = 0; j <= n; j++) {
349 printf( "%e ", H (i, j) );
350 }
351 printf( "\n" );
352 }
353 printf( "];\n" );*/
354
355 return rank;
356 }
357
358
359 //! CGS1 specialization
360 int
361 projectAndNormalizeSingleReduce_CGS1 (Teuchos::FancyOStream* outPtr,
362 int n,
363 const SolverInput<SC>& input,
364 MV& Q,
365 dense_matrix_type& H,
366 dense_matrix_type& WORK) const
367 {
368 Teuchos::BLAS<LO, SC> blas;
369 const SC one = STS::one ();
370 const real_type eps = STS::eps ();
371 const real_type tolFactor = static_cast<real_type> (10.0);
372 const real_type tolOrtho = tolFactor * eps;
373
374 int rank = 0;
375 Teuchos::Range1D index_all(0, n+1);
376 Teuchos::Range1D index_prev(0, n);
377 const MV Qall = * (Q.subView(index_all));
378 const MV Qprev = * (Q.subView(index_prev));
379
380 dense_matrix_type h_all (Teuchos::View, H, n+2, 1, 0, n);
381 dense_matrix_type h_prev (Teuchos::View, H, n+1, 1, 0, n);
382
383 // Q(:,0:j+1)'*Q(:,j+1)
384 vec_type AP = * (Q.getVectorNonConst (n+1));
385 MVT::MvTransMv(one, Qall, AP, h_all);
386
387 // orthogonalize (project)
388 MVT::MvTimesMatAddMv (-one, Qprev, h_prev, one, AP);
389
390 // save the norm before ortho
391 real_type oldNorm = STS::real (H(n+1, n));
392
393 // reorthogonalize if requested
394 if (input.delayedNorm) {
395 // Q(:,0:j+1)'*Q(:,j+1)
396 dense_matrix_type w_all (Teuchos::View, WORK, n+2, 1, 0, n);
397 dense_matrix_type w_prev (Teuchos::View, WORK, n+1, 1, 0, n);
398 MVT::MvTransMv(one, Qall, AP, w_all);
399 // orthogonalize (project)
400 MVT::MvTimesMatAddMv (-one, Qprev, w_prev, one, AP);
401 // recompute the norm
402 oldNorm = STS::real (w_all(n+1, 0));
403 for (int i = 0; i <= n; ++i) {
404 w_all(n+1, 0) -= (w_prev(i, 0)*w_prev(i, 0));
405 }
406
407 // accumulate results
408 blas.AXPY (n+1, one, w_prev.values (), 1, h_prev.values (), 1);
409 H(n+1, n) = w_all(n+1, 0);
410 } else {
411 for (int i = 0; i <= n; ++i) {
412 H(n+1, n) -= (H(i, n)*H(i, n));
413 }
414 }
415
416 // check for negative norm
417 TEUCHOS_TEST_FOR_EXCEPTION
418 (STS::real (H(n+1, n)) < STM::zero (), std::runtime_error, "At iteration "
419 << n << ", H(" << n+1 << ", " << n << ") = " << H(n+1, n) << " < 0.");
420 // Check for zero norm. OK to take real part of H(n+1, n), since
421 // this is a magnitude (cosine) anyway and therefore should always
422 // be real.
423 const real_type H_np1_n = STS::real (H(n+1, n));
424 if (H_np1_n > oldNorm * tolOrtho) {
425 const real_type H_np1_n_sqrt = STM::squareroot (H_np1_n);
426 H(n+1, n) = SC {H_np1_n_sqrt};
427 MVT::MvScale (AP, STS::one () / H(n+1, n)); // normalize
428 rank = 1;
429 }
430 else {
431 if (outPtr != nullptr) {
432 *outPtr << " > newNorm = " << H_np1_n << " -> H(" << n+1 << ", " << ") = zero"
433 << std::endl;
434 }
435 H(n+1, n) = STS::zero ();
436 rank = 0;
437 }
438 return rank;
439 }
440
441
442 SolverOutput<SC>
443 solveOneVec (Teuchos::FancyOStream* outPtr,
444 vec_type& X, // in X/out X
445 vec_type& B, // in B
446 const OP& A,
447 const OP& M,
448 const SolverInput<SC>& input)
449 {
450 using std::endl;
451 int restart = input.resCycle;
452 const SC zero = STS::zero ();
453 const SC one = STS::one ();
454 const bool computeRitzValues = input.computeRitzValues;
455
456
457 // timers
458 Teuchos::RCP< Teuchos::Time > spmvTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::matrix-apply ");
459 Teuchos::RCP< Teuchos::Time > precTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::precondition ");
460 Teuchos::RCP< Teuchos::Time > orthTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::orthogonalize");
461
462 Teuchos::RCP< Teuchos::Time > totalTimer = Teuchos::TimeMonitor::getNewCounter ("GmresSingleReduce::total ");
463 Teuchos::TimeMonitor GmresTimer (*totalTimer);
464
465 SolverOutput<SC> output {};
466 // initialize output parameters
467 output.numRests = 0;
468 output.numIters = 0;
469 output.converged = false;
470
471 real_type b_norm; // initial residual norm
472 real_type b0_norm; // initial residual norm, not left-preconditioned
473 real_type r_norm;
474 real_type r_norm_imp;
475
476 bool zeroOut = false;
477 MV Q (B.getMap (), restart+1, zeroOut);
478 vec_type R (B.getMap (), zeroOut);
479 vec_type Y (B.getMap (), zeroOut);
480 vec_type MP (B.getMap (), zeroOut);
481 vec_type P0 = * (Q.getVectorNonConst (0));
482
483 Teuchos::BLAS<LO ,SC> blas;
484 dense_matrix_type H (restart+1, restart, true);
485 dense_matrix_type T (restart+1, restart+2, true);
486 dense_vector_type y (restart+1, true);
487 dense_vector_type z (restart+1, true);
488 std::vector<real_type> cs (restart);
489 std::vector<SC> sn (restart);
490
491 #ifdef HAVE_TPETRA_DEBUG
492 dense_matrix_type H2 (restart+1, restart, true);
493 dense_matrix_type H3 (restart+1, restart, true);
494 #endif
495
496 // initial residual (making sure R = B - Ax)
497 {
498 Teuchos::TimeMonitor LocalTimer (*spmvTimer);
499 A.apply (X, R);
500 }
501 R.update (one, B, -one);
502 b0_norm = R.norm2 (); // initial residual norm, no-preconditioned
503 if (input.precoSide == "left") {
504 {
505 Teuchos::TimeMonitor LocalTimer (*precTimer);
506 M.apply (R, P0);
507 }
508 r_norm = P0.norm2 (); // initial residual norm, left-preconditioned
509 } else {
510 r_norm = b0_norm;
511 }
512 b_norm = r_norm;
513
514 real_type metric = this->getConvergenceMetric (b0_norm, b0_norm, input);
515 if (metric <= input.tol) {
516 if (outPtr != nullptr) {
517 *outPtr << "Initial guess' residual norm " << b0_norm
518 << " meets tolerance " << input.tol << endl;
519 }
520 output.absResid = b0_norm;
521 output.relResid = STM::one ();
522 output.converged = true;
523 return output;
524 } else if (outPtr != nullptr) {
525 *outPtr << "Initial guess' residual norm " << b0_norm << endl;
526 }
527
528 // compute Ritz values as Newton shifts
529 if (computeRitzValues) {
530 // Invoke ordinary GMRES for the first restart
531 SolverInput<SC> input_gmres = input;
532 input_gmres.maxNumIters = input.resCycle;
533 input_gmres.computeRitzValues = computeRitzValues;
534 if (input.orthoType == "MGS") {
535 // MGS1 or MGS2
536 input_gmres.orthoType = "IMGS";
537 if (input.needToReortho) {
538 input_gmres.maxOrthoSteps = 2;
539 } else {
540 input_gmres.maxOrthoSteps = 1;
541 }
542 } else if (input.orthoType == "CGS") {
543 // CGS1 or CGS2
544 input_gmres.orthoType = "ICGS";
545 if (input.needToReortho) {
546 input_gmres.maxOrthoSteps = 2;
547 } else {
548 input_gmres.maxOrthoSteps = 1;
549 }
550 } else if (input.orthoType == "CGS2") {
551 // CGS2
552 input_gmres.orthoType = "ICGS";
553 input_gmres.maxOrthoSteps = 2;
554 }
555 if (outPtr != nullptr) {
556 *outPtr << "Run standard GMRES for first restart cycle" << endl;
557 }
558
559 Tpetra::deep_copy (R, B);
560 output = Gmres<SC, MV, OP>::solveOneVec (outPtr, X, R, A, M,
561 input_gmres);
562 if (output.converged) {
563 return output; // standard GMRES converged
564 }
565
566 if (input.precoSide == "left") {
567 {
568 Teuchos::TimeMonitor LocalTimer (*precTimer);
569 M.apply (R, P0);
570 }
571 r_norm = P0.norm2 (); // residual norm
572 }
573 else {
574 r_norm = output.absResid;
575 }
576 output.numRests++;
577 }
578
579 // initialize starting vector
580 if (input.precoSide != "left") {
581 Tpetra::deep_copy (P0, R);
582 }
583 P0.scale (one / r_norm);
584 y[0] = SC {r_norm};
585 const int s = getStepSize ();
586 // main loop
587 bool delayed_ortho = ((input.orthoType == "MGS" && input.delayedNorm) ||
588 (input.orthoType == "CGS2"));
589 int iter = 0;
590 while (output.numIters < input.maxNumIters && ! output.converged) {
591 if (input.maxNumIters < output.numIters+restart) {
592 restart = input.maxNumIters-output.numIters;
593 }
594 // restart cycle
595 for (; iter < restart && (metric > input.tol && !STS::isnaninf (metric)); ++iter) {
596 // AP = A*P
597 vec_type P = * (Q.getVectorNonConst (iter));
598 vec_type AP = * (Q.getVectorNonConst (iter+1));
599 if (input.precoSide == "none") { // no preconditioner
600 Teuchos::TimeMonitor LocalTimer (*spmvTimer);
601 A.apply (P, AP);
602 }
603 else if (input.precoSide == "right") {
604 {
605 Teuchos::TimeMonitor LocalTimer (*precTimer);
606 M.apply (P, MP);
607 }
608 {
609 Teuchos::TimeMonitor LocalTimer (*spmvTimer);
610 A.apply (MP, AP);
611 }
612 }
613 else {
614 {
615 Teuchos::TimeMonitor LocalTimer (*spmvTimer);
616 A.apply (P, MP);
617 }
618 {
619 Teuchos::TimeMonitor LocalTimer (*precTimer);
620 M.apply (MP, AP);
621 }
622 }
623 // Shift for Newton basis
624 if (computeRitzValues) {
625 //AP.update (-output.ritzValues[iter], P, one);
626 const complex_type theta = output.ritzValues[iter % s];
627 UpdateNewton<SC, MV>::updateNewtonV (iter, Q, theta);
628 }
629 output.numIters++;
630
631 // Orthogonalization
632 {
633 Teuchos::TimeMonitor LocalTimer (*orthTimer);
634 projectAndNormalizeSingleReduce (outPtr, iter, input, Q, H, T);
635 }
636
637 // Convergence check
638 if (!delayed_ortho || iter > 0) {
639 int check = (delayed_ortho ? iter-1 : iter);
640 if (outPtr != nullptr) {
641 *outPtr << "> Current single-reduce iteration: iter=" << iter
642 << ", restart=" << restart
643 << ", metric=" << metric << endl;
644 Indent indent3 (outPtr);
645 }
646
647 // Shift back for Newton basis
648 if (computeRitzValues) {
649 const complex_type theta = output.ritzValues[check % s];
650 UpdateNewton<SC, MV>::updateNewtonH (check, H, theta);
651 }
652 #ifdef HAVE_TPETRA_DEBUG
653 this->checkNumerics (outPtr, iter, check, A, M, Q, X, B, y,
654 H, H2, H3, cs, sn, input);
655
656 if (outPtr != nullptr) {
657 dense_matrix_type T2 (check+1, check+1, true);
658 for (int j = 0; j < check+1; j++) {
659 blas.COPY (check+1, &(T(0, j)), 1, &(T2(0, j)), 1);
660 T2 (j, j) -= one;
661 }
662 real_type ortho_error = this->computeNorm(T2);
663 *outPtr << " > norm(T-I) = " << ortho_error << endl;
664 }
665 #endif
666
667 if (H(check+1, check) != zero) {
668 // Apply Givens rotations to new column of H and y
669 this->reduceHessenburgToTriangular (check, H, cs, sn, y.values ());
670 // Convergence check
671 metric = this->getConvergenceMetric (STS::magnitude (y[check+1]),
672 b_norm, input);
673 }
674 else {
675 if (outPtr != nullptr) {
676 *outPtr << " > warning: H(" << check+1 << ", " << check << ") = zero" << endl;
677 }
678 H(check+1, check) = zero;
679 metric = STM::zero ();
680 }
681 }
682 } // end of restart cycle
683
684 if (delayed_ortho) {
685 // Orthogonalization, cleanup
686 {
687 Teuchos::TimeMonitor LocalTimer (*orthTimer);
688 projectAndNormalizeSingleReduce_cleanup (outPtr, iter, input, Q, H, T);
689 }
690
691 int check = iter-1;
692 // Shift back for Newton basis
693 if (computeRitzValues) {
694 const complex_type theta = output.ritzValues[check % s];
695 UpdateNewton<SC, MV>::updateNewtonH (check, H, theta);
696 }
697 #ifdef HAVE_TPETRA_DEBUG
698 this->checkNumerics (outPtr, iter, check, A, M, Q, X, B, y,
699 H, H2, H3, cs, sn, input);
700 #endif
701
702 if (H(check+1, check) != zero) {
703 // Apply Givens rotations to new column of H and y
704 this->reduceHessenburgToTriangular (check, H, cs, sn, y.values ());
705 // Convergence check
706 metric = this->getConvergenceMetric (STS::magnitude (y[check+1]),
707 b_norm, input);
708 }
709 else {
710 H(check+1, check) = zero;
711 metric = STM::zero ();
712 }
713 }
714
715 if (iter < restart) {
716 // save the old solution, just in case explicit residual norm failed the convergence test
717 Tpetra::deep_copy (Y, X);
718 blas.COPY (1+iter, y.values(), 1, z.values(), 1);
719 }
720 r_norm_imp = STS::magnitude (y (iter)); // save implicit residual norm
721 if (iter > 0) {
722 // Update solution
723 blas.TRSM (Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
724 Teuchos::NON_UNIT_DIAG, iter, 1, one,
725 H.values(), H.stride(), y.values (), y.stride ());
726 Teuchos::Range1D cols(0, iter-1);
727 Teuchos::RCP<const MV> Qj = Q.subView(cols);
728 y.resize (iter);
729 if (input.precoSide == "right") {
730 MVT::MvTimesMatAddMv (one, *Qj, y, zero, R);
731 {
732 Teuchos::TimeMonitor LocalTimer (*precTimer);
733 M.apply (R, MP);
734 }
735 X.update (one, MP, one);
736 }
737 else {
738 MVT::MvTimesMatAddMv (one, *Qj, y, one, X);
739 }
740 y.resize (restart+1);
741 }
742
743 // Compute explicit residual vector in preparation for restart.
744 {
745 Teuchos::TimeMonitor LocalTimer (*spmvTimer);
746 A.apply (X, R);
747 }
748 R.update (one, B, -one);
749 r_norm = R.norm2 (); // residual norm
750 output.absResid = r_norm;
751 output.relResid = r_norm / b0_norm;
752 if (outPtr != nullptr) {
753 *outPtr << "Implicit and explicit residual norms at restart: " << r_norm_imp << ", " << r_norm << endl;
754 }
755
756 metric = this->getConvergenceMetric (r_norm, b0_norm, input);
757 if (metric <= input.tol) {
758 output.converged = true;
759 return output;
760 }
761 else if (output.numIters < input.maxNumIters) {
762 // Restart, only if max inner-iteration was reached.
763 // Otherwise continue the inner-iteration.
764 if (iter >= restart || H(iter,iter-1) == zero) { // done with restart cycyle, or probably lost ortho
765 // Initialize starting vector for restart
766 if (outPtr != nullptr && H(iter,iter-1) == zero) {
767 *outPtr << " > restarting with explicit residual vector"
768 << " since H(" << iter << ", " << iter-1 << ") = zero" << endl;
769 }
770 iter = 0;
771 P0 = * (Q.getVectorNonConst (0));
772 if (input.precoSide == "left") {
773 {
774 Teuchos::TimeMonitor LocalTimer (*precTimer);
775 M.apply (R, P0);
776 }
777 // FIXME (mfh 14 Aug 2018) Didn't we already compute this above?
778 r_norm = P0.norm2 ();
779 }
780 else {
781 // set the starting vector
782 Tpetra::deep_copy (P0, R);
783 }
784 P0.scale (one / r_norm);
785 y[0] = SC {r_norm};
786 for (int i=1; i < restart+1; i++) {
787 y[i] = zero;
788 }
789 output.numRests++; // restart
790 }
791 else {
792 // reset to the old solution
793 if (outPtr != nullptr) {
794 *outPtr << " > not-restart with iter=" << iter << endl;
795 }
796 Tpetra::deep_copy (X, Y);
797 blas.COPY (1+iter, z.values(), 1, y.values(), 1);
798 }
799 }
800 }
801
802 return output;
803 }
804
805 int stepSize_; // "step size" for Newton basis
806 };
807
808 template<class SC, class MV, class OP,
809 template<class, class, class> class KrylovSubclassType>
810 class SolverManager;
811
812 // This is the Belos::SolverManager subclass that gets registered with
813 // Belos::SolverFactory.
814 template<class SC, class MV, class OP>
815 using GmresSingleReduceSolverManager = SolverManager<SC, MV, OP, GmresSingleReduce>;
816
817 /// \brief Register GmresSingleReduceSolverManager for all enabled Tpetra
818 /// template parameter combinations.
819 void register_GmresSingleReduce (const bool verbose);
820
821 } // namespace Impl
822 } // namespace BelosTpetra
823
824 #endif // BELOS_TPETRA_GMRES_SINGLE_REDUCE_HPP