NEST main@caf0ae8
 
Loading...
Searching...
No Matches
per_thread_bool_indicator.h
Go to the documentation of this file.
1/*
2 * per_thread_bool_indicator.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef PER_THREAD_BOOL_INDICATOR_H
24#define PER_THREAD_BOOL_INDICATOR_H
25
26// C++ includes:
27#include <atomic>
28#include <cassert>
29#include <cstddef>
30#include <cstdint>
31
32// Includes from nestkernel:
33#include "nest_types.h"
34#include "vp_manager.h"
35
36
37namespace nest
38{
39
46{
47public:
49 BoolIndicatorUInt64( const bool status );
50
51 bool is_true() const;
52 bool is_false() const;
53
54
55protected:
56 void set_true();
57 void set_false();
58 void logical_and( const bool status );
59
60private:
61 static constexpr std::uint_fast64_t true_uint64 = true;
62 static constexpr std::uint_fast64_t false_uint64 = false;
63 std::uint_fast64_t status_;
65};
66
67inline bool
69{
70 return ( status_ == true_uint64 );
71}
72
73inline bool
75{
76 return ( status_ == false_uint64 );
77}
78
79inline void
84
85inline void
90
91inline void
93{
94 status_ = ( static_cast< bool >( status_ ) and status );
95}
96
104{
105public:
107
108 BoolIndicatorUInt64& operator[]( const size_t tid );
109
110 void
111 set_true( const size_t tid )
112 {
113 if ( per_thread_status_[ tid ].is_false() )
114 {
115 are_true_++;
116 per_thread_status_[ tid ].set_true();
117 }
118 }
119
120 void
121 set_false( const size_t tid )
122 {
123 if ( per_thread_status_[ tid ].is_true() )
124 {
125 are_true_--;
126 per_thread_status_[ tid ].set_false();
127 }
128 }
129
130 void
131 logical_and( const size_t tid, const bool status )
132 {
133 if ( per_thread_status_[ tid ].is_true() and not status )
134 {
135 are_true_--;
136 per_thread_status_[ tid ].set_false();
137 }
138 }
139
143 void initialize( const size_t num_threads, const bool status );
144
148 bool all_false() const;
149
153 bool all_true() const;
154
158 bool any_false() const;
159
163 bool any_true() const;
164
165private:
166 std::vector< BoolIndicatorUInt64 > per_thread_status_;
167 int size_ { 0 };
168
175 std::atomic< int > are_true_ { 0 };
176};
177
178} // namespace nest
179
180#endif /* PER_THREAD_BOOL_INDICATOR_H */
A wrapper class for an integer that is only allowed to take the values 0 and 1.
Definition per_thread_bool_indicator.h:46
static constexpr std::uint_fast64_t true_uint64
Definition per_thread_bool_indicator.h:61
void logical_and(const bool status)
Definition per_thread_bool_indicator.h:92
std::uint_fast64_t status_
Definition per_thread_bool_indicator.h:63
bool is_false() const
Definition per_thread_bool_indicator.h:74
void set_true()
Definition per_thread_bool_indicator.h:80
BoolIndicatorUInt64()
Definition per_thread_bool_indicator.cpp:32
void set_false()
Definition per_thread_bool_indicator.h:86
static constexpr std::uint_fast64_t false_uint64
Definition per_thread_bool_indicator.h:62
bool is_true() const
Definition per_thread_bool_indicator.h:68
A thread-safe vector to keep track of the status across threads, for example during gather operations...
Definition per_thread_bool_indicator.h:104
bool all_false() const
Waits for all threads and returns whether all elements are false.
Definition per_thread_bool_indicator.cpp:66
void set_false(const size_t tid)
Definition per_thread_bool_indicator.h:121
std::vector< BoolIndicatorUInt64 > per_thread_status_
Definition per_thread_bool_indicator.h:166
bool all_true() const
Waits for all threads and returns whether all elements are true.
Definition per_thread_bool_indicator.cpp:82
int size_
Definition per_thread_bool_indicator.h:167
std::atomic< int > are_true_
Number of per-thread indicators currently true.
Definition per_thread_bool_indicator.h:175
BoolIndicatorUInt64 & operator[](const size_t tid)
Definition per_thread_bool_indicator.cpp:43
PerThreadBoolIndicator()
Definition per_thread_bool_indicator.h:106
bool any_true() const
Waits for all threads and returns whether any elements are true.
Definition per_thread_bool_indicator.cpp:105
void initialize(const size_t num_threads, const bool status)
Resize to the given number of threads and set all elements to false.
Definition per_thread_bool_indicator.cpp:49
void set_true(const size_t tid)
Definition per_thread_bool_indicator.h:111
void logical_and(const size_t tid, const bool status)
Definition per_thread_bool_indicator.h:131
bool any_false() const
Waits for all threads and returns whether any elements are false.
Definition per_thread_bool_indicator.cpp:93
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33