DASH  0.3.0
SharedCounter.h
1 #ifndef DASH__SHARED_COUNTER_H_
2 #define DASH__SHARED_COUNTER_H_
3 
4 #include <dash/Array.h>
5 
6 namespace dash {
7 
14 template<typename ValueType = int>
16 private:
18 
19 public:
24  : _num_units(dash::Team::All().size()),
25  _myid(dash::Team::All().myid()),
26  _local_counts(_num_units)
27  {
28  _local_counts.local[0] = 0;
29  _local_counts.barrier();
30  }
31 
33  : _num_units(team.size()),
34  _myid(team.myid()),
35  _local_counts(_num_units, team)
36  {
37  _local_counts.local[0] = 0;
38  _local_counts.barrier();
39  }
40 
44  void inc(
46  ValueType increment)
47  {
48  _local_counts[_myid] += increment;
49  }
50 
54  void dec(
56  ValueType increment)
57  {
58  _local_counts[_myid] -= increment;
59  }
60 
68  ValueType get() const
69  {
70  ValueType acc = 0;
71  for (team_unit_t i{0}; i < _num_units; ++i) {
72  // use local access on own counter value:
73  acc += (i == _myid
74  ? _local_counts.local[0]
75  : _local_counts[i]);
76  }
77  return acc;
78  }
79 
80 private:
82  size_t _num_units;
84  team_unit_t _myid;
86  dash::Array<ValueType> _local_counts;
87 };
88 
89 } // namespace dash
90 
91 #endif // DASH__SHARED_COUNTER_H_
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
size_t size()
Return the number of units in the global team.
A simple shared counter that allows atomic increment- and decrement operations.
Definition: SharedCounter.h:15
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
SharedCounter()
Constructor.
Definition: SharedCounter.h:23
size_t size() const
The number of units in this team.
Definition: Team.h:498
A Team instance specifies a subset of all available units.
Definition: Team.h:41
void inc(ValueType increment)
Increment the shared counter value, atomic operation.
Definition: SharedCounter.h:44
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
struct dash::dart_operation ValueType
Reduce operands to their minimum value.
void dec(ValueType increment)
Decrement the shared counter value, atomic operation.
Definition: SharedCounter.h:54
local_type local
Local proxy object, allows use in range-based for loops.
Definition: Array.h:732