Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.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 <limits>
10 #include <type_traits>
11 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/math.hpp>
14 #include <ginkgo/core/base/types.hpp>
15 #include <ginkgo/core/base/utils.hpp>
16 #include <ginkgo/core/matrix/dense.hpp>
17 #include <ginkgo/core/stop/criterion.hpp>
18 
19 
20 namespace gko {
21 namespace stop {
22 
23 
38 enum class mode { absolute, initial_resnorm, rhs_norm };
39 
40 
50 template <typename ValueType>
52  : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
53  friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
54  Criterion>;
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  {
125  reduction_factor,
126  5 * std::numeric_limits<remove_complex<ValueType>>::epsilon());
127 
132  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
133  };
134  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
136 
137 protected:
138  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
139  : ResidualNormBase<ValueType>(exec)
140  {}
141 
142  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
143  : ResidualNormBase<ValueType>(
144  factory->get_executor(), args,
145  factory->get_parameters().reduction_factor,
146  factory->get_parameters().baseline),
147  parameters_{factory->get_parameters()}
148  {}
149 };
150 
151 
169 template <typename ValueType = default_precision>
170 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
171 public:
175 
177  {
182  reduction_factor,
183  5 * std::numeric_limits<remove_complex<ValueType>>::epsilon());
184 
189  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
190  };
191  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
194 
195 protected:
196  // check_impl needs to be overwritten again since we focus on the implicit
197  // residual here
198  bool check_impl(uint8 stoppingId, bool setFinalized,
199  array<stopping_status>* stop_status, bool* one_changed,
200  const Criterion::Updater& updater) override;
201 
202  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
203  : ResidualNormBase<ValueType>(exec)
204  {}
205 
206  explicit ImplicitResidualNorm(const Factory* factory,
207  const CriterionArgs& args)
208  : ResidualNormBase<ValueType>(
209  factory->get_executor(), args,
210  factory->get_parameters().reduction_factor,
211  factory->get_parameters().baseline),
212  parameters_{factory->get_parameters()}
213  {}
214 };
215 
216 
217 // The following classes are deprecated, but they internally reference
218 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
219 // for the definition of these classes.
220 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
221 
222 
242 template <typename ValueType = default_precision>
243 class GKO_DEPRECATED(
244  "Please use the class ResidualNorm with the factory parameter baseline = "
245  "mode::initial_resnorm") ResidualNormReduction
246  : public ResidualNormBase<ValueType> {
247 public:
248  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
249  using NormVector = matrix::Dense<remove_complex<ValueType>>;
250  using Vector = matrix::Dense<ValueType>;
251 
252  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
253  {
257  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
258  reduction_factor,
259  5 * std::numeric_limits<remove_complex<ValueType>>::epsilon());
260  };
261  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
262  Factory);
263  GKO_ENABLE_BUILD_METHOD(Factory);
264 
265 protected:
266  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
267  : ResidualNormBase<ValueType>(exec)
268  {}
269 
270  explicit ResidualNormReduction(const Factory* factory,
271  const CriterionArgs& args)
272  : ResidualNormBase<ValueType>(
273  factory->get_executor(), args,
274  factory->get_parameters().reduction_factor,
275  mode::initial_resnorm),
276  parameters_{factory->get_parameters()}
277  {}
278 };
279 
280 
299 template <typename ValueType = default_precision>
300 class GKO_DEPRECATED(
301  "Please use the class ResidualNorm with the factory parameter baseline = "
302  "mode::rhs_norm") RelativeResidualNorm
303  : public ResidualNormBase<ValueType> {
304 public:
305  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
306  using NormVector = matrix::Dense<remove_complex<ValueType>>;
307  using Vector = matrix::Dense<ValueType>;
308 
309  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
310  {
314  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
315  tolerance,
316  5 * std::numeric_limits<remove_complex<ValueType>>::epsilon());
317  };
318  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
319  Factory);
320  GKO_ENABLE_BUILD_METHOD(Factory);
321 
322 protected:
323  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
324  : ResidualNormBase<ValueType>(exec)
325  {}
326 
327  explicit RelativeResidualNorm(const Factory* factory,
328  const CriterionArgs& args)
329  : ResidualNormBase<ValueType>(factory->get_executor(), args,
330  factory->get_parameters().tolerance,
331  mode::rhs_norm),
332  parameters_{factory->get_parameters()}
333  {}
334 };
335 
336 
354 template <typename ValueType = default_precision>
355 class GKO_DEPRECATED(
356  "Please use the class ResidualNorm with the factory parameter baseline = "
357  "mode::absolute") AbsoluteResidualNorm
358  : public ResidualNormBase<ValueType> {
359 public:
360  using NormVector = matrix::Dense<remove_complex<ValueType>>;
361  using Vector = matrix::Dense<ValueType>;
362 
363  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
364  {
368  remove_complex<ValueType> GKO_FACTORY_PARAMETER_SCALAR(
369  tolerance,
370  5 * std::numeric_limits<remove_complex<ValueType>>::epsilon());
371  };
372  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
373  Factory);
374  GKO_ENABLE_BUILD_METHOD(Factory);
375 
376 protected:
377  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
378  : ResidualNormBase<ValueType>(exec)
379  {}
380 
381  explicit AbsoluteResidualNorm(const Factory* factory,
382  const CriterionArgs& args)
383  : ResidualNormBase<ValueType>(factory->get_executor(), args,
384  factory->get_parameters().tolerance,
385  mode::absolute),
386  parameters_{factory->get_parameters()}
387  {}
388 };
389 
390 
391 GKO_END_DISABLE_DEPRECATION_WARNINGS
392 
393 
394 } // namespace stop
395 } // namespace gko
396 
397 
398 #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:118
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:113
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:192
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:38
gko::stop::ResidualNorm::Factory
Definition: residual_norm.hpp:134
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:170
gko::stop::ResidualNormBase
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition: residual_norm.hpp:51
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:260
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667