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
criterion.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_CRITERION_HPP_
34#define GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
35
36
37#include <ginkgo/core/base/abstract_factory.hpp>
38#include <ginkgo/core/base/array.hpp>
39#include <ginkgo/core/base/executor.hpp>
40#include <ginkgo/core/base/lin_op.hpp>
41#include <ginkgo/core/base/polymorphic_object.hpp>
42#include <ginkgo/core/base/utils.hpp>
43#include <ginkgo/core/log/logger.hpp>
44#include <ginkgo/core/stop/stopping_status.hpp>
45
46
47namespace gko {
53namespace stop {
54
55
64class Criterion : public EnableAbstractPolymorphicObject<Criterion> {
65public:
83 class Updater {
84 friend class Criterion;
85
86 public:
92 Updater(const Updater&) = delete;
93 Updater(Updater&&) = delete;
94 Updater& operator=(const Updater&) = delete;
95 Updater& operator=(Updater&&) = delete;
96
101 bool check(uint8 stopping_id, bool set_finalized,
103 {
104 auto converged = parent_->check(stopping_id, set_finalized,
105 stop_status, one_changed, *this);
106 return converged;
107 }
108
112#define GKO_UPDATER_REGISTER_PARAMETER(_type, _name) \
113 const Updater& _name(_type const& value) const \
114 { \
115 _name##_ = value; \
116 return *this; \
117 } \
118 mutable _type _name##_ {}
119#define GKO_UPDATER_REGISTER_PTR_PARAMETER(_type, _name) \
120 const Updater& _name(ptr_param<_type> value) const \
121 { \
122 _name##_ = value.get(); \
123 return *this; \
124 } \
125 mutable _type* _name##_ {}
126
127 GKO_UPDATER_REGISTER_PARAMETER(size_type, num_iterations);
128 // ignore_residual_check default is false
129 GKO_UPDATER_REGISTER_PARAMETER(bool, ignore_residual_check);
130 GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, residual);
131 GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, residual_norm);
132 GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp,
133 implicit_sq_residual_norm);
134 GKO_UPDATER_REGISTER_PTR_PARAMETER(const LinOp, solution);
135
136#undef GKO_UPDATER_REGISTER_PTR_PARAMETER
137#undef GKO_UPDATER_REGISTER_PARAMETER
138
139 private:
140 Updater(Criterion* parent) : parent_{parent} {}
141
142 Criterion* parent_;
143 };
144
150 Updater update() { return {this}; }
151
165 bool check(uint8 stopping_id, bool set_finalized,
167 const Updater& updater)
168 {
170 this, updater.num_iterations_, updater.residual_,
171 updater.residual_norm_, updater.solution_, stopping_id,
172 set_finalized);
173 auto all_converged = this->check_impl(
174 stopping_id, set_finalized, stop_status, one_changed, updater);
176 this, updater.num_iterations_, updater.residual_,
177 updater.residual_norm_, updater.implicit_sq_residual_norm_,
178 updater.solution_, stopping_id, set_finalized, stop_status,
180 return all_converged;
181 }
182
183protected:
200 virtual bool check_impl(uint8 stopping_id, bool set_finalized,
202 bool* one_changed, const Updater& updater) = 0;
203
214 void set_all_statuses(uint8 stopping_id, bool set_finalized,
216
217 explicit Criterion(std::shared_ptr<const gko::Executor> exec)
219 {}
220};
221
222
234 std::shared_ptr<const LinOp> system_matrix;
235 std::shared_ptr<const LinOp> b;
236 const LinOp* x;
237 const LinOp* initial_residual;
238
239
240 CriterionArgs(std::shared_ptr<const LinOp> system_matrix,
241 std::shared_ptr<const LinOp> b, const LinOp* x,
242 const LinOp* initial_residual = nullptr)
243 : system_matrix{system_matrix},
244 b{b},
245 x{x},
246 initial_residual{initial_residual}
247 {}
248};
249
250
255
256
272template <typename ConcreteFactory, typename ConcreteCriterion,
273 typename ParametersType, typename PolymorphicBase = CriterionFactory>
276 PolymorphicBase>;
277
278
303#define GKO_ENABLE_CRITERION_FACTORY(_criterion, _parameters_name, \
304 _factory_name) \
305public: \
306 const _parameters_name##_type& get_##_parameters_name() const \
307 { \
308 return _parameters_name##_; \
309 } \
310 \
311 class _factory_name \
312 : public ::gko::stop::EnableDefaultCriterionFactory< \
313 _factory_name, _criterion, _parameters_name##_type> { \
314 friend class ::gko::EnablePolymorphicObject< \
315 _factory_name, ::gko::stop::CriterionFactory>; \
316 friend class ::gko::enable_parameters_type<_parameters_name##_type, \
317 _factory_name>; \
318 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec) \
319 : ::gko::stop::EnableDefaultCriterionFactory< \
320 _factory_name, _criterion, _parameters_name##_type>( \
321 std::move(exec)) \
322 {} \
323 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec, \
324 const _parameters_name##_type& parameters) \
325 : ::gko::stop::EnableDefaultCriterionFactory< \
326 _factory_name, _criterion, _parameters_name##_type>( \
327 std::move(exec), parameters) \
328 {} \
329 }; \
330 friend ::gko::stop::EnableDefaultCriterionFactory< \
331 _factory_name, _criterion, _parameters_name##_type>; \
332 \
333private: \
334 _parameters_name##_type _parameters_name##_; \
335 \
336public: \
337 static_assert(true, \
338 "This assert is used to counter the false positive extra " \
339 "semi-colon warnings")
340
341
342} // namespace stop
343} // namespace gko
344
345
346#endif // GKO_PUBLIC_CORE_STOP_CRITERION_HPP_
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition abstract_factory.hpp:75
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:374
This mixin provides a default implementation of a concrete factory.
Definition abstract_factory.hpp:154
Definition lin_op.hpp:146
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
The Updater class serves for convenient argument passing to the Criterion's check function.
Definition criterion.hpp:83
bool check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status, bool *one_changed) const
Calls the parent Criterion object's check method.
Definition criterion.hpp:101
Updater(const Updater &)=delete
Prevent copying and moving the object This is to enforce the use of argument passing and calling chec...
The Criterion class is a base class for all stopping criteria.
Definition criterion.hpp:64
Updater update()
Returns the updater object.
Definition criterion.hpp:150
bool check(uint8 stopping_id, bool set_finalized, array< stopping_status > *stop_status, bool *one_changed, const Updater &updater)
This checks whether convergence was reached for a certain criterion.
Definition criterion.hpp:165
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
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
This struct is used to pass parameters to the EnableDefaultCriterionFactoryCriterionFactory::generate...
Definition criterion.hpp:233