Hardware Locality (hwloc)  3.0.0a1-git
linux-libnuma.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright © 2009 CNRS
4  * Copyright © 2009-2023 Inria. All rights reserved.
5  * Copyright © 2009-2010, 2012 Université Bordeaux
6  * See COPYING in top-level directory.
7  */
8 
16 #ifndef HWLOC_LINUX_LIBNUMA_H
17 #define HWLOC_LINUX_LIBNUMA_H
18 
19 #include "hwloc.h"
20 
21 #include <numa.h>
22 
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
60 static __hwloc_inline int
62  unsigned long *mask, unsigned long *maxnode)
63 {
64  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
65  unsigned long outmaxnode = -1;
66  hwloc_obj_t node = NULL;
67 
68  /* round-up to the next ulong and clear all bytes */
69  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
70  memset(mask, 0, *maxnode/8);
71 
72  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
73  if (node->os_index >= *maxnode)
74  continue;
75  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
76  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
77  outmaxnode = node->os_index;
78  }
79 
80  *maxnode = outmaxnode+1;
81  return 0;
82 }
83 
96 static __hwloc_inline int
98  unsigned long *mask, unsigned long *maxnode)
99 {
100  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
101  unsigned long outmaxnode = -1;
102  hwloc_obj_t node = NULL;
103 
104  /* round-up to the next ulong and clear all bytes */
105  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
106  memset(mask, 0, *maxnode/8);
107 
108  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
109  if (node->os_index >= *maxnode)
110  continue;
111  if (!hwloc_bitmap_isset(nodeset, node->os_index))
112  continue;
113  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
114  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
115  outmaxnode = node->os_index;
116  }
117 
118  *maxnode = outmaxnode+1;
119  return 0;
120 }
121 
134 static __hwloc_inline int
136  const unsigned long *mask, unsigned long maxnode)
137 {
138  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
139  hwloc_obj_t node = NULL;
140  hwloc_bitmap_zero(cpuset);
141  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
142  if (node->os_index < maxnode
143  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
144  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
145  return -1;
146  return 0;
147 }
148 
161 static __hwloc_inline int
163  const unsigned long *mask, unsigned long maxnode)
164 {
165  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
166  hwloc_obj_t node = NULL;
167  hwloc_bitmap_zero(nodeset);
168  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
169  if (node->os_index < maxnode
170  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
171  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
172  return -1;
173  return 0;
174 }
175 
208 static __hwloc_inline struct bitmask *
209 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
210 static __hwloc_inline struct bitmask *
212 {
213  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
214  hwloc_obj_t node = NULL;
215  struct bitmask *bitmask = numa_allocate_cpumask();
216  if (!bitmask)
217  return NULL;
218  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
219  if (node->attr->numanode.local_memory)
220  numa_bitmask_setbit(bitmask, node->os_index);
221  return bitmask;
222 }
223 
233 static __hwloc_inline struct bitmask *
234 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
235 static __hwloc_inline struct bitmask *
237 {
238  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
239  hwloc_obj_t node = NULL;
240  struct bitmask *bitmask = numa_allocate_cpumask();
241  if (!bitmask)
242  return NULL;
243  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
244  if (hwloc_bitmap_isset(nodeset, node->os_index) && node->attr->numanode.local_memory)
245  numa_bitmask_setbit(bitmask, node->os_index);
246  return bitmask;
247 }
248 
257 static __hwloc_inline int
259  const struct bitmask *bitmask)
260 {
261  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
262  hwloc_obj_t node = NULL;
263  hwloc_bitmap_zero(cpuset);
264  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
265  if (numa_bitmask_isbitset(bitmask, node->os_index))
266  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
267  return -1;
268  return 0;
269 }
270 
279 static __hwloc_inline int
281  const struct bitmask *bitmask)
282 {
283  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
284  hwloc_obj_t node = NULL;
285  hwloc_bitmap_zero(nodeset);
286  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
287  if (numa_bitmask_isbitset(bitmask, node->os_index))
288  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
289  return -1;
290  return 0;
291 }
292 
296 #ifdef __cplusplus
297 } /* extern "C" */
298 #endif
299 
300 
301 #endif /* HWLOC_LINUX_NUMA_H */
int hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
int hwloc_bitmap_isset(hwloc_const_bitmap_t bitmap, unsigned id)
Test whether index id is part of bitmap bitmap.
int hwloc_bitmap_or(hwloc_bitmap_t res, hwloc_const_bitmap_t bitmap1, hwloc_const_bitmap_t bitmap2)
Or bitmaps bitmap1 and bitmap2 and store the result in bitmap res.
void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:783
hwloc_obj_t hwloc_get_next_obj_covering_cpuset_by_depth(hwloc_topology_t topology, hwloc_const_cpuset_t set, int depth, hwloc_obj_t prev)
Iterate through same-depth objects covering at least CPU set set.
Definition: helper.h:429
hwloc_obj_t hwloc_get_next_obj_by_depth(hwloc_topology_t topology, int depth, hwloc_obj_t prev)
Returns the next object at depth depth.
int hwloc_get_type_depth(hwloc_topology_t topology, hwloc_obj_type_t type)
Returns the depth of objects of type type.
struct bitmask * hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset)
Convert hwloc CPU set cpuset into the returned libnuma bitmask.
Definition: linux-libnuma.h:211
int hwloc_nodeset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_nodeset_t nodeset, const struct bitmask *bitmask)
Convert libnuma bitmask bitmask into hwloc NUMA node set nodeset.
Definition: linux-libnuma.h:280
int hwloc_cpuset_from_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const struct bitmask *bitmask)
Convert libnuma bitmask bitmask into hwloc CPU set cpuset.
Definition: linux-libnuma.h:258
struct bitmask * hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset)
Convert hwloc NUMA node set nodeset into the returned libnuma bitmask.
Definition: linux-libnuma.h:236
int hwloc_nodeset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset, unsigned long *mask, unsigned long *maxnode)
Convert hwloc NUMA node set nodeset into the array of unsigned long mask.
Definition: linux-libnuma.h:97
int hwloc_cpuset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_cpuset_t cpuset, const unsigned long *mask, unsigned long maxnode)
Convert the array of unsigned long mask into hwloc CPU set.
Definition: linux-libnuma.h:135
int hwloc_nodeset_from_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_nodeset_t nodeset, const unsigned long *mask, unsigned long maxnode)
Convert the array of unsigned long mask into hwloc NUMA node set.
Definition: linux-libnuma.h:162
int hwloc_cpuset_to_linux_libnuma_ulongs(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset, unsigned long *mask, unsigned long *maxnode)
Convert hwloc CPU set cpuset into the array of unsigned long mask.
Definition: linux-libnuma.h:61
hwloc_const_bitmap_t hwloc_const_cpuset_t
A non-modifiable hwloc_cpuset_t.
Definition: hwloc.h:167
hwloc_const_bitmap_t hwloc_const_nodeset_t
A non-modifiable hwloc_nodeset_t.
Definition: hwloc.h:186
hwloc_bitmap_t hwloc_nodeset_t
A node set is a bitmap whose bits are set according to NUMA memory node physical OS indexes.
Definition: hwloc.h:183
hwloc_bitmap_t hwloc_cpuset_t
A CPU set is a bitmap whose bits are set according to CPU physical OS indexes.
Definition: hwloc.h:165
@ HWLOC_OBJ_NUMANODE
NUMA node. An object that contains memory that is directly and byte-accessible to the host processors...
Definition: hwloc.h:273
hwloc_uint64_t local_memory
Local memory (in bytes)
Definition: hwloc.h:705
Structure of a topology object.
Definition: hwloc.h:492
unsigned os_index
OS-provided physical index number. It is not guaranteed unique across the entire machine,...
Definition: hwloc.h:497
hwloc_cpuset_t cpuset
CPUs covered by this object.
Definition: hwloc.h:608
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:511
struct hwloc_obj_attr_u::hwloc_numanode_attr_s numanode