CG-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: CG-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Generic CG(conjugate gradient) operator functions.
6 > Created Time: 2017/08/13
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_CG_IMPL_H
10 #define CUBBYFLOW_CG_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 namespace CubbyFlow
15 {
16  template <typename BLASType>
17  void CG(
18  const typename BLASType::MatrixType& A,
19  const typename BLASType::VectorType& b,
20  unsigned int maxNumberOfIterations,
21  double tolerance,
22  typename BLASType::VectorType* x,
23  typename BLASType::VectorType* r,
24  typename BLASType::VectorType* d,
25  typename BLASType::VectorType* q,
26  typename BLASType::VectorType* s,
27  unsigned int* lastNumberOfIterations,
28  double* lastResidualNorm)
29  {
30  using PrecondType = NullCGPreconditioner<BLASType>;
31  PrecondType precond;
32 
33  PCG<BLASType, PrecondType>(
34  A,
35  b,
36  maxNumberOfIterations,
37  tolerance,
38  &precond,
39  x,
40  r,
41  d,
42  q,
43  s,
44  lastNumberOfIterations,
45  lastResidualNorm);
46  }
47 
48  template <typename BLASType, typename PrecondType>
49  void PCG(
50  const typename BLASType::MatrixType& A,
51  const typename BLASType::VectorType& b,
52  unsigned int maxNumberOfIterations,
53  double tolerance,
54  PrecondType* M,
55  typename BLASType::VectorType* x,
56  typename BLASType::VectorType* r,
57  typename BLASType::VectorType* d,
58  typename BLASType::VectorType* q,
59  typename BLASType::VectorType* s,
60  unsigned int* lastNumberOfIterations,
61  double* lastResidualNorm)
62  {
63  // Clear
64  BLASType::Set(0, r);
65  BLASType::Set(0, d);
66  BLASType::Set(0, q);
67  BLASType::Set(0, s);
68 
69  // r = b - Ax
70  BLASType::Residual(A, *x, b, r);
71 
72  // d = M^-1r
73  M->Solve(*r, d);
74 
75  // sigmaNew = r.d
76  double sigmaNew = BLASType::Dot(*r, *d);
77 
78  unsigned int iter = 0;
79  bool trigger = false;
80 
81  while (sigmaNew > Square(tolerance) && iter < maxNumberOfIterations)
82  {
83  // q = Ad
84  BLASType::MVM(A, *d, q);
85 
86  // alpha = sigmaNew / d.q
87  double alpha = sigmaNew / BLASType::Dot(*d, *q);
88 
89  // x = x + alpha * d
90  BLASType::AXPlusY(alpha, *d, *x, x);
91 
92  // if i is divisible by 50...
93  if (trigger || (iter % 50 == 0 && iter > 0))
94  {
95  // r = b - Ax
96  BLASType::Residual(A, *x, b, r);
97  trigger = false;
98  }
99  else
100  {
101  // r = r - alpha * q
102  BLASType::AXPlusY(-alpha, *q, *r, r);
103  }
104 
105  // s = M^-1r
106  M->Solve(*r, s);
107 
108  // sigmaOld = sigmaNew
109  double sigmaOld = sigmaNew;
110 
111  // sigmaNew = r.s
112  sigmaNew = BLASType::Dot(*r, *s);
113 
114  if (sigmaNew > sigmaOld)
115  {
116  trigger = true;
117  }
118 
119  // beta = sigmaNew / sigmaOld
120  double beta = sigmaNew / sigmaOld;
121 
122  // d = s + beta*d
123  BLASType::AXPlusY(beta, *d, *s, d);
124 
125  ++iter;
126  }
127 
128  *lastNumberOfIterations = iter;
129 
130  // std::fabs(sigmaNew) - Workaround for negative zero
131  *lastResidualNorm = std::sqrt(std::fabs(sigmaNew));
132  }
133 }
134 
135 #endif
T Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.h:111
Definition: pybind11Utils.h:24
No-op pre-conditioner for conjugate gradient.
Definition: CG.h:23
void CG(const typename BLASType::MatrixType &A, const typename BLASType::VectorType &b, unsigned int maxNumberOfIterations, double tolerance, typename BLASType::VectorType *x, typename BLASType::VectorType *r, typename BLASType::VectorType *d, typename BLASType::VectorType *q, typename BLASType::VectorType *s, unsigned int *lastNumberOfIterations, double *lastResidualNorm)
Solves conjugate gradient.
Definition: CG-Impl.h:17
void PCG(const typename BLASType::MatrixType &A, const typename BLASType::VectorType &b, unsigned int maxNumberOfIterations, double tolerance, PrecondType *M, typename BLASType::VectorType *x, typename BLASType::VectorType *r, typename BLASType::VectorType *d, typename BLASType::VectorType *q, typename BLASType::VectorType *s, unsigned int *lastNumberOfIterations, double *lastResidualNorm)
Solves pre-conditioned conjugate gradient.
Definition: CG-Impl.h:49