Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.4.3

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