118 void solve (V& a_sol, V
const& a_rhs,
RT a_tol_rel,
RT a_tol_abs,
int a_its=-1);
145 [[nodiscard]]
int getStatus ()
const {
return m_status; }
155 void allocate_scratch ();
156 void cycle (V& a_xx,
int& a_status,
int& a_itcount,
RT& a_rnorm0,
int a_maxiter);
157 void build_solution (V& a_xx,
int it);
158 void compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb);
160 [[nodiscard]]
bool converged (
RT r0,
RT r)
const;
162 void gram_schmidt_orthogonalization (
int it);
163 void update_hessenberg (
int it,
bool happyend,
RT& res);
166 int m_maxiter = 2000;
169 int m_restrtlen = 30;
170 bool m_nonzero_guess =
false;
171 RT m_res = std::numeric_limits<RT>::max();
172 RT m_rnorm0 = std::numeric_limits<RT>::max();
182 std::unique_ptr<V> m_v_tmp_rhs;
183 std::unique_ptr<V> m_v_tmp_lhs;
185 M* m_linop =
nullptr;
188template <
typename V,
typename M>
194template <
typename V,
typename M>
197 int rs = m_restrtlen;
199 m_hh_1d.resize(std::size_t(rs + 2) * (rs + 1));
200 m_hh =
Table2D<RT>(m_hh_1d.data(), {0,0}, {rs+1,rs});
202 m_hes_1d.resize(std::size_t(rs + 2) * (rs + 1));
203 m_hes =
Table2D<RT>(m_hes_1d.data(), {0,0}, {rs+1,rs});
205 m_grs.resize(rs + 2);
210template <
typename V,
typename M>
213 if (m_restrtlen != rl) {
220template <
typename V,
typename M>
227template <
typename V,
typename M>
232 m_res = std::numeric_limits<RT>::max();
239template <
typename V,
typename M>
240bool GMRES<V,M>::converged (RT r0, RT r)
const
242 return (r < r0*m_rtol) || (r < m_atol);
245template <
typename V,
typename M>
254 if (m_v_tmp_rhs ==
nullptr) {
255 m_v_tmp_rhs = std::make_unique<V>(m_linop->makeVecRHS());
257 if (m_v_tmp_lhs ==
nullptr) {
258 m_v_tmp_lhs = std::make_unique<V>(m_linop->makeVecLHS());
261 m_vv.reserve(m_restrtlen+1);
262 for (
int i = 0; i < 2; ++i) {
263 m_vv.emplace_back(m_linop->makeVecRHS());
270 if (a_its < 0) { a_its = m_maxiter; }
274 if (m_nonzero_guess) {
275 compute_residual(m_vv[0], a_sol, a_rhs);
277 m_linop->assign(m_vv[0], a_rhs);
278 m_linop->setToZero(a_sol);
283 cycle(a_sol, m_status, m_its, rnorm0, a_its);
285 while (m_status == -1 && m_its < a_its) {
286 compute_residual(m_vv[0], a_sol, a_rhs);
287 cycle(a_sol, m_status, m_its, rnorm0, a_its);
290 if (m_status == -1 && m_its >= a_its) { m_status = 1; }
299 amrex::Print() <<
"GMRES: Solve Time = " << t1-t0 <<
'\n';
303template <
typename V,
typename M>
304void GMRES<V,M>::cycle (V& a_xx,
int& a_status,
int& a_itcount, RT& a_rnorm0,
int a_maxiter)
308 m_res = m_linop->norm2(m_vv[0]);
311 if (m_res == RT(0.0)) {
316 m_linop->scale(m_vv[0], RT(1.0)/m_res);
318 if (a_itcount == 0) { a_rnorm0 = m_res; }
320 a_status = converged(a_rnorm0,m_res) ? 0 : -1;
323 while (it < m_restrtlen && a_itcount < a_maxiter)
327 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
331 if (a_status == 0) {
break; }
333 while (m_vv.size() < it+2) {
334 m_vv.emplace_back(m_linop->makeVecRHS());
337 auto const& vv_it = m_vv[it ];
338 auto & vv_it1 = m_vv[it+1];
340 m_linop->precond(*m_v_tmp_lhs, vv_it);
341 m_linop->apply(vv_it1, *m_v_tmp_lhs);
343 gram_schmidt_orthogonalization(it);
345 auto tt = m_linop->norm2(vv_it1);
347 auto const sml = RT((
sizeof(RT) == 8) ? 1.e-99 : 1.e-30);
348 bool happyend = (tt < sml);
350 m_linop->scale(vv_it1, RT(1.0)/tt);
356 update_hessenberg(it, happyend, m_res);
360 a_status = converged(a_rnorm0, m_res) ? 0 : -1;
361 if (happyend) {
break; }
364 if ((m_verbose > 1) && (a_status != 0 || a_itcount >= a_maxiter)) {
366 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
370 build_solution(a_xx, it-1);
373template <
typename V,
typename M>
374void GMRES<V,M>::gram_schmidt_orthogonalization (
int const it)
380 auto& vv_1 = m_vv[it+1];
382 Vector<RT> lhh(it+1);
384 for (
int j = 0; j <= it; ++j) {
385 m_hh (j,it) = RT(0.0);
386 m_hes(j,it) = RT(0.0);
389 for (
int ncnt = 0; ncnt < 2 ; ++ncnt)
391 for (
int j = 0; j <= it; ++j) {
392 lhh[j] = m_linop->dotProduct(vv_1, m_vv[j]);
395 for (
int j = 0; j <= it; ++j) {
396 m_linop->increment(vv_1, m_vv[j], -lhh[j]);
397 m_hh (j,it) += lhh[j];
398 m_hes(j,it) -= lhh[j];
403template <
typename V,
typename M>
404void GMRES<V,M>::update_hessenberg (
int it,
bool happyend, RT& res)
408 for (
int j = 1; j <= it; ++j) {
409 auto tt = m_hh(j-1,it);
410 m_hh(j-1,it) = m_cc[j-1] * tt + m_ss[j-1] * m_hh(j,it);
411 m_hh(j ,it) = m_cc[j-1] * m_hh(j,it) - m_ss[j-1] * tt;
416 auto tt = std::sqrt(m_hh(it,it)*m_hh(it,it) + m_hh(it+1,it)*m_hh(it+1,it));
417 m_cc[it] = m_hh(it ,it) / tt;
418 m_ss[it] = m_hh(it+1,it) / tt;
419 m_grs[it+1] = - (m_ss[it] * m_grs[it]);
420 m_grs[it ] = m_cc[it] * m_grs[it];
421 m_hh(it,it) = m_cc[it] * m_hh(it,it) + m_ss[it] * m_hh(it+1,it);
422 res = std::abs(m_grs[it+1]);
430template <
typename V,
typename M>
431void GMRES<V,M>::build_solution (V& a_xx,
int const it)
435 if (it < 0) {
return; }
437 if (m_hh(it,it) != RT(0.0)) {
438 m_grs[it] /= m_hh(it,it);
443 for (
int ii = 1; ii <= it; ++ii) {
446 for (
int j = k+1; j <= it; ++j) {
447 tt -= m_hh(k,j) * m_grs[j];
449 m_grs[k] = tt / m_hh(k,k);
452 m_linop->setToZero(*m_v_tmp_rhs);
453 for (
int ii = 0; ii < it+1; ++ii) {
454 m_linop->increment(*m_v_tmp_rhs, m_vv[ii], m_grs[ii]);
457 m_linop->precond(*m_v_tmp_lhs, *m_v_tmp_rhs);
458 m_linop->increment(a_xx, *m_v_tmp_lhs, RT(1.0));
461template <
typename V,
typename M>
462void GMRES<V,M>::compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb)
465 m_linop->assign(*m_v_tmp_lhs, a_xx);
466 m_linop->apply(*m_v_tmp_rhs, *m_v_tmp_lhs);
467 m_linop->linComb(a_rr, RT(1.0), a_bb, RT(-1.0), *m_v_tmp_rhs);