8#include <kernel/lafem/null_matrix.hpp>
9#include <kernel/lafem/sparse_matrix_bcsr.hpp>
10#include <kernel/lafem/sparse_matrix_csr.hpp>
11#include <kernel/lafem/saddle_point_matrix.hpp>
12#include <kernel/lafem/tuple_matrix.hpp>
13#include <kernel/global/matrix.hpp>
14#include <kernel/solver/base.hpp>
15#include <kernel/solver/amavanka_base.hpp>
16#include <kernel/util/stop_watch.hpp>
62 template<
typename Matrix_,
typename Filter_>
77 typedef typename Intern::AmaVankaMatrixHelper<Matrix_>::VankaMatrix
VankaMatrixType;
127 explicit AmaVanka(
const Matrix_& matrix,
const Filter_& filter,
154 this->_macro_dofs.clear();
169 if(
int(
_macro_dofs.size()) >= Intern::AmaVankaMatrixHelper<Matrix_>::num_blocks)
170 XABORTM(
"all macro-dofs graphs have already been added");
174 XABORTM(
"macro count mismatch");
177 _macro_dofs.emplace_back(std::forward<Adjacency::Graph>(dofs));
192 this->_num_steps = num_steps;
204 this->_omega = omega;
215 this->_skip_singular = skip_sing;
223 std::size_t s =
_vanka.bytes();
225 s +=
sizeof(
Index) * std::size_t(g.get_num_nodes_domain() + g.get_num_indices());
226 for(
const auto& g : _dof_macros)
227 s +=
sizeof(
Index) * std::size_t(g.get_num_nodes_domain() + g.get_num_indices());
241 return std::size_t(
_vanka.num_nzes_raw());
249 watch_init_symbolic.
reset();
250 watch_init_numeric.
reset();
259 return watch_init_symbolic.
elapsed();
267 return watch_init_numeric.
elapsed();
287 watch_init_symbolic.
start();
292 if(this->_num_steps >
Index(1))
294 this->_vec_c = this->_matrix.create_vector_l();
295 this->_vec_d = this->_matrix.create_vector_l();
299 if(this->_auto_macros)
302 if(!Intern::AmaVankaCore::deduct_macro_dofs(this->_matrix, this->_macro_dofs))
303 XABORTM(
"Cannot auto-deduct macros for this matrix type");
308 "invalid number of macro-dof graphs; did you push all of them?");
311 this->_dof_macros.resize(this->_macro_dofs.size());
312 for(std::size_t i(0); i < this->_macro_dofs.size(); ++i)
315 XASSERT(this->_macro_dofs.at(i).get_num_nodes_domain() == this->_macro_dofs.front().get_num_nodes_domain());
322 if(this->_skip_singular)
323 this->_macro_mask.resize(this->_macro_dofs.front().get_num_nodes_domain(), 0);
325 Solver::Intern::AmaVankaCore::alloc(this->_vanka, this->_dof_macros, this->_macro_dofs,
Index(0),
Index(0));
326 this->get_vanka_matrix().init_workspace();
328 watch_init_symbolic.
stop();
334 this->get_vanka_matrix().reset_workspace();
335 this->_vanka.clear();
336 this->_macro_mask.clear();
337 this->_dof_macros.clear();
338 if(this->_auto_macros)
339 this->_macro_dofs.clear();
340 if(this->_num_steps >
Index(1))
342 this->_vec_d.clear();
343 this->_vec_c.clear();
351 const DataType eps = Math::eps<DataType>();
353 watch_init_numeric.
start();
356 this->_vanka.format();
359 const Index num_macros =
Index(this->_macro_dofs.front().get_num_nodes_domain());
360 const Index stride = Intern::AmaVankaCore::calc_stride(this->_vanka, this->_macro_dofs);
362 FEAT_PRAGMA_OMP(parallel)
365 std::vector<DataType> vec_local(stride*stride,
DataType(0)), vec_local_t(stride*stride,
DataType(0));
366 std::vector<Index> vec_pivot(stride);
368 DataType* local_t = vec_local_t.data();
369 Index* pivot = vec_pivot.data();
373 for(
Index imacro = 0; imacro < num_macros; ++imacro)
376 const std::pair<Index,Index> nrc = Intern::AmaVankaCore::gather(this->_matrix,
380 XASSERTM(nrc.first == nrc.second,
"local matrix is not square");
383 bool singular =
false;
386 if(this->_skip_singular)
396 for(
Index i(0); i < nrc.first; ++i)
397 for(
Index j(0); j < nrc.second; ++j)
398 local_t[i*stride+j] = local[i*stride+j];
405 for(
Index i(0); i < nrc.first; ++i)
407 for(
Index j(0); j < nrc.first; ++j)
410 for(
Index k(0); k < nrc.first; ++k)
411 xij -= local_t[i*stride+k] * local[k*stride+j];
420 singular = !(norm < eps);
423 this->_macro_mask[imacro] = (singular ? 0 : 1);
434 FEAT_PRAGMA_OMP(critical)
436 Intern::AmaVankaCore::scatter_add(this->_vanka, local, stride, imacro, this->_macro_dofs,
442 for(
Index i(0); i < nrc.first; ++i)
443 for(
Index j(0); j < nrc.second; ++j)
449 Solver::Intern::AmaVankaCore::scale_rows(this->_vanka, this->_omega, this->_dof_macros, this->_macro_mask,
Index(0),
Index(0));
451 watch_init_numeric.
stop();
464 this->_vanka.apply(vec_x, vec_b);
465 this->_filter.filter_cor(vec_x);
471 this->_matrix.apply(this->_vec_d, vec_x, vec_b, -
DataType(1));
473 this->_filter.filter_def(this->_vec_d);
475 this->_vanka.apply(this->_vec_c, this->_vec_d);
477 this->_filter.filter_cor(this->_vec_c);
479 vec_x.axpy(this->_vec_c);
512 template<
typename Matrix_,
typename Filter_>
513 std::shared_ptr<AmaVanka<Matrix_, Filter_>>
new_amavanka(
const Matrix_& matrix,
const Filter_& filter,
514 typename Matrix_::DataType omega =
typename Matrix_::DataType(1),
Index num_steps =
Index(1))
516 return std::make_shared<AmaVanka<Matrix_, Filter_>>(matrix, filter, omega, num_steps);
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Adjacency Graph implementation.
Additive Macro-wise Matrix-based Vanka preconditioner/smoother.
std::size_t bytes() const
Returns the total number of bytes currently allocated in this object.
virtual String name() const override
Returns the name of the solver.
const Matrix_ & _matrix
the system matrix
std::vector< int > _macro_mask
the macro mask
Matrix_::DataType DataType
our data type
virtual Status apply(VectorType &vec_x, const VectorType &vec_b) override
applies the preconditioner
double time_apply() const
Returns the total accumulated time for the solver application.
Index _num_steps
number of steps
void set_omega(DataType omega)
Sets the damping parameter omega.
VectorType _vec_c
temporary vectors
Matrix_::IndexType IndexType
our index type
bool _auto_macros
deduct macro dofs automatically?
DataType _omega
damping parameter
bool _skip_singular
skip singular macros?
int _num_threads
number of threads for numeric factorization
virtual void init_numeric() override
Performs numeric factorization.
void reset_timings()
Resets the internal stop watches for time measurement.
virtual void init_symbolic() override
Performs symbolic factorization.
double time_init_symbolic() const
Returns the total accumulated time for symbolic initialization.
std::size_t data_size() const
Returns the total data size used by the AmaVanka smoother.
void set_skip_singular(bool skip_sing)
Sets whether singular macros are to be skipped.
const Filter_ & _filter
the system filter
Matrix_::VectorTypeL VectorType
our vector type
AmaVanka(const Matrix_ &matrix, const Filter_ &filter, const DataType omega=DataType(1), const Index num_steps=Index(1))
Constructor.
double time_init_numeric() const
Returns the total accumulated time for numeric initialization.
VankaMatrixType _vanka
the Vanka preconditioner matrix
virtual void done_numeric() override
Numeric finalization method.
virtual void done_symbolic() override
Releases the symbolic factorization data.
void set_num_steps(Index num_steps)
Sets the number of smoothing steps.
Solver::SolverBase< typename Matrix_::VectorTypeL > BaseClass
our base-class
void push_macro_dofs(Adjacency::Graph &&dofs)
Pushes the dofs-at-macro graph of the next block.
void clear_macro_dofs()
Clears the macro dofs graphs.
Intern::AmaVankaMatrixHelper< Matrix_ >::VankaMatrix VankaMatrixType
the type of our Vanka matrix
std::vector< Adjacency::Graph > _macro_dofs
the DOF-macro graphs
Polymorphic solver interface.
virtual void init_symbolic()
Symbolic initialization method.
virtual void init_numeric()
Numeric initialization method.
virtual void done_symbolic()
Symbolic finalization method.
double elapsed() const
Returns the total elapsed time in seconds.
void start()
Starts the stop-watch.
void reset()
Resets the elapsed time.
void stop()
Stops the stop-watch and increments elapsed time.
String class implementation.
@ transpose
Render-Transpose mode.
DT_ invert_matrix(const IT_ n, const IT_ stride, DT_ a[], IT_ p[])
Inverts a matrix and returns its determinant.
std::shared_ptr< AmaVanka< Matrix_, Filter_ > > new_amavanka(const Matrix_ &matrix, const Filter_ &filter, typename Matrix_::DataType omega=typename Matrix_::DataType(1), Index num_steps=Index(1))
Creates a new AmaVanka smoother object.
Status
Solver status return codes enumeration.
@ success
solving successful (convergence criterion fulfilled)
std::uint64_t Index
Index data type.