Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.3.3

  |   Home   |   Support   |   FAQ   |  
linux-libnuma.h
1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2010 inria. All rights reserved.
4  * Copyright © 2009-2010, 2012 Université Bordeaux 1
5  * See COPYING in top-level directory.
6  */
7 
24 #ifndef HWLOC_LINUX_LIBNUMA_H
25 #define HWLOC_LINUX_LIBNUMA_H
26 
27 #include <hwloc.h>
28 #include <numa.h>
29 
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 
51 static inline int
53  unsigned long *mask, unsigned long *maxnode)
54 {
55  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
56  unsigned long outmaxnode = -1;
57 
58  /* round-up to the next ulong and clear all bytes */
59  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
60  memset(mask, 0, *maxnode/8);
61 
62  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
63  hwloc_obj_t node = NULL;
64  while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL) {
65  if (node->os_index >= *maxnode)
66  continue;
67  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
68  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
69  outmaxnode = node->os_index;
70  }
71 
72  } else {
73  /* if no numa, libnuma assumes we have a single node */
74  if (!hwloc_bitmap_iszero(cpuset)) {
75  mask[0] = 1;
76  outmaxnode = 0;
77  }
78  }
79 
80  *maxnode = outmaxnode+1;
81  return 0;
82 }
83 
94 static inline int
96  unsigned long *mask, unsigned long *maxnode)
97 {
98  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
99  unsigned long outmaxnode = -1;
100 
101  /* round-up to the next ulong and clear all bytes */
102  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
103  memset(mask, 0, *maxnode/8);
104 
105  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
106  hwloc_obj_t node = NULL;
107  while ((node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NODE, node)) != NULL) {
108  if (node->os_index >= *maxnode)
109  continue;
110  if (!hwloc_bitmap_isset(nodeset, node->os_index))
111  continue;
112  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
113  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
114  outmaxnode = node->os_index;
115  }
116 
117  } else {
118  /* if no numa, libnuma assumes we have a single node */
119  if (!hwloc_bitmap_iszero(nodeset)) {
120  mask[0] = 1;
121  outmaxnode = 0;
122  }
123  }
124 
125  *maxnode = outmaxnode+1;
126  return 0;
127 }
128 
138 static inline int
140  const unsigned long *mask, unsigned long maxnode)
141 {
142  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
143 
144  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
145  hwloc_obj_t node = NULL;
146  hwloc_bitmap_zero(cpuset);
147  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
148  if (node->os_index < maxnode
149  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
150  hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
151  } else {
152  /* if no numa, libnuma assumes we have a single node */
153  if (mask[0] & 1)
155  else
156  hwloc_bitmap_zero(cpuset);
157  }
158 
159  return 0;
160 }
161 
171 static inline int
173  const unsigned long *mask, unsigned long maxnode)
174 {
175  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
176 
177  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
178  hwloc_obj_t node = NULL;
179  hwloc_bitmap_zero(nodeset);
180  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
181  if (node->os_index < maxnode
182  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
183  hwloc_bitmap_set(nodeset, node->os_index);
184  } else {
185  /* if no numa, libnuma assumes we have a single node */
186  if (mask[0] & 1)
187  hwloc_bitmap_fill(nodeset);
188  else
189  hwloc_bitmap_zero(nodeset);
190  }
191 
192  return 0;
193 }
194 
213 static inline struct bitmask *
215 static inline struct bitmask *
217 {
218  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
219  struct bitmask *bitmask = numa_allocate_cpumask();
220  if (!bitmask)
221  return NULL;
222 
223  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
224  hwloc_obj_t node = NULL;
225  while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
226  if (node->memory.local_memory)
227  numa_bitmask_setbit(bitmask, node->os_index);
228  } else {
229  /* if no numa, libnuma assumes we have a single node */
230  if (!hwloc_bitmap_iszero(cpuset))
231  numa_bitmask_setbit(bitmask, 0);
232  }
233 
234  return bitmask;
235 }
236 
246 static inline struct bitmask *
248 static inline struct bitmask *
250 {
251  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
252  struct bitmask *bitmask = numa_allocate_cpumask();
253  if (!bitmask)
254  return NULL;
255 
256  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
257  hwloc_obj_t node = NULL;
258  while ((node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NODE, node)) != NULL)
259  if (hwloc_bitmap_isset(nodeset, node->os_index) && node->memory.local_memory)
260  numa_bitmask_setbit(bitmask, node->os_index);
261  } else {
262  /* if no numa, libnuma assumes we have a single node */
263  if (!hwloc_bitmap_iszero(nodeset))
264  numa_bitmask_setbit(bitmask, 0);
265  }
266 
267  return bitmask;
268 }
269 
275 static inline int
277  const struct bitmask *bitmask)
278 {
279  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
280 
281  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
282  hwloc_obj_t node = NULL;
283  hwloc_bitmap_zero(cpuset);
284  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
285  if (numa_bitmask_isbitset(bitmask, node->os_index))
286  hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
287  } else {
288  /* if no numa, libnuma assumes we have a single node */
289  if (numa_bitmask_isbitset(bitmask, 0))
291  else
292  hwloc_bitmap_zero(cpuset);
293  }
294 
295  return 0;
296 }
297 
303 static inline int
305  const struct bitmask *bitmask)
306 {
307  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
308 
309  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
310  hwloc_obj_t node = NULL;
311  hwloc_bitmap_zero(nodeset);
312  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
313  if (numa_bitmask_isbitset(bitmask, node->os_index))
314  hwloc_bitmap_set(nodeset, node->os_index);
315  } else {
316  /* if no numa, libnuma assumes we have a single node */
317  if (numa_bitmask_isbitset(bitmask, 0))
318  hwloc_bitmap_fill(nodeset);
319  else
320  hwloc_bitmap_zero(nodeset);
321  }
322 
323  return 0;
324 }
325 
330 #ifdef NUMA_VERSION1_COMPATIBILITY
331 
347 static inline int
349  nodemask_t *nodemask)
350 {
351  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
352 
353  nodemask_zero(nodemask);
354 
355  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
356  hwloc_obj_t node = NULL;
357  while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
358  nodemask_set(nodemask, node->os_index);
359  } else {
360  /* if no numa, libnuma assumes we have a single node */
361  if (!hwloc_bitmap_iszero(cpuset))
362  nodemask_set(nodemask, 0);
363  }
364 
365  return 0;
366 }
367 
373 static inline int
375  nodemask_t *nodemask)
376 {
377  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
378 
379  nodemask_zero(nodemask);
380 
381  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
382  hwloc_obj_t node = NULL;
383  while ((node = hwloc_get_next_obj_by_type(topology, HWLOC_OBJ_NODE, node)) != NULL)
384  if (hwloc_bitmap_isset(nodeset, node->os_index))
385  nodemask_set(nodemask, node->os_index);
386  } else {
387  /* if no numa, libnuma assumes we have a single node */
388  if (!hwloc_bitmap_iszero(nodeset))
389  nodemask_set(nodemask, 0);
390  }
391 
392  return 0;
393 }
394 
400 static inline int
402  const nodemask_t *nodemask)
403 {
404  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
405 
406  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
407  hwloc_obj_t node = NULL;
408  hwloc_bitmap_zero(cpuset);
409  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
410  if (nodemask_isset(nodemask, node->os_index))
411  hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
412  } else {
413  /* if no numa, libnuma assumes we have a single node */
414  if (nodemask_isset(nodemask, 0))
416  else
417  hwloc_bitmap_zero(cpuset);
418  }
419 
420  return 0;
421 }
422 
428 static inline int
430  const nodemask_t *nodemask)
431 {
432  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
433 
434  if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
435  hwloc_obj_t node = NULL;
436  hwloc_bitmap_zero(nodeset);
437  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
438  if (nodemask_isset(nodemask, node->os_index))
439  hwloc_bitmap_set(nodeset, node->os_index);
440  } else {
441  /* if no numa, libnuma assumes we have a single node */
442  if (nodemask_isset(nodemask, 0))
443  hwloc_bitmap_fill(nodeset);
444  else
445  hwloc_bitmap_zero(nodeset);
446  }
447 
448  return 0;
449 }
450 
452 #endif /* NUMA_VERSION1_COMPATIBILITY */
453 
454 
455 #ifdef __cplusplus
456 } /* extern "C" */
457 #endif
458 
459 
460 #endif /* HWLOC_LINUX_NUMA_H */