Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.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 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/math.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 
55 protected:
56  using absolute_type = remove_complex<ValueType>;
60  bool check_impl(uint8 stoppingId, bool setFinalized,
61  array<stopping_status>* stop_status, bool* one_changed,
62  const Criterion::Updater& updater) override;
63 
64  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
66  device_storage_{exec, 2}
67  {}
68 
69  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
70  const CriterionArgs& args,
71  absolute_type reduction_factor, mode baseline);
72 
73  remove_complex<ValueType> reduction_factor_{};
74  std::unique_ptr<NormVector> starting_tau_{};
75  std::unique_ptr<NormVector> u_dense_tau_{};
76  /* Contains device side: all_converged and one_changed booleans */
77  array<bool> device_storage_;
78 
79 private:
80  mode baseline_{mode::rhs_norm};
81  std::shared_ptr<const LinOp> system_matrix_{};
82  std::shared_ptr<const LinOp> b_{};
83  /* one/neg_one for residual computation */
84  std::shared_ptr<const Vector> one_{};
85  std::shared_ptr<const Vector> neg_one_{};
86 };
87 
88 
109 template <typename ValueType = default_precision>
110 class ResidualNorm : public ResidualNormBase<ValueType> {
111 public:
115 
117  {
122  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
123 
128  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
129  };
130  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
132 
133 protected:
134  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
135  : ResidualNormBase<ValueType>(exec)
136  {}
137 
138  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
139  : ResidualNormBase<ValueType>(
140  factory->get_executor(), args,
141  factory->get_parameters().reduction_factor,
142  factory->get_parameters().baseline),
143  parameters_{factory->get_parameters()}
144  {}
145 };
146 
147 
165 template <typename ValueType = default_precision>
166 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
167 public:
171 
173  {
178  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
179 
184  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
185  };
186  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
189 
190 protected:
191  // check_impl needs to be overwritten again since we focus on the implicit
192  // residual here
193  bool check_impl(uint8 stoppingId, bool setFinalized,
194  array<stopping_status>* stop_status, bool* one_changed,
195  const Criterion::Updater& updater) override;
196 
197  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
198  : ResidualNormBase<ValueType>(exec)
199  {}
200 
201  explicit ImplicitResidualNorm(const Factory* factory,
202  const CriterionArgs& args)
203  : ResidualNormBase<ValueType>(
204  factory->get_executor(), args,
205  factory->get_parameters().reduction_factor,
206  factory->get_parameters().baseline),
207  parameters_{factory->get_parameters()}
208  {}
209 };
210 
211 
212 // The following classes are deprecated, but they internally reference
213 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
214 // for the definition of these classes.
215 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
216 
217 
237 template <typename ValueType = default_precision>
238 class GKO_DEPRECATED(
239  "Please use the class ResidualNorm with the factory parameter baseline = "
240  "mode::initial_resnorm") ResidualNormReduction
241  : public ResidualNormBase<ValueType> {
242 public:
243  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
244  using NormVector = matrix::Dense<remove_complex<ValueType>>;
245  using Vector = matrix::Dense<ValueType>;
246 
247  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
248  {
252  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
253  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
254  };
255  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
256  Factory);
257  GKO_ENABLE_BUILD_METHOD(Factory);
258 
259 protected:
260  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
261  : ResidualNormBase<ValueType>(exec)
262  {}
263 
264  explicit ResidualNormReduction(const Factory* factory,
265  const CriterionArgs& args)
266  : ResidualNormBase<ValueType>(
267  factory->get_executor(), args,
268  factory->get_parameters().reduction_factor,
269  mode::initial_resnorm),
270  parameters_{factory->get_parameters()}
271  {}
272 };
273 
274 
293 template <typename ValueType = default_precision>
294 class GKO_DEPRECATED(
295  "Please use the class ResidualNorm with the factory parameter baseline = "
296  "mode::rhs_norm") RelativeResidualNorm
297  : public ResidualNormBase<ValueType> {
298 public:
299  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
300  using NormVector = matrix::Dense<remove_complex<ValueType>>;
301  using Vector = matrix::Dense<ValueType>;
302 
303  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
304  {
308  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
309  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
310  };
311  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
312  Factory);
313  GKO_ENABLE_BUILD_METHOD(Factory);
314 
315 protected:
316  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
317  : ResidualNormBase<ValueType>(exec)
318  {}
319 
320  explicit RelativeResidualNorm(const Factory* factory,
321  const CriterionArgs& args)
322  : ResidualNormBase<ValueType>(factory->get_executor(), args,
323  factory->get_parameters().tolerance,
324  mode::rhs_norm),
325  parameters_{factory->get_parameters()}
326  {}
327 };
328 
329 
347 template <typename ValueType = default_precision>
348 class GKO_DEPRECATED(
349  "Please use the class ResidualNorm with the factory parameter baseline = "
350  "mode::absolute") AbsoluteResidualNorm
351  : public ResidualNormBase<ValueType> {
352 public:
353  using NormVector = matrix::Dense<remove_complex<ValueType>>;
354  using Vector = matrix::Dense<ValueType>;
355 
356  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
357  {
361  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
362  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
363  };
364  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
365  Factory);
366  GKO_ENABLE_BUILD_METHOD(Factory);
367 
368 protected:
369  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
370  : ResidualNormBase<ValueType>(exec)
371  {}
372 
373  explicit AbsoluteResidualNorm(const Factory* factory,
374  const CriterionArgs& args)
375  : ResidualNormBase<ValueType>(factory->get_executor(), args,
376  factory->get_parameters().tolerance,
377  mode::absolute),
378  parameters_{factory->get_parameters()}
379  {}
380 };
381 
382 
383 GKO_END_DISABLE_DEPRECATION_WARNINGS
384 
385 
386 } // namespace stop
387 } // namespace gko
388 
389 
390 #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:137
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:20
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:27
gko::stop::ResidualNorm
The ResidualNorm class is a stopping criterion which stops the iteration process when the actual resi...
Definition: residual_norm.hpp:110
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:187
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:130
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:166
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:326
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:662