Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
residual_norm.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 <type_traits>
10 
11 #include <ginkgo/core/base/array.hpp>
12 #include <ginkgo/core/base/math.hpp>
13 #include <ginkgo/core/base/utils.hpp>
14 #include <ginkgo/core/matrix/dense.hpp>
15 #include <ginkgo/core/stop/criterion.hpp>
16 
17 
18 namespace gko {
19 namespace stop {
20 
21 
36 enum class mode { absolute, initial_resnorm, rhs_norm };
37 
38 
48 template <typename ValueType>
50  : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
51  friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
52  Criterion>;
53 
54 protected:
55  using absolute_type = remove_complex<ValueType>;
59  bool check_impl(uint8 stoppingId, bool setFinalized,
60  array<stopping_status>* stop_status, bool* one_changed,
61  const Criterion::Updater& updater) override;
62 
63  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
65  device_storage_{exec, 2}
66  {}
67 
68  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
69  const CriterionArgs& args,
70  absolute_type reduction_factor, mode baseline);
71 
72  remove_complex<ValueType> reduction_factor_{};
73  std::unique_ptr<NormVector> starting_tau_{};
74  std::unique_ptr<NormVector> u_dense_tau_{};
75  /* Contains device side: all_converged and one_changed booleans */
76  array<bool> device_storage_;
77 
78 private:
79  mode baseline_{mode::rhs_norm};
80  std::shared_ptr<const LinOp> system_matrix_{};
81  std::shared_ptr<const LinOp> b_{};
82  /* one/neg_one for residual computation */
83  std::shared_ptr<const Vector> one_{};
84  std::shared_ptr<const Vector> neg_one_{};
85 };
86 
87 
108 template <typename ValueType = default_precision>
109 class ResidualNorm : public ResidualNormBase<ValueType> {
110 public:
114 
116  {
121  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
122 
127  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
128  };
129  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
131 
132 protected:
133  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
134  : ResidualNormBase<ValueType>(exec)
135  {}
136 
137  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
138  : ResidualNormBase<ValueType>(
139  factory->get_executor(), args,
140  factory->get_parameters().reduction_factor,
141  factory->get_parameters().baseline),
142  parameters_{factory->get_parameters()}
143  {}
144 };
145 
146 
164 template <typename ValueType = default_precision>
165 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
166 public:
170 
172  {
177  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
178 
183  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
184  };
185  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
188 
189 protected:
190  // check_impl needs to be overwritten again since we focus on the implicit
191  // residual here
192  bool check_impl(uint8 stoppingId, bool setFinalized,
193  array<stopping_status>* stop_status, bool* one_changed,
194  const Criterion::Updater& updater) override;
195 
196  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
197  : ResidualNormBase<ValueType>(exec)
198  {}
199 
200  explicit ImplicitResidualNorm(const Factory* factory,
201  const CriterionArgs& args)
202  : ResidualNormBase<ValueType>(
203  factory->get_executor(), args,
204  factory->get_parameters().reduction_factor,
205  factory->get_parameters().baseline),
206  parameters_{factory->get_parameters()}
207  {}
208 };
209 
210 
211 // The following classes are deprecated, but they internally reference
212 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
213 // for the definition of these classes.
214 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
215 
216 
236 template <typename ValueType = default_precision>
237 class GKO_DEPRECATED(
238  "Please use the class ResidualNorm with the factory parameter baseline = "
239  "mode::initial_resnorm") ResidualNormReduction
240  : public ResidualNormBase<ValueType> {
241 public:
242  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
243  using NormVector = matrix::Dense<remove_complex<ValueType>>;
244  using Vector = matrix::Dense<ValueType>;
245 
246  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
247  {
251  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
252  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
253  };
254  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
255  Factory);
256  GKO_ENABLE_BUILD_METHOD(Factory);
257 
258 protected:
259  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
260  : ResidualNormBase<ValueType>(exec)
261  {}
262 
263  explicit ResidualNormReduction(const Factory* factory,
264  const CriterionArgs& args)
265  : ResidualNormBase<ValueType>(
266  factory->get_executor(), args,
267  factory->get_parameters().reduction_factor,
268  mode::initial_resnorm),
269  parameters_{factory->get_parameters()}
270  {}
271 };
272 
273 
292 template <typename ValueType = default_precision>
293 class GKO_DEPRECATED(
294  "Please use the class ResidualNorm with the factory parameter baseline = "
295  "mode::rhs_norm") RelativeResidualNorm
296  : public ResidualNormBase<ValueType> {
297 public:
298  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
299  using NormVector = matrix::Dense<remove_complex<ValueType>>;
300  using Vector = matrix::Dense<ValueType>;
301 
302  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
303  {
307  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
308  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
309  };
310  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
311  Factory);
312  GKO_ENABLE_BUILD_METHOD(Factory);
313 
314 protected:
315  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
316  : ResidualNormBase<ValueType>(exec)
317  {}
318 
319  explicit RelativeResidualNorm(const Factory* factory,
320  const CriterionArgs& args)
321  : ResidualNormBase<ValueType>(factory->get_executor(), args,
322  factory->get_parameters().tolerance,
323  mode::rhs_norm),
324  parameters_{factory->get_parameters()}
325  {}
326 };
327 
328 
346 template <typename ValueType = default_precision>
347 class GKO_DEPRECATED(
348  "Please use the class ResidualNorm with the factory parameter baseline = "
349  "mode::absolute") AbsoluteResidualNorm
350  : public ResidualNormBase<ValueType> {
351 public:
352  using NormVector = matrix::Dense<remove_complex<ValueType>>;
353  using Vector = matrix::Dense<ValueType>;
354 
355  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
356  {
360  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
361  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
362  };
363  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
364  Factory);
365  GKO_ENABLE_BUILD_METHOD(Factory);
366 
367 protected:
368  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
369  : ResidualNormBase<ValueType>(exec)
370  {}
371 
372  explicit AbsoluteResidualNorm(const Factory* factory,
373  const CriterionArgs& args)
374  : ResidualNormBase<ValueType>(factory->get_executor(), args,
375  factory->get_parameters().tolerance,
376  mode::absolute),
377  parameters_{factory->get_parameters()}
378  {}
379 };
380 
381 
382 GKO_END_DISABLE_DEPRECATION_WARNINGS
383 
384 
385 } // namespace stop
386 } // namespace gko
387 
388 
389 #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:115
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:19
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:109
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:186
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:36
gko::stop::ResidualNorm::Factory
Definition: residual_norm.hpp:129
gko::stop::Criterion::Updater
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition: criterion.hpp:55
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:165
gko::stop::ResidualNormBase
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition: residual_norm.hpp:49
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:325
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:661