Ginkgo Generated from branch based on main. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
stopping_status.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_STOP_STOPPING_STATUS_HPP_
6#define GKO_PUBLIC_CORE_STOP_STOPPING_STATUS_HPP_
7
8
9#include <ginkgo/core/base/array.hpp>
10#include <ginkgo/core/base/types.hpp>
11
12
13namespace gko {
14
15
22 friend GKO_ATTRIBUTES GKO_INLINE bool operator==(
23 const stopping_status& x, const stopping_status& y) noexcept;
24 friend GKO_ATTRIBUTES GKO_INLINE bool operator!=(
25 const stopping_status& x, const stopping_status& y) noexcept;
26
27public:
32 GKO_ATTRIBUTES GKO_INLINE bool has_stopped() const noexcept
33 {
34 return get_id();
35 }
36
41 GKO_ATTRIBUTES GKO_INLINE bool has_converged() const noexcept
42 {
43 return data_ & converged_mask_;
44 }
45
51 GKO_ATTRIBUTES GKO_INLINE bool is_finalized() const noexcept
52 {
53 return data_ & finalized_mask_;
54 }
55
60 GKO_ATTRIBUTES GKO_INLINE uint8 get_id() const noexcept
61 {
62 return data_ & id_mask_;
63 }
64
68 GKO_ATTRIBUTES GKO_INLINE void reset() noexcept { data_ = uint8{0}; }
69
77 GKO_ATTRIBUTES GKO_INLINE void stop(uint8 id,
78 bool set_finalized = true) noexcept
79 {
80 if (!this->has_stopped()) {
81 data_ |= (id & id_mask_);
82 if (set_finalized) {
83 data_ |= finalized_mask_;
84 }
85 }
86 }
87
94 GKO_ATTRIBUTES GKO_INLINE void converge(uint8 id,
95 bool set_finalized = true) noexcept
96 {
97 if (!this->has_stopped()) {
98 data_ |= converged_mask_ | (id & id_mask_);
99 if (set_finalized) {
100 data_ |= finalized_mask_;
101 }
102 }
103 }
104
109 GKO_ATTRIBUTES GKO_INLINE void finalize() noexcept
110 {
111 if (this->has_stopped()) {
112 data_ |= finalized_mask_;
113 }
114 }
115
116private:
117 static constexpr uint8 converged_mask_ = uint8{1} << 7;
118 static constexpr uint8 finalized_mask_ = uint8{1} << 6;
119 static constexpr uint8 id_mask_ = (uint8{1} << 6) - uint8{1};
120
121 uint8 data_;
122};
123
124
134GKO_ATTRIBUTES GKO_INLINE bool operator==(const stopping_status& x,
135 const stopping_status& y) noexcept
136{
137 return x.data_ == y.data_;
138}
139
140
149GKO_ATTRIBUTES GKO_INLINE bool operator!=(const stopping_status& x,
150 const stopping_status& y) noexcept
151{
152 return x.data_ != y.data_;
153}
154
155
156} // namespace gko
157
158
159#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:21
friend bool operator==(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are equivalent.
Definition stopping_status.hpp:134
void finalize() noexcept
Set the result to be finalized (it needs to be stopped or converged first).
Definition stopping_status.hpp:109
void converge(uint8 id, bool set_finalized=true) noexcept
Call if convergence occurred.
Definition stopping_status.hpp:94
bool is_finalized() const noexcept
Check if the corresponding vector stores the finalized result.
Definition stopping_status.hpp:51
bool has_converged() const noexcept
Check if convergence was reached.
Definition stopping_status.hpp:41
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:77
friend bool operator!=(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are different.
Definition stopping_status.hpp:149
uint8 get_id() const noexcept
Get the id of the stopping criterion which caused the stop.
Definition stopping_status.hpp:60
bool has_stopped() const noexcept
Check if any stopping criteria was fulfilled.
Definition stopping_status.hpp:32
void reset() noexcept
Clear all flags.
Definition stopping_status.hpp:68
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::uint8_t uint8
8-bit unsigned integral type.
Definition types.hpp:118