Hardware Locality (hwloc)  2.10.0
linux-libnuma.h
1 /*
2  * Copyright © 2009 CNRS
3  * Copyright © 2009-2023 Inria. All rights reserved.
4  * Copyright © 2009-2010, 2012 Université Bordeaux
5  * See COPYING in top-level directory.
6  */
7 
15 #ifndef HWLOC_LINUX_LIBNUMA_H
16 #define HWLOC_LINUX_LIBNUMA_H
17 
18 #include "hwloc.h"
19 
20 #include <numa.h>
21 
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 
56 static __hwloc_inline int
58  unsigned long *mask, unsigned long *maxnode)
59 {
60  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
61  unsigned long outmaxnode = -1;
62  hwloc_obj_t node = NULL;
63 
64  /* round-up to the next ulong and clear all bytes */
65  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
66  memset(mask, 0, *maxnode/8);
67 
68  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
69  if (node->os_index >= *maxnode)
70  continue;
71  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
72  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
73  outmaxnode = node->os_index;
74  }
75 
76  *maxnode = outmaxnode+1;
77  return 0;
78 }
79 
92 static __hwloc_inline int
94  unsigned long *mask, unsigned long *maxnode)
95 {
96  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
97  unsigned long outmaxnode = -1;
98  hwloc_obj_t node = NULL;
99 
100  /* round-up to the next ulong and clear all bytes */
101  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
102  memset(mask, 0, *maxnode/8);
103 
104  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
105  if (node->os_index >= *maxnode)
106  continue;
107  if (!hwloc_bitmap_isset(nodeset, node->os_index))
108  continue;
109  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
110  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
111  outmaxnode = node->os_index;
112  }
113 
114  *maxnode = outmaxnode+1;
115  return 0;
116 }
117 
130 static __hwloc_inline int
132  const unsigned long *mask, unsigned long maxnode)
133 {
134  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
135  hwloc_obj_t node = NULL;
136  hwloc_bitmap_zero(cpuset);
137  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
138  if (node->os_index < maxnode
139  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
140  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
141  return -1;
142  return 0;
143 }
144 
157 static __hwloc_inline int
159  const unsigned long *mask, unsigned long maxnode)
160 {
161  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
162  hwloc_obj_t node = NULL;
163  hwloc_bitmap_zero(nodeset);
164  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
165  if (node->os_index < maxnode
166  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
167  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
168  return -1;
169  return 0;
170 }
171 
201 static __hwloc_inline struct bitmask *
202 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
203 static __hwloc_inline struct bitmask *
205 {
206  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
207  hwloc_obj_t node = NULL;
208  struct bitmask *bitmask = numa_allocate_cpumask();
209  if (!bitmask)
210  return NULL;
211  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
212  if (node->attr->numanode.local_memory)
213  numa_bitmask_setbit(bitmask, node->os_index);
214  return bitmask;
215 }
216 
226 static __hwloc_inline struct bitmask *
227 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
228 static __hwloc_inline struct bitmask *
230 {
231  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
232  hwloc_obj_t node = NULL;
233  struct bitmask *bitmask = numa_allocate_cpumask();
234  if (!bitmask)
235  return NULL;
236  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
237  if (hwloc_bitmap_isset(nodeset, node->os_index) && node->attr->numanode.local_memory)
238  numa_bitmask_setbit(bitmask, node->os_index);
239  return bitmask;
240 }
241 
250 static __hwloc_inline int
252  const struct bitmask *bitmask)
253 {
254  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
255  hwloc_obj_t node = NULL;
256  hwloc_bitmap_zero(cpuset);
257  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
258  if (numa_bitmask_isbitset(bitmask, node->os_index))
259  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
260  return -1;
261  return 0;
262 }
263 
272 static __hwloc_inline int
274  const struct bitmask *bitmask)
275 {
276  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
277  hwloc_obj_t node = NULL;
278  hwloc_bitmap_zero(nodeset);
279  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
280  if (numa_bitmask_isbitset(bitmask, node->os_index))
281  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
282  return -1;
283  return 0;
284 }
285 
289 #ifdef __cplusplus
290 } /* extern "C" */
291 #endif
292 
293 
294 #endif /* HWLOC_LINUX_NUMA_H */
hwloc_const_bitmap_t hwloc_const_cpuset_t
A non-modifiable hwloc_cpuset_t.
Definition: hwloc.h:163
hwloc_const_bitmap_t hwloc_const_nodeset_t
A non-modifiable hwloc_nodeset_t.
Definition: hwloc.h:181
hwloc_bitmap_t hwloc_nodeset_t
A node set is a bitmap whose bits are set according to NUMA memory node physical OS indexes.
Definition: hwloc.h:178
hwloc_bitmap_t hwloc_cpuset_t
A CPU set is a bitmap whose bits are set according to CPU physical OS indexes.
Definition: hwloc.h:161
@ HWLOC_OBJ_NUMANODE
NUMA node. An object that contains memory that is directly and byte-accessible to the host processors...
Definition: hwloc.h:257
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:742
int hwloc_get_type_depth(hwloc_topology_t topology, hwloc_obj_type_t type)
Returns the depth of objects of type type.
static hwloc_obj_t hwloc_get_next_obj_by_depth(hwloc_topology_t topology, int depth, hwloc_obj_t prev)
Returns the next object at depth depth.
static hwloc_obj_t hwloc_get_next_obj_covering_cpuset_by_depth(hwloc_topology_t topology, hwloc_const_cpuset_t set, int depth, hwloc_obj_t prev)
Iterate through same-depth objects covering at least CPU set set.
Definition: helper.h:428
int hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
int hwloc_bitmap_isset(hwloc_const_bitmap_t bitmap, unsigned id)
Test whether index id is part of bitmap bitmap.
int hwloc_bitmap_or(hwloc_bitmap_t res, hwloc_const_bitmap_t bitmap1, hwloc_const_bitmap_t bitmap2)
Or bitmaps bitmap1 and bitmap2 and store the result in bitmap res.
void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
static int hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const unsigned long *mask, unsigned long maxnode)
Convert the array of unsigned long mask into hwloc CPU set.
Definition: linux-libnuma.h:131
static int hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset, unsigned long *mask, unsigned long *maxnode)
Convert hwloc NUMA node set nodeset into the array of unsigned long mask.
Definition: linux-libnuma.h:93
static int hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset, const unsigned long *mask, unsigned long maxnode)
Convert the array of unsigned long mask into hwloc NUMA node set.
Definition: linux-libnuma.h:158
static int hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset, unsigned long *mask, unsigned long *maxnode)
Convert hwloc CPU set cpuset into the array of unsigned long mask.
Definition: linux-libnuma.h:57
static int hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const struct bitmask *bitmask)
Convert libnuma bitmask bitmask into hwloc CPU set cpuset.
Definition: linux-libnuma.h:251
static int hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset, const struct bitmask *bitmask)
Convert libnuma bitmask bitmask into hwloc NUMA node set nodeset.
Definition: linux-libnuma.h:273
static struct bitmask * hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
Convert hwloc CPU set cpuset into the returned libnuma bitmask.
Definition: linux-libnuma.h:204
static struct bitmask * hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
Convert hwloc NUMA node set nodeset into the returned libnuma bitmask.
Definition: linux-libnuma.h:229
Structure of a topology object.
Definition: hwloc.h:431
unsigned os_index
OS-provided physical index number. It is not guaranteed unique across the entire machine,...
Definition: hwloc.h:436
hwloc_cpuset_t cpuset
CPUs covered by this object.
Definition: hwloc.h:547
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:450
struct hwloc_obj_attr_u::hwloc_numanode_attr_s numanode
hwloc_uint64_t local_memory
Local memory (in bytes)
Definition: hwloc.h:637