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
stopping_status.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_STOPPING_STATUS_HPP_
34#define GKO_PUBLIC_CORE_STOP_STOPPING_STATUS_HPP_
35
36
37#include <ginkgo/core/base/array.hpp>
38#include <ginkgo/core/base/types.hpp>
39
40
41namespace gko {
42
43
50 friend GKO_ATTRIBUTES GKO_INLINE bool operator==(
51 const stopping_status& x, const stopping_status& y) noexcept;
52 friend GKO_ATTRIBUTES GKO_INLINE bool operator!=(
53 const stopping_status& x, const stopping_status& y) noexcept;
54
55public:
60 GKO_ATTRIBUTES GKO_INLINE bool has_stopped() const noexcept
61 {
62 return get_id();
63 }
64
69 GKO_ATTRIBUTES GKO_INLINE bool has_converged() const noexcept
70 {
71 return data_ & converged_mask_;
72 }
73
79 GKO_ATTRIBUTES GKO_INLINE bool is_finalized() const noexcept
80 {
81 return data_ & finalized_mask_;
82 }
83
88 GKO_ATTRIBUTES GKO_INLINE uint8 get_id() const noexcept
89 {
90 return data_ & id_mask_;
91 }
92
96 GKO_ATTRIBUTES GKO_INLINE void reset() noexcept { data_ = uint8{0}; }
97
105 GKO_ATTRIBUTES GKO_INLINE void stop(uint8 id,
106 bool set_finalized = true) noexcept
107 {
108 if (!this->has_stopped()) {
109 data_ |= (id & id_mask_);
110 if (set_finalized) {
111 data_ |= finalized_mask_;
112 }
113 }
114 }
115
122 GKO_ATTRIBUTES GKO_INLINE void converge(uint8 id,
123 bool set_finalized = true) noexcept
124 {
125 if (!this->has_stopped()) {
126 data_ |= converged_mask_ | (id & id_mask_);
127 if (set_finalized) {
128 data_ |= finalized_mask_;
129 }
130 }
131 }
132
137 GKO_ATTRIBUTES GKO_INLINE void finalize() noexcept
138 {
139 if (this->has_stopped()) {
140 data_ |= finalized_mask_;
141 }
142 }
143
144private:
145 static constexpr uint8 converged_mask_ = uint8{1} << 7;
146 static constexpr uint8 finalized_mask_ = uint8{1} << 6;
147 static constexpr uint8 id_mask_ = (uint8{1} << 6) - uint8{1};
148
149 uint8 data_;
150};
151
152
162GKO_ATTRIBUTES GKO_INLINE bool operator==(const stopping_status& x,
163 const stopping_status& y) noexcept
164{
165 return x.data_ == y.data_;
166}
167
168
177GKO_ATTRIBUTES GKO_INLINE bool operator!=(const stopping_status& x,
178 const stopping_status& y) noexcept
179{
180 return x.data_ != y.data_;
181}
182
183
184} // namespace gko
185
186
187#endif // GKO_PUBLIC_CORE_STOP_STOPPING_STATUS_HPP_
This class is used to keep track of the stopping status of one vector.
Definition stopping_status.hpp:49
void finalize() noexcept
Set the result to be finalized (it needs to be stopped or converged first).
Definition stopping_status.hpp:137
void converge(uint8 id, bool set_finalized=true) noexcept
Call if convergence occurred.
Definition stopping_status.hpp:122
friend bool operator!=(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are different.
Definition stopping_status.hpp:177
bool is_finalized() const noexcept
Check if the corresponding vector stores the finalized result.
Definition stopping_status.hpp:79
friend bool operator==(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are equivalent.
Definition stopping_status.hpp:162
bool has_converged() const noexcept
Check if convergence was reached.
Definition stopping_status.hpp:69
void stop(uint8 id, bool set_finalized=true) noexcept
Call if a stop occurred due to a hard limit (and convergence was not reached).
Definition stopping_status.hpp:105
uint8 get_id() const noexcept
Get the id of the stopping criterion which caused the stop.
Definition stopping_status.hpp:88
bool has_stopped() const noexcept
Check if any stopping criteria was fulfilled.
Definition stopping_status.hpp:60
void reset() noexcept
Clear all flags.
Definition stopping_status.hpp:96
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
constexpr bool operator!=(const dim< Dimensionality, DimensionType > &x, const dim< Dimensionality, DimensionType > &y)
Checks if two dim objects are different.
Definition dim.hpp:258