23 template<
class T,
class Z>
25 auto operator()( T w, Z z) {
30 template<
class Z0,
class T,
class Z1>
32 auto operator()( Z0 z0, T w, Z1 z1) {
33 return w*(z0.real()*z1.real() + z0.imag()*z1.imag());
103template<
class ContainerType, ComplexMode complex_mode = dg::complex_hermitian>
119 m_r(
copyable), m_p(m_r), m_ap(m_r), m_max_iter(max_iterations){}
122 void set_max(
unsigned new_max) {m_max_iter = new_max;}
137 m_throw_on_fail = throw_on_fail;
141 template<
class ...Params>
145 *
this =
PCG( std::forward<Params>( ps)...);
175 template<
class MatrixType0,
class ContainerType0,
class ContainerType1,
class MatrixType1,
class ContainerType2 >
176 unsigned solve( MatrixType0&& A, ContainerType0& x,
const ContainerType1& b, MatrixType1&& P,
const ContainerType2& W,
double eps = 1e-12,
double nrmb_correction = 1,
int test_frequency = 1);
179 template<
class ContainerType1,
class ContainerType2>
180 auto nrm(
const ContainerType1& w,
const ContainerType2& x)
184 if constexpr (is_complex)
189 template<
class ContainerType0,
class ContainerType1,
class ContainerType2>
190 auto dot(
const ContainerType0& x0,
const ContainerType1& w,
const ContainerType2& x1)
199 ContainerType m_r, m_p, m_ap;
201 bool m_verbose =
false, m_throw_on_fail =
true, m_cocg =
false;
208template<
class ContainerType>
212template<
class ContainerType, ComplexMode mode>
213template<
class Matrix,
class ContainerType0,
class ContainerType1,
class Preconditioner,
class ContainerType2>
214unsigned PCG< ContainerType, mode>::solve(
Matrix&& A, ContainerType0& x,
const ContainerType1& b, Preconditioner&& P,
const ContainerType2& W,
double eps,
double nrmb_correction,
int save_on_dots )
218 auto nrmb = nrm( W, b), nrmr(nrmb);
219 double tol = eps*(nrmb + nrmb_correction);
222 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
226 DG_RANK0 std::cout <<
"# Norm of W b "<<nrmb <<
"\n";
227 DG_RANK0 std::cout <<
"# Residual errors: \n";
240 blas2::symv( std::forward<Preconditioner>(P), m_r, m_p );
241 auto dotzr_old = dot( m_p, W, m_r),
alpha(dotzr_old), dotzr_new(dotzr_old);
242 for(
unsigned i=1; i<m_max_iter; i++)
245 alpha = dotzr_old/dot( m_p, W, m_ap);
248 if( 0 == i%save_on_dots )
253 DG_RANK0 std::cout <<
"# Absolute r*W*r "<<nrmr <<
"\t ";
254 DG_RANK0 std::cout <<
"# < Critical "<<tol <<
"\t ";
255 DG_RANK0 std::cout <<
"# (Relative "<<nrmr/nrmb <<
")\n";
260 blas2::symv(std::forward<Preconditioner>(P),m_r,m_ap);
261 dotzr_new = dot( m_ap, W, m_r);
268 <<
"After "<<m_max_iter<<
" PCG iterations with rtol "<<eps<<
" and atol "<<eps*nrmb_correction );
small class holding a stringstream
Definition exceptions.h:29
Preconditioned conjugate gradient method to solve .
Definition pcg.h:105
const ContainerType & copyable() const
Return an object of same size as the object used for construction.
Definition pcg.h:128
unsigned solve(MatrixType0 &&A, ContainerType0 &x, const ContainerType1 &b, MatrixType1 &&P, const ContainerType2 &W, double eps=1e-12, double nrmb_correction=1, int test_frequency=1)
Solve using a preconditioned conjugate gradient method.
get_value_type< ContainerType > value_type
value type of the ContainerType class
Definition pcg.h:108
PCG()=default
Allocate nothing, Call construct method before usage.
ContainerType container_type
Definition pcg.h:107
void set_verbose(bool verbose)
Set or unset debugging output during iterations.
Definition pcg.h:132
void set_throw_on_fail(bool throw_on_fail)
Set or unset a throw on failure-to-converge.
Definition pcg.h:136
void construct(Params &&...ps)
Perfect forward parameters to one of the constructors.
Definition pcg.h:142
PCG(const ContainerType ©able, unsigned max_iterations)
Allocate memory for the pcg method.
Definition pcg.h:118
unsigned get_max() const
Get the current maximum number of iterations.
Definition pcg.h:125
void set_max(unsigned new_max)
Set the maximum number of iterations.
Definition pcg.h:122
#define _ping_
Definition exceptions.h:12
void copy(const ContainerTypeIn &source, ContainerTypeOut &target)
Definition blas1.h:243
void axpby(value_type alpha, const ContainerType1 &x, value_type1 beta, ContainerType &y)
Definition blas1.h:306
auto vdot(Functor f, const ContainerType &x, const ContainerTypes &...xs) -> std::invoke_result_t< Functor, dg::get_value_type< ContainerType >, dg::get_value_type< ContainerTypes >... >
Extended Precision transform reduce
Definition blas1.h:90
auto dot(const ContainerType1 &x, const MatrixType &m, const ContainerType2 &y)
; Binary reproducible general dot product
Definition blas2.h:94
void symv(MatrixType &&M, const ContainerType1 &x, ContainerType2 &y)
Definition blas2.h:331
constexpr bool is_scalar_v
Utility typedef.
Definition predicate.h:72
typename TensorTraits< std::decay_t< Vector > >::value_type get_value_type
Definition tensor_traits.h:45
ComplexMode
Determine algorithm for complex matrices.
Definition pcg.h:51
@ complex_symmetric
choose COCG algorithm
Definition pcg.h:52
@ complex_hermitian
choose Hermitian algorithm
Definition pcg.h:53
#define DG_DEVICE
Expands to __host__ __device__ if compiled with nvcc else is empty.
Definition dg_doc.h:378
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Ell Sparse Block Matrix format.
Definition sparseblockmat.h:46
Indicate failure to converge.
Definition exceptions.h:126
Useful typedefs of commonly used types.
#define DG_RANK0
Definition typedefs.h:152