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>
23#include <kernel/solver/amavanka_gmres.hpp>
25#include <kernel/solver/backend_aux/richardson_apply_helper.hpp>
28#include <kernel/util/cuda_util.hpp>
38 namespace VankaRichardson
40 template<
typename DT_,
typename IT_,
int NC_>
45 template<
typename DT_,
typename IT_,
int NC_>
54 template<
typename,
typename =
void>
55 static constexpr int ricci_dim_vector = 1;
57 static constexpr int ricci_dim_vector<T_, std::void_t<
decltype(T_::block_size)>> = T_::block_size;
78 template<
typename Matrix_,
typename Filter_>
82 typename LocalMatrix_,
83 typename LocalFilter_,
87 public IterativeSolver<typename Global::Matrix<LocalMatrix_, MirrorRow_, MirrorCol_>::VectorTypeR>
93 typedef typename MatrixType::DataType DataType;
94 typedef typename LocalMatrix_::IndexType IndexType;
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");
118 bool _always_use_parallel_kernel;
130 std::shared_ptr<VankaType> vanka, DataType omega = DataType(1)) :
132 _system_matrix(matrix),
133 _system_filter(filter),
134 _comm(matrix.get_comm()),
135 _vanka(std::move(vanka)),
140 _always_use_parallel_kernel(false)
142 this->_set_comm_by_matrix(matrix);
147 virtual String name()
const override {
return "AmaVankaRichardson"; }
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; }
155 BaseClass::init_symbolic();
156 this->_vanka->init_symbolic();
165 BaseClass::init_numeric();
167 this->_vanka->init_numeric();
172 this->_vanka->done_numeric();
173 BaseClass::done_numeric();
178 this->_vec_cor.
clear();
179 this->_vec_def.
clear();
180 this->_vanka->done_symbolic();
181 BaseClass::done_symbolic();
188 this->_status = _apply_intern_dispatch(vec_cor, vec_def);
189 this->plot_summary();
190 return this->_status;
196 this->_status = _apply_intern_dispatch(vec_sol, vec_rhs);
197 this->plot_summary();
198 return this->_status;
202 Status _apply_intern_dispatch(VectorType& vec_sol,
const VectorType& vec_rhs)
204 #ifdef FEAT_HAVE_CUDA
206 return this->_cuda_apply_intern(vec_sol, vec_rhs);
208 return this->_apply_intern(vec_sol, vec_rhs);
220 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
224 const auto& vanka_mat = this->_vanka->get_vanka_matrix();
227 for(
Index step(0); step < steps; ++step)
230 this->_system_matrix.
apply(vec_def, vec_sol, vec_rhs, -DataType(1));
231 this->_system_filter.filter_def(vec_def);
234 vanka_mat.apply(vec_cor.
local(), vec_def.
local());
236 this->_system_filter.filter_cor(vec_cor);
239 vec_sol.
axpy(vec_cor, this->_omega);
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()));
250 template<
int bh_,
int bw_>
264 mb.block_height = bh_;
265 mb.block_width = bw_;
272 template<
typename MA_,
typename MB_,
typename MD_>
273 static void _extract_matrix(RichardsonHelper::MetaMatrix<DataType, IndexType, num_comp, num_comp>& out,
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());
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)
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>());
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)
300 if constexpr(C_ < num_comp)
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)>>;
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();
315 auto& gw = info.gates[C_];
316 gw.comm =
const_cast<Dist::Comm*
>(this->_comm);
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)
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());
330 _fill_components<C_ + 1>(info, vec_sol, vec_rhs, gate);
334 virtual Status _cuda_apply_intern(VectorType& vec_sol,
const VectorType& vec_rhs)
336 Statistics::add_solver_expression(std::make_shared<ExpressionStartSolve>(this->name()));
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());
343 _extract_filters<0>(info);
347 _fill_components<0>(info, vec_sol, vec_rhs, gate);
349 info.omega = this->_omega;
350 info.min_iter = this->_min_iter;
352 info.always_use_parallel_kernel = this->_always_use_parallel_kernel;
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);
358 this->_status = VankaRichardson::cuda_apply_amavanka_richardson_parallel(info, stat);
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;
369 void _extract_filters(RichardsonHelper::RichardsonApplyInfo<DataType, IndexType, num_comp>& info)
371 if constexpr(C_ < num_comp)
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);
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);
387 std::array<LAFEM::UnitFilter<DataType, IndexType>, std::size_t(num_comp)> _unit_filters;
399 template<
typename Matrix_,
typename Filter_,
typename Vanka_>
401 const Matrix_& matrix,
const Filter_& filter, std::shared_ptr<Vanka_> vanka,
402 typename Matrix_::DataType omega =
typename Matrix_::DataType(1))
404 return std::make_shared<AmaVankaRichardson<Matrix_, Filter_>>(matrix, filter, std::move(vanka), omega);
int size() const
Returns the size of this communicator.
Global Filter wrapper class template.
Global Matrix wrapper class template.
VectorTypeR create_vector_r() const
Creates and returns a new R-compatible global vector object.
const GateRowType * get_row_gate() const
Returns a const pointer to the internal row gate of the matrix.
void apply(VectorTypeL &r, const VectorTypeR &x) const
Performs a matrix-vector multiplication: r <- A*x.
Global vector wrapper class template.
void sync_1()
Performs a type-1 synchronization of the vector, i.e. averages all local DOF contributions.
void format(DataType alpha=DataType(0))
Reset all elements of the container to a given value or zero if missing.
LocalVector_ & local()
Returns a reference to the internal local LAFEM vector object.
void axpy(const Vector &x, const DataType alpha=DataType(1))
Performs an AXPY operation: this <- this + alpha*x.
void clear()
Clears the underlying vector.
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.
const FilterType & _system_filter
the system filter
DataType _omega
Richardson damping factor.
VectorType _vec_def
scratch vectors
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 void init_symbolic() override
Symbolic initialization method.
virtual String name() const override
Returns a descriptive string.
virtual void init_numeric() override
Numeric initialization method.
virtual Status _apply_intern(VectorType &vec_sol, const VectorType &vec_rhs)
Generic (CPU/reference) Richardson(Schwarz(AmaVanka)) loop.
virtual void done_symbolic() override
Symbolic finalization method.
const Dist::Comm * _comm
the communicator
AmaVankaRichardson(const MatrixType &matrix, const FilterType &filter, std::shared_ptr< VankaType > vanka, DataType omega=DataType(1))
Constructor.
const MatrixType & _system_matrix
the system matrix
virtual void done_numeric() override
Numeric finalization method.
Fused Richardson(Schwarz(AmaVanka)) smoother specialization for a global (meta) system.
Abstract base-class for iterative solvers.
String class implementation.
RAII scope guard for an NVTX profiling range.
T_ max(T_ a, T_ b)
Returns the maximum of two values.
VankaType
Vanka type enumeration.
Status
Solver status return codes enumeration.
@ 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.
std::uint64_t Index
Index data type.
Everything the fused Richardson driver needs for one apply.