Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
residual_norm.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
34#define GKO_PUBLIC_CORE_STOP_RESIDUAL_NORM_HPP_
35
36
37#include <type_traits>
38
39
40#include <ginkgo/core/base/array.hpp>
41#include <ginkgo/core/base/math.hpp>
42#include <ginkgo/core/base/utils.hpp>
43#include <ginkgo/core/matrix/dense.hpp>
44#include <ginkgo/core/stop/criterion.hpp>
45
46
47namespace gko {
48namespace stop {
49
50
65enum class mode { absolute, initial_resnorm, rhs_norm };
66
67
77template <typename ValueType>
79 : public EnablePolymorphicObject<ResidualNormBase<ValueType>, Criterion> {
80 friend class EnablePolymorphicObject<ResidualNormBase<ValueType>,
81 Criterion>;
82
83protected:
84 using absolute_type = remove_complex<ValueType>;
88 bool check_impl(uint8 stoppingId, bool setFinalized,
90 const Criterion::Updater& updater) override;
91
92 explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec)
94 device_storage_{exec, 2}
95 {}
96
97 explicit ResidualNormBase(std::shared_ptr<const gko::Executor> exec,
98 const CriterionArgs& args,
99 absolute_type reduction_factor, mode baseline);
100
101 remove_complex<ValueType> reduction_factor_{};
102 std::unique_ptr<NormVector> starting_tau_{};
103 std::unique_ptr<NormVector> u_dense_tau_{};
104 /* Contains device side: all_converged and one_changed booleans */
105 array<bool> device_storage_;
106
107private:
108 mode baseline_{mode::rhs_norm};
109 std::shared_ptr<const LinOp> system_matrix_{};
110 std::shared_ptr<const LinOp> b_{};
111 /* one/neg_one for residual computation */
112 std::shared_ptr<const Vector> one_{};
113 std::shared_ptr<const Vector> neg_one_{};
114};
115
116
137template <typename ValueType = default_precision>
138class ResidualNorm : public ResidualNormBase<ValueType> {
139public:
143
158 GKO_ENABLE_CRITERION_FACTORY(ResidualNorm<ValueType>, parameters, Factory);
160
161protected:
162 explicit ResidualNorm(std::shared_ptr<const gko::Executor> exec)
163 : ResidualNormBase<ValueType>(exec)
164 {}
165
166 explicit ResidualNorm(const Factory* factory, const CriterionArgs& args)
167 : ResidualNormBase<ValueType>(
168 factory->get_executor(), args,
169 factory->get_parameters().reduction_factor,
170 factory->get_parameters().baseline),
171 parameters_{factory->get_parameters()}
172 {}
173};
174
175
193template <typename ValueType = default_precision>
194class ImplicitResidualNorm : public ResidualNormBase<ValueType> {
195public:
199
214 GKO_ENABLE_CRITERION_FACTORY(ImplicitResidualNorm<ValueType>, parameters,
217
218protected:
219 // check_impl needs to be overwritten again since we focus on the implicit
220 // residual here
221 bool check_impl(uint8 stoppingId, bool setFinalized,
223 const Criterion::Updater& updater) override;
224
225 explicit ImplicitResidualNorm(std::shared_ptr<const gko::Executor> exec)
226 : ResidualNormBase<ValueType>(exec)
227 {}
228
229 explicit ImplicitResidualNorm(const Factory* factory,
230 const CriterionArgs& args)
231 : ResidualNormBase<ValueType>(
232 factory->get_executor(), args,
233 factory->get_parameters().reduction_factor,
234 factory->get_parameters().baseline),
235 parameters_{factory->get_parameters()}
236 {}
237};
238
239
240// The following classes are deprecated, but they internally reference
241// themselves. To reduce unnecessary warnings, we disable deprecation warnings
242// for the definition of these classes.
243GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
244
245
265template <typename ValueType = default_precision>
266class GKO_DEPRECATED(
267 "Please use the class ResidualNorm with the factory parameter baseline = "
268 "mode::initial_resnorm") ResidualNormReduction
269 : public ResidualNormBase<ValueType> {
270public:
274
283 GKO_ENABLE_CRITERION_FACTORY(ResidualNormReduction<ValueType>, parameters,
286
287protected:
288 explicit ResidualNormReduction(std::shared_ptr<const gko::Executor> exec)
289 : ResidualNormBase<ValueType>(exec)
290 {}
291
292 explicit ResidualNormReduction(const Factory* factory,
293 const CriterionArgs& args)
294 : ResidualNormBase<ValueType>(
295 factory->get_executor(), args,
296 factory->get_parameters().reduction_factor,
297 mode::initial_resnorm),
298 parameters_{factory->get_parameters()}
299 {}
300};
301
302
321template <typename ValueType = default_precision>
322class GKO_DEPRECATED(
323 "Please use the class ResidualNorm with the factory parameter baseline = "
324 "mode::rhs_norm") RelativeResidualNorm
325 : public ResidualNormBase<ValueType> {
326public:
330
339 GKO_ENABLE_CRITERION_FACTORY(RelativeResidualNorm<ValueType>, parameters,
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:
383
392 GKO_ENABLE_CRITERION_FACTORY(AbsoluteResidualNorm<ValueType>, parameters,
395
396protected:
397 explicit AbsoluteResidualNorm(std::shared_ptr<const gko::Executor> exec)
398 : ResidualNormBase<ValueType>(exec)
399 {}
400
401 explicit AbsoluteResidualNorm(const Factory* factory,
402 const CriterionArgs& args)
403 : ResidualNormBase<ValueType>(factory->get_executor(), args,
404 factory->get_parameters().tolerance,
405 mode::absolute),
406 parameters_{factory->get_parameters()}
407 {}
408};
409
410
411GKO_END_DISABLE_DEPRECATION_WARNINGS
412
413
414} // namespace stop
415} // namespace gko
416
417
418#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:691
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
Definition residual_norm.hpp:393
The AbsoluteResidualNorm class is a stopping criterion which stops the iteration process when the res...
Definition residual_norm.hpp:379
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition criterion.hpp:83
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:64
Definition residual_norm.hpp:215
The ImplicitResidualNorm class is a stopping criterion which stops the iteration process when the imp...
Definition residual_norm.hpp:194
Definition residual_norm.hpp:340
The RelativeResidualNorm class is a stopping criterion which stops the iteration process when the res...
Definition residual_norm.hpp:325
The ResidualNormBase class provides a framework for stopping criteria related to the residual norm.
Definition residual_norm.hpp:79
Definition residual_norm.hpp:284
The ResidualNormReduction class is a stopping criterion which stops the iteration process when the re...
Definition residual_norm.hpp:269
Definition residual_norm.hpp:158
The ResidualNorm class is a stopping criterion which stops the iteration process when the actual resi...
Definition residual_norm.hpp:138
#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:308
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition abstract_factory.hpp:473
#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:422
mode
The mode for the residual norm criterion.
Definition residual_norm.hpp:65
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:149
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:354
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition criterion.hpp:233