Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_Tpetra_CG.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef STOKHOS_TPETRA_CG_HPP
11 #define STOKHOS_TPETRA_CG_HPP
12 
13 namespace Stokhos {
14 
15 // A simple CG solver for Tpetra-like objects
16 template <typename Matrix,
17  typename Vector,
18  typename Ordinal>
19 bool CG_Solve(const Matrix& A, Vector& x, const Vector& b,
20  typename Vector::mag_type tol, Ordinal max_its,
21  std::ostream* out = 0)
22 {
23  typedef typename Vector::mag_type mag_type;
24  typedef typename Vector::dot_type dot_type;
25 
26  Vector r(b.getMap());
27  Vector p(x.getMap());
28  Vector w(x.getMap());
29  A.apply(x, r);
30  r.update(1.0, b, -1.0);
31 
32  dot_type rho = r.dot(r);
33  dot_type rho_old = rho;
34  mag_type nrm = std::sqrt(rho);
35  Ordinal k=0;
36  dot_type alpha, beta, pAp;
37  while (k < max_its && nrm > tol) {
38  if (k == 0) {
39  p.update(1.0, r, 0.0);
40  }
41  else {
42  beta = rho / rho_old;
43  p.update(1.0, r, beta);
44  }
45  A.apply(p, w);
46  pAp = p.dot(w);
47  alpha = rho/pAp;
48  x.update( alpha, p, 1.0);
49  r.update(-alpha, w, 1.0);
50 
51  rho_old = rho;
52  rho = r.dot(r);
53  nrm = std::sqrt(rho);
54  ++k;
55 
56  if (out)
57  *out << k << ": " << nrm << std::endl;
58  }
59 
60  if (nrm <= tol)
61  return true;
62  return false;
63 }
64 
65 // A simple preconditioned CG solver based for Tpetra-like objects
66 template <typename Matrix,
67  typename Vector,
68  typename Prec,
69  typename Ordinal>
70 bool PCG_Solve(const Matrix& A, Vector& x, const Vector& b, const Prec& M,
71  typename Vector::mag_type tol, Ordinal max_its,
72  std::ostream* out = 0)
73 {
74  typedef typename Vector::mag_type mag_type;
75  typedef typename Vector::dot_type dot_type;
76 
77  Vector r(b.getMap());
78  Vector p(x.getMap());
79  Vector w(x.getMap());
80  A.apply(x, r);
81  r.update(1.0, b, -1.0);
82 
83  mag_type nrm = r.norm2();
84  dot_type rho = 1.0;
85  Ordinal k=0;
86  dot_type rho_old, alpha, beta, pAp;
87  while (k < max_its && nrm > tol) {
88  M.apply(r, w);
89  rho_old = rho;
90  rho = r.dot(w);
91 
92  if (k == 0) {
93  p.update(1.0, w, 0.0);
94  }
95  else {
96  beta = rho / rho_old;
97  p.update(1.0, w, beta);
98  }
99  A.apply(p, w);
100  pAp = p.dot(w);
101  alpha = rho/pAp;
102  x.update( alpha, p, 1.0);
103  r.update(-alpha, w, 1.0);
104 
105  nrm = r.norm2();
106  ++k;
107 
108  if (out)
109  *out << k << ": " << nrm << std::endl;
110  }
111 
112  if (nrm <= tol)
113  return true;
114  return false;
115 }
116 
117 }
118 
119 #endif // STOKHOS_TPETRA_CG
KOKKOS_INLINE_FUNCTION PCE< Storage > sqrt(const PCE< Storage > &a)
bool CG_Solve(const Matrix &A, Vector &x, const Vector &b, typename Vector::mag_type tol, Ordinal max_its, std::ostream *out=0)
bool PCG_Solve(const Matrix &A, Vector &x, const Vector &b, const Prec &M, typename Vector::mag_type tol, Ordinal max_its, std::ostream *out=0)