FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
amavanka_richardson.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
9#include <kernel/solver/iterative.hpp>
10#include <kernel/global/matrix.hpp>
11#include <kernel/global/filter.hpp>
12#include <kernel/global/vector.hpp>
13#include <kernel/lafem/saddle_point_matrix.hpp>
14#include <kernel/lafem/tuple_matrix.hpp>
15#include <kernel/lafem/tuple_vector.hpp>
16#include <kernel/lafem/tuple_mirror.hpp>
17#include <kernel/lafem/vector_mirror.hpp>
18#include <kernel/lafem/sparse_matrix_bcsr.hpp>
19#include <kernel/lafem/sparse_matrix_csr.hpp>
20#include <kernel/lafem/unit_filter.hpp>
21#include <kernel/solver/amavanka.hpp>
22// reuse the recursive Intern::FilterHelper (meta-filter -> flat unit-filter map) from the GMRES variant
23#include <kernel/solver/amavanka_gmres.hpp>
24
25#include <kernel/solver/backend_aux/richardson_apply_helper.hpp>
26
27#ifdef FEAT_HAVE_CUDA
28#include <kernel/util/cuda_util.hpp>
29#endif
30
31#include <vector>
32
33namespace FEAT
34{
35 namespace Solver
36 {
38 namespace VankaRichardson
39 {
40 template<typename DT_, typename IT_, int NC_>
41 FEAT::Solver::Status cuda_apply_amavanka_richardson_seq(
44
45 template<typename DT_, typename IT_, int NC_>
46 FEAT::Solver::Status cuda_apply_amavanka_richardson_parallel(
49 }
50
51 namespace Intern
52 {
53 // block-size deduction: DenseVectorBlocked exposes ::block_size, plain DenseVector does not (=> 1)
54 template<typename, typename = void>
55 static constexpr int ricci_dim_vector = 1;
56 template<typename T_>
57 static constexpr int ricci_dim_vector<T_, std::void_t<decltype(T_::block_size)>> = T_::block_size;
58 }
59
78 template<typename Matrix_, typename Filter_>
80
81 template<
82 typename LocalMatrix_,
83 typename LocalFilter_,
84 typename MirrorRow_,
85 typename MirrorCol_>
86 class AmaVankaRichardson<Global::Matrix<LocalMatrix_, MirrorRow_, MirrorCol_>, Global::Filter<LocalFilter_, MirrorRow_>> :
87 public IterativeSolver<typename Global::Matrix<LocalMatrix_, MirrorRow_, MirrorCol_>::VectorTypeR>
88 {
89 public:
92 typedef typename MatrixType::VectorTypeR VectorType;
93 typedef typename MatrixType::DataType DataType;
94 typedef typename LocalMatrix_::IndexType IndexType;
97
99 static constexpr int num_comp = LocalMatrix_::num_row_blocks;
100 static_assert(LocalMatrix_::num_row_blocks == LocalMatrix_::num_col_blocks,
101 "AmaVankaRichardson requires a square meta system");
102
103 protected:
111 std::shared_ptr<VankaType> _vanka;
113 DataType _omega;
116
117 bool _use_cuda;
118 bool _always_use_parallel_kernel;
119
120 public:
129 explicit AmaVankaRichardson(const MatrixType& matrix, const FilterType& filter,
130 std::shared_ptr<VankaType> vanka, DataType omega = DataType(1)) :
131 BaseClass("AmaVankaRichardson"),
132 _system_matrix(matrix),
133 _system_filter(filter),
134 _comm(matrix.get_comm()),
135 _vanka(std::move(vanka)),
136 _omega(omega),
137 _vec_def(),
138 _vec_cor(),
139 _use_cuda(false),
140 _always_use_parallel_kernel(false)
141 {
142 this->_set_comm_by_matrix(matrix);
143 }
144
145 virtual ~AmaVankaRichardson() {}
146
147 virtual String name() const override { return "AmaVankaRichardson"; }
148
149 virtual void set_use_cuda(bool use) { this->_use_cuda = use; }
150 virtual void set_use_parallel_kernel(bool use) { this->_always_use_parallel_kernel = use; }
151 virtual void set_omega(DataType omega) { this->_omega = omega; }
152
153 virtual void init_symbolic() override
154 {
155 BaseClass::init_symbolic();
156 this->_vanka->init_symbolic();
157 this->_vec_def = this->_system_matrix.create_vector_r();
158 this->_vec_cor = this->_system_matrix.create_vector_r();
159 this->_vec_def.format();
160 this->_vec_cor.format();
161 }
162
163 virtual void init_numeric() override
164 {
165 BaseClass::init_numeric();
166 Util::ScopedNvtxRange nvtx("Vanka: Init Numeric");
167 this->_vanka->init_numeric();
168 }
169
170 virtual void done_numeric() override
171 {
172 this->_vanka->done_numeric();
173 BaseClass::done_numeric();
174 }
175
176 virtual void done_symbolic() override
177 {
178 this->_vec_cor.clear();
179 this->_vec_def.clear();
180 this->_vanka->done_symbolic();
181 BaseClass::done_symbolic();
182 }
183
185 virtual Status apply(VectorType& vec_cor, const VectorType& vec_def) override
186 {
187 vec_cor.format();
188 this->_status = _apply_intern_dispatch(vec_cor, vec_def);
189 this->plot_summary();
190 return this->_status;
191 }
192
194 virtual Status correct(VectorType& vec_sol, const VectorType& vec_rhs) override
195 {
196 this->_status = _apply_intern_dispatch(vec_sol, vec_rhs);
197 this->plot_summary();
198 return this->_status;
199 }
200
201 protected:
202 Status _apply_intern_dispatch(VectorType& vec_sol, const VectorType& vec_rhs)
203 {
204 #ifdef FEAT_HAVE_CUDA
205 if(this->_use_cuda)
206 return this->_cuda_apply_intern(vec_sol, vec_rhs);
207 #endif
208 return this->_apply_intern(vec_sol, vec_rhs);
209 }
210
218 virtual Status _apply_intern(VectorType& vec_sol, const VectorType& vec_rhs)
219 {
220 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
221
222 VectorType& vec_def(this->_vec_def);
223 VectorType& vec_cor(this->_vec_cor);
224 const auto& vanka_mat = this->_vanka->get_vanka_matrix();
225
226 const Index steps = Math::max(this->_max_iter, Index(1));
227 for(Index step(0); step < steps; ++step)
228 {
229 // d = rhs - A*sol (global apply includes the type-0 synchronization)
230 this->_system_matrix.apply(vec_def, vec_sol, vec_rhs, -DataType(1));
231 this->_system_filter.filter_def(vec_def);
232
233 // cor = V*d (local AmaVanka apply) + Schwarz sync_1 + correction filter
234 vanka_mat.apply(vec_cor.local(), vec_def.local());
235 vec_cor.sync_1();
236 this->_system_filter.filter_cor(vec_cor);
237
238 // u += omega * cor
239 vec_sol.axpy(vec_cor, this->_omega);
240 }
241
242 this->_num_iter = steps;
243 this->_def_cur = this->_def_init = DataType(0);
244 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), Status::success, this->get_num_iter()));
245 return Status::success;
246 }
247
248#ifdef FEAT_HAVE_CUDA
250 template<int bh_, int bw_>
251 static void _fill_block(RichardsonHelper::MetaBlock<DataType, IndexType>& mb,
253 {
254 if(m.num_nzes() == Index(0))
255 return; // leave the block empty
256 // ensure this block owns a persistent cuSPARSE SpMV workspace (built once, reused across
257 // applies; required by the cuSPARSE BSR SpMV path on CUDA >= 13) and hand it to the driver
258 if(!m.get_workspace(PreferredBackend::cuda))
259 m.init_workspace();
260 mb.workspace = m.get_workspace(PreferredBackend::cuda).get();
261 mb.val = &m.val_arbiter();
262 mb.row = &m.row_ptr_arbiter();
263 mb.col = &m.col_idx_arbiter();
264 mb.block_height = bh_;
265 mb.block_width = bw_;
266 mb.num_rows = m.num_rows();
267 mb.num_cols = m.num_cols();
268 mb.num_nnz = m.num_nzes();
269 }
270
272 template<typename MA_, typename MB_, typename MD_>
273 static void _extract_matrix(RichardsonHelper::MetaMatrix<DataType, IndexType, num_comp, num_comp>& out,
275 {
276 static_assert(num_comp == 2, "saddle-point extraction expects two components");
277 _fill_block(out.at(0, 0), mat.block_a());
278 _fill_block(out.at(0, 1), mat.block_b());
279 _fill_block(out.at(1, 0), mat.block_d());
280 // (1,1) stays empty
281 }
282
284 template<typename Row0_, typename Row1_>
285 static void _extract_matrix(RichardsonHelper::MetaMatrix<DataType, IndexType, num_comp, num_comp>& out,
286 const LAFEM::TupleMatrix<Row0_, Row1_>& mat)
287 {
288 static_assert(num_comp == 2, "tuple extraction expects two components");
289 _fill_block(out.at(0, 0), mat.template at<0, 0>());
290 _fill_block(out.at(0, 1), mat.template at<0, 1>());
291 _fill_block(out.at(1, 0), mat.template at<1, 0>());
292 _fill_block(out.at(1, 1), mat.template at<1, 1>());
293 }
294
296 template<int C_, typename LocGate_>
297 void _fill_components(RichardsonHelper::RichardsonApplyInfo<DataType, IndexType, num_comp>& info,
298 VectorType& vec_sol, const VectorType& vec_rhs, const LocGate_* gate)
299 {
300 if constexpr(C_ < num_comp)
301 {
302 auto& sc = vec_sol.local().template at<C_>();
303 const auto& rc = vec_rhs.local().template at<C_>();
304 auto& dc = this->_vec_def.local().template at<C_>();
305 auto& cc = this->_vec_cor.local().template at<C_>();
306 constexpr int bs = Intern::ricci_dim_vector<std::decay_t<decltype(sc)>>;
307
308 info.components[C_] = RichardsonHelper::MetaVectorInfo{bs, sc.size()};
309 info.vec_sol[C_] = &sc.elements_arbiter();
310 info.vec_rhs[C_] = &rc.elements_arbiter();
311 info.vec_def[C_] = &dc.elements_arbiter();
312 info.vec_cor[C_] = &cc.elements_arbiter();
313
314 // gate for this component (empty if no row gate)
315 auto& gw = info.gates[C_];
316 gw.comm = const_cast<Dist::Comm*>(this->_comm);
317 if(gate != nullptr)
318 {
319 const auto& ranks = gate->get_ranks();
320 const auto& mirrors = gate->get_mirrors();
321 gw.ranks.assign(ranks.begin(), ranks.end());
322 gw.freqs = &gate->get_freqs().template at<C_>().elements_arbiter();
323 for(std::size_t k(0); k < ranks.size(); ++k)
324 {
325 const auto& sub = mirrors[k].template at<C_>();
326 gw.mirror_idx.push_back(&sub.indices_arbiter());
327 gw.mirror_size.push_back(sub.num_indices());
328 }
329 }
330 _fill_components<C_ + 1>(info, vec_sol, vec_rhs, gate);
331 }
332 }
333
334 virtual Status _cuda_apply_intern(VectorType& vec_sol, const VectorType& vec_rhs)
335 {
336 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
337
338 RichardsonHelper::RichardsonApplyInfo<DataType, IndexType, num_comp> info;
339 _extract_matrix(info.mat_sys, this->_system_matrix.local());
340 _extract_matrix(info.mat_vanka, this->_vanka->get_vanka_matrix());
341
342 // per-component unit-defect filters
343 _extract_filters<0>(info);
344
345 // per-component vectors + gates
346 const auto* gate = this->_system_matrix.get_row_gate();
347 _fill_components<0>(info, vec_sol, vec_rhs, gate);
348
349 info.omega = this->_omega;
350 info.min_iter = this->_min_iter;
351 info.max_iter = Math::max(this->_max_iter, Index(1));
352 info.always_use_parallel_kernel = this->_always_use_parallel_kernel;
353
354 RichardsonHelper::RichardsonStatus<DataType> stat;
355 if((this->_comm == nullptr || this->_comm->size() == 1) && !this->_always_use_parallel_kernel)
356 this->_status = VankaRichardson::cuda_apply_amavanka_richardson_seq(info, stat);
357 else
358 this->_status = VankaRichardson::cuda_apply_amavanka_richardson_parallel(info, stat);
359
360 this->_num_iter = stat.num_iter;
361 this->_def_cur = stat.def_norm;
362 this->_def_init = stat.def_init;
363 Statistics::add_solver_expression(std::make_shared<ExpressionEndSolve>(this->name(), this->_status, stat.num_iter));
364 return this->_status;
365 }
366
368 template<int C_>
369 void _extract_filters(RichardsonHelper::RichardsonApplyInfo<DataType, IndexType, num_comp>& info)
370 {
371 if constexpr(C_ < num_comp)
372 {
373 auto& vc = this->_vec_def.local().template at<C_>();
374 std::map<IndexType, DataType> fmap;
375 Intern::FilterHelper<std::decay_t<decltype(this->_system_filter.local().template at<C_>())>>::add_filter(
376 fmap, this->_system_filter.local().template at<C_>(), vc);
377 // stash the flattened unit filter so its arbiters stay alive for the whole apply
378 auto& uf = _unit_filters[C_];
379 uf = LAFEM::UnitFilter<DataType, IndexType>(vc.size_raw(), fmap);
380 info.def_filters[C_] = RichardsonHelper::UnitFilterInfo<DataType, IndexType>{
381 &uf.elements_arbiter(), &uf.indices_arbiter(), uf.num_nzes_raw()};
382 _extract_filters<C_ + 1>(info);
383 }
384 }
385
387 std::array<LAFEM::UnitFilter<DataType, IndexType>, std::size_t(num_comp)> _unit_filters;
388#endif // FEAT_HAVE_CUDA
389 }; // class AmaVankaRichardson<...>
390
399 template<typename Matrix_, typename Filter_, typename Vanka_>
400 inline std::shared_ptr<AmaVankaRichardson<Matrix_, Filter_>> new_amavanka_richardson(
401 const Matrix_& matrix, const Filter_& filter, std::shared_ptr<Vanka_> vanka,
402 typename Matrix_::DataType omega = typename Matrix_::DataType(1))
403 {
404 return std::make_shared<AmaVankaRichardson<Matrix_, Filter_>>(matrix, filter, std::move(vanka), omega);
405 }
406 } // namespace Solver
407} // namespace FEAT
Communicator class.
Definition: dist.hpp:1657
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
Global Filter wrapper class template.
Definition: filter.hpp:21
Global Matrix wrapper class template.
Definition: matrix.hpp:40
VectorTypeR create_vector_r() const
Creates and returns a new R-compatible global vector object.
Definition: matrix.hpp:217
const GateRowType * get_row_gate() const
Returns a const pointer to the internal row gate of the matrix.
Definition: matrix.hpp:164
void apply(VectorTypeL &r, const VectorTypeR &x) const
Performs a matrix-vector multiplication: r <- A*x.
Definition: matrix.hpp:337
Global vector wrapper class template.
Definition: vector.hpp:68
void sync_1()
Performs a type-1 synchronization of the vector, i.e. averages all local DOF contributions.
Definition: vector.hpp:221
void format(DataType alpha=DataType(0))
Reset all elements of the container to a given value or zero if missing.
Definition: vector.hpp:318
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
Definition: vector.hpp:122
void axpy(const Vector &x, const DataType alpha=DataType(1))
Performs an AXPY operation: this <- this + alpha*x.
Definition: vector.hpp:366
void clear()
Clears the underlying vector.
Definition: vector.hpp:308
Saddle-Point matrix meta class template.
MatrixTypeA & block_a()
Returns the sub-matrix block A.
MatrixTypeB & block_b()
Returns the sub-matrix block B.
MatrixTypeD & block_d()
Returns the sub-matrix block D.
CSR based blocked sparse matrix.
Memory::Arbiter & val_arbiter()
Returns a reference to the values array arbiter.
Index num_rows() const
Retrieve matrix row count.
void init_workspace() const
Inits persistent workspace.
Index num_nzes() const
Retrieve non zero element count.
Memory::Arbiter & row_ptr_arbiter()
Returns a reference to the row-pointer array arbiter.
Index num_cols() const
Retrieve matrix column count.
Memory::Arbiter & col_idx_arbiter()
Returns a reference to the column-index array arbiter.
Additive Macro-wise Matrix-based Vanka preconditioner/smoother.
Definition: amavanka.hpp:65
std::shared_ptr< VankaType > _vanka
the AmaVanka preconditioner (kept alive; its stored matrix is referenced directly)
virtual Status apply(VectorType &vec_cor, const VectorType &vec_def) override
Solver application method.
virtual Status correct(VectorType &vec_sol, const VectorType &vec_rhs) override
Solver correction method.
virtual Status _apply_intern(VectorType &vec_sol, const VectorType &vec_rhs)
Generic (CPU/reference) Richardson(Schwarz(AmaVanka)) loop.
AmaVankaRichardson(const MatrixType &matrix, const FilterType &filter, std::shared_ptr< VankaType > vanka, DataType omega=DataType(1))
Constructor.
Fused Richardson(Schwarz(AmaVanka)) smoother specialization for a global (meta) system.
Abstract base-class for iterative solvers.
Definition: iterative.hpp:198
String class implementation.
Definition: string.hpp:47
RAII scope guard for an NVTX profiling range.
Definition: cuda_util.hpp:252
T_ max(T_ a, T_ b)
Returns the maximum of two values.
Definition: math.hpp:137
VankaType
Vanka type enumeration.
Definition: vanka.hpp:952
Status
Solver status return codes enumeration.
Definition: base.hpp:47
@ success
solving successful (convergence criterion fulfilled)
std::shared_ptr< AmaVankaRichardson< Matrix_, Filter_ > > new_amavanka_richardson(const Matrix_ &matrix, const Filter_ &filter, std::shared_ptr< Vanka_ > vanka, typename Matrix_::DataType omega=typename Matrix_::DataType(1))
Creates a new AmaVankaRichardson smoother object.
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.
A single (k x l) BCSR meta-block, referenced by its data arbiters.
Everything the fused Richardson driver needs for one apply.