DASH  0.3.0
Coevent.h
1 #ifndef DASH__COEVENT_H__INCLUDED
2 #define DASH__COEVENT_H__INCLUDED
3 
4 #include <thread>
5 
6 #include <dash/Array.h>
7 #include <dash/Atomic.h>
8 #include <dash/GlobPtr.h>
9 #include <dash/Exception.h>
10 #include <dash/Team.h>
11 #include <dash/Types.h>
12 
13 #include <dash/algorithm/Fill.h>
14 
15 #include <dash/coarray/CoEventIter.h>
16 #include <dash/coarray/CoEventRef.h>
17 
18 #include <dash/memory/MemorySpace.h>
19 
20 namespace dash {
21 
45 class Coevent {
46 private:
49  using pointer = typename array::pointer;
50  using const_pointer = typename pointer::const_type;
51 
52 public:
53  // Types
57  using size_type = int;
58 
59 private:
60  array _event_counts;
61 public:
62 
66  explicit Coevent(Team & team = dash::Team::All())
67  : _team(&team) {
69  initialize(team);
70  }
71  }
72 
73  iterator begin() DASH_NOEXCEPT {
74  return iterator(static_cast<pointer>(_event_counts.begin()));
75  }
76 
77  const_iterator begin() const DASH_NOEXCEPT {
78  // TODO FIXME
79  //CoeventIter is not const correct, so we need a hack and unpack a nonconst
80  //pointer from a const iterator to make the comiler happy.
81  //return const_iterator(static_cast<const_pointer>(_event_counts.begin()));
82  auto dart_ptr = _event_counts.begin().dart_gptr();
83  pointer nonconst_ptr{dart_ptr};
84  return const_iterator(nonconst_ptr);
85  }
86 
87  iterator end() {
88  DASH_ASSERT_MSG(dash::is_initialized(), "DASH is not initialized");
89  return iterator(static_cast<pointer>(_event_counts.end()));
90  }
91 
92  const_iterator end() const {
93  DASH_ASSERT_MSG(dash::is_initialized(), "DASH is not initialized");
94  // TODO FIXME
95  //CoevenIter is not const correct, so we need a hack and unpack a nonconst
96  //pointer from a const iterator to make the comiler happy.
97  //return const_iterator(static_cast<const_pointer>(_event_counts.end()));
98  auto dart_ptr = _event_counts.end().dart_gptr();
99  pointer nonconst_ptr{dart_ptr};
100  return const_iterator(nonconst_ptr);
101  }
102 
103  size_type size() const {
104  DASH_ASSERT_MSG(dash::is_initialized(), "DASH is not initialized");
105  return _team->size();
106  }
107 
112  inline void wait(int count = 1) {
113  auto gref = _event_counts.at(_team->myid().id);
114  int current;
115  do {
116 #ifdef DASH_DEBUG
117  // avoid spamming the logs while busy waiting
118  DASH_LOG_DEBUG("waiting for event at gptr",
119  static_cast<pointer>(_event_counts.begin()
120  +_team->myid().id));
121  std::this_thread::sleep_for(std::chrono::milliseconds(100));
122 #endif
123  current = gref.get();
124  } while (current < count);
125  // decrement the counter
126  gref.sub(count);
127  }
128 
129  inline int test() {
130  DASH_LOG_DEBUG("test for events on this unit");
131  return _event_counts.at(static_cast<int>(_team->myid())).load();
132  }
133 
138  inline void initialize(Team & team = dash::Team::All()) {
139  if(!_is_initialized){
140  _team = &team;
141  _event_counts.allocate(_team->size());
142  dash::fill(_event_counts.begin(), _event_counts.end(), 0);
143  _event_counts.barrier();
144  _is_initialized = true;
145  }
146  }
147 
148  inline Team & team() {
149  return *_team;
150  }
151 
155  inline reference operator()(const int & unit) DASH_ASSERT_NOEXCEPT {
156  DASH_ASSERT_MSG(dash::is_initialized(), "DASH is not initialized");
157  auto ptr = static_cast<pointer>(_event_counts.begin() + unit);
158  return reference(ptr);
159  }
160 
164  inline reference operator()(const team_unit_t & unit) DASH_ASSERT_NOEXCEPT {
165  return this->operator()(static_cast<int>(unit));
166  }
167 
168 private:
169  Team * _team;
170  bool _is_initialized = false;
171 };
172 
173 } // namespace dash
174 
175 #endif /* DASH__COEVENT_H__INCLUDED */
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
void wait(int count=1)
wait for a given number of incoming events.
Definition: Coevent.h:112
iterator end() noexcept
Global pointer to the end of the array.
Definition: Array.h:1057
Type wrapper to mark any trivial type atomic.
bool is_initialized()
Check whether DASH has been initialized already.
void fill(GlobIterType first, GlobIterType last, const typename GlobIterType::value_type &value)
Assigns the given value to the elements in the range [first, last)
Definition: Fill.h:35
size_t size() const
The number of units in this team.
Definition: Team.h:498
Coevent(Team &team=dash::Team::All())
Constructor to setup and initialize an Coevent.
Definition: Coevent.h:66
A Team instance specifies a subset of all available units.
Definition: Team.h:41
reference at(size_type global_pos)
Random access assignment operator, range-checked.
Definition: Array.h:1132
reference operator()(const int &unit)
Operator to select event at given unit.
Definition: Coevent.h:155
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
void barrier() const
Establish a barrier for all units operating on the array, publishing all changes to all units...
Definition: Array.h:1254
A fortran style coevent.
Definition: Coevent.h:45
reference operator()(const team_unit_t &unit)
Operator to select event at given unit.
Definition: Coevent.h:164
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
void initialize(Team &team=dash::Team::All())
initializes the Coevent.
Definition: Coevent.h:138
iterator begin() noexcept
Global pointer to the beginning of the array.
Definition: Array.h:1040
bool allocate(size_type nelem, dash::DistributionSpec< 1 > distribution, dash::Team &team=dash::Team::All())
Delayed allocation of global memory using a one-dimensional distribution spec.
Definition: Array.h:1319