• 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] |= 1UL << (node->os_index % (sizeof(*mask)*8));
00057       if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
00058         outmaxnode = node->os_index;
00059     }
00060 
00061   } else {
00062     /* if no numa, libnuma assumes we have a single node */
00063     if (!hwloc_cpuset_iszero(cpuset)) {
00064       mask[0] = 1;
00065       outmaxnode = 0;
00066     }
00067   }
00068 
00069   *maxnode = outmaxnode+1;
00070   return 0;
00071 }
00072 
00082 static __hwloc_inline int
00083 hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00084                                       const unsigned long *mask, unsigned long maxnode)
00085 {
00086   hwloc_obj_t node;
00087   int depth;
00088   unsigned i;
00089 
00090   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
00091   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
00092 
00093   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
00094     /* if no numa, libnuma assumes we have a single node */
00095     if (mask[0] & 1)
00096       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
00097     else
00098       hwloc_cpuset_zero(cpuset);
00099 
00100   } else {
00101     hwloc_cpuset_zero(cpuset);
00102     for(i=0; i<maxnode; i++)
00103       if (mask[i/sizeof(*mask)/8] & (1UL << (i% (sizeof(*mask)*8)))) {
00104         node = hwloc_get_obj_by_depth(topology, depth, i);
00105         if (node)
00106           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
00107       }
00108   }
00109 
00110   return 0;
00111 }
00112 
00131 static __hwloc_inline struct bitmask * __hwloc_attribute_malloc
00132 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
00133 {
00134   struct bitmask *bitmask;
00135   hwloc_obj_t node = NULL;
00136   unsigned nbnodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NODE);
00137 
00138   if (nbnodes) {
00139     bitmask = numa_bitmask_alloc(nbnodes);
00140     if (!bitmask)
00141       return NULL;
00142     while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
00143       numa_bitmask_setbit(bitmask, node->os_index);
00144 
00145   } else {
00146     /* if no numa, libnuma assumes we have a single node */
00147     bitmask = numa_bitmask_alloc(1);
00148     if (!bitmask)
00149       return NULL;
00150     if (!hwloc_cpuset_iszero(cpuset))
00151       numa_bitmask_setbit(bitmask, 0);
00152   }
00153 
00154   return bitmask;
00155 }
00156 
00162 static __hwloc_inline int
00163 hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00164                                        const struct bitmask *bitmask)
00165 {
00166   hwloc_obj_t node;
00167   int depth;
00168   int i;
00169 
00170   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
00171   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
00172 
00173   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
00174     /* if no numa, libnuma assumes we have a single node */
00175     if (numa_bitmask_isbitset(bitmask, 0))
00176       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
00177     else
00178       hwloc_cpuset_zero(cpuset);
00179 
00180   } else {
00181     hwloc_cpuset_zero(cpuset);
00182     for(i=0; i<NUMA_NUM_NODES; i++)
00183       if (numa_bitmask_isbitset(bitmask, i)) {
00184         node = hwloc_get_obj_by_depth(topology, depth, i);
00185         if (node)
00186           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
00187       }
00188   }
00189 
00190   return 0;
00191 }
00192 
00197 #ifdef NUMA_VERSION1_COMPATIBILITY
00198 
00208 static __hwloc_inline int
00209 hwloc_cpuset_to_linux_libnuma_nodemask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset,
00210                                       nodemask_t *nodemask)
00211 {
00212   hwloc_obj_t node = NULL;
00213   unsigned nbnodes = hwloc_get_nbobjs_by_type(topology, HWLOC_OBJ_NODE);
00214 
00215   nodemask_zero(nodemask);
00216   if (nbnodes) {
00217     while ((node = hwloc_get_next_obj_covering_cpuset_by_type(topology, cpuset, HWLOC_OBJ_NODE, node)) != NULL)
00218       nodemask_set(nodemask, node->os_index);
00219 
00220   } else {
00221     /* if no numa, libnuma assumes we have a single node */
00222     if (!hwloc_cpuset_iszero(cpuset))
00223       nodemask_set(nodemask, 0);
00224   }
00225 
00226   return 0;
00227 }
00228 
00234 static __hwloc_inline int
00235 hwloc_cpuset_from_linux_libnuma_nodemask(hwloc_topology_t topology, hwloc_cpuset_t cpuset,
00236                                         const nodemask_t *nodemask)
00237 {
00238   hwloc_obj_t node;
00239   int depth;
00240   int i;
00241 
00242   depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NODE);
00243   /* cannot be HWLOC_TYPE_DEPTH_MULTIPLE */
00244 
00245   if (depth == HWLOC_TYPE_DEPTH_UNKNOWN) {
00246     /* if no numa, libnuma assumes we have a single node */
00247     if (nodemask_isset(nodemask, 0))
00248       hwloc_cpuset_copy(cpuset, hwloc_topology_get_complete_cpuset(topology));
00249     else
00250       hwloc_cpuset_zero(cpuset);
00251 
00252   } else {
00253     hwloc_cpuset_zero(cpuset);
00254     for(i=0; i<NUMA_NUM_NODES; i++)
00255       if (nodemask_isset(nodemask, i)) {
00256         node = hwloc_get_obj_by_depth(topology, depth, i);
00257         if (node)
00258           hwloc_cpuset_or(cpuset, cpuset, node->cpuset);
00259       }
00260   }
00261 
00262   return 0;
00263 }
00264 
00266 #endif /* NUMA_VERSION1_COMPATIBILITY */
00267 
00268 
00269 #ifdef __cplusplus
00270 } /* extern "C" */
00271 #endif
00272 
00273 
00274 #endif /* HWLOC_LINUX_NUMA_H */

Generated on Thu Dec 16 2010 06:44:16 for Hardware Locality (hwloc) by  doxygen 1.7.1