Ginkgo  Generated from pipelines/1554403166 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  // workspace for reduction
86  mutable gko::array<char> reduction_tmp_;
87 };
88 
89 
110 template <typename ValueType = default_precision>
111 class ResidualNorm : public ResidualNormBase<ValueType> {
112 public:
116 
118  {
123  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
124 
129  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
130  };
131  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
133 
134 protected:
135  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
136  : ResidualNormBase<ValueType>(exec)
137  {}
138 
139  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
140  : ResidualNormBase<ValueType>(
141  factory->get_executor(), args,
142  factory->get_parameters().reduction_factor,
143  factory->get_parameters().baseline),
144  parameters_{factory->get_parameters()}
145  {}
146 };
147 
148 
166 template <typename ValueType = default_precision>
167 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
168 public:
172 
174  {
179  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
180 
185  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
186  };
187  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
190 
191 protected:
192  // check_impl needs to be overwritten again since we focus on the implicit
193  // residual here
194  bool check_impl(uint8 stoppingId, bool setFinalized,
195  array<stopping_status>* stop_status, bool* one_changed,
196  const Criterion::Updater& updater) override;
197 
198  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
199  : ResidualNormBase<ValueType>(exec)
200  {}
201 
202  explicit ImplicitResidualNorm(const Factory* factory,
203  const CriterionArgs& args)
204  : ResidualNormBase<ValueType>(
205  factory->get_executor(), args,
206  factory->get_parameters().reduction_factor,
207  factory->get_parameters().baseline),
208  parameters_{factory->get_parameters()}
209  {}
210 };
211 
212 
213 // The following classes are deprecated, but they internally reference
214 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
215 // for the definition of these classes.
216 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
217 
218 
238 template <typename ValueType = default_precision>
239 class GKO_DEPRECATED(
240  "Please use the class ResidualNorm with the factory parameter baseline = "
241  "mode::initial_resnorm") ResidualNormReduction
242  : public ResidualNormBase<ValueType> {
243 public:
244  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
245  using NormVector = matrix::Dense<remove_complex<ValueType>>;
246  using Vector = matrix::Dense<ValueType>;
247 
248  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
249  {
253  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
254  reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
255  };
256  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
257  Factory);
258  GKO_ENABLE_BUILD_METHOD(Factory);
259 
260 protected:
261  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
262  : ResidualNormBase<ValueType>(exec)
263  {}
264 
265  explicit ResidualNormReduction(const Factory* factory,
266  const CriterionArgs& args)
267  : ResidualNormBase<ValueType>(
268  factory->get_executor(), args,
269  factory->get_parameters().reduction_factor,
270  mode::initial_resnorm),
271  parameters_{factory->get_parameters()}
272  {}
273 };
274 
275 
294 template <typename ValueType = default_precision>
295 class GKO_DEPRECATED(
296  "Please use the class ResidualNorm with the factory parameter baseline = "
297  "mode::rhs_norm") RelativeResidualNorm
298  : public ResidualNormBase<ValueType> {
299 public:
300  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
301  using NormVector = matrix::Dense<remove_complex<ValueType>>;
302  using Vector = matrix::Dense<ValueType>;
303 
304  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
305  {
309  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
310  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
311  };
312  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
313  Factory);
314  GKO_ENABLE_BUILD_METHOD(Factory);
315 
316 protected:
317  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
318  : ResidualNormBase<ValueType>(exec)
319  {}
320 
321  explicit RelativeResidualNorm(const Factory* factory,
322  const CriterionArgs& args)
323  : ResidualNormBase<ValueType>(factory->get_executor(), args,
324  factory->get_parameters().tolerance,
325  mode::rhs_norm),
326  parameters_{factory->get_parameters()}
327  {}
328 };
329 
330 
348 template <typename ValueType = default_precision>
349 class GKO_DEPRECATED(
350  "Please use the class ResidualNorm with the factory parameter baseline = "
351  "mode::absolute") AbsoluteResidualNorm
352  : public ResidualNormBase<ValueType> {
353 public:
354  using NormVector = matrix::Dense<remove_complex<ValueType>>;
355  using Vector = matrix::Dense<ValueType>;
356 
357  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
358  {
362  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
363  tolerance, static_cast<remove_complex<ValueType>>(1e-15));
364  };
365  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
366  Factory);
367  GKO_ENABLE_BUILD_METHOD(Factory);
368 
369 protected:
370  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
371  : ResidualNormBase<ValueType>(exec)
372  {}
373 
374  explicit AbsoluteResidualNorm(const Factory* factory,
375  const CriterionArgs& args)
376  : ResidualNormBase<ValueType>(factory->get_executor(), args,
377  factory->get_parameters().tolerance,
378  mode::absolute),
379  parameters_{factory->get_parameters()}
380  {}
381 };
382 
383 
384 GKO_END_DISABLE_DEPRECATION_WARNINGS
385 
386 
387 } // namespace stop
388 } // namespace gko
389 
390 
391 #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:111
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:188
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:131
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:167
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