Ginkgo  Generated from pipelines/3136675824 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
criterion.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
6 #define GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
7 
8 
9 #include <ginkgo/core/base/abstract_factory.hpp>
10 #include <ginkgo/core/base/array.hpp>
11 #include <ginkgo/core/base/executor.hpp>
12 #include <ginkgo/core/base/lin_op.hpp>
13 #include <ginkgo/core/base/polymorphic_object.hpp>
14 #include <ginkgo/core/base/utils.hpp>
15 #include <ginkgo/core/log/logger.hpp>
16 #include <ginkgo/core/stop/stopping_status.hpp>
17 
18 
19 namespace gko {
25 namespace stop {
26 
27 
36 class Criterion : public PolymorphicObject {
37 public:
59  class Updater {
60  friend class Criterion;
61 
62  public:
68  Updater(const Updater&) = delete;
69  Updater(Updater&&) = delete;
70  Updater& operator=(const Updater&) = delete;
71  Updater& operator=(Updater&&) = delete;
72 
78  stopping_indicators check(uint8 stopping_id, bool set_finalized,
79  array<stopping_status>* stop_status) const
80  {
81  return parent_->check(stopping_id, set_finalized, stop_status,
82  *this);
83  }
84 
88 #define GKO_UPDATER_REGISTER_PARAMETER(_type, _name) \
89  const Updater& _name(_type const& value) const \
90  { \
91  _name##_ = value; \
92  return *this; \
93  } \
94  mutable _type _name##_ {}
95 #define GKO_UPDATER_REGISTER_PTR_PARAMETER(_type, _name) \
96  const Updater& _name(ptr_param<_type> value) const \
97  { \
98  _name##_ = value.get(); \
99  return *this; \
100  } \
101  mutable _type* _name##_ {}
102 
103  GKO_UPDATER_REGISTER_PARAMETER(size_type, num_iterations);
104  // ignore_residual_check default is false
105  GKO_UPDATER_REGISTER_PARAMETER(bool, ignore_residual_check);
106  GKO_UPDATER_REGISTER_PTR_PARAMETER(const AbstractMultiVector, residual);
107  GKO_UPDATER_REGISTER_PTR_PARAMETER(const AbstractMultiVector,
108  residual_norm);
109  GKO_UPDATER_REGISTER_PTR_PARAMETER(const AbstractMultiVector,
110  implicit_sq_residual_norm);
111  GKO_UPDATER_REGISTER_PTR_PARAMETER(const AbstractMultiVector, solution);
112 
113 #undef GKO_UPDATER_REGISTER_PTR_PARAMETER
114 #undef GKO_UPDATER_REGISTER_PARAMETER
115 
116  private:
117  Updater(Criterion* parent) : parent_{parent} {}
118 
119  Criterion* parent_;
120  };
121 
127  Updater update() { return {this}; }
128 
142  stopping_indicators check(uint8 stopping_id, bool set_finalized,
143  array<stopping_status>* stop_status,
144  const Updater& updater)
145  {
146  this->template log<log::Logger::criterion_check_started>(
147  this, updater.num_iterations_, updater.residual_,
148  updater.residual_norm_, updater.solution_, stopping_id,
149  set_finalized);
150  auto indicators =
151  this->check_impl(stopping_id, set_finalized, stop_status, updater);
152  this->template log<log::Logger::criterion_check_completed>(
153  this, updater.num_iterations_, updater.residual_,
154  updater.residual_norm_, updater.implicit_sq_residual_norm_,
155  updater.solution_, stopping_id, set_finalized, stop_status,
156  indicators.one_changed, indicators.all_converged);
157  return indicators;
158  }
159 
160 protected:
177  virtual stopping_indicators check_impl(uint8 stopping_id,
178  bool set_finalized,
179  array<stopping_status>* stop_status,
180  const Updater& updater) = 0;
181 
192  void set_all_statuses(uint8 stopping_id, bool set_finalized,
193  array<stopping_status>* stop_status);
194 
195  explicit Criterion(std::shared_ptr<const gko::Executor> exec)
196  : PolymorphicObject(exec)
197  {}
198 };
199 
200 
212  std::shared_ptr<const LinOp> system_matrix;
213  std::shared_ptr<const AbstractMultiVector> b;
214  const AbstractMultiVector* x;
215  const AbstractMultiVector* initial_residual;
216 
217 
218  CriterionArgs(std::shared_ptr<const LinOp> system_matrix,
219  std::shared_ptr<const AbstractMultiVector> b,
220  const AbstractMultiVector* x,
221  const AbstractMultiVector* initial_residual = nullptr)
222  : system_matrix{system_matrix},
223  b{b},
224  x{x},
225  initial_residual{initial_residual}
226  {}
227 };
228 
229 
234 
235 
251 template <typename ConcreteFactory, typename ConcreteCriterion,
252  typename ParametersType, typename PolymorphicBase = CriterionFactory>
254  EnableDefaultFactory<ConcreteFactory, ConcreteCriterion, ParametersType,
255  PolymorphicBase>;
256 
257 
282 #define GKO_ENABLE_CRITERION_FACTORY(_criterion, _parameters_name, \
283  _factory_name) \
284 public: \
285  const _parameters_name##_type& get_##_parameters_name() const \
286  { \
287  return _parameters_name##_; \
288  } \
289  \
290  class _factory_name \
291  : public ::gko::stop::EnableDefaultCriterionFactory< \
292  _factory_name, _criterion, _parameters_name##_type> { \
293  friend class ::gko::enable_parameters_type<_parameters_name##_type, \
294  _factory_name>; \
295  explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec) \
296  : ::gko::stop::EnableDefaultCriterionFactory< \
297  _factory_name, _criterion, _parameters_name##_type>( \
298  std::move(exec)) \
299  {} \
300  explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec, \
301  const _parameters_name##_type& parameters) \
302  : ::gko::stop::EnableDefaultCriterionFactory< \
303  _factory_name, _criterion, _parameters_name##_type>( \
304  std::move(exec), parameters) \
305  {} \
306  }; \
307  friend ::gko::stop::EnableDefaultCriterionFactory< \
308  _factory_name, _criterion, _parameters_name##_type>; \
309  \
310 private: \
311  _parameters_name##_type _parameters_name##_; \
312  \
313 public: \
314  static_assert(true, \
315  "This assert is used to counter the false positive extra " \
316  "semi-colon warnings")
317 
318 
319 } // namespace stop
320 } // namespace gko
321 
322 
323 #endif // GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
gko::stop::CriterionArgs
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition: criterion.hpp:211
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:131
gko::AbstractFactory
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:77
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:46
gko::stop::Criterion::update
Updater update()
Returns the updater object.
Definition: criterion.hpp:127
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::EnableDefaultFactory
This mixin provides a default implementation of a concrete factory.
Definition: abstract_factory.hpp:154
gko::stop::Criterion::Updater::Updater
Updater(const Updater &)=delete
Prevent copying and moving the object This is to enforce the use of argument passing and calling chec...
gko::stopping_indicators
The result of a stopping criterion check over all vectors.
Definition: stopping_status.hpp:161
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:26
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:36
gko::stop::Criterion::Updater
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition: criterion.hpp:59
gko::stop::Criterion::check
stopping_indicators check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status, const Updater &updater)
This checks whether convergence was reached for a certain criterion.
Definition: criterion.hpp:142
gko::stop::Criterion::Updater::check
stopping_indicators check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status) const
Calls the parent Criterion object's check method.
Definition: criterion.hpp:78
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81