Hardware Locality (hwloc)  v2.2-20200401.0300.gitd2f52ab
linux-libnuma.h
00001 /*
00002  * Copyright © 2009 CNRS
00003  * Copyright © 2009-2017 Inria.  All rights reserved.
00004  * Copyright © 2009-2010, 2012 Université Bordeaux
00005  * See COPYING in top-level directory.
00006  */
00007 
00015 #ifndef HWLOC_LINUX_LIBNUMA_H
00016 #define HWLOC_LINUX_LIBNUMA_H
00017 
00018 #include "hwloc.h"
00019 
00020 #include <numa.h>
00021 
00022 
00023 #ifdef __cplusplus
00024 extern "C" {
00025 #endif
00026 
00027 
00054 static __hwloc_inline int
00055 hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
00056                                     unsigned long *mask, unsigned long *maxnode)
00057 {
00058   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00059   unsigned long outmaxnode = -1;
00060   hwloc_obj_t node = NULL;
00061 
00062   /* round-up to the next ulong and clear all bytes */
00063   *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
00064   memset(mask, 0, *maxnode/8);
00065 
00066   while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
00067     if (node->os_index >= *maxnode)
00068       continue;
00069     mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
00070     if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
00071       outmaxnode = node->os_index;
00072   }
00073 
00074   *maxnode = outmaxnode+1;
00075   return 0;
00076 }
00077 
00088 static __hwloc_inline int
00089 hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset,
00090                                       unsigned long *mask, unsigned long *maxnode)
00091 {
00092   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00093   unsigned long outmaxnode = -1;
00094   hwloc_obj_t node = NULL;
00095 
00096   /* round-up to the next ulong and clear all bytes */
00097   *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
00098   memset(mask, 0, *maxnode/8);
00099 
00100   while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
00101     if (node->os_index >= *maxnode)
00102       continue;
00103     if (!hwloc_bitmap_isset(nodeset, node->os_index))
00104       continue;
00105     mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
00106     if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
00107       outmaxnode = node->os_index;
00108   }
00109 
00110   *maxnode = outmaxnode+1;
00111   return 0;
00112 }
00113 
00123 static __hwloc_inline int
00124 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00125                                       const unsigned long *mask, unsigned long maxnode)
00126 {
00127   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00128   hwloc_obj_t node = NULL;
00129   hwloc_bitmap_zero(cpuset);
00130   while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00131     if (node->os_index < maxnode
00132         && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
00133       hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
00134   return 0;
00135 }
00136 
00146 static __hwloc_inline int
00147 hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
00148                                         const unsigned long *mask, unsigned long maxnode)
00149 {
00150   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00151   hwloc_obj_t node = NULL;
00152   hwloc_bitmap_zero(nodeset);
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_set(nodeset, node->os_index);
00157   return 0;
00158 }
00159 
00189 static __hwloc_inline struct bitmask *
00190 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
00191 static __hwloc_inline struct bitmask *
00192 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
00193 {
00194   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00195   hwloc_obj_t node = NULL;
00196   struct bitmask *bitmask = numa_allocate_cpumask();
00197   if (!bitmask)
00198     return NULL;
00199   while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
00200     if (node->attr->numanode.local_memory)
00201       numa_bitmask_setbit(bitmask, node->os_index);
00202   return bitmask;
00203 }
00204 
00214 static __hwloc_inline struct bitmask *
00215 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
00216 static __hwloc_inline struct bitmask *
00217 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
00218 {
00219   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00220   hwloc_obj_t node = NULL;
00221   struct bitmask *bitmask = numa_allocate_cpumask();
00222   if (!bitmask)
00223     return NULL;
00224   while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00225     if (hwloc_bitmap_isset(nodeset, node->os_index) && node->attr->numanode.local_memory)
00226       numa_bitmask_setbit(bitmask, node->os_index);
00227   return bitmask;
00228 }
00229 
00235 static __hwloc_inline int
00236 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00237                                         const struct bitmask *bitmask)
00238 {
00239   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00240   hwloc_obj_t node = NULL;
00241   hwloc_bitmap_zero(cpuset);
00242   while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00243     if (numa_bitmask_isbitset(bitmask, node->os_index))
00244       hwloc_bitmap_or(cpuset, cpuset, node->cpuset);
00245   return 0;
00246 }
00247 
00253 static __hwloc_inline int
00254 hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset,
00255                                          const struct bitmask *bitmask)
00256 {
00257   int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
00258   hwloc_obj_t node = NULL;
00259   hwloc_bitmap_zero(nodeset);
00260   while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
00261     if (numa_bitmask_isbitset(bitmask, node->os_index))
00262       hwloc_bitmap_set(nodeset, node->os_index);
00263   return 0;
00264 }
00265 
00269 #ifdef __cplusplus
00270 } /* extern "C" */
00271 #endif
00272 
00273 
00274 #endif /* HWLOC_LINUX_NUMA_H */