Ginkgo  Generated from pipelines/2222355941 branch based on develop. Ginkgo version 1.12.0
A numerical linear algebra library targeting many-core architectures
residual_norm.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
6 #define GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
7 
8 
9 #include <limits>
10 
11 #include <ginkgo/core/base/array.hpp>
12 #include <ginkgo/core/base/math.hpp>
13 #include <ginkgo/core/base/types.hpp>
14 #include <ginkgo/core/base/utils.hpp>
15 #include <ginkgo/core/matrix/dense.hpp>
16 #include <ginkgo/core/stop/criterion.hpp>
17 
18 
19 namespace gko {
20 namespace stop {
21 
22 
37 enum class mode { absolute, initial_resnorm, rhs_norm };
38 
39 
49 template <typename ValueType>
51  : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
52  friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
53  Criterion>;
54  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
55 
56 protected:
57  using absolute_type = remove_complex<ValueType>;
61  bool check_impl(uint8 stoppingId, bool setFinalized,
62  array<stopping_status>* stop_status, bool* one_changed,
63  const Criterion::Updater& updater) override;
64 
65  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
67  device_storage_{exec, 2}
68  {}
69 
70  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
71  const CriterionArgs& args,
72  absolute_type reduction_factor, mode baseline);
73 
74  remove_complex<ValueType> reduction_factor_{};
75  std::unique_ptr<NormVector> starting_tau_{};
76  std::unique_ptr<NormVector> u_dense_tau_{};
77  /* Contains device side: all_converged and one_changed booleans */
78  array<bool> device_storage_;
79 
80 private:
81  mode baseline_{mode::rhs_norm};
82  std::shared_ptr<const LinOp> system_matrix_{};
83  std::shared_ptr<const LinOp> b_{};
84  /* one/neg_one for residual computation */
85  std::shared_ptr<const Vector> one_{};
86  std::shared_ptr<const Vector> neg_one_{};
87  // workspace for reduction
88  mutable gko::array<char> reduction_tmp_;
89 };
90 
91 
112 template <typename ValueType = default_precision>
113 class ResidualNorm : public ResidualNormBase<ValueType> {
114 public:
118 
120  {
124  remove_complex<ValueType> reduction_factor{
125  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
126 
127  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
128  {
129  this->reduction_factor = value;
130  return *this;
131  }
132 
137  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
138  };
139  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
141 
142 protected:
143  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
144  : ResidualNormBase<ValueType>(exec)
145  {}
146 
147  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
148  : ResidualNormBase<ValueType>(
149  factory->get_executor(), args,
150  factory->get_parameters().reduction_factor,
151  factory->get_parameters().baseline),
152  parameters_{factory->get_parameters()}
153  {}
154 };
155 
156 
174 template <typename ValueType = default_precision>
175 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
176 public:
180 
182  {
186  remove_complex<ValueType> reduction_factor{
187  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
188 
189  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
190  {
191  this->reduction_factor = value;
192  return *this;
193  }
194 
199  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
200  };
201  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
204 
205 protected:
206  // check_impl needs to be overwritten again since we focus on the implicit
207  // residual here
208  bool check_impl(uint8 stoppingId, bool setFinalized,
209  array<stopping_status>* stop_status, bool* one_changed,
210  const Criterion::Updater& updater) override;
211 
212  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
213  : ResidualNormBase<ValueType>(exec)
214  {}
215 
216  explicit ImplicitResidualNorm(const Factory* factory,
217  const CriterionArgs& args)
218  : ResidualNormBase<ValueType>(
219  factory->get_executor(), args,
220  factory->get_parameters().reduction_factor,
221  factory->get_parameters().baseline),
222  parameters_{factory->get_parameters()}
223  {}
224 };
225 
226 
258 deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
259  double tolerance);
260 
262 deferred_factory_parameter<CriterionFactory> relative_residual_norm(
263  double tolerance);
264 
266 deferred_factory_parameter<CriterionFactory> initial_residual_norm(
267  double tolerance);
268 
270 deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
271  double tolerance);
272 
274 deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
275  double tolerance);
276 
278 deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
279  double tolerance);
280 
281 
282 // The following classes are deprecated, but they internally reference
283 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
284 // for the definition of these classes.
285 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
286 
287 
307 template <typename ValueType = default_precision>
308 class GKO_DEPRECATED(
309  "Please use the class ResidualNorm with the factory parameter baseline = "
310  "mode::initial_resnorm") ResidualNormReduction
311  : public ResidualNormBase<ValueType> {
312 public:
313  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
314  using NormVector = matrix::Dense<remove_complex<ValueType>>;
315  using Vector = matrix::Dense<ValueType>;
316 
317  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
318  {
322  remove_complex<ValueType> reduction_factor{
323  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
324 
325  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
326  {
327  this->reduction_factor = value;
328  return *this;
329  }
330  };
331  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
332  Factory);
333  GKO_ENABLE_BUILD_METHOD(Factory);
334 
335 protected:
336  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
337  : ResidualNormBase<ValueType>(exec)
338  {}
339 
340  explicit ResidualNormReduction(const Factory* factory,
341  const CriterionArgs& args)
342  : ResidualNormBase<ValueType>(
343  factory->get_executor(), args,
344  factory->get_parameters().reduction_factor,
345  mode::initial_resnorm),
346  parameters_{factory->get_parameters()}
347  {}
348 };
349 
350 
369 template <typename ValueType = default_precision>
370 class GKO_DEPRECATED(
371  "Please use the class ResidualNorm with the factory parameter baseline = "
372  "mode::rhs_norm") RelativeResidualNorm
373  : public ResidualNormBase<ValueType> {
374 public:
375  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
376  using NormVector = matrix::Dense<remove_complex<ValueType>>;
377  using Vector = matrix::Dense<ValueType>;
378 
379  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
380  {
384  remove_complex<ValueType> tolerance{
385  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
386 
387 
388  parameters_type& with_tolerance(remove_complex<ValueType> value)
389  {
390  this->tolerance = value;
391  return *this;
392  }
393  };
394  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
395  Factory);
396  GKO_ENABLE_BUILD_METHOD(Factory);
397 
398 protected:
399  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
400  : ResidualNormBase<ValueType>(exec)
401  {}
402 
403  explicit RelativeResidualNorm(const Factory* factory,
404  const CriterionArgs& args)
405  : ResidualNormBase<ValueType>(factory->get_executor(), args,
406  factory->get_parameters().tolerance,
407  mode::rhs_norm),
408  parameters_{factory->get_parameters()}
409  {}
410 };
411 
412 
430 template <typename ValueType = default_precision>
431 class GKO_DEPRECATED(
432  "Please use the class ResidualNorm with the factory parameter baseline = "
433  "mode::absolute") AbsoluteResidualNorm
434  : public ResidualNormBase<ValueType> {
435 public:
436  using NormVector = matrix::Dense<remove_complex<ValueType>>;
437  using Vector = matrix::Dense<ValueType>;
438 
439  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
440  {
444  remove_complex<ValueType> tolerance{
445  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
446 
447  parameters_type& with_tolerance(remove_complex<ValueType> value)
448  {
449  this->tolerance = value;
450  return *this;
451  }
452  };
453  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
454  Factory);
455  GKO_ENABLE_BUILD_METHOD(Factory);
456 
457 protected:
458  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
459  : ResidualNormBase<ValueType>(exec)
460  {}
461 
462  explicit AbsoluteResidualNorm(const Factory* factory,
463  const CriterionArgs& args)
464  : ResidualNormBase<ValueType>(factory->get_executor(), args,
465  factory->get_parameters().tolerance,
466  mode::absolute),
467  parameters_{factory->get_parameters()}
468  {}
469 };
470 
471 
472 GKO_END_DISABLE_DEPRECATION_WARNINGS
473 
474 
475 } // namespace stop
476 } // namespace gko
477 
478 
479 #endif // GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
gko::stop::CriterionArgs
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition: criterion.hpp:205
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:119
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::stop::initial_residual_norm
deferred_factory_parameter< CriterionFactory > initial_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
gko::stop::absolute_residual_norm
deferred_factory_parameter< CriterionFactory > absolute_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
gko::stop::absolute_implicit_residual_norm
deferred_factory_parameter< CriterionFactory > absolute_implicit_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
gko::stop::relative_residual_norm
deferred_factory_parameter< CriterionFactory > relative_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: abstract_factory.hpp:445
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
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::ResidualNorm
The ResidualNorm class is a stopping criterion which stops the iteration process when the actual resi...
Definition: residual_norm.hpp:113
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:202
gko::stop::initial_implicit_residual_norm
deferred_factory_parameter< CriterionFactory > initial_implicit_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
gko::stop::Criterion
The Criterion class is a base class for all stopping criteria.
Definition: criterion.hpp:36
gko::stop::mode
mode
The mode for the residual norm criterion.
Definition: residual_norm.hpp:37
gko::stop::ResidualNorm::Factory
Definition: residual_norm.hpp:139
gko::stop::Criterion::Updater
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition: criterion.hpp:55
gko::stop::ResidualNorm::parameters_type
Definition: residual_norm.hpp:119
gko::stop::ImplicitResidualNorm::parameters_type
Definition: residual_norm.hpp:181
GKO_ENABLE_BUILD_METHOD
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition: abstract_factory.hpp:394
GKO_CREATE_FACTORY_PARAMETERS
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition: abstract_factory.hpp:280
gko::stop::ImplicitResidualNorm
The ImplicitResidualNorm class is a stopping criterion which stops the iteration process when the imp...
Definition: residual_norm.hpp:175
gko::stop::ResidualNormBase
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition: residual_norm.hpp:50
gko::remove_complex
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition: math.hpp:264
gko::stop::relative_implicit_residual_norm
deferred_factory_parameter< CriterionFactory > relative_implicit_residual_norm(double tolerance)
Creates the precursor to a ResidualNorm stopping criterion factory, to be used in conjunction with ....
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667