10#include <kernel/solver/base.hpp>
18 int cuda_ssor_forward_apply(
int m,
double * y,
const double * x,
const double * csrVal,
const int * csrColInd,
int ncolors,
double omega,
19 int * colored_row_ptr,
int * rows_per_color,
int * inverse_row_ptr);
20 int cuda_ssor_backward_apply(
int m,
double * y,
const double * x,
const double * csrVal,
const int * csrColInd,
int ncolors,
double omega,
21 int * colored_row_ptr,
int * rows_per_color,
int * inverse_row_ptr);
22 template <
int block_size_>
23 int cuda_ssor_forward_bcsr_apply(
int m,
double * y,
const double * x,
const double * csrVal,
const int * csrColInd,
int ncolors,
double omega,
24 int * colored_row_ptr,
int * rows_per_color,
int * inverse_row_ptr);
25 template <
int block_size_>
26 int cuda_ssor_backward_bcsr_apply(
int m,
double * y,
const double * x,
const double * csrVal,
const int * csrColInd,
int ncolors,
double omega,
27 int * colored_row_ptr,
int * rows_per_color,
int * inverse_row_ptr);
28 void cuda_sor_done_symbolic(
int * colored_row_ptr,
int * rows_per_color,
int * inverse_row_ptr);
29 void cuda_sor_init_symbolic(
int m,
int nnz,
const double * csrVal,
const int * csrRowPtr,
const int * csrColInd,
int & ncolors,
30 int* & colored_row_ptr,
int* & rows_per_color,
int* & inverse_row_ptr);
47 template<
typename Matrix_>
51 typedef typename Matrix_::DataType DataType;
52 typedef typename Matrix_::VectorTypeL VectorType;
54 virtual void set_omega(DataType omega) = 0;
56 virtual void init_symbolic() = 0;
58 virtual void done_symbolic() = 0;
60 virtual String name()
const = 0;
62 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def) = 0;
67 template<PreferredBackend backend_,
typename Matrix_,
typename Filter_>
83 template<
typename Filter_,
typename DT_,
typename IT_>
90 typedef IT_ IndexType;
91 typedef Filter_ FilterType;
96 const FilterType& _filter;
120 XABORTM(
"Matrix is not square!");
125 const MatrixType& matrix,
const FilterType& filter) :
132 XABORTM(
"Matrix is not square!");
135 auto omega_p = section->
query(
"omega");
136 if(omega_p.second && !omega_p.first.parse(this->_omega))
137 throw ParseError(section_name +
".omega", omega_p.first,
"a positive float");
159 virtual void init_symbolic()
override
163 virtual void done_symbolic()
override
167 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
169 XASSERTM(_matrix.
num_rows() == vec_cor.size(),
"matrix / vector size mismatch!");
170 XASSERTM(_matrix.
num_rows() == vec_def.size(),
"matrix / vector size mismatch!");
175 vec_cor.copy(vec_def);
177 _apply_intern(_matrix, vec_cor, vec_def);
179 vec_cor.scale(vec_cor, _omega * (DataType(2.0) - _omega));
181 this->_filter.filter_cor(vec_cor);
184 Statistics::add_time_precon(ts_stop.elapsed(ts_start));
191 void _apply_intern(
const LAFEM::SparseMatrixCSR<DataType, IndexType>& matrix, VectorType& vec_cor,
const VectorType& vec_def)
194 Memory::TypedView<DataType> pout(vec_cor.elements_view_rw());
195 const Memory::TypedView<DataType> pin(vec_def.elements_view_r());
196 const Memory::TypedView<DataType> pval(matrix.val_view_r());
197 const Memory::TypedView<IndexType> pcol_idx(matrix.col_idx_view_r());
198 const Memory::TypedView<IndexType> prow_ptr(matrix.row_ptr_view_r());
199 const IndexType n((IndexType(matrix.num_rows())));
203 for (
Index i(0); i < n; ++i)
208 for (col = prow_ptr[i]; pcol_idx[col] < i; ++col)
210 d += pval[col] * pout[pcol_idx[col]];
212 pout[i] = (pin[i] - _omega * d) / pval[col];
217 for (
Index i(n); i > 0;)
223 for (col = prow_ptr[i+1] - IndexType(1); pcol_idx[col] > i; --col)
225 d += pval[col] * pout[pcol_idx[col]];
227 pout[i] -= _omega * d / pval[col];
232 template<
typename Filter_,
typename DT_,
typename IT_,
int block_height_,
int block_w
idth_>
234 public SSORPrecondBase<typename LAFEM::SparseMatrixBCSR<DT_, IT_, block_height_, block_width_>>
236 static_assert(block_height_ == block_width_,
"only square blocks are supported!");
239 typedef Filter_ FilterType;
246 const FilterType& _filter;
249 void _apply_intern(
const MatrixType & matrix, VectorType& vec_cor,
const VectorType& vec_def)
252 auto* pout(vec_cor.elements());
253 const auto* pin(vec_def.elements());
254 const auto* pval(matrix.val());
255 const IndexType * pcol_idx(matrix.col_idx());
256 const IndexType * prow_ptr(matrix.row_ptr());
257 const IndexType n((IndexType(matrix.
num_rows())));
262 for (
Index i(0); i < n; ++i)
265 typename VectorType::ValueType d(0);
267 for (col = prow_ptr[i]; pcol_idx[col] < i; ++col)
269 d += pval[col] * pout[pcol_idx[col]];
273 pout[i] = inverse * (pin[i] - _omega * d);
278 for (
Index i(n); i > 0;)
282 typename VectorType::ValueType d(0);
284 for (col = prow_ptr[i+1] - IndexType(1); pcol_idx[col] > i; --col)
286 d += pval[col] * pout[pcol_idx[col]];
290 pout[i] = pout[i] - (_omega * inverse * d);
315 XABORTM(
"Matrix is not square!");
320 const MatrixType& matrix,
const FilterType& filter) :
327 XABORTM(
"Matrix is not square!");
331 auto omega_p = section->
query(
"omega");
334 set_omega(DataType(std::stod(omega_p.first)));
357 virtual void init_symbolic()
override
361 virtual void done_symbolic()
override
365 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
367 XASSERTM(_matrix.
num_rows() == vec_cor.size(),
"matrix / vector size mismatch!");
368 XASSERTM(_matrix.
num_rows() == vec_def.size(),
"matrix / vector size mismatch!");
373 vec_cor.copy(vec_def);
375 _apply_intern(_matrix, vec_cor, vec_def);
377 this->_filter.filter_cor(vec_cor);
380 Statistics::add_time_precon(ts_stop.elapsed(ts_start));
404 template<
typename Filter_>
405 class SSORPrecondWithBackend<
PreferredBackend::
cuda, LAFEM::SparseMatrixCSR<double, unsigned int>, Filter_> :
406 public SSORPrecondBase<LAFEM::SparseMatrixCSR<double, unsigned int>>
409 typedef LAFEM::SparseMatrixCSR<double, unsigned int> MatrixType;
410 typedef Filter_ FilterType;
411 typedef typename MatrixType::VectorTypeL VectorType;
412 typedef typename MatrixType::DataType DataType;
413 typedef typename MatrixType::IndexType IndexType;
416 const MatrixType& _matrix;
417 const FilterType& _filter;
420 int * _colored_row_ptr;
422 int * _rows_per_color;
424 int * _inverse_row_ptr;
442 explicit SSORPrecondWithBackend(
const MatrixType& matrix,
const FilterType& filter,
const DataType omega = DataType(1)) :
446 _colored_row_ptr(nullptr),
447 _rows_per_color(nullptr),
448 _inverse_row_ptr(nullptr),
451 if (_matrix.num_cols() != _matrix.num_rows())
453 XABORTM(
"Matrix is not square!");
457 explicit SSORPrecondWithBackend(
const String& ,
const PropertyMap* section,
458 const MatrixType& matrix,
const FilterType& filter) :
462 _colored_row_ptr(nullptr),
463 _rows_per_color(nullptr),
464 _inverse_row_ptr(nullptr),
467 if (_matrix.num_cols() != _matrix.num_rows())
469 XABORTM(
"Matrix is not square!");
473 auto omega_p = section->query(
"omega");
476 set_omega(DataType(std::stod(omega_p.first)));
481 virtual String name()
const override
493 virtual void set_omega(DataType omega)
override
499 virtual void init_symbolic()
override
503 Memory::TypedView<int> row_ptr(std::forward<Memory::View&&>(_matrix.row_ptr_view_r(
Memory::Location::cuda)));
504 Memory::TypedView<int> col_idx(std::forward<Memory::View&&>(_matrix.col_idx_view_r(
Memory::Location::cuda)));
505 Intern::cuda_sor_init_symbolic((
int)_matrix.num_rows(), (
int)_matrix.num_nzes(), a.get_r(), row_ptr.get_r(), col_idx.get_r(), _ncolors,
506 _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
509 virtual void done_symbolic()
override
511 Intern::cuda_sor_done_symbolic(_colored_row_ptr, _rows_per_color, _inverse_row_ptr);
514 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
516 XASSERTM(_matrix.num_rows() == vec_cor.size(),
"matrix / vector size mismatch!");
517 XASSERTM(_matrix.num_rows() == vec_def.size(),
"matrix / vector size mismatch!");
528 Memory::TypedView<int> col_idx(std::forward<Memory::View&&>(_matrix.col_idx_view_r(
Memory::Location::cuda)));
530 status |= Intern::cuda_ssor_forward_apply((
int)vec_cor.size(), vx.get_w(), vb.get_r(), a.get_r(), col_idx.get_r(), _ncolors, _omega, _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
531 status |= Intern::cuda_ssor_backward_apply((
int)vec_cor.size(), vx.get_w(), vb.get_r(), a.get_r(), col_idx.get_r(), _ncolors, _omega, _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
534 vec_cor.scale(vec_cor, _omega * (2.0 - _omega));
536 this->_filter.filter_cor(vec_cor);
539 Statistics::add_time_precon(ts_stop.elapsed(ts_start));
546 template<
typename Filter_,
int block_height_,
int block_w
idth_>
547 class SSORPrecondWithBackend<
PreferredBackend::
cuda, LAFEM::SparseMatrixBCSR<double, unsigned int, block_height_, block_width_>, Filter_> :
548 public SSORPrecondBase<typename LAFEM::SparseMatrixBCSR<double, unsigned int, block_height_, block_width_>>
550 static_assert(block_height_ == block_width_,
"only square blocks are supported!");
552 typedef LAFEM::SparseMatrixBCSR<double, unsigned int, block_height_, block_width_> MatrixType;
553 typedef Filter_ FilterType;
554 typedef typename MatrixType::VectorTypeL VectorType;
555 typedef typename MatrixType::DataType DataType;
556 typedef typename MatrixType::IndexType IndexType;
559 const MatrixType& _matrix;
560 const FilterType& _filter;
563 int * _colored_row_ptr;
565 int * _rows_per_color;
567 int * _inverse_row_ptr;
585 explicit SSORPrecondWithBackend(
const MatrixType& matrix,
const FilterType& filter,
const DataType omega = DataType(1)) :
589 _colored_row_ptr(nullptr),
590 _rows_per_color(nullptr),
591 _inverse_row_ptr(nullptr),
594 if (_matrix.num_cols() != _matrix.num_rows())
596 XABORTM(
"Matrix is not square!");
600 explicit SSORPrecondWithBackend(
const String& ,
const PropertyMap* section,
601 const MatrixType& matrix,
const FilterType& filter) :
605 _colored_row_ptr(nullptr),
606 _rows_per_color(nullptr),
607 _inverse_row_ptr(nullptr),
610 if (_matrix.num_cols() != _matrix.num_rows())
612 XABORTM(
"Matrix is not square!");
616 auto omega_p = section->query(
"omega");
619 set_omega(DataType(std::stod(omega_p.first)));
624 virtual String name()
const override
636 virtual void set_omega(DataType omega)
override
642 virtual void init_symbolic()
override
647 Intern::cuda_sor_init_symbolic((
int)_matrix.num_rows(), (
int)_matrix.num_nzes(), a.get_r(), row_ptr.get_r(), col_idx.get_r(), _ncolors,
648 _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
651 virtual void done_symbolic()
override
653 Intern::cuda_sor_done_symbolic(_colored_row_ptr, _rows_per_color, _inverse_row_ptr);
656 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
658 XASSERTM(_matrix.num_rows() == vec_cor.size(),
"matrix / vector size mismatch!");
659 XASSERTM(_matrix.num_rows() == vec_def.size(),
"matrix / vector size mismatch!");
671 status |= Intern::cuda_ssor_forward_bcsr_apply<block_height_>((
int)vec_cor.size(), vx.get_w(),vb.get_r(), a.get_r(), col_idx.get_r(), _ncolors, _omega, _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
672 status |= Intern::cuda_ssor_backward_bcsr_apply<block_height_>((
int)vec_cor.size(), vx.get_w(), vb.get_r(), a.get_r(), col_idx.get_r(), _ncolors, _omega, _colored_row_ptr, _rows_per_color, _inverse_row_ptr);
675 vec_cor.scale(vec_cor, _omega * (2.0 - _omega));
677 this->_filter.filter_cor(vec_cor);
680 Statistics::add_time_precon(ts_stop.elapsed(ts_start));
689 template<PreferredBackend backend_,
typename Matrix_,
typename Filter_>
694 template<
typename DT_>
707 virtual Status apply(
typename Matrix_::VectorTypeL &,
const typename Matrix_::VectorTypeL &)
override
709 XABORTM(
"not implemented yet!");
712 virtual String name()
const override
714 XABORTM(
"not implemented yet!");
717 virtual void init_symbolic()
override
719 XABORTM(
"not implemented yet!");
722 virtual void done_symbolic()
override
724 XABORTM(
"not implemented yet!");
727 virtual void set_omega(
typename Matrix_::DataType )
override
729 XABORTM(
"not implemented yet!");
745 template<
typename Matrix_,
typename Filter_>
749 std::shared_ptr<SSORPrecondBase<Matrix_>> _impl;
752 typedef Matrix_ MatrixType;
753 typedef Filter_ FilterType;
754 typedef typename MatrixType::VectorTypeL VectorType;
755 typedef typename MatrixType::DataType DataType;
782 _impl = std::make_shared<SSORPrecondWithBackend<PreferredBackend::cuda, Matrix_, Filter_>>(matrix, filter, omega);
787 _impl = std::make_shared<SSORPrecondWithBackend<PreferredBackend::generic, Matrix_, Filter_>>(matrix, filter, omega);
816 _impl = std::make_shared<SSORPrecondWithBackend<PreferredBackend::cuda, Matrix_, Filter_>>(section_name, section, matrix, filter, omega);
821 _impl = std::make_shared<SSORPrecondWithBackend<PreferredBackend::generic, Matrix_, Filter_>>(section_name, section, matrix, filter, omega);
835 return _impl->name();
847 _impl->set_omega(omega);
850 virtual Status apply(VectorType& vec_cor,
const VectorType& vec_def)
override
852 return _impl->apply(vec_cor, vec_def);
857 _impl->init_symbolic();
862 _impl->done_symbolic();
884 template<
typename Matrix_,
typename Filter_>
886 const PreferredBackend backend,
const Matrix_& matrix,
const Filter_& filter,
887 const typename Matrix_::DataType omega =
typename Matrix_::DataType(1))
889 return std::make_shared<SSORPrecond<Matrix_, Filter_>>
890 (backend, matrix, filter, omega);
914 template<
typename Matrix_,
typename Filter_>
917 const Matrix_& matrix,
const Filter_& filter)
919 return std::make_shared<SSORPrecond<Matrix_, Filter_>>
920 (backend, section_name, section, matrix, filter);
#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.
CSR based blocked sparse matrix.
Index num_rows() const
Retrieve matrix row count.
IT_ IndexType
Our indextype.
DT_ DataType
Our datatype.
Intern::BCSRVectorHelper< DT_, IT_, block_height_ >::VectorType VectorTypeL
Compatible L-vector type.
Index num_nzes_raw() const
Returns the scalar matrix nonzero element count.
Index num_cols() const
Retrieve matrix column count.
Index num_cols() const
Retrieve matrix column count.
Index num_rows() const
Retrieve matrix row count.
Index num_nzes() const
Retrieve non zero element count.
Class for parser related errors.
A class organizing a tree of key-value pairs.
std::pair< String, bool > query(String key_path) const
Queries a value by its key path.
Inheritances inside sor_precond.hpp.
SSOR preconditioner implementation.
virtual void done_symbolic() override
Symbolic finalization method.
SSORPrecond(const PreferredBackend backend, const MatrixType &matrix, const FilterType &filter, DataType omega=DataType(1))
Constructor.
SSORPrecond(const PreferredBackend backend, const String §ion_name, const PropertyMap *section, const MatrixType &matrix, const FilterType &filter, DataType omega=DataType(1))
Constructor using a PropertyMap.
virtual ~SSORPrecond()
Empty virtual destructor.
virtual void init_symbolic() override
Symbolic initialization method.
virtual void set_omega(DataType omega)
Sets the damping parameter.
virtual String name() const override
Returns the name of the solver.
SolverBase< VectorType > BaseClass
Our base class.
SSORPrecondWithBackend(const MatrixType &matrix, const FilterType &filter, const DataType omega=DataType(1))
Constructor.
virtual String name() const override
Returns the name of the solver.
virtual void set_omega(DataType omega) override
Sets the damping parameter.
SSORPrecondWithBackend(const MatrixType &matrix, const FilterType &filter, const DataType omega=DataType(1))
Constructor.
virtual String name() const override
Returns the name of the solver.
virtual void set_omega(DataType omega) override
Sets the damping parameter.
Dummy class for not implemented specializations.
Polymorphic solver interface.
static void add_flops(Index flops)
Add an amount of flops to the global flop counter.
String class implementation.
Tiny Matrix class template.
CUDA_HOST_DEVICE Matrix & set_inverse(const Matrix< T_, m_, n_, sma_, sna_ > &a)
Sets this matrix to the inverse of another matrix.
std::shared_ptr< SSORPrecond< Matrix_, Filter_ > > new_ssor_precond(const PreferredBackend backend, const Matrix_ &matrix, const Filter_ &filter, const typename Matrix_::DataType omega=typename Matrix_::DataType(1))
Creates a new SSORPrecond solver object.
Status
Solver status return codes enumeration.
@ success
solving successful (convergence criterion fulfilled)
@ aborted
premature abort (solver aborted due to internal errors or preconditioner failure)
PreferredBackend
The backend that shall be used in all compute heavy calculations.
std::uint64_t Index
Index data type.