Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
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 <type_traits>
10
11
12#include <ginkgo/core/base/array.hpp>
13#include <ginkgo/core/base/math.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>
51 : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
52 friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
53 Criterion>;
54
55protected:
56 using absolute_type = remove_complex<ValueType>;
60 bool check_impl(uint8 stoppingId, bool setFinalized,
61 array<stopping_status>* stop_status, bool* one_changed,
62 const Criterion::Updater& updater) override;
63
64 explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
66 device_storage_{exec, 2}
67 {}
68
69 explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
70 const CriterionArgs& args,
71 absolute_type reduction_factor, mode baseline);
72
73 remove_complex<ValueType> reduction_factor_{};
74 std::unique_ptr<NormVector> starting_tau_{};
75 std::unique_ptr<NormVector> u_dense_tau_{};
76 /* Contains device side: all_converged and one_changed booleans */
77 array<bool> device_storage_;
78
79private:
80 mode baseline_{mode::rhs_norm};
81 std::shared_ptr<const LinOp> system_matrix_{};
82 std::shared_ptr<const LinOp> b_{};
83 /* one/neg_one for residual computation */
84 std::shared_ptr<const Vector> one_{};
85 std::shared_ptr<const Vector> neg_one_{};
86};
87
88
109template <typename ValueType = default_precision>
110class ResidualNorm : public ResidualNormBase<ValueType> {
111public:
115
117 {
122 reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
123
128 mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
129 };
130 GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
132
133protected:
134 explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
135 : ResidualNormBase<ValueType>(exec)
136 {}
137
138 explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
139 : ResidualNormBase<ValueType>(
140 factory->get_executor(), args,
141 factory->get_parameters().reduction_factor,
142 factory->get_parameters().baseline),
143 parameters_{factory->get_parameters()}
144 {}
145};
146
147
165template <typename ValueType = default_precision>
166class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
167public:
171
173 {
178 reduction_factor, static_cast<remove_complex<ValueType>>(1e-15));
179
184 mode GKO_FACTORY_PARAMETER_SCALAR(baseline, mode::rhs_norm);
185 };
186 GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
189
190protected:
191 // check_impl needs to be overwritten again since we focus on the implicit
192 // residual here
193 bool check_impl(uint8 stoppingId, bool setFinalized,
194 array<stopping_status>* stop_status, bool* one_changed,
195 const Criterion::Updater& updater) override;
196
197 explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
198 : ResidualNormBase<ValueType>(exec)
199 {}
200
201 explicit ImplicitResidualNorm(const Factory* factory,
202 const CriterionArgs& args)
203 : ResidualNormBase<ValueType>(
204 factory->get_executor(), args,
205 factory->get_parameters().reduction_factor,
206 factory->get_parameters().baseline),
207 parameters_{factory->get_parameters()}
208 {}
209};
210
211
212// The following classes are deprecated, but they internally reference
213// themselves. To reduce unnecessary warnings, we disable deprecation warnings
214// for the definition of these classes.
215GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
216
217
237template <typename ValueType = default_precision>
238class GKO_DEPRECATED(
239 "Please use the class ResidualNorm with the factory parameter baseline = "
240 "mode::initial_resnorm") ResidualNormReduction
241 : public ResidualNormBase<ValueType> {
242public:
246
255 GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
258
259protected:
260 explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
261 : ResidualNormBase<ValueType>(exec)
262 {}
263
264 explicit ResidualNormReduction(const Factory* factory,
265 const CriterionArgs& args)
266 : ResidualNormBase<ValueType>(
267 factory->get_executor(), args,
268 factory->get_parameters().reduction_factor,
269 mode::initial_resnorm),
270 parameters_{factory->get_parameters()}
271 {}
272};
273
274
293template <typename ValueType = default_precision>
294class GKO_DEPRECATED(
295 "Please use the class ResidualNorm with the factory parameter baseline = "
296 "mode::rhs_norm") RelativeResidualNorm
297 : public ResidualNormBase<ValueType> {
298public:
302
311 GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
314
315protected:
316 explicit RelativeResidualNorm(std::shared_ptr<const gko::Executor> exec)
317 : ResidualNormBase<ValueType>(exec)
318 {}
319
320 explicit RelativeResidualNorm(const Factory* factory,
321 const CriterionArgs& args)
322 : ResidualNormBase<ValueType>(factory->get_executor(), args,
323 factory->get_parameters().tolerance,
324 mode::rhs_norm),
325 parameters_{factory->get_parameters()}
326 {}
327};
328
329
347template <typename ValueType = default_precision>
348class GKO_DEPRECATED(
349 "Please use the class ResidualNorm with the factory parameter baseline = "
350 "mode::absolute") AbsoluteResidualNorm
351 : public ResidualNormBase<ValueType> {
352public:
355
364 GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
367
368protected:
369 explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
370 : ResidualNormBase<ValueType>(exec)
371 {}
372
373 explicit AbsoluteResidualNorm(const Factory* factory,
374 const CriterionArgs& args)
375 : ResidualNormBase<ValueType>(factory->get_executor(), args,
376 factory->get_parameters().tolerance,
377 mode::absolute),
378 parameters_{factory->get_parameters()}
379 {}
380};
381
382
383GKO_END_DISABLE_DEPRECATION_WARNINGS
384
385
386} // namespace stop
387} // namespace gko
388
389
390#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:663
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
Dense is a matrix format which explicitly stores all values of the matrix.
Definition sparsity_csr.hpp:26
Definition residual_norm.hpp:365
The AbsoluteResidualNorm class is a stopping criterion which stops the iteration process when the res...
Definition residual_norm.hpp:351
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:187
The ImplicitResidualNorm class is a stopping criterion which stops the iteration process when the imp...
Definition residual_norm.hpp:166
Definition residual_norm.hpp:312
The RelativeResidualNorm class is a stopping criterion which stops the iteration process when the res...
Definition residual_norm.hpp:297
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition residual_norm.hpp:51
Definition residual_norm.hpp:256
The ResidualNormReduction class is a stopping criterion which stops the iteration process when the re...
Definition residual_norm.hpp:241
Definition residual_norm.hpp:130
The ResidualNorm class is a stopping criterion which stops the iteration process when the actual resi...
Definition residual_norm.hpp:110
#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 Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:115
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:326
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition criterion.hpp:205