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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_TPETRA_CG_HPP
43 #define STOKHOS_TPETRA_CG_HPP
44 
45 namespace Stokhos {
46 
47 // A simple CG solver for Tpetra-like objects
48 template <typename Matrix,
49  typename Vector,
50  typename Ordinal>
51 bool CG_Solve(const Matrix& A, Vector& x, const Vector& b,
52  typename Vector::mag_type tol, Ordinal max_its,
53  std::ostream* out = 0)
54 {
55  typedef typename Vector::mag_type mag_type;
56  typedef typename Vector::dot_type dot_type;
57 
58  Vector r(b.getMap());
59  Vector p(x.getMap());
60  Vector w(x.getMap());
61  A.apply(x, r);
62  r.update(1.0, b, -1.0);
63 
64  dot_type rho = r.dot(r);
65  dot_type rho_old = rho;
66  mag_type nrm = std::sqrt(rho);
67  Ordinal k=0;
68  dot_type alpha, beta, pAp;
69  while (k < max_its && nrm > tol) {
70  if (k == 0) {
71  p.update(1.0, r, 0.0);
72  }
73  else {
74  beta = rho / rho_old;
75  p.update(1.0, r, beta);
76  }
77  A.apply(p, w);
78  pAp = p.dot(w);
79  alpha = rho/pAp;
80  x.update( alpha, p, 1.0);
81  r.update(-alpha, w, 1.0);
82 
83  rho_old = rho;
84  rho = r.dot(r);
85  nrm = std::sqrt(rho);
86  ++k;
87 
88  if (out)
89  *out << k << ": " << nrm << std::endl;
90  }
91 
92  if (nrm <= tol)
93  return true;
94  return false;
95 }
96 
97 // A simple preconditioned CG solver based for Tpetra-like objects
98 template <typename Matrix,
99  typename Vector,
100  typename Prec,
101  typename Ordinal>
102 bool PCG_Solve(const Matrix& A, Vector& x, const Vector& b, const Prec& M,
103  typename Vector::mag_type tol, Ordinal max_its,
104  std::ostream* out = 0)
105 {
106  typedef typename Vector::mag_type mag_type;
107  typedef typename Vector::dot_type dot_type;
108 
109  Vector r(b.getMap());
110  Vector p(x.getMap());
111  Vector w(x.getMap());
112  A.apply(x, r);
113  r.update(1.0, b, -1.0);
114 
115  mag_type nrm = r.norm2();
116  dot_type rho = 1.0;
117  Ordinal k=0;
118  dot_type rho_old, alpha, beta, pAp;
119  while (k < max_its && nrm > tol) {
120  M.apply(r, w);
121  rho_old = rho;
122  rho = r.dot(w);
123 
124  if (k == 0) {
125  p.update(1.0, w, 0.0);
126  }
127  else {
128  beta = rho / rho_old;
129  p.update(1.0, w, beta);
130  }
131  A.apply(p, w);
132  pAp = p.dot(w);
133  alpha = rho/pAp;
134  x.update( alpha, p, 1.0);
135  r.update(-alpha, w, 1.0);
136 
137  nrm = r.norm2();
138  ++k;
139 
140  if (out)
141  *out << k << ": " << nrm << std::endl;
142  }
143 
144  if (nrm <= tol)
145  return true;
146  return false;
147 }
148 
149 }
150 
151 #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)