Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_KrylovMV.H
Go to the documentation of this file.
1#ifndef AMREX_KRYLOV_MV_H_
2#define AMREX_KRYLOV_MV_H_
3
4#include <AMReX_Algebra.H>
5#include <AMReX_Smoother_MV.H>
6#include <functional>
7#include <type_traits>
8
9namespace amrex {
10
17template <typename V, typename M> class GMRES;
18
28template <template <typename, typename> class S, typename T>
30{
31public:
32 using RT = T;
35 using Solver = S<VEC,KrylovMV<S,T>>;
36 using GM = Solver;
37 using PC = std::function<void(VEC&,VEC const&)>;
38
44 KrylovMV (MAT const* a_mat);
45
46 ~KrylovMV () = default;
47
48 // The solver holds a pointer to this object.
49 KrylovMV (const KrylovMV&) = delete;
50 KrylovMV (KrylovMV&&) = delete;
51 KrylovMV& operator= (const KrylovMV&) = delete;
53
61 void setPrecond (PC a_pc) { m_pc = std::move(a_pc); }
62
71 void solve (VEC& a_sol, VEC const& a_rhs, T a_tol_rel, T a_tol_abs);
72
74 void setVerbose (int v) { m_solver.setVerbose(v); }
75
77 Solver& getSolver () { return m_solver; }
78
80 Solver& getGMRES () requires std::is_same_v<Solver, GMRES<VEC,KrylovMV<S,T>>>
81 { return m_solver; }
82
84 [[nodiscard]] VEC makeVecRHS () const;
85
87 [[nodiscard]] VEC makeVecLHS () const;
88
90 static T norm2 (VEC const& vec);
91
93 static void scale (VEC& vec, T scale_factor);
94
96 static T dotProduct (VEC const& vec1, VEC const& vec2);
97
99 static void setToZero (VEC& lhs);
100
102 static void assign (VEC& lhs, VEC const& rhs);
103
105 static void increment (VEC& lhs, VEC const& rhs, T a);
106
108 static void linComb (VEC& lhs, T a, VEC const& rhs_a, T b, VEC const& rhs_b);
109
115 void apply (VEC& lhs, VEC& rhs) const;
116
118 void precond (VEC& lhs, VEC const& rhs) const;
119
120private:
121 Solver m_solver;
122 MAT const* m_mat = nullptr;
123 PC m_pc;
124};
125
126template <template <typename, typename> class S, typename T>
128 : m_mat(a_mat)
129{
130 m_solver.define(*this);
131}
132
133template <template <typename, typename> class S, typename T>
134void KrylovMV<S,T>::solve (VEC& a_sol, VEC const& a_rhs, T a_tol_rel, T a_tol_abs)
135{
136 m_solver.solve(a_sol, a_rhs, a_tol_rel, a_tol_abs);
137}
138
139template <template <typename, typename> class S, typename T>
141{
142 return VEC(m_mat->partition());
143}
144
145template <template <typename, typename> class S, typename T>
147{
148 return VEC(m_mat->partition());
149}
150
151template <template <typename, typename> class S, typename T>
153{
154 return vec.norm2();
155}
156
157template <template <typename, typename> class S, typename T>
158void KrylovMV<S,T>::scale (VEC& vec, T scale_factor)
159{
160 vec.scaleAsync(scale_factor);
161}
162
163template <template <typename, typename> class S, typename T>
164T KrylovMV<S,T>::dotProduct (VEC const& vec1, VEC const& vec2)
165{
166 return amrex::Dot(vec1,vec2);
167}
168
169template <template <typename, typename> class S, typename T>
171{
172 lhs.setValAsync(0);
173}
174
175template <template <typename, typename> class S, typename T>
176void KrylovMV<S,T>::assign (VEC& lhs, VEC const& rhs)
177{
178 lhs.copyAsync(rhs);
179}
180
181template <template <typename, typename> class S, typename T>
182void KrylovMV<S,T>::increment (VEC& lhs, VEC const& rhs, T a)
183{
184 amrex::Axpy(lhs, a, rhs);
185}
186
187template <template <typename, typename> class S, typename T>
188void KrylovMV<S,T>::linComb (VEC& lhs, T a, VEC const& rhs_a, T b, VEC const& rhs_b)
189{
190 amrex::LinComb(lhs, a, rhs_a, b, rhs_b);
191}
192
193template <template <typename, typename> class S, typename T>
194void KrylovMV<S,T>::apply (VEC& lhs, VEC& rhs) const
195{
196 amrex::SpMV(lhs, *m_mat, rhs);
197}
198
199template <template <typename, typename> class S, typename T>
200void KrylovMV<S,T>::precond (VEC& lhs, VEC const& rhs) const
201{
202 if (m_pc) {
203 m_pc(lhs, rhs);
204 } else {
205 lhs.copyAsync(rhs);
206 }
207}
208
209}
210#endif
Distributed dense vector that mirrors the layout of an AlgPartition.
Definition AMReX_AlgVector.H:29
void setValAsync(T val)
Definition AMReX_AlgVector.H:280
T norm2(bool local=false) const
Return the 2-norm.
Definition AMReX_AlgVector.H:383
void copyAsync(AlgVector< T, Allocator > const &rhs)
Definition AMReX_AlgVector.H:295
void scaleAsync(T scale_factor)
Definition AMReX_AlgVector.H:329
GMRES.
Definition AMReX_GMRES.H:87
Binds a Krylov solver to an SpMatrix.
Definition AMReX_KrylovMV.H:30
VEC makeVecRHS() const
Return an AlgVector that mirrors the matrix partition for RHS storage.
Definition AMReX_KrylovMV.H:140
static void assign(VEC &lhs, VEC const &rhs)
Copy rhs into lhs.
Definition AMReX_KrylovMV.H:176
static void increment(VEC &lhs, VEC const &rhs, T a)
Accumulate lhs += a * rhs.
Definition AMReX_KrylovMV.H:182
void setPrecond(PC a_pc)
Supply an optional right-preconditioner functor.
Definition AMReX_KrylovMV.H:61
void solve(VEC &a_sol, VEC const &a_rhs, T a_tol_rel, T a_tol_abs)
Solve the linear system.
Definition AMReX_KrylovMV.H:134
static T dotProduct(VEC const &vec1, VEC const &vec2)
Dot product between vec1 and vec2.
Definition AMReX_KrylovMV.H:164
static void setToZero(VEC &lhs)
Reset lhs to zero.
Definition AMReX_KrylovMV.H:170
T RT
Definition AMReX_KrylovMV.H:32
void precond(VEC &lhs, VEC const &rhs) const
Apply the optional preconditioner (or copy if none is provided).
Definition AMReX_KrylovMV.H:200
std::function< void(VEC &, VEC const &)> PC
Definition AMReX_KrylovMV.H:37
KrylovMV(const KrylovMV &)=delete
S< VEC, KrylovMV< S, T > > Solver
Definition AMReX_KrylovMV.H:35
static T norm2(VEC const &vec)
Euclidean norm of vec.
Definition AMReX_KrylovMV.H:152
void apply(VEC &lhs, VEC &rhs) const
Apply the sparse operator: lhs = A rhs.
Definition AMReX_KrylovMV.H:194
static void linComb(VEC &lhs, T a, VEC const &rhs_a, T b, VEC const &rhs_b)
Form the linear combination lhs = a * rhs_a + b * rhs_b.
Definition AMReX_KrylovMV.H:188
~KrylovMV()=default
KrylovMV(KrylovMV &&)=delete
Solver & getGMRES()
Same as getSolver(), for GMRES_MV only.
Definition AMReX_KrylovMV.H:80
Solver & getSolver()
Access the underlying solver for additional settings and results.
Definition AMReX_KrylovMV.H:77
Solver GM
Definition AMReX_KrylovMV.H:36
void setVerbose(int v)
Set verbosity level v for the underlying solver.
Definition AMReX_KrylovMV.H:74
static void scale(VEC &vec, T scale_factor)
Scale vec in place by scale_factor.
Definition AMReX_KrylovMV.H:158
KrylovMV(MAT const *a_mat)
Bind the solver to a sparse matrix described by a_mat.
Definition AMReX_KrylovMV.H:127
VEC makeVecLHS() const
Return another AlgVector with the same partition for LHS storage.
Definition AMReX_KrylovMV.H:146
KrylovMV & operator=(const KrylovMV &)=delete
Distributed CSR matrix that manages storage and GPU-friendly partitions.
Definition AMReX_SpMatrix.H:65
Definition AMReX_Amr.cpp:50
FAB::value_type Dot(FabArray< FAB > const &x, int xcomp, FabArray< FAB > const &y, int ycomp, int ncomp, IntVect const &nghost, bool local=false)
Compute dot products of two FabArrays.
Definition AMReX_FabArrayUtility.H:1898
void LinComb(MF &dst, typename MF::value_type a, MF const &src_a, int acomp, typename MF::value_type b, MF const &src_b, int bcomp, int dcomp, int ncomp, IntVect const &nghost)
dst = a*src_a + b*src_b
Definition AMReX_FabArrayUtility.H:2241
void Axpy(AlgVector< T, Allocator > &y, T a, AlgVector< T, Allocator > const &x)
y = ax + y. For GPU builds this is asynchronous with respect to the host.
Definition AMReX_AlgVecUtil.H:188
void SpMV(Long nrows, Long ncols, T *__restrict__ py, CsrView< T const, I > const &A, T const *__restrict__ px)
Perform y = A * x using CSR data (GPU/CPU aware).
Definition AMReX_SpMV.H:30