Ginkgo  Generated from pipelines/3136675824 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/multivector.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>;
56  using NormVector = typename Vector::norm_type;
57  stopping_indicators check_impl(uint8 stoppingId, bool setFinalized,
58  array<stopping_status>* stop_status,
59  const Criterion::Updater& updater) override;
60 
61  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
62  : Criterion(exec), device_storage_{exec, 1}
63  {}
64 
65  explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
66  const CriterionArgs& args,
67  absolute_type reduction_factor, mode baseline);
68 
69  remove_complex<ValueType> reduction_factor_{};
70  std::unique_ptr<NormVector> starting_tau_{};
71  std::unique_ptr<NormVector> u_dense_tau_{};
72  /* Contains device side: all_converged and one_changed booleans */
73  array<stopping_indicators> device_storage_;
74 
75 private:
76  mode baseline_{mode::rhs_norm};
77  std::shared_ptr<const LinOp> system_matrix_{};
78  std::shared_ptr<const AbstractMultiVector> b_{};
79  /* one/neg_one for residual computation */
80  std::shared_ptr<const Vector> one_{};
81  std::shared_ptr<const Vector> neg_one_{};
82  // workspace for reduction
83  mutable gko::array<char> reduction_tmp_;
84  // workspace for the residual b - A x
85  std::unique_ptr<AbstractMultiVector> residual_{};
86 };
87 
88 
126 template <typename ValueType = default_precision>
127 class ResidualNorm : public ResidualNormBase<ValueType> {
128 public:
130  using NormVector = typename Vector::norm_type;
131 
133  {
137  remove_complex<ValueType> reduction_factor{
138  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
139 
140  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
141  {
142  this->reduction_factor = value;
143  return *this;
144  }
145 
150  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
151  };
152  GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
154 
155 protected:
156  explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
157  : ResidualNormBase<ValueType>(exec)
158  {}
159 
160  explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
161  : ResidualNormBase<ValueType>(
162  factory->get_executor(), args,
163  factory->get_parameters().reduction_factor,
164  factory->get_parameters().baseline),
165  parameters_{factory->get_parameters()}
166  {}
167 };
168 
169 
207 template <typename ValueType = default_precision>
208 class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
209 public:
211  using NormVector = typename Vector::norm_type;
212 
214  {
218  remove_complex<ValueType> reduction_factor{
219  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
220 
221  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
222  {
223  this->reduction_factor = value;
224  return *this;
225  }
226 
231  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
232  };
233  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
236 
237 protected:
238  // check_impl needs to be overwritten again since we focus on the implicit
239  // residual here
240  stopping_indicators check_impl(uint8 stoppingId, bool setFinalized,
241  array<stopping_status>* stop_status,
242  const Criterion::Updater& updater) override;
243 
244  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
245  : ResidualNormBase<ValueType>(exec)
246  {}
247 
248  explicit ImplicitResidualNorm(const Factory* factory,
249  const CriterionArgs& args)
250  : ResidualNormBase<ValueType>(
251  factory->get_executor(), args,
252  factory->get_parameters().reduction_factor,
253  factory->get_parameters().baseline),
254  parameters_{factory->get_parameters()}
255  {}
256 };
257 
258 
291 deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
292  double tolerance);
293 
295 deferred_factory_parameter<CriterionFactory> relative_residual_norm(
296  double tolerance);
297 
299 deferred_factory_parameter<CriterionFactory> initial_residual_norm(
300  double tolerance);
301 
303 deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
304  double tolerance);
305 
307 deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
308  double tolerance);
309 
311 deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
312  double tolerance);
313 
314 
315 // The following classes are deprecated, but they internally reference
316 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
317 // for the definition of these classes.
318 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
319 
320 
340 template <typename ValueType = default_precision>
341 class GKO_DEPRECATED(
342  "Please use the class ResidualNorm with the factory parameter baseline = "
343  "mode::initial_resnorm") ResidualNormReduction
344  : public ResidualNormBase<ValueType> {
345 public:
346  using Vector = matrix::MultiVector<ValueType>;
347  using NormVector = typename Vector::norm_type;
348 
349  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
350  {
354  remove_complex<ValueType> reduction_factor{
355  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
356 
357  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
358  {
359  this->reduction_factor = value;
360  return *this;
361  }
362  };
363  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
364  Factory);
365  GKO_ENABLE_BUILD_METHOD(Factory);
366 
367 protected:
368  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
369  : ResidualNormBase<ValueType>(exec)
370  {}
371 
372  explicit ResidualNormReduction(const Factory* factory,
373  const CriterionArgs& args)
374  : ResidualNormBase<ValueType>(
375  factory->get_executor(), args,
376  factory->get_parameters().reduction_factor,
377  mode::initial_resnorm),
378  parameters_{factory->get_parameters()}
379  {}
380 };
381 
382 
401 template <typename ValueType = default_precision>
402 class GKO_DEPRECATED(
403  "Please use the class ResidualNorm with the factory parameter baseline = "
404  "mode::rhs_norm") RelativeResidualNorm
405  : public ResidualNormBase<ValueType> {
406 public:
407  using Vector = matrix::MultiVector<ValueType>;
408  using NormVector = typename Vector::norm_type;
409 
410  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
411  {
415  remove_complex<ValueType> tolerance{
416  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
417 
418 
419  parameters_type& with_tolerance(remove_complex<ValueType> value)
420  {
421  this->tolerance = value;
422  return *this;
423  }
424  };
425  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
426  Factory);
427  GKO_ENABLE_BUILD_METHOD(Factory);
428 
429 protected:
430  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
431  : ResidualNormBase<ValueType>(exec)
432  {}
433 
434  explicit RelativeResidualNorm(const Factory* factory,
435  const CriterionArgs& args)
436  : ResidualNormBase<ValueType>(factory->get_executor(), args,
437  factory->get_parameters().tolerance,
438  mode::rhs_norm),
439  parameters_{factory->get_parameters()}
440  {}
441 };
442 
443 
461 template <typename ValueType = default_precision>
462 class GKO_DEPRECATED(
463  "Please use the class ResidualNorm with the factory parameter baseline = "
464  "mode::absolute") AbsoluteResidualNorm
465  : public ResidualNormBase<ValueType> {
466 public:
467  using Vector = matrix::MultiVector<ValueType>;
468  using NormVector = typename Vector::norm_type;
469 
470  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
471  {
475  remove_complex<ValueType> tolerance{
476  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
477 
478  parameters_type& with_tolerance(remove_complex<ValueType> value)
479  {
480  this->tolerance = value;
481  return *this;
482  }
483  };
484  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
485  Factory);
486  GKO_ENABLE_BUILD_METHOD(Factory);
487 
488 protected:
489  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
490  : ResidualNormBase<ValueType>(exec)
491  {}
492 
493  explicit AbsoluteResidualNorm(const Factory* factory,
494  const CriterionArgs& args)
495  : ResidualNormBase<ValueType>(factory->get_executor(), args,
496  factory->get_parameters().tolerance,
497  mode::absolute),
498  parameters_{factory->get_parameters()}
499  {}
500 };
501 
502 
503 GKO_END_DISABLE_DEPRECATION_WARNINGS
504 
505 
506 } // namespace stop
507 } // namespace gko
508 
509 
510 #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:211
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:131
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:469
gko::stopping_indicators
The result of a stopping criterion check over all vectors.
Definition: stopping_status.hpp:161
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
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
Stopping criterion based on the explicit residual norm .
Definition: residual_norm.hpp:127
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:234
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:152
gko::matrix::MultiVector
MultiVector is a multivector format which explicitly stores all values of the vector.
Definition: dense_cache.hpp:28
gko::stop::Criterion::Updater
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition: criterion.hpp:59
gko::stop::ResidualNorm::parameters_type
Definition: residual_norm.hpp:132
gko::stop::ImplicitResidualNorm::parameters_type
Definition: residual_norm.hpp:213
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:418
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:304
gko::stop::ImplicitResidualNorm
Stopping criterion based on a solver-maintained squared residual-norm estimate .
Definition: residual_norm.hpp:208
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:69
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:263
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 ....