DASH  0.3.0
Comutex.h
1 #ifndef COMUTEX_H_INCLUDED
2 #define COMUTEX_H_INCLUDED
3 
4 #include <dash/Exception.h>
5 #include <dash/Team.h>
6 #include <dash/Mutex.h>
7 
8 #include <vector>
9 
10 namespace dash {
11 
46 class Comutex {
47 private:
48  using _storage_type = std::vector<dash::Mutex>;
49 public:
50  // Types
51  using iterator = typename _storage_type::iterator;
52  using const_iterator = typename _storage_type::const_iterator;
53  using reference = typename _storage_type::reference;
54  using size_type = typename _storage_type::size_type;
55 
56 public:
57 
62  explicit Comutex(Team & team = dash::Team::All()) {
64  initialize(team);
65  }
66  }
67 
68  iterator begin() noexcept {
69  return _mutexes.begin();
70  }
71 
72  const_iterator begin() const noexcept {
73  return _mutexes.begin();
74  }
75 
76  iterator end() noexcept {
77  return _mutexes.end();
78  }
79 
80  const_iterator end() const noexcept {
81  return _mutexes.end();
82  }
83 
84  size_type size() const noexcept {
85  return _mutexes.size();
86  }
87 
92  inline void initialize(Team & team){
93  if(!_is_initialized){
94  _team = &team;
95  _mutexes.reserve(team.size());
96  for(decltype(team.size()) i = 0; i<team.size(); ++i){
97  _mutexes.emplace_back(team);
98  }
99  } else {
100  DASH_ASSERT_MSG((team == *_team),
101  "Comutex was initialized with a different team");
102  }
103  _is_initialized = true;
104  }
105 
106  inline Team & team() {
107  return *_team;
108  }
109 
113  inline reference operator()(const int & unit) {
114  DASH_ASSERT_MSG(_is_initialized, "Comutex is not initialized");
115  return _mutexes.at(unit);
116  }
117 
121  inline reference operator()(const team_unit_t & unit) {
122  return this->operator()(static_cast<int>(unit));
123  }
124 
125 private:
126  _storage_type _mutexes;
127  Team * _team{};
128  bool _is_initialized = false;
129 };
130 
131 } // namespace dash
132 
133 #endif /* COMUTEX_H_INCLUDED */
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
bool is_initialized()
Check whether DASH has been initialized already.
Comutex(Team &team=dash::Team::All())
Constructor to setup and initialize an Comutex.
Definition: Comutex.h:62
size_t size() const
The number of units in this team.
Definition: Team.h:498
A fortran style comutex.
Definition: Comutex.h:46
A Team instance specifies a subset of all available units.
Definition: Team.h:41
void initialize(Team &team)
initializeds the mutexes.
Definition: Comutex.h:92
reference operator()(const int &unit)
Operator to select mutex at given unit.
Definition: Comutex.h:113
reference operator()(const team_unit_t &unit)
Operator to select mutex at given unit.
Definition: Comutex.h:121
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
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213