Ginkgo  Generated from pipelines/1554403166 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
criterion.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 EnableAbstractPolymorphicObject<Criterion> {
37 public:
55  class Updater {
56  friend class Criterion;
57 
58  public:
64  Updater(const Updater&) = delete;
65  Updater(Updater&&) = delete;
66  Updater& operator=(const Updater&) = delete;
67  Updater& operator=(Updater&&) = delete;
68 
73  bool check(uint8 stopping_id, bool set_finalized,
74  array<stopping_status>* stop_status, bool* one_changed) const
75  {
76  auto converged = parent_->check(stopping_id, set_finalized,
77  stop_status, one_changed, *this);
78  return converged;
79  }
80 
84 #define GKO_UPDATER_REGISTER_PARAMETER(_type, _name) \
85  const Updater& _name(_type const& value) const \
86  { \
87  _name##_ = value; \
88  return *this; \
89  } \
90  mutable _type _name##_ {}
91 #define GKO_UPDATER_REGISTER_PTR_PARAMETER(_type, _name) \
92  const Updater& _name(ptr_param<_type> value) const \
93  { \
94  _name##_ = value.get(); \
95  return *this; \
96  } \
97  mutable _type* _name##_ {}
98 
99  GKO_UPDATER_REGISTER_PARAMETER(size_type, num_iterations);
100  // ignore_residual_check default is false
101  GKO_UPDATER_REGISTER_PARAMETER(bool, ignore_residual_check);
102  GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, residual);
103  GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, residual_norm);
104  GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp,
105  implicit_sq_residual_norm);
106  GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, solution);
107 
108 #undef GKO_UPDATER_REGISTER_PTR_PARAMETER
109 #undef GKO_UPDATER_REGISTER_PARAMETER
110 
111  private:
112  Updater(Criterion* parent) : parent_{parent} {}
113 
114  Criterion* parent_;
115  };
116 
122  Updater update() { return {this}; }
123 
137  bool check(uint8 stopping_id, bool set_finalized,
138  array<stopping_status>* stop_status, bool* one_changed,
139  const Updater& updater)
140  {
141  this->template log<log::Logger::criterion_check_started>(
142  this, updater.num_iterations_, updater.residual_,
143  updater.residual_norm_, updater.solution_, stopping_id,
144  set_finalized);
145  auto all_converged = this->check_impl(
146  stopping_id, set_finalized, stop_status, one_changed, updater);
147  this->template log<log::Logger::criterion_check_completed>(
148  this, updater.num_iterations_, updater.residual_,
149  updater.residual_norm_, updater.implicit_sq_residual_norm_,
150  updater.solution_, stopping_id, set_finalized, stop_status,
151  *one_changed, all_converged);
152  return all_converged;
153  }
154 
155 protected:
172  virtual bool check_impl(uint8 stopping_id, bool set_finalized,
173  array<stopping_status>* stop_status,
174  bool* one_changed, const Updater& updater) = 0;
175 
186  void set_all_statuses(uint8 stopping_id, bool set_finalized,
187  array<stopping_status>* stop_status);
188 
189  explicit Criterion(std::shared_ptr<const gko::Executor> exec)
191  {}
192 };
193 
194 
206  std::shared_ptr<const LinOp> system_matrix;
207  std::shared_ptr<const LinOp> b;
208  const LinOp* x;
209  const LinOp* initial_residual;
210 
211 
212  CriterionArgs(std::shared_ptr<const LinOp> system_matrix,
213  std::shared_ptr<const LinOp> b, const LinOp* x,
214  const LinOp* initial_residual = nullptr)
215  : system_matrix{system_matrix},
216  b{b},
217  x{x},
218  initial_residual{initial_residual}
219  {}
220 };
221 
222 
227 
228 
244 template <typename ConcreteFactory, typename ConcreteCriterion,
245  typename ParametersType, typename PolymorphicBase = CriterionFactory>
247  EnableDefaultFactory<ConcreteFactory, ConcreteCriterion, ParametersType,
248  PolymorphicBase>;
249 
250 
275 #define GKO_ENABLE_CRITERION_FACTORY(_criterion, _parameters_name, \
276  _factory_name) \
277 public: \
278  const _parameters_name##_type& get_##_parameters_name() const \
279  { \
280  return _parameters_name##_; \
281  } \
282  \
283  class _factory_name \
284  : public ::gko::stop::EnableDefaultCriterionFactory< \
285  _factory_name, _criterion, _parameters_name##_type> { \
286  friend class ::gko::EnablePolymorphicObject< \
287  _factory_name, ::gko::stop::CriterionFactory>; \
288  friend class ::gko::enable_parameters_type<_parameters_name##_type, \
289  _factory_name>; \
290  explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec) \
291  : ::gko::stop::EnableDefaultCriterionFactory< \
292  _factory_name, _criterion, _parameters_name##_type>( \
293  std::move(exec)) \
294  {} \
295  explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec, \
296  const _parameters_name##_type& parameters) \
297  : ::gko::stop::EnableDefaultCriterionFactory< \
298  _factory_name, _criterion, _parameters_name##_type>( \
299  std::move(exec), parameters) \
300  {} \
301  }; \
302  friend ::gko::stop::EnableDefaultCriterionFactory< \
303  _factory_name, _criterion, _parameters_name##_type>; \
304  \
305 private: \
306  _parameters_name##_type _parameters_name##_; \
307  \
308 public: \
309  static_assert(true, \
310  "This assert is used to counter the false positive extra " \
311  "semi-colon warnings")
312 
313 
314 } // namespace stop
315 } // namespace gko
316 
317 
318 #endif // GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
gko::stop::CriterionArgs
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition: criterion.hpp:205
gko::stop::Criterion::Updater::check
bool check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status, bool *one_changed) const
Calls the parent Criterion object's check method.
Definition: criterion.hpp:73
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:115
gko::LinOp
Definition: lin_op.hpp:117
gko::AbstractFactory
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:45
gko::EnableAbstractPolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:345
gko::stop::Criterion::update
Updater update()
Returns the updater object.
Definition: criterion.hpp:122
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:86
gko::EnableDefaultFactory
This mixin provides a default implementation of a concrete factory.
Definition: abstract_factory.hpp:124
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
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::stop::Criterion::check
bool check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status, bool *one_changed, const Updater &updater)
This checks whether convergence was reached for a certain criterion.
Definition: criterion.hpp:137
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:55