Hardware Locality (hwloc) 1.5.1

- Main Page
- Related Pages
- Modules
- Data Structures

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
54 hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
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_depth(topology, cpuset, depth, 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
97 hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset,
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_depth(topology, depth, 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
141 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
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)
156 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
157 else
158 hwloc_bitmap_zero(cpuset);
159 }
160
161 return 0;
162 }
163
173 static inline int
174 hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
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 *
216 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) ;
217 static inline struct bitmask *
218 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
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_depth(topology, cpuset, depth, 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 *
249 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) ;
250 static inline struct bitmask *
251 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
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_depth(topology, depth, 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
278 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
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))
292 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
293 else
294 hwloc_bitmap_zero(cpuset);
295 }
296
297 return 0;
298 }
299
305 static inline int
306 hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
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
331 #ifdef __cplusplus
332 } /* extern "C" */
333 #endif
334
335
336 #endif /* HWLOC_LINUX_NUMA_H */