Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
stopping_status.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_STOP_STOPPING_STATUS_HPP_
34 #define GKO_CORE_STOP_STOPPING_STATUS_HPP_
35 
36 
37 #include <ginkgo/core/base/array.hpp>
38 #include <ginkgo/core/base/types.hpp>
39 
40 
41 namespace 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 
55 public:
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 
144 private:
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 
162 GKO_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 
177 GKO_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_CORE_STOP_STOPPING_STATUS_HPP_
friend bool operator!=(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are different.
Definition: stopping_status.hpp:177
friend bool operator==(const stopping_status &x, const stopping_status &y) noexcept
Checks if two stopping statuses are equivalent.
Definition: stopping_status.hpp:162
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
This class is used to keep track of the stopping status of one vector.
Definition: stopping_status.hpp:49
bool has_stopped() const noexcept
Check if any stopping criteria was fulfilled.
Definition: stopping_status.hpp:60
std::uint8_t uint8
8-bit unsigned integral type.
Definition: types.hpp:123
uint8 get_id() const noexcept
Get the id of the stopping criterion which caused the stop.
Definition: stopping_status.hpp:88
void reset() noexcept
Clear all flags.
Definition: stopping_status.hpp:96
bool is_finalized() const noexcept
Check if the corresponding vector stores the finalized result.
Definition: stopping_status.hpp:79
void stop(uint8 id, bool set_finalized=true) noexcept
Call if a stop occured due to a hard limit (and convergence was not reached).
Definition: stopping_status.hpp:105
bool has_converged() const noexcept
Check if convergence was reached.
Definition: stopping_status.hpp:69
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 occured.
Definition: stopping_status.hpp:122