DASH  0.3.0
UnitLocality.h
1 #ifndef DASH__UTIL__UNIT_LOCALITY_H__INCLUDED
2 #define DASH__UTIL__UNIT_LOCALITY_H__INCLUDED
3 
4 #include <dash/util/Locality.h>
5 #include <dash/util/LocalityDomain.h>
6 #include <dash/util/Config.h>
7 
8 #include <dash/algorithm/internal/String.h>
9 
12 
13 #include <dash/Exception.h>
14 #include <dash/Team.h>
15 
16 #include <string>
17 #include <vector>
18 #include <unordered_map>
19 #include <utility>
20 #include <iterator>
21 #include <algorithm>
22 
23 
24 namespace dash {
25 namespace util {
26 
31 {
32 private:
33  typedef UnitLocality self_t;
34 
35 public:
36 
38  const dash::Team & team,
39  team_unit_t unit)
40  : _team(&team)
41  {
42  DASH_ASSERT_RETURNS(
44  _team->dart_id(), unit, &_unit_locality),
45  DART_OK);
46 
47  dart_domain_locality_t * team_domain;
48  DASH_ASSERT_RETURNS(
50  team.dart_id(), ".", &team_domain),
51  DART_OK);
52 
53  DASH_ASSERT_RETURNS(
55  team_domain, _unit_locality->domain_tag, &_unit_domain),
56  DART_OK);
57 
58  dart_domain_locality_t * node_locality = _unit_domain;
59  while (node_locality->scope > DART_LOCALITY_SCOPE_NODE) {
60  node_locality = node_locality->parent;
61  }
62  _node_domain = dash::util::LocalityDomain(node_locality);
63  }
64 
66  global_unit_t unit)
68  { }
69 
70  UnitLocality()
72  { }
73 
74  UnitLocality(const UnitLocality &) = default;
75  UnitLocality & operator=(const UnitLocality &) = default;
76 
77  inline const dart_hwinfo_t & hwinfo() const
78  {
79  DASH_ASSERT(nullptr != _unit_locality);
80  return _unit_locality->hwinfo;
81  }
82 
83  inline dart_hwinfo_t & hwinfo()
84  {
85  DASH_ASSERT(nullptr != _unit_locality);
86  return _unit_locality->hwinfo;
87  }
88 
89  inline dart_domain_locality_t & domain()
90  {
91  DASH_ASSERT(nullptr != _unit_locality);
92  return *_unit_domain;
93  }
94 
95  inline const dart_domain_locality_t & domain() const
96  {
97  DASH_ASSERT(nullptr != _unit_locality);
98  return *_unit_domain;
99  }
100 
101  inline const dash::Team & team() const
102  {
103  if (nullptr == _team) {
104  return dash::Team::Null();
105  }
106  return *_team;
107  }
108 
109  inline team_unit_t unit_id() const
110  {
111  return nullptr == _unit_locality
113  : team_unit_t(_unit_locality->unit);
114  }
115 
116  inline dash::util::LocalityDomain & node_domain()
117  {
118  return _node_domain;
119  }
120 
121  inline dash::util::LocalityDomain parent()
122  {
123  return dash::util::LocalityDomain(*_unit_domain->parent);
124  }
125 
126  inline dash::util::LocalityDomain parent_in_scope(
127  dash::util::Locality::Scope scope)
128  {
129  if (scope == dash::util::Locality::Scope::Node) {
130  return node_domain();
131  }
132 
133  dart_domain_locality_t * parent_domain = _unit_domain;
134  for (int rlevel = _unit_locality->hwinfo.num_scopes;
135  rlevel >= 0;
136  rlevel--) {
137  if (parent_domain == nullptr) {
138  DASH_THROW(
140  "Unit domain is undefined");
141  }
142  if (static_cast<int>(_unit_locality->hwinfo.scopes[rlevel].scope) <=
143  static_cast<int>(scope)) {
144  return dash::util::LocalityDomain(*parent_domain);
145  }
146  parent_domain = parent_domain->parent;
147  }
148  DASH_THROW(
150  "Could not find parent domain of unit in scope " << scope);
151  }
152 
153  inline std::string domain_tag() const
154  {
155  DASH_ASSERT(nullptr != _unit_locality);
156  return _unit_domain->domain_tag;
157  }
158 
159  inline std::string host() const
160  {
161  DASH_ASSERT(nullptr != _unit_locality);
162  return _unit_locality->hwinfo.host;
163  }
164 
165  inline void set_domain_tag(
166  const std::string & tag)
167  {
168  strcpy(_unit_domain->domain_tag, tag.c_str());
169  }
170 
171  inline void set_host(
172  const std::string & hostname)
173  {
174  strcpy(_unit_locality->hwinfo.host, hostname.c_str());
175  }
176 
177  inline int num_cores() const
178  {
179  DASH_ASSERT(nullptr != _unit_locality);
180  return (_unit_locality->hwinfo.num_cores);
181  }
182 
183  inline int min_threads()
184  {
185  return (_unit_locality == nullptr)
186  ? -1 : std::max<int>(_unit_locality->hwinfo.min_threads, 1);
187  }
188 
189  inline int max_threads()
190  {
191  return (_unit_locality == nullptr)
192  ? -1 : std::max<int>(_unit_locality->hwinfo.max_threads, 1);
193  }
194 
195  inline int num_threads() const
196  {
197  DASH_ASSERT(nullptr != _unit_locality);
198  return (dash::util::Config::get<bool>("DASH_MAX_SMT")
199  ? _unit_locality->hwinfo.max_threads
200  : _unit_locality->hwinfo.min_threads);
201  }
202 
203  inline int num_numa() const
204  {
205  dart_domain_locality_t * dom = _unit_domain;
206  while (dom->scope >= DART_LOCALITY_SCOPE_NUMA) {
207  dom = dom->parent;
208  }
209  return dom->num_domains;
210  }
211 
212  inline int numa_id() const
213  {
214  return (nullptr == _unit_locality ? -1 : _unit_locality->hwinfo.numa_id);
215  }
216 
217  inline int cpu_id() const
218  {
219  return (nullptr == _unit_locality ? -1 : _unit_locality->hwinfo.cpu_id);
220  }
221 
222  inline int cpu_mhz() const
223  {
224  DASH_ASSERT(nullptr != _unit_locality);
225  return (_unit_locality->hwinfo.max_cpu_mhz);
226  }
227 
228  inline int max_shmem_mbps() const
229  {
230  DASH_ASSERT(nullptr != _unit_locality);
231  return (_unit_locality->hwinfo.max_shmem_mbps);
232  }
233 
234  inline int max_cpu_mhz()
235  {
236  return (_unit_locality == nullptr)
237  ? -1 : std::max<int>(_unit_locality->hwinfo.max_cpu_mhz, 1);
238  }
239 
240  inline int min_cpu_mhz()
241  {
242  return (_unit_locality == nullptr)
243  ? -1 : std::max<int>(_unit_locality->hwinfo.min_cpu_mhz, 1);
244  }
245 
246  inline int cache_line_size(int cache_level)
247  {
248  return (_unit_locality == nullptr)
249  ? 64 : std::max<int>(
250  _unit_locality->hwinfo.cache_line_sizes[cache_level],
251  64);
252  }
253 
254  inline std::string hostname()
255  {
256  return (_unit_locality == nullptr) ? "" : _unit_locality->hwinfo.host;
257  }
258 
306  inline int num_domain_threads()
307  {
308  auto n_threads = num_cores();
309  if (dash::util::Config::get<bool>("DASH_DISABLE_THREADS")) {
310  // Threads disabled in unit scope:
311  n_threads = 1;
312  } else if (dash::util::Config::get<bool>("DASH_MAX_SMT")) {
313  // Configured to use SMT (hyperthreads):
314  n_threads *= max_threads();
315  } else {
316  // Start one thread on every physical core assigned to this unit:
317  n_threads *= min_threads();
318  }
319  if (dash::util::Config::is_set("DASH_MAX_UNIT_THREADS")) {
320  n_threads = std::min(dash::util::Config::get<int>(
321  "DASH_MAX_UNIT_THREADS"),
322  n_threads);
323  }
324  return n_threads;
325  }
326 
327 private:
328 
329  const dash::Team * _team = nullptr;
330  dart_unit_locality_t * _unit_locality = nullptr;
331  dart_domain_locality_t * _unit_domain = nullptr;
332  dash::util::LocalityDomain _node_domain;
333 
334 }; // class UnitLocality
335 
336 } // namespace util
337 } // namespace dash
338 
339 #endif // DASH__UTIL__UNIT_LOCALITY_H__INCLUDED
constexpr team_unit_t UNDEFINED_TEAM_UNIT_ID
Invalid local unit ID.
Definition: Types.h:341
char host[((int)(256))]
Hostname of the domain&#39;s node or 0 if unspecified.
Definition: dart_types.h:370
int max_cpu_mhz
Maximum clock frequency of CPUs in the domain.
Definition: dart_types.h:391
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
Locality and topology information of a single unit.
Definition: dart_types.h:672
dart_ret_t dart_unit_locality(dart_team_t team, dart_team_unit_t unit, dart_unit_locality_t **loc)
Locality information of the unit with the specified team-relative id.
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
int num_cores
Total number of CPUs in the associated domain.
Definition: dart_types.h:373
Signals success.
Definition: dart_types.h:33
Node-level locality scope, components may be heterogenous.
Definition: dart_types.h:301
char domain_tag[((int)(32))]
Hierarchical domain identifier, represented as dot-separated list of relative indices on every level ...
Definition: dart_types.h:606
struct dash::unit_id< dash::global_unit, dart_global_unit_t > global_unit_t
Unit ID to use for global IDs.
Definition: Types.h:332
dart_hwinfo_t hwinfo
Hardware specification of the unit&#39;s affinity.
Definition: dart_types.h:680
int num_domains
Number of subordinate domains.
Definition: dart_types.h:627
A domain is a group of processing entities such as cores in a specific NUMA domain or a Intel MIC ent...
Definition: dart_types.h:597
int num_domain_threads()
Number of threads currently available to the active unit.
Definition: UnitLocality.h:306
int min_cpu_mhz
Minimum clock frequency of CPUs in the domain.
Definition: dart_types.h:389
A Team instance specifies a subset of all available units.
Definition: Team.h:41
dart_ret_t dart_domain_team_locality(dart_team_t team, const char *domain_tag, dart_domain_locality_t **team_domain_out)
Locality information of the team domain with the specified id tag.
dart_locality_scope_t scope
The domain&#39;s scope identifier.
Definition: dart_types.h:352
int max_threads
Maximum number of CPU threads per core.
Definition: dart_types.h:403
Locality of homogenous components in different NUMA domains.
Definition: dart_types.h:306
dart_locality_scope_t scope
Locality scope of the domain.
Definition: dart_types.h:613
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
Wrapper of a single dart_domain_locality_t object.
int cache_line_sizes[((int)(5))]
Cache line sizes by cache level (L1, L2, L3).
Definition: dart_types.h:396
Hardware locality information for a single locality domain.
Definition: dart_types.h:367
dart_ret_t dart_domain_find(const dart_domain_locality_t *domain_in, const char *domain_tag, dart_domain_locality_t **subdomain_out)
Locality information of the subdomain with the specified id tag.
dart_locality_scope_pos_t scopes[((int)(12))]
Ancestor locality scopes in bottom-up hierarchical order.
Definition: dart_types.h:415
static Team & Null()
The invariant Team instance representing an undefined team.
Definition: Team.h:229
int min_threads
Minimum number of CPU threads per core.
Definition: dart_types.h:401
int max_shmem_mbps
Maximum local shared memory bandwidth in MB/s.
Definition: dart_types.h:406
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
struct dart_domain_locality_s * parent
Pointer to descriptor of parent domain or 0 if no parent domain is specified.
Definition: dart_types.h:624
dart_team_unit_t unit
Unit ID relative to team.
Definition: dart_types.h:674
int cpu_id
The unit&#39;s affine processing unit (SMP), unique identifier within a processing module.
Definition: dart_types.h:386
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
Definition: Team.h:522
Wrapper of a single dart_unit_locality_t object.
Definition: UnitLocality.h:30