00001
00002
00003
00004
00005
00006
00007
00015 #ifndef HWLOC_LINUX_LIBNUMA_H
00016 #define HWLOC_LINUX_LIBNUMA_H
00017
00018 #include <hwloc.h>
00019 #include <numa.h>
00020
00021
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif
00025
00026
00057 static __hwloc_inline int
00058 hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
00059 unsigned long *mask, unsigned long *maxnode)
00060 {
00061 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00062 unsigned long outmaxnode = -1;
00063
00064
00065 *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
00066 memset(mask, 0, *maxnode/8);
00067
00068 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00069 hwloc_obj_t node = NULL;
00070 while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
00071 if (node->os_index >= *maxnode)
00072 continue;
00073 mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
00074 if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
00075 outmaxnode = node->os_index;
00076 }
00077
00078 } else {
00079
00080 if (!hwloc_bitmap_iszero(cpuset)) {
00081 mask[0] = 1;
00082 outmaxnode = 0;
00083 }
00084 }
00085
00086 *maxnode = outmaxnode+1;
00087 return 0;
00088 }
00089
00100 static __hwloc_inline int
00101 hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset,
00102 unsigned long *mask, unsigned long *maxnode)
00103 {
00104 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00105 unsigned long outmaxnode = -1;
00106
00107
00108 *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
00109 memset(mask, 0, *maxnode/8);
00110
00111 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00112 hwloc_obj_t node = NULL;
00113 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
00114 if (node->os_index >= *maxnode)
00115 continue;
00116 if (!hwloc_bitmap_isset(nodeset, node->os_index))
00117 continue;
00118 mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
00119 if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
00120 outmaxnode = node->os_index;
00121 }
00122
00123 } else {
00124
00125 if (!hwloc_bitmap_iszero(nodeset)) {
00126 mask[0] = 1;
00127 outmaxnode = 0;
00128 }
00129 }
00130
00131 *maxnode = outmaxnode+1;
00132 return 0;
00133 }
00134
00144 static __hwloc_inline int
00145 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00146 const unsigned long *mask, unsigned long maxnode)
00147 {
00148 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00149
00150 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00151 hwloc_obj_t node = NULL;
00152 hwloc_bitmap_zero(cpuset);
00153 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00154 if (node->os_index < maxnode
00155 && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
00156 hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
00157 } else {
00158
00159 if (mask[0] & 1)
00160 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
00161 else
00162 hwloc_bitmap_zero(cpuset);
00163 }
00164
00165 return 0;
00166 }
00167
00177 static __hwloc_inline int
00178 hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
00179 const unsigned long *mask, unsigned long maxnode)
00180 {
00181 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00182
00183 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00184 hwloc_obj_t node = NULL;
00185 hwloc_bitmap_zero(nodeset);
00186 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00187 if (node->os_index < maxnode
00188 && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
00189 hwloc_bitmap_set(nodeset, node->os_index);
00190 } else {
00191
00192 if (mask[0] & 1)
00193 hwloc_bitmap_fill(nodeset);
00194 else
00195 hwloc_bitmap_zero(nodeset);
00196 }
00197
00198 return 0;
00199 }
00200
00234 static __hwloc_inline struct bitmask *
00235 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
00236 static __hwloc_inline struct bitmask *
00237 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
00238 {
00239 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00240 struct bitmask *bitmask = numa_allocate_cpumask();
00241 if (!bitmask)
00242 return NULL;
00243
00244 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00245 hwloc_obj_t node = NULL;
00246 while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
00247 if (node->memory.local_memory)
00248 numa_bitmask_setbit(bitmask, node->os_index);
00249 } else {
00250
00251 if (!hwloc_bitmap_iszero(cpuset))
00252 numa_bitmask_setbit(bitmask, 0);
00253 }
00254
00255 return bitmask;
00256 }
00257
00267 static __hwloc_inline struct bitmask *
00268 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
00269 static __hwloc_inline struct bitmask *
00270 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
00271 {
00272 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00273 struct bitmask *bitmask = numa_allocate_cpumask();
00274 if (!bitmask)
00275 return NULL;
00276
00277 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00278 hwloc_obj_t node = NULL;
00279 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00280 if (hwloc_bitmap_isset(nodeset, node->os_index) && node->memory.local_memory)
00281 numa_bitmask_setbit(bitmask, node->os_index);
00282 } else {
00283
00284 if (!hwloc_bitmap_iszero(nodeset))
00285 numa_bitmask_setbit(bitmask, 0);
00286 }
00287
00288 return bitmask;
00289 }
00290
00296 static __hwloc_inline int
00297 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00298 const struct bitmask *bitmask)
00299 {
00300 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00301
00302 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00303 hwloc_obj_t node = NULL;
00304 hwloc_bitmap_zero(cpuset);
00305 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00306 if (numa_bitmask_isbitset(bitmask, node->os_index))
00307 hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
00308 } else {
00309
00310 if (numa_bitmask_isbitset(bitmask, 0))
00311 hwloc_bitmap_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
00312 else
00313 hwloc_bitmap_zero(cpuset);
00314 }
00315
00316 return 0;
00317 }
00318
00324 static __hwloc_inline int
00325 hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
00326 const struct bitmask *bitmask)
00327 {
00328 int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00329
00330 if (depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
00331 hwloc_obj_t node = NULL;
00332 hwloc_bitmap_zero(nodeset);
00333 while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00334 if (numa_bitmask_isbitset(bitmask, node->os_index))
00335 hwloc_bitmap_set(nodeset, node->os_index);
00336 } else {
00337
00338 if (numa_bitmask_isbitset(bitmask, 0))
00339 hwloc_bitmap_fill(nodeset);
00340 else
00341 hwloc_bitmap_zero(nodeset);
00342 }
00343
00344 return 0;
00345 }
00346
00350 #ifdef __cplusplus
00351 }
00352 #endif
00353
00354
00355 #endif