Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_PCGSolver.H
Go to the documentation of this file.
1#ifndef AMREX_PCG_SOLVER_H_
2#define AMREX_PCG_SOLVER_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_Algorithm.H>
6#include <AMReX_Array.H>
7#include <cmath>
8#include <type_traits>
9
10namespace amrex {
11
33template <int N, typename T, typename M, typename P>
36 M const& mat, P const& precond, int maxiter, T rel_tol)
37{
38 static_assert(std::is_floating_point_v<T>);
39
40 T rnorm0 = 0;
41 for (int i = 0; i < N; ++i) {
42 rnorm0 = std::max(rnorm0, std::abs(r[i]));
43 }
44 if (rnorm0 == 0) { return 0; }
45
46 int iter = 0;
47 T rho_prev = T(1.0); // initialized to quiet gcc warning
48 T p[N] = {}; // initialized to quiet gcc warning
49 for (iter = 1; iter <= maxiter; ++iter) {
50 T z[N];
51 precond(z, r);
52 T rho = 0;
53 for (int i = 0; i < N; ++i) { rho += r[i]*z[i]; }
54 if (rho == 0) { break; }
55 if (iter == 1) {
56 for (int i = 0; i < N; ++i) { p[i] = z[i]; }
57 } else {
58 auto rr = rho * (T(1.0)/rho_prev);
59 for (int i = 0; i < N; ++i) {
60 p[i] = z[i] + rr * p[i];
61 }
62 }
63 T q[N];
64 mat(q, p);
65 T pq = 0;
66 for (int i = 0; i < N; ++i) { pq += p[i]*q[i]; }
67 if (pq == 0) { break; }
68 T alpha = rho * (T(1.0)/pq);
69 T rnorm = 0;
70 for (int i = 0; i < N; ++i) {
71 x[i] += alpha * p[i];
72 r[i] -= alpha * q[i];
73 rnorm = std::max(rnorm, std::abs(r[i]));
74 }
75 if (rnorm <= rnorm0*rel_tol) { break; }
76 rho_prev = rho;
77 }
78 return iter;
79}
80
81}
82
83#endif
General-purpose algorithm utilities available on both host and device.
Fixed-size array types for use on GPU and CPU.
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_RESTRICT
Definition AMReX_Extension.H:37
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Definition AMReX_Amr.cpp:50
__host__ __device__ int pcg_solve(T *__restrict__ x, T *__restrict__ r, M const &mat, P const &precond, int maxiter, T rel_tol)
Fixed-size preconditioned conjugate-gradient solver.
Definition AMReX_PCGSolver.H:35