Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
residual_norm.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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>
50 class ResidualNormBase : public Criterion {
51  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
52 
53 protected:
54  using absolute_type = remove_complex<ValueType>;
58  bool check_impl(uint8 stoppingId, bool setFinalized,
59  array<stopping_status>* stop_status, bool* one_changed,
60  const Criterion::Updater& updater) override;
61 
62  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
63  : Criterion(exec), device_storage_{exec, 2}
64  {}
65 
66  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
67  const CriterionArgs& args,
68  absolute_type reduction_factor, mode baseline);
69 
70  remove_complex<ValueType> reduction_factor_{};
71  std::unique_ptr<NormVector> starting_tau_{};
72  std::unique_ptr<NormVector> u_dense_tau_{};
73  /* Contains device side: all_converged and one_changed booleans */
74  array<bool> device_storage_;
75 
76 private:
77  mode baseline_{mode::rhs_norm};
78  std::shared_ptr<const LinOp> system_matrix_{};
79  std::shared_ptr<const LinOp> b_{};
80  /* one/neg_one for residual computation */
81  std::shared_ptr<const Vector> one_{};
82  std::shared_ptr<const Vector> neg_one_{};
83  // workspace for reduction
84  mutable gko::array<char> reduction_tmp_;
85 };
86 
87 
108 template <typename ValueType = default_precision>
109 class ResidualNorm : public ResidualNormBase<ValueType> {
110 public:
114 
116  {
120  remove_complex<ValueType> reduction_factor{
121  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
122 
123  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
124  {
125  this->reduction_factor = value;
126  return *this;
127  }
128 
133  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
134  };
135  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
137 
138 protected:
139  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
140  : ResidualNormBase<ValueType>(exec)
141  {}
142 
143  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
144  : ResidualNormBase<ValueType>(
145  factory->get_executor(), args,
146  factory->get_parameters().reduction_factor,
147  factory->get_parameters().baseline),
148  parameters_{factory->get_parameters()}
149  {}
150 };
151 
152 
170 template <typename ValueType = default_precision>
171 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
172 public:
176 
178  {
182  remove_complex<ValueType> reduction_factor{
183  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
184 
185  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
186  {
187  this->reduction_factor = value;
188  return *this;
189  }
190 
195  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
196  };
197  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
200 
201 protected:
202  // check_impl needs to be overwritten again since we focus on the implicit
203  // residual here
204  bool check_impl(uint8 stoppingId, bool setFinalized,
205  array<stopping_status>* stop_status, bool* one_changed,
206  const Criterion::Updater& updater) override;
207 
208  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
209  : ResidualNormBase<ValueType>(exec)
210  {}
211 
212  explicit ImplicitResidualNorm(const Factory* factory,
213  const CriterionArgs& args)
214  : ResidualNormBase<ValueType>(
215  factory->get_executor(), args,
216  factory->get_parameters().reduction_factor,
217  factory->get_parameters().baseline),
218  parameters_{factory->get_parameters()}
219  {}
220 };
221 
222 
254 deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
255  double tolerance);
256 
258 deferred_factory_parameter<CriterionFactory> relative_residual_norm(
259  double tolerance);
260 
262 deferred_factory_parameter<CriterionFactory> initial_residual_norm(
263  double tolerance);
264 
266 deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
267  double tolerance);
268 
270 deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
271  double tolerance);
272 
274 deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
275  double tolerance);
276 
277 
278 // The following classes are deprecated, but they internally reference
279 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
280 // for the definition of these classes.
281 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
282 
283 
303 template <typename ValueType = default_precision>
304 class GKO_DEPRECATED(
305  "Please use the class ResidualNorm with the factory parameter baseline = "
306  "mode::initial_resnorm") ResidualNormReduction
307  : public ResidualNormBase<ValueType> {
308 public:
309  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
310  using NormVector = matrix::Dense<remove_complex<ValueType>>;
311  using Vector = matrix::Dense<ValueType>;
312 
313  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
314  {
318  remove_complex<ValueType> reduction_factor{
319  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
320 
321  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
322  {
323  this->reduction_factor = value;
324  return *this;
325  }
326  };
327  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
328  Factory);
329  GKO_ENABLE_BUILD_METHOD(Factory);
330 
331 protected:
332  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
333  : ResidualNormBase<ValueType>(exec)
334  {}
335 
336  explicit ResidualNormReduction(const Factory* factory,
337  const CriterionArgs& args)
338  : ResidualNormBase<ValueType>(
339  factory->get_executor(), args,
340  factory->get_parameters().reduction_factor,
341  mode::initial_resnorm),
342  parameters_{factory->get_parameters()}
343  {}
344 };
345 
346 
365 template <typename ValueType = default_precision>
366 class GKO_DEPRECATED(
367  "Please use the class ResidualNorm with the factory parameter baseline = "
368  "mode::rhs_norm") RelativeResidualNorm
369  : public ResidualNormBase<ValueType> {
370 public:
371  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
372  using NormVector = matrix::Dense<remove_complex<ValueType>>;
373  using Vector = matrix::Dense<ValueType>;
374 
375  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
376  {
380  remove_complex<ValueType> tolerance{
381  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
382 
383 
384  parameters_type& with_tolerance(remove_complex<ValueType> value)
385  {
386  this->tolerance = value;
387  return *this;
388  }
389  };
390  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
391  Factory);
392  GKO_ENABLE_BUILD_METHOD(Factory);
393 
394 protected:
395  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
396  : ResidualNormBase<ValueType>(exec)
397  {}
398 
399  explicit RelativeResidualNorm(const Factory* factory,
400  const CriterionArgs& args)
401  : ResidualNormBase<ValueType>(factory->get_executor(), args,
402  factory->get_parameters().tolerance,
403  mode::rhs_norm),
404  parameters_{factory->get_parameters()}
405  {}
406 };
407 
408 
426 template <typename ValueType = default_precision>
427 class GKO_DEPRECATED(
428  "Please use the class ResidualNorm with the factory parameter baseline = "
429  "mode::absolute") AbsoluteResidualNorm
430  : public ResidualNormBase<ValueType> {
431 public:
432  using NormVector = matrix::Dense<remove_complex<ValueType>>;
433  using Vector = matrix::Dense<ValueType>;
434 
435  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
436  {
440  remove_complex<ValueType> tolerance{
441  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
442 
443  parameters_type& with_tolerance(remove_complex<ValueType> value)
444  {
445  this->tolerance = value;
446  return *this;
447  }
448  };
449  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
450  Factory);
451  GKO_ENABLE_BUILD_METHOD(Factory);
452 
453 protected:
454  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
455  : ResidualNormBase<ValueType>(exec)
456  {}
457 
458  explicit AbsoluteResidualNorm(const Factory* factory,
459  const CriterionArgs& args)
460  : ResidualNormBase<ValueType>(factory->get_executor(), args,
461  factory->get_parameters().tolerance,
462  mode::absolute),
463  parameters_{factory->get_parameters()}
464  {}
465 };
466 
467 
468 GKO_END_DISABLE_DEPRECATION_WARNINGS
469 
470 
471 } // namespace stop
472 } // namespace gko
473 
474 
475 #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:437
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:198
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:135
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:115
gko::stop::ImplicitResidualNorm::parameters_type
Definition: residual_norm.hpp:177
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:386
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:272
gko::stop::ImplicitResidualNorm
The ImplicitResidualNorm class is a stopping criterion which stops the iteration process when the imp...
Definition: residual_norm.hpp:171
gko::stop::ResidualNormBase
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition: residual_norm.hpp:50
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:62
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 ....