Hardware Locality (hwloc) 1.11.9

linux-libnuma.h
1 /*
2 * Copyright © 2009 CNRS
3 * Copyright © 2009-2014 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 #include <numa.h>
20
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26
57 static __hwloc_inline int
58 hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
59 unsigned long *mask, unsigned long *maxnode)
60 {
61 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
62 unsigned long outmaxnode = -1;
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 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
69 hwloc_obj_t node = NULL;
70 while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
71 if (node->os_index >= *maxnode)
72 continue;
73 mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
74 if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
75 outmaxnode = node->os_index;
76 }
77
78 } else {
79 /* if no numa, libnuma assumes we have a single node */
80 if (!hwloc_bitmap_iszero(cpuset)) {
81 mask[0] = 1;
82 outmaxnode = 0;
83 }
84 }
85
86 *maxnode = outmaxnode+1;
87 return 0;
88 }
89
100 static __hwloc_inline int
101 hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset,
102 unsigned long *mask, unsigned long *maxnode)
103 {
104 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
105 unsigned long outmaxnode = -1;
106
107 /* round-up to the next ulong and clear all bytes */
108 *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
109 memset(mask, 0, *maxnode/8);
110
111 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
112 hwloc_obj_t node = NULL;
113 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
114 if (node->os_index >= *maxnode)
115 continue;
116 if (!hwloc_bitmap_isset(nodeset, node->os_index))
117 continue;
118 mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
119 if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
120 outmaxnode = node->os_index;
121 }
122
123 } else {
124 /* if no numa, libnuma assumes we have a single node */
125 if (!hwloc_bitmap_iszero(nodeset)) {
126 mask[0] = 1;
127 outmaxnode = 0;
128 }
129 }
130
131 *maxnode = outmaxnode+1;
132 return 0;
133 }
134
144 static __hwloc_inline int
145 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
146 const unsigned long *mask, unsigned long maxnode)
147 {
148 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
149
150 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
151 hwloc_obj_t node = NULL;
152 hwloc_bitmap_zero(cpuset);
153 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
154 if (node->os_index < maxnode
155 && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
156 hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
157 } else {
158 /* if no numa, libnuma assumes we have a single node */
159 if (mask[0] & 1)
160 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
161 else
162 hwloc_bitmap_zero(cpuset);
163 }
164
165 return 0;
166 }
167
177 static __hwloc_inline int
178 hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
179 const unsigned long *mask, unsigned long maxnode)
180 {
181 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
182
183 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
184 hwloc_obj_t node = NULL;
185 hwloc_bitmap_zero(nodeset);
186 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
187 if (node->os_index < maxnode
188 && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
189 hwloc_bitmap_set(nodeset, node->os_index);
190 } else {
191 /* if no numa, libnuma assumes we have a single node */
192 if (mask[0] & 1)
193 hwloc_bitmap_fill(nodeset);
194 else
195 hwloc_bitmap_zero(nodeset);
196 }
197
198 return 0;
199 }
200
234 static __hwloc_inline struct bitmask *
235 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
236 static __hwloc_inline struct bitmask *
237 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
238 {
239 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
240 struct bitmask *bitmask = numa_allocate_cpumask();
241 if (!bitmask)
242 return NULL;
243
244 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
245 hwloc_obj_t node = NULL;
246 while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
247 if (node->memory.local_memory)
248 numa_bitmask_setbit(bitmask, node->os_index);
249 } else {
250 /* if no numa, libnuma assumes we have a single node */
251 if (!hwloc_bitmap_iszero(cpuset))
252 numa_bitmask_setbit(bitmask, 0);
253 }
254
255 return bitmask;
256 }
257
267 static __hwloc_inline struct bitmask *
268 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
269 static __hwloc_inline struct bitmask *
270 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
271 {
272 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
273 struct bitmask *bitmask = numa_allocate_cpumask();
274 if (!bitmask)
275 return NULL;
276
277 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
278 hwloc_obj_t node = NULL;
279 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
280 if (hwloc_bitmap_isset(nodeset, node->os_index) && node->memory.local_memory)
281 numa_bitmask_setbit(bitmask, node->os_index);
282 } else {
283 /* if no numa, libnuma assumes we have a single node */
284 if (!hwloc_bitmap_iszero(nodeset))
285 numa_bitmask_setbit(bitmask, 0);
286 }
287
288 return bitmask;
289 }
290
296 static __hwloc_inline int
297 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
298 const struct bitmask *bitmask)
299 {
300 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
301
302 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
303 hwloc_obj_t node = NULL;
304 hwloc_bitmap_zero(cpuset);
305 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
306 if (numa_bitmask_isbitset(bitmask, node->os_index))
307 hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
308 } else {
309 /* if no numa, libnuma assumes we have a single node */
310 if (numa_bitmask_isbitset(bitmask, 0))
311 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
312 else
313 hwloc_bitmap_zero(cpuset);
314 }
315
316 return 0;
317 }
318
324 static __hwloc_inline int
325 hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
326 const struct bitmask *bitmask)
327 {
328 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
329
330 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
331 hwloc_obj_t node = NULL;
332 hwloc_bitmap_zero(nodeset);
333 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
334 if (numa_bitmask_isbitset(bitmask, node->os_index))
335 hwloc_bitmap_set(nodeset, node->os_index);
336 } else {
337 /* if no numa, libnuma assumes we have a single node */
338 if (numa_bitmask_isbitset(bitmask, 0))
339 hwloc_bitmap_fill(nodeset);
340 else
341 hwloc_bitmap_zero(nodeset);
342 }
343
344 return 0;
345 }
346
350 #ifdef __cplusplus
351 } /* extern "C" */
352 #endif
353
354
355 #endif /* HWLOC_LINUX_NUMA_H */
hwloc_get_next_obj_by_depth
static hwloc_obj_t hwloc_get_next_obj_by_depth(hwloc_topology_t topology, unsigned depth, hwloc_obj_t prev)
Returns the next object at depth depth.
hwloc_const_nodeset_t
hwloc_const_bitmap_t hwloc_const_nodeset_t
A non-modifiable hwloc_nodeset_t.
Definition: hwloc.h:154
hwloc_obj_memory_s::local_memory
hwloc_uint64_t local_memory
Local memory (in bytes)
Definition: hwloc.h:339
HWLOC_OBJ_NUMANODE
NUMA node. An object that contains memory that is directly and byte-accessible to the host processors...
Definition: hwloc.h:190
hwloc_cpuset_t
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:132
hwloc_cpuset_from_linux_libnuma_ulongs
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:145
hwloc_get_next_obj_covering_cpuset_by_depth
static hwloc_obj_t hwloc_get_next_obj_covering_cpuset_by_depth(hwloc_topology_t topology, hwloc_const_cpuset_t set, unsigned depth, hwloc_obj_t prev)
Iterate through same-depth objects covering at least CPU set set.
Definition: helper.h:328
hwloc_bitmap_isset
int hwloc_bitmap_isset(hwloc_const_bitmap_t bitmap, unsigned id)
Test whether index id is part of bitmap bitmap.
hwloc_bitmap_copy
void hwloc_bitmap_copy(hwloc_bitmap_t dst, hwloc_const_bitmap_t src)
Copy the contents of bitmap src into the already allocated bitmap dst.
hwloc_nodeset_t
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:151
hwloc_const_cpuset_t
hwloc_const_bitmap_t hwloc_const_cpuset_t
A non-modifiable hwloc_cpuset_t.
Definition: hwloc.h:134
hwloc_bitmap_zero
void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
hwloc_bitmap_fill
void hwloc_bitmap_fill(hwloc_bitmap_t bitmap)
Fill bitmap bitmap with all possible indexes (even if those objects don&#39;t exist or are otherwise unav...
hwloc_get_type_depth
int hwloc_get_type_depth(hwloc_topology_t topology, hwloc_obj_type_t type)
Returns the depth of objects of type type.
hwloc_nodeset_from_linux_libnuma_ulongs
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:178
hwloc_bitmap_set
void hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
hwloc_bitmap_or
void 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.
hwloc_obj::memory
struct hwloc_obj_memory_s memory
Memory attributes.
Definition: hwloc.h:371
hwloc_cpuset_to_linux_libnuma_bitmask
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:237
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:632
hwloc_nodeset_from_linux_libnuma_bitmask
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:325
hwloc_obj::os_index
unsigned os_index
OS-provided physical index number. It is not guaranteed unique across the entire machine, except for PUs and NUMA nodes.
Definition: hwloc.h:362
hwloc_nodeset_to_linux_libnuma_ulongs
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:101
hwloc_obj::cpuset
hwloc_cpuset_t cpuset
CPUs covered by this object.
Definition: hwloc.h:421
hwloc_nodeset_to_linux_libnuma_bitmask
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:270
hwloc_cpuset_from_linux_libnuma_bitmask
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:297
hwloc_cpuset_to_linux_libnuma_ulongs
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:58
HWLOC_TYPE_DEPTH_UNKNOWN
No object of given type exists in the topology.
Definition: hwloc.h:1223
hwloc_topology_get_complete_cpuset
static hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
Definition: helper.h:803
hwloc_obj
Structure of a topology object.
Definition: hwloc.h:358
hwloc_bitmap_iszero
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.