Ginkgo  Generated from pipelines/2837190956 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 
125 template <typename ValueType = default_precision>
126 class ResidualNorm : public ResidualNormBase<ValueType> {
127 public:
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:
213 
215  {
219  remove_complex<ValueType> reduction_factor{
220  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
221 
222  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
223  {
224  this->reduction_factor = value;
225  return *this;
226  }
227 
232  mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
233  };
234  GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
237 
238 protected:
239  // check_impl needs to be overwritten again since we focus on the implicit
240  // residual here
241  bool check_impl(uint8 stoppingId, bool setFinalized,
242  array<stopping_status>* stop_status, bool* one_changed,
243  const Criterion::Updater& updater) override;
244 
245  explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
246  : ResidualNormBase<ValueType>(exec)
247  {}
248 
249  explicit ImplicitResidualNorm(const Factory* factory,
250  const CriterionArgs& args)
251  : ResidualNormBase<ValueType>(
252  factory->get_executor(), args,
253  factory->get_parameters().reduction_factor,
254  factory->get_parameters().baseline),
255  parameters_{factory->get_parameters()}
256  {}
257 };
258 
259 
292 deferred_factory_parameter<CriterionFactory> absolute_residual_norm(
293  double tolerance);
294 
296 deferred_factory_parameter<CriterionFactory> relative_residual_norm(
297  double tolerance);
298 
300 deferred_factory_parameter<CriterionFactory> initial_residual_norm(
301  double tolerance);
302 
304 deferred_factory_parameter<CriterionFactory> absolute_implicit_residual_norm(
305  double tolerance);
306 
308 deferred_factory_parameter<CriterionFactory> relative_implicit_residual_norm(
309  double tolerance);
310 
312 deferred_factory_parameter<CriterionFactory> initial_implicit_residual_norm(
313  double tolerance);
314 
315 
316 // The following classes are deprecated, but they internally reference
317 // themselves. To reduce unnecessary warnings, we disable deprecation warnings
318 // for the definition of these classes.
319 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
320 
321 
341 template <typename ValueType = default_precision>
342 class GKO_DEPRECATED(
343  "Please use the class ResidualNorm with the factory parameter baseline = "
344  "mode::initial_resnorm") ResidualNormReduction
345  : public ResidualNormBase<ValueType> {
346 public:
347  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
348  using NormVector = matrix::Dense<remove_complex<ValueType>>;
349  using Vector = matrix::Dense<ValueType>;
350 
351  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
352  {
356  remove_complex<ValueType> reduction_factor{
357  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
358 
359  parameters_type& with_reduction_factor(remove_complex<ValueType> value)
360  {
361  this->reduction_factor = value;
362  return *this;
363  }
364  };
365  GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
366  Factory);
367  GKO_ENABLE_BUILD_METHOD(Factory);
368 
369 protected:
370  explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
371  : ResidualNormBase<ValueType>(exec)
372  {}
373 
374  explicit ResidualNormReduction(const Factory* factory,
375  const CriterionArgs& args)
376  : ResidualNormBase<ValueType>(
377  factory->get_executor(), args,
378  factory->get_parameters().reduction_factor,
379  mode::initial_resnorm),
380  parameters_{factory->get_parameters()}
381  {}
382 };
383 
384 
403 template <typename ValueType = default_precision>
404 class GKO_DEPRECATED(
405  "Please use the class ResidualNorm with the factory parameter baseline = "
406  "mode::rhs_norm") RelativeResidualNorm
407  : public ResidualNormBase<ValueType> {
408 public:
409  using ComplexVector = matrix::Dense<to_complex<ValueType>>;
410  using NormVector = matrix::Dense<remove_complex<ValueType>>;
411  using Vector = matrix::Dense<ValueType>;
412 
413  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
414  {
418  remove_complex<ValueType> tolerance{
419  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
420 
421 
422  parameters_type& with_tolerance(remove_complex<ValueType> value)
423  {
424  this->tolerance = value;
425  return *this;
426  }
427  };
428  GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
429  Factory);
430  GKO_ENABLE_BUILD_METHOD(Factory);
431 
432 protected:
433  explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
434  : ResidualNormBase<ValueType>(exec)
435  {}
436 
437  explicit RelativeResidualNorm(const Factory* factory,
438  const CriterionArgs& args)
439  : ResidualNormBase<ValueType>(factory->get_executor(), args,
440  factory->get_parameters().tolerance,
441  mode::rhs_norm),
442  parameters_{factory->get_parameters()}
443  {}
444 };
445 
446 
464 template <typename ValueType = default_precision>
465 class GKO_DEPRECATED(
466  "Please use the class ResidualNorm with the factory parameter baseline = "
467  "mode::absolute") AbsoluteResidualNorm
468  : public ResidualNormBase<ValueType> {
469 public:
470  using NormVector = matrix::Dense<remove_complex<ValueType>>;
471  using Vector = matrix::Dense<ValueType>;
472 
473  GKO_CREATE_FACTORY_PARAMETERS(parameters, Factory)
474  {
478  remove_complex<ValueType> tolerance{
479  5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
480 
481  parameters_type& with_tolerance(remove_complex<ValueType> value)
482  {
483  this->tolerance = value;
484  return *this;
485  }
486  };
487  GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
488  Factory);
489  GKO_ENABLE_BUILD_METHOD(Factory);
490 
491 protected:
492  explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
493  : ResidualNormBase<ValueType>(exec)
494  {}
495 
496  explicit AbsoluteResidualNorm(const Factory* factory,
497  const CriterionArgs& args)
498  : ResidualNormBase<ValueType>(factory->get_executor(), args,
499  factory->get_parameters().tolerance,
500  mode::absolute),
501  parameters_{factory->get_parameters()}
502  {}
503 };
504 
505 
506 GKO_END_DISABLE_DEPRECATION_WARNINGS
507 
508 
509 } // namespace stop
510 } // namespace gko
511 
512 
513 #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:209
gko::uint8
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:130
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:469
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:126
gko::stop::ImplicitResidualNorm::Factory
Definition: residual_norm.hpp:235
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::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:214
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 ....