102 void solve (V& a_sol, V
const& a_rhs,
RT a_tol_rel,
RT a_tol_abs,
int a_its=-1);
117 [[nodiscard]]
int getStatus ()
const {
return m_status; }
124 void allocate_scratch ();
125 void cycle (V& a_xx,
int& a_status,
int& a_itcount,
RT& a_rnorm0);
126 void build_solution (V& a_xx,
int it);
127 void compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb);
129 bool converged (
RT r0,
RT r)
const;
131 void gram_schmidt_orthogonalization (
int it);
132 void update_hessenberg (
int it,
bool happyend,
RT& res);
135 int m_maxiter = 2000;
138 int m_restrtlen = 30;
139 RT m_res = std::numeric_limits<RT>::max();
149 std::unique_ptr<V> m_v_tmp_rhs;
150 std::unique_ptr<V> m_v_tmp_lhs;
152 M* m_linop =
nullptr;
155template <
typename V,
typename M>
161template <
typename V,
typename M>
164 int rs = m_restrtlen;
166 m_hh_1d.resize(std::size_t(rs + 2) * (rs + 1));
167 m_hh =
Table2D<RT>(m_hh_1d.data(), {0,0}, {rs+1,rs});
169 m_hes_1d.resize(std::size_t(rs + 2) * (rs + 1));
170 m_hes =
Table2D<RT>(m_hes_1d.data(), {0,0}, {rs+1,rs});
172 m_grs.resize(rs + 2);
177template <
typename V,
typename M>
180 if (m_restrtlen != rl) {
187template <
typename V,
typename M>
194template <
typename V,
typename M>
199 m_res = std::numeric_limits<RT>::max();
206template <
typename V,
typename M>
207bool GMRES<V,M>::converged (RT r0, RT r)
const
209 return (r < r0*m_rtol) || (r < m_atol);
212template <
typename V,
typename M>
221 if (m_v_tmp_rhs ==
nullptr) {
222 m_v_tmp_rhs = std::make_unique<V>(m_linop->makeVecRHS());
224 if (m_v_tmp_lhs ==
nullptr) {
225 m_v_tmp_lhs = std::make_unique<V>(m_linop->makeVecLHS());
228 m_vv.reserve(m_restrtlen+1);
229 for (
int i = 0; i < 2; ++i) {
230 m_vv.emplace_back(m_linop->makeVecRHS());
237 if (a_its < 0) { a_its = m_maxiter; }
241 m_linop->assign(m_vv[0], a_rhs);
242 m_linop->setToZero(a_sol);
246 cycle(a_sol, m_status, m_its, rnorm0);
248 while (m_status == -1 && m_its < a_its) {
249 compute_residual(m_vv[0], a_sol, a_rhs);
250 cycle(a_sol, m_status, m_its, rnorm0);
253 if (m_status == -1 && m_its >= a_its) { m_status = 1; }
261 amrex::Print() <<
"GMRES: Solve Time = " << t1-t0 <<
'\n';
265template <
typename V,
typename M>
270 m_res = m_linop->norm2(m_vv[0]);
273 if (m_res == RT(0.0)) {
278 m_linop->scale(m_vv[0], RT(1.0)/m_res);
280 if (a_itcount == 0) { a_rnorm0 = m_res; }
282 a_status = converged(a_rnorm0,m_res) ? 0 : -1;
285 while (it < m_restrtlen && a_itcount < m_maxiter)
289 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
293 if (a_status == 0) {
break; }
295 while (m_vv.size() < it+2) {
296 m_vv.emplace_back(m_linop->makeVecRHS());
299 auto const& vv_it = m_vv[it ];
300 auto & vv_it1 = m_vv[it+1];
302 m_linop->precond(*m_v_tmp_lhs, vv_it);
303 m_linop->apply(vv_it1, *m_v_tmp_lhs);
305 gram_schmidt_orthogonalization(it);
307 auto tt = m_linop->norm2(vv_it1);
309 auto const sml = RT((
sizeof(RT) == 8) ? 1.e-99 : 1.e-30);
310 bool happyend = (tt < sml);
312 m_linop->scale(vv_it1, RT(1.0)/tt);
318 update_hessenberg(it, happyend, m_res);
322 a_status = converged(a_rnorm0, m_res) ? 0 : -1;
323 if (happyend) {
break; }
326 if ((m_verbose > 1) && (a_status != 0 || a_itcount >= m_maxiter)) {
328 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
332 build_solution(a_xx, it-1);
335template <
typename V,
typename M>
336void GMRES<V,M>::gram_schmidt_orthogonalization (
int const it)
342 auto& vv_1 = m_vv[it+1];
344 Vector<RT> lhh(it+1);
346 for (
int j = 0; j <= it; ++j) {
347 m_hh (j,it) = RT(0.0);
348 m_hes(j,it) = RT(0.0);
351 for (
int ncnt = 0; ncnt < 2 ; ++ncnt)
353 for (
int j = 0; j <= it; ++j) {
354 lhh[j] = m_linop->dotProduct(vv_1, m_vv[j]);
357 for (
int j = 0; j <= it; ++j) {
358 m_linop->increment(vv_1, m_vv[j], -lhh[j]);
359 m_hh (j,it) += lhh[j];
360 m_hes(j,it) -= lhh[j];
365template <
typename V,
typename M>
366void GMRES<V,M>::update_hessenberg (
int it,
bool happyend, RT& res)
370 for (
int j = 1; j <= it; ++j) {
371 auto tt = m_hh(j-1,it);
372 m_hh(j-1,it) = m_cc[j-1] * tt + m_ss[j-1] * m_hh(j,it);
373 m_hh(j ,it) = m_cc[j-1] * m_hh(j,it) - m_ss[j-1] * tt;
378 auto tt = std::sqrt(m_hh(it,it)*m_hh(it,it) + m_hh(it+1,it)*m_hh(it+1,it));
379 m_cc[it] = m_hh(it ,it) / tt;
380 m_ss[it] = m_hh(it+1,it) / tt;
381 m_grs[it+1] = - (m_ss[it] * m_grs[it]);
382 m_grs[it ] = m_cc[it] * m_grs[it];
383 m_hh(it,it) = m_cc[it] * m_hh(it,it) + m_ss[it] * m_hh(it+1,it);
384 res = std::abs(m_grs[it+1]);
392template <
typename V,
typename M>
393void GMRES<V,M>::build_solution (V& a_xx,
int const it)
397 if (it < 0) {
return; }
399 if (m_hh(it,it) != RT(0.0)) {
400 m_grs[it] /= m_hh(it,it);
405 for (
int ii = 1; ii <= it; ++ii) {
408 for (
int j = k+1; j <= it; ++j) {
409 tt -= m_hh(k,j) * m_grs[j];
411 m_grs[k] = tt / m_hh(k,k);
414 m_linop->setToZero(*m_v_tmp_rhs);
415 for (
int ii = 0; ii < it+1; ++ii) {
416 m_linop->increment(*m_v_tmp_rhs, m_vv[ii], m_grs[ii]);
419 m_linop->precond(*m_v_tmp_lhs, *m_v_tmp_rhs);
420 m_linop->increment(a_xx, *m_v_tmp_lhs, RT(1.0));
423template <
typename V,
typename M>
424void GMRES<V,M>::compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb)
427 m_linop->assign(*m_v_tmp_lhs, a_xx);
428 m_linop->apply(*m_v_tmp_rhs, *m_v_tmp_lhs);
429 m_linop->linComb(a_rr, RT(1.0), a_bb, RT(-1.0), *m_v_tmp_rhs);