DASH  0.3.0
dash::SharedCounter< ValueType > Class Template Reference

A simple shared counter that allows atomic increment- and decrement operations. More...

#include <SharedCounter.h>

Public Member Functions

 SharedCounter ()
 Constructor. More...
 
 SharedCounter (dash::Team &team)
 
void inc (ValueType increment)
 Increment the shared counter value, atomic operation. More...
 
void dec (ValueType increment)
 Decrement the shared counter value, atomic operation. More...
 
ValueType get () const
 Read the current value of the shared counter. More...
 

Detailed Description

template<typename ValueType = int>
class dash::SharedCounter< ValueType >

A simple shared counter that allows atomic increment- and decrement operations.

TODO: Should probably be based on MPI_ACCUMULATE.

Definition at line 15 of file SharedCounter.h.

Constructor & Destructor Documentation

◆ SharedCounter()

template<typename ValueType = int>
dash::SharedCounter< ValueType >::SharedCounter ( )
inline

Constructor.

Definition at line 23 of file SharedCounter.h.

References dash::Array< ElementType, IndexType, PatternType, LocalMemSpaceT >::barrier(), dash::Array< ElementType, IndexType, PatternType, LocalMemSpaceT >::local, and dash::Team::size().

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  }
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.
void barrier() const
Establish a barrier for all units operating on the array, publishing all changes to all units...
Definition: Array.h:1254
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
local_type local
Local proxy object, allows use in range-based for loops.
Definition: Array.h:732

Member Function Documentation

◆ dec()

template<typename ValueType = int>
void dash::SharedCounter< ValueType >::dec ( ValueType  increment)
inline

Decrement the shared counter value, atomic operation.

Parameters
incrementDecrement value

Definition at line 54 of file SharedCounter.h.

57  {
58  _local_counts[_myid] -= increment;
59  }

◆ get()

template<typename ValueType = int>
ValueType dash::SharedCounter< ValueType >::get ( ) const
inline

Read the current value of the shared counter.

Accumulates increment/decrement values of every unit. Reading a shared is not atomic, use Team::barrier() to synchronize.

Time complexity
O(u) for u units in the associated team

Definition at line 68 of file SharedCounter.h.

References dash::Array< ElementType, IndexType, PatternType, LocalMemSpaceT >::local, and dash::ValueType.

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  }
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
struct dash::dart_operation ValueType
Reduce operands to their minimum value.
local_type local
Local proxy object, allows use in range-based for loops.
Definition: Array.h:732

◆ inc()

template<typename ValueType = int>
void dash::SharedCounter< ValueType >::inc ( ValueType  increment)
inline

Increment the shared counter value, atomic operation.

Parameters
incrementIncrement value

Definition at line 44 of file SharedCounter.h.

47  {
48  _local_counts[_myid] += increment;
49  }

The documentation for this class was generated from the following file: