- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List

# linux-libnuma.h

    00001 /*
    00002  * Copyright © 2009 CNRS, INRIA, Université Bordeaux 1
    00003  * See COPYING in top-level directory.
    00004  */
    00005
    00013 #ifndef HWLOC_LINUX_LIBNUMA_H
    00014 #define HWLOC_LINUX_LIBNUMA_H
    00015
    00016 #include <hwloc.h>
    00017 #include <numa.h>
    00018
    00019
    00020 #ifdef __cplusplus
    00021 extern "C" {
    00022 #endif
    00023
    00024
    00040 static __hwloc_inline int
    00041 hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
    00042                                     unsigned long *mask, unsigned long *maxnode)
    00043 {
    00044   unsigned long outmaxnode = -1;
    00045   hwloc_obj_t node = NULL;
    00046   unsigned nbnodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NODE);
    00047   unsigned i;
    00048
    00049   for(i=0; i<*maxnode/sizeof(*mask)/8; i++)
    00050     mask[i] = 0;
    00051
    00052   if (nbnodes) {
    00053     while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL) {
    00054       if (node->os_index >= *maxnode)
    00055         break;
    00056       mask[node->os_index/sizeof(*mask)/8] |= 1 << (node->os_index % (sizeof(*mask)*8));
    00057       outmaxnode = node->os_index;
    00058     }
    00059
    00060   } else {
    00061     /* if no numa, libnuma assumes we have a single node */
    00062     if (!hwloc_cpuset_iszero(cpuset)) {
    00063       mask[0] = 1;
    00064       outmaxnode = 0;
    00065     }
    00066   }
    00067
    00068   *maxnode = outmaxnode+1;
    00069   return 0;
    00070 }
    00071
    00081 static __hwloc_inline int
    00082 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
    00083                                       const unsigned long *mask, unsigned long maxnode)
    00084 {
    00085   hwloc_obj_t node;
    00086   int depth;
    00087   unsigned i;
    00088
    00089   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
    00090   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
    00091
    00092   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
    00093     /* if no numa, libnuma assumes we have a single node */
    00094     if (mask[0] & 1)
    00095       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
    00096     else
    00097       hwloc_cpuset_zero(cpuset);
    00098
    00099   } else {
    00100     hwloc_cpuset_zero(cpuset);
    00101     for(i=0; i<maxnode; i++)
    00102       if (mask[i/sizeof(*mask)/8] & (1 << (i% (sizeof(*mask)*8)))) {
    00103         node = hwloc_get_obj_by_depth(topology, depth, i);
    00104         if (node)
    00105           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
    00106       }
    00107   }
    00108
    00109   return 0;
    00110 }
    00111
    00130 static __hwloc_inline struct bitmask * __hwloc_attribute_malloc
    00131 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
    00132 {
    00133   struct bitmask *bitmask;
    00134   hwloc_obj_t node = NULL;
    00135   unsigned nbnodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NODE);
    00136
    00137   if (nbnodes) {
    00138     bitmask = numa_bitmask_alloc(nbnodes);
    00139     if (!bitmask)
    00140       return NULL;
    00141     while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
    00142       numa_bitmask_setbit(bitmask, node->os_index);
    00143
    00144   } else {
    00145     /* if no numa, libnuma assumes we have a single node */
    00146     bitmask = numa_bitmask_alloc(1);
    00147     if (!bitmask)
    00148       return NULL;
    00149     if (!hwloc_cpuset_iszero(cpuset))
    00150       numa_bitmask_setbit(bitmask, 0);
    00151   }
    00152
    00153   return bitmask;
    00154 }
    00155
    00161 static __hwloc_inline int
    00162 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
    00163                                        const struct bitmask *bitmask)
    00164 {
    00165   hwloc_obj_t node;
    00166   int depth;
    00167   int i;
    00168
    00169   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
    00170   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
    00171
    00172   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
    00173     /* if no numa, libnuma assumes we have a single node */
    00174     if (numa_bitmask_isbitset(bitmask, 0))
    00175       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
    00176     else
    00177       hwloc_cpuset_zero(cpuset);
    00178
    00179   } else {
    00180     hwloc_cpuset_zero(cpuset);
    00181     for(i=0; i<NUMA_NUM_NODES; i++)
    00182       if (numa_bitmask_isbitset(bitmask, i)) {
    00183         node = hwloc_get_obj_by_depth(topology, depth, i);
    00184         if (node)
    00185           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
    00186       }
    00187   }
    00188
    00189   return 0;
    00190 }
    00191
    00196 #ifdef NUMA_VERSION1_COMPATIBILITY
    00197
    00207 static __hwloc_inline int
    00208 hwloc_cpuset_to_linux_libnuma_nodemask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
    00209                                       nodemask_t *nodemask)
    00210 {
    00211   hwloc_obj_t node = NULL;
    00212   unsigned nbnodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NODE);
    00213
    00214   nodemask_zero(nodemask);
    00215   if (nbnodes) {
    00216     while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
    00217       nodemask_set(nodemask, node->os_index);
    00218
    00219   } else {
    00220     /* if no numa, libnuma assumes we have a single node */
    00221     if (!hwloc_cpuset_iszero(cpuset))
    00222       nodemask_set(nodemask, 0);
    00223   }
    00224
    00225   return 0;
    00226 }
    00227
    00233 static __hwloc_inline int
    00234 hwloc_cpuset_from_linux_libnuma_nodemask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
    00235                                         const nodemask_t *nodemask)
    00236 {
    00237   hwloc_obj_t node;
    00238   int depth;
    00239   int i;
    00240
    00241   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
    00242   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
    00243
    00244   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
    00245     /* if no numa, libnuma assumes we have a single node */
    00246     if (nodemask_isset(nodemask, 0))
    00247       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
    00248     else
    00249       hwloc_cpuset_zero(cpuset);
    00250
    00251   } else {
    00252     hwloc_cpuset_zero(cpuset);
    00253     for(i=0; i<NUMA_NUM_NODES; i++)
    00254       if (nodemask_isset(nodemask, i)) {
    00255         node = hwloc_get_obj_by_depth(topology, depth, i);
    00256         if (node)
    00257           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
    00258       }
    00259   }
    00260
    00261   return 0;
    00262 }
    00263
    00265 #endif /* NUMA_VERSION1_COMPATIBILITY */
    00266
    00267
    00268 #ifdef __cplusplus
    00269 } /* extern "C" */
    00270 #endif
    00271
    00272
    00273 #endif /* HWLOC_LINUX_NUMA_H */