Ginkgo Generated from branch based on main. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
residual_norm.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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
19namespace gko {
20namespace stop {
21
22
37enum class mode { absolute, initial_resnorm, rhs_norm };
38
39
49template <typename ValueType>
50class ResidualNormBase
51 : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
52 friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
53 Criterion>;
54 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
55
56protected:
57 using absolute_type = remove_complex<ValueType>;
58 using ComplexVector = matrix::Dense<to_complex<ValueType>>;
59 using NormVector = matrix::Dense<absolute_type>;
60 using Vector = matrix::Dense<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
80private:
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
112template <typename ValueType = default_precision>
113class ResidualNorm : public ResidualNormBase<ValueType> {
114public:
115 using ComplexVector = matrix::Dense<to_complex<ValueType>>;
116 using NormVector = matrix::Dense<remove_complex<ValueType>>;
117 using Vector = matrix::Dense<ValueType>;
118
120 {
125 5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
126
127 parameters_type& with_reduction_factor(remove_complex<ValueType> value)
128 {
129 this->reduction_factor = value;
130 return *this;
131 }
132
138 };
139 GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
141
142protected:
143 explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
144 : ResidualNormBase<ValueType>(exec)
145 {}
146
147 explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
148 : ResidualNormBase<ValueType>(
149 factory->get_executor(), args,
150 factory->get_parameters().reduction_factor,
151 factory->get_parameters().baseline),
152 parameters_{factory->get_parameters()}
153 {}
154};
155
156
174template <typename ValueType = default_precision>
175class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
176public:
177 using ComplexVector = matrix::Dense<to_complex<ValueType>>;
178 using NormVector = matrix::Dense<remove_complex<ValueType>>;
179 using Vector = matrix::Dense<ValueType>;
180
182 {
187 5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
188
189 parameters_type& with_reduction_factor(remove_complex<ValueType> value)
190 {
191 this->reduction_factor = value;
192 return *this;
193 }
194
200 };
201 GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
202 Factory);
204
205protected:
206 // check_impl needs to be overwritten again since we focus on the implicit
207 // residual here
208 bool check_impl(uint8 stoppingId, bool setFinalized,
209 array<stopping_status>* stop_status, bool* one_changed,
210 const Criterion::Updater& updater) override;
211
212 explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
213 : ResidualNormBase<ValueType>(exec)
214 {}
215
216 explicit ImplicitResidualNorm(const Factory* factory,
217 const CriterionArgs& args)
218 : ResidualNormBase<ValueType>(
219 factory->get_executor(), args,
220 factory->get_parameters().reduction_factor,
221 factory->get_parameters().baseline),
222 parameters_{factory->get_parameters()}
223 {}
224};
225
226
227// The following classes are deprecated, but they internally reference
228// themselves. To reduce unnecessary warnings, we disable deprecation warnings
229// for the definition of these classes.
230GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
231
232
252template <typename ValueType = default_precision>
253class GKO_DEPRECATED(
254 "Please use the class ResidualNorm with the factory parameter baseline = "
255 "mode::initial_resnorm") ResidualNormReduction
256 : public ResidualNormBase<ValueType> {
257public:
258 using ComplexVector = matrix::Dense<to_complex<ValueType>>;
259 using NormVector = matrix::Dense<remove_complex<ValueType>>;
260 using Vector = matrix::Dense<ValueType>;
261
263 {
268 5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
269
270 parameters_type& with_reduction_factor(remove_complex<ValueType> value)
271 {
272 this->reduction_factor = value;
273 return *this;
274 }
275 };
276 GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
277 Factory);
279
280protected:
281 explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
282 : ResidualNormBase<ValueType>(exec)
283 {}
284
285 explicit ResidualNormReduction(const Factory* factory,
286 const CriterionArgs& args)
287 : ResidualNormBase<ValueType>(
288 factory->get_executor(), args,
289 factory->get_parameters().reduction_factor,
290 mode::initial_resnorm),
291 parameters_{factory->get_parameters()}
292 {}
293};
294
295
314template <typename ValueType = default_precision>
315class GKO_DEPRECATED(
316 "Please use the class ResidualNorm with the factory parameter baseline = "
317 "mode::rhs_norm") RelativeResidualNorm
318 : public ResidualNormBase<ValueType> {
319public:
320 using ComplexVector = matrix::Dense<to_complex<ValueType>>;
321 using NormVector = matrix::Dense<remove_complex<ValueType>>;
322 using Vector = matrix::Dense<ValueType>;
323
325 {
330 5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
331
332
333 parameters_type& with_tolerance(remove_complex<ValueType> value)
334 {
335 this->tolerance = value;
336 return *this;
337 }
338 };
339 GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
340 Factory);
342
343protected:
344 explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
345 : ResidualNormBase<ValueType>(exec)
346 {}
347
348 explicit RelativeResidualNorm(const Factory* factory,
349 const CriterionArgs& args)
350 : ResidualNormBase<ValueType>(factory->get_executor(), args,
351 factory->get_parameters().tolerance,
352 mode::rhs_norm),
353 parameters_{factory->get_parameters()}
354 {}
355};
356
357
375template <typename ValueType = default_precision>
376class GKO_DEPRECATED(
377 "Please use the class ResidualNorm with the factory parameter baseline = "
378 "mode::absolute") AbsoluteResidualNorm
379 : public ResidualNormBase<ValueType> {
380public:
381 using NormVector = matrix::Dense<remove_complex<ValueType>>;
382 using Vector = matrix::Dense<ValueType>;
383
385 {
390 5 * std ::numeric_limits<remove_complex<ValueType>>::epsilon()};
391
392 parameters_type& with_tolerance(remove_complex<ValueType> value)
393 {
394 this->tolerance = value;
395 return *this;
396 }
397 };
398 GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
399 Factory);
401
402protected:
403 explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
404 : ResidualNormBase<ValueType>(exec)
405 {}
406
407 explicit AbsoluteResidualNorm(const Factory* factory,
408 const CriterionArgs& args)
409 : ResidualNormBase<ValueType>(factory->get_executor(), args,
410 factory->get_parameters().tolerance,
411 mode::absolute),
412 parameters_{factory->get_parameters()}
413 {}
414};
415
416
417GKO_END_DISABLE_DEPRECATION_WARNINGS
418
419
420} // namespace stop
421} // namespace gko
422
423
424#endif // GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
Definition residual_norm.hpp:399
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition criterion.hpp:55
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:36
Definition residual_norm.hpp:202
Definition residual_norm.hpp:340
Definition residual_norm.hpp:277
Definition residual_norm.hpp:139
#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
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:445
#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
mode
The mode for the residual norm criterion.
Definition residual_norm.hpp:37
The Stopping criterion namespace.
Definition logger.hpp:50
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:119
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
Definition residual_norm.hpp:385
remove_complex< ValueType > tolerance
Absolute residual norm goal.
Definition residual_norm.hpp:389
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition criterion.hpp:205
Definition residual_norm.hpp:182
remove_complex< ValueType > reduction_factor
Implicit Residual norm goal.
Definition residual_norm.hpp:186
mode baseline
The quantity the reduction is relative to.
Definition residual_norm.hpp:199
Definition residual_norm.hpp:325
remove_complex< ValueType > tolerance
Relative residual norm goal.
Definition residual_norm.hpp:329
remove_complex< ValueType > reduction_factor
Factor by which the residual norm will be reduced.
Definition residual_norm.hpp:267
Definition residual_norm.hpp:120
remove_complex< ValueType > reduction_factor
Residual norm reduction factor.
Definition residual_norm.hpp:124
mode baseline
The quantity the reduction is relative to.
Definition residual_norm.hpp:137