Allium  0.1.0
gmres.impl.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_GMRES_IMPL_HPP
16 #define ALLIUM_LA_GMRES_IMPL_HPP
17 
18 #include "gmres.hpp"
19 
20 #include "local_vector.hpp"
21 
22 #include <vector>
23 
24 namespace allium {
25 
33  template <typename N>
34  void real_givens(N& c, N& s, N a, N b)
35  {
36  if (b == 0.0) {
37  c = 1;
38  s = 0;
39  } else {
40  if (std::abs(b) > std::abs(a)) {
41  N tau = -a/b;
42  s = 1.0 / sqrt(1.0 + tau*tau);
43  c = s * tau;
44  } else {
45  N tau = -b/a;
46  c = 1.0 / sqrt(1.0 + tau*tau);
47  s = c * tau;
48  }
49  }
50  }
51 
52  template <typename N> void givens(N& c, N& s, N a, N b)
53  {
54  using Real = real_part_t<N>;
55 
56  Real c_alpha, s_alpha;
57  real_givens(c_alpha, s_alpha, std::real(a), std::imag(a));
58  auto r_a = c_alpha * std::real(a) - s_alpha * std::imag(a);
59 
60  Real c_beta, s_beta;
61  real_givens(c_beta, s_beta, std::real(b), std::imag(b));
62  auto r_b = c_beta * std::real(b) - s_beta * std::imag(b);
63 
64  Real c_theta, s_theta;
65  real_givens(c_theta, s_theta, r_a, r_b);
66 
67  c = c_theta;
68  s = s_theta * N(c_alpha*c_beta+s_alpha*s_beta,
69  c_alpha*s_beta-c_beta*s_alpha);
70  }
71 
72  template <> inline void givens(float& c, float& s, float a, float b)
73  { return real_givens<float>(c, s, a, b); }
74  template <> inline void givens(double& c, double& s, double a, double b)
75  { return real_givens<double>(c, s, a, b); }
76 
77  template <typename N>
78  N conj(N z) {
79  return std::conj(z);
80  }
81  template <>
82  double inline conj(double z) { return z; }
83  template <>
84  float inline conj(float z) { return z; }
85 
90  template <typename N, typename V>
91  void gmres_rotate(size_t i1, size_t i2, N c, N s, V& u)
92  {
93  using Number = N;
94 
95  Number new_entry1 = c * u[i1] - s * u[i2];
96  Number new_entry2 = conj<N>(s) * u[i1] + c * u[i2];
97 
98  u[i1] = new_entry1;
99  u[i2] = new_entry2;
100  }
101 
103 
105  template <typename N>
106  class HessenbergQr {
107  public:
108  using Number = N;
109  using Real = real_part_t<Number>;
110  using Vector = LocalVector<Number>;
111 
112  HessenbergQr(Number first_rhs_entry);
113 
114  void add_column(LocalVector<N> next_column, Number next_rhs_entry);
115 
117  Real residual_norm() const;
118 
120  Vector solution() const;
121  private:
122  std::vector<LocalVector<N>> m_r_columns; // columns of the matrix R
123  std::vector<Number> m_cos_list;
124  std::vector<Number> m_sin_list;
125  std::vector<Number> m_rhs;
126  };
128 
129  template <typename N>
130  HessenbergQr<N>::HessenbergQr(Number first_rhs_entry)
131  : m_rhs({first_rhs_entry})
132  {}
133 
134  template <typename N>
135  void HessenbergQr<N>::add_column(LocalVector<N> next_column, Number next_rhs_entry)
136  {
137  size_t n_columns = m_r_columns.size();
138 
139  assert(next_column.rows() == n_columns + 2);
140 
141  // First, apply previous rotations to the new column
142  for (size_t i_rot = 0; i_rot < n_columns; ++i_rot) {
143  gmres_rotate<Number>(i_rot, i_rot+1, m_cos_list[i_rot], m_sin_list[i_rot], next_column);
144  }
145 
146  // Second, compute the new rotation coefficients
147 
148  // Currently, we assume that the last entry in the next_column is a real
149  // number, which is true for the GMRES method.
150  //assert(std::imag(next_column[n_columns+1]) == 0.0);
151 
152  Number r = next_column[n_columns];
153  Number h = next_column[n_columns+1];
154 
155  Number new_c, new_s;
156  givens(new_c, new_s, r, h);
157 
158  m_cos_list.push_back(new_c);
159  m_sin_list.push_back(new_s);
160 
161  // Third, apply new rotation
162  gmres_rotate<Number>(n_columns, n_columns+1, new_c, new_s, next_column);
163 
164  // Fourth, apply the new rotation to rhs
165  m_rhs.push_back(next_rhs_entry);
166  gmres_rotate<Number>(n_columns, n_columns+1, new_c, new_s, m_rhs);
167 
168  // Store new column of R
169  m_r_columns.push_back(std::move(next_column));
170  }
171 
172  template <typename N>
173  typename HessenbergQr<N>::Vector HessenbergQr<N>::solution() const
174  {
175  size_t n_columns = m_r_columns.size();
176 
177  Vector result(n_columns);
178 
179  // upper triangular backward solve
180  for (int i_row = n_columns-1; i_row >= 0; i_row--)
181  {
182  Number acc = m_rhs[i_row];
183  for (size_t i_col = i_row+1; i_col < n_columns; ++i_col) {
184  acc -= m_r_columns[i_col][i_row] * result[i_col];
185  }
186  result[i_row] = acc / m_r_columns[i_row][i_row];
187  }
188 
189  return result;
190  }
191 
193  template <typename N>
194  typename HessenbergQr<N>::Real HessenbergQr<N>::residual_norm() const
195  {
196  assert(m_rhs.size() == m_r_columns.size()+1);
197  size_t n_columns = m_r_columns.size();
198  return abs(m_rhs[n_columns]);
199  }
200 
201  template <typename N>
202  void GmresSolverBase<N>::solve(VectorStorage<N>& x,
203  const VectorStorage<N>& rhs,
204  InitialGuess initial_guess)
205  {
206  auto tmp1 = allocate_like(rhs);
207 
208  m_max_krylov_size = 30;
209 
210  if (initial_guess == InitialGuess::NOT_PROVIDED) {
211  set_zero(x);
212  }
213 
214  // residual = rhs - mat * x
215  auto residual = allocate_like(rhs);
216  residual->assign(rhs);
217  this->apply_matrix(*tmp1, x);
218  residual->add_scaled(-1.0, *tmp1);
219  auto residual_norm = residual->l2_norm();
220 
221  Real abs_tol = tolerance() * rhs.l2_norm();
222 
223  m_iteration_count = 0;
224  while (true) {
225  if (residual_norm <= abs_tol)
226  break;
227 
228  bool success = inner_solve(x, *residual, residual_norm, abs_tol);
229  if (success)
230  break;
231 
232  // residual = rhs - mat * x
233  residual->assign(rhs);
234  this->apply_matrix(*tmp1, x);
235  residual->add_scaled(-1.0, *tmp1);
236  residual_norm = residual->l2_norm();
237  }
238  }
239 
240  template <typename N>
241  bool GmresSolverBase<N>::inner_solve(VectorStorage<N>& x,
242  const VectorStorage<N>& residual,
243  real_part_t<N> residual_norm,
244  real_part_t<N> abs_tol)
245  {
246  using Number = N;
247  using Real = real_part_t<Number>;
248 
249  bool success = false;
250 
251  std::vector<std::unique_ptr<VectorStorage<N>>> krylov_base;
252 
253  Real beta = residual_norm;
254  // v0 = residual / beta
255  auto v0 = allocate_like(residual);
256  v0->assign(residual);
257  *v0 *= (1.0 / beta);
258  krylov_base.push_back(std::move(v0));
259 
260  HessenbergQr<N> qr(beta);
261 
262  for (size_t i_iteration = 0;
263  i_iteration < m_max_krylov_size && !success;
264  ++i_iteration)
265  {
266  m_iteration_count++;
267 
268  auto v_hat = allocate_like(residual);
269  this->apply_matrix(*v_hat, *krylov_base.at(i_iteration));
270 
271  // current column of the Hessenberg matrix
272  LocalVector<N> hessenberg_column(i_iteration + 2);
273 
274  // orthogonalize and store coefficients using modified Gram-Schmidt
275  // @todo: The last orthogonalization does not need to be computed.
276  // Hence, we could save some work here.
277  for (size_t i_base = 0; i_base <= i_iteration; ++i_base) {
278  hessenberg_column[i_base] = v_hat->dot(*krylov_base.at(i_base));
279  v_hat->add_scaled(-hessenberg_column[i_base], *krylov_base.at(i_base));
280  }
281 
282  // entry (i_iteration+2, i_iteration+1) in the Hessenberg matrix
283  Real hessenberg_extra = v_hat->l2_norm();
284  hessenberg_column[i_iteration+1] = hessenberg_extra;
285 
286  // normalize new basis vector
287  //
288  // @todo: normalization can be avoided if the scaling coefficient is
289  // stored instead
290  *v_hat *= (1.0 / hessenberg_extra);
291  krylov_base.push_back(std::move(v_hat));
292 
293  // add new column to Hessenberg-QR decomposition and new RHS entry 0
294  qr.add_column(std::move(hessenberg_column), 0.0);
295 
296  if (qr.residual_norm() <= abs_tol)
297  success = true;
298  }
299 
300  LocalVector<N> y = qr.solution();
301  // compute linear combination of basis vectors to approximate the
302  // solution of the LGS
303  assert(y.rows() == krylov_base.size() - 1);
304  for (size_t i_base = 0; i_base < y.rows(); ++i_base) {
305  x.add_scaled(y[i_base], *krylov_base[i_base]);
306  }
307 
308  return success;
309  }
310 
311 }
312 
313 #endif
InitialGuess
Behavior for the initial guess.
Definition: linear_solver.hpp:32