Hardware Locality (hwloc)  PR-737-20250925.0822.gite8f69c77f
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 
57 static __hwloc_inline int
59  unsigned long *mask, unsigned long *maxnode)
60 {
61  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
62  unsigned long outmaxnode = -1;
63  hwloc_obj_t node = NULL;
64 
65  /* round-up to the next ulong and clear all bytes */
66  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
67  memset(mask, 0, *maxnode/8);
68 
69  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL) {
70  if (node->os_index >= *maxnode)
71  continue;
72  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
73  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
74  outmaxnode = node->os_index;
75  }
76 
77  *maxnode = outmaxnode+1;
78  return 0;
79 }
80 
93 static __hwloc_inline int
95  unsigned long *mask, unsigned long *maxnode)
96 {
97  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
98  unsigned long outmaxnode = -1;
99  hwloc_obj_t node = NULL;
100 
101  /* round-up to the next ulong and clear all bytes */
102  *maxnode = (*maxnode + 8*sizeof(*mask) - 1) & ~(8*sizeof(*mask) - 1);
103  memset(mask, 0, *maxnode/8);
104 
105  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL) {
106  if (node->os_index >= *maxnode)
107  continue;
108  if (!hwloc_bitmap_isset(nodeset, node->os_index))
109  continue;
110  mask[node->os_index/sizeof(*mask)/8] |= 1UL << (node->os_index % (sizeof(*mask)*8));
111  if (outmaxnode == (unsigned long) -1 || outmaxnode < node->os_index)
112  outmaxnode = node->os_index;
113  }
114 
115  *maxnode = outmaxnode+1;
116  return 0;
117 }
118 
131 static __hwloc_inline int
133  const unsigned long *mask, unsigned long maxnode)
134 {
135  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
136  hwloc_obj_t node = NULL;
137  hwloc_bitmap_zero(cpuset);
138  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
139  if (node->os_index < maxnode
140  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
141  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
142  return -1;
143  return 0;
144 }
145 
158 static __hwloc_inline int
160  const unsigned long *mask, unsigned long maxnode)
161 {
162  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
163  hwloc_obj_t node = NULL;
164  hwloc_bitmap_zero(nodeset);
165  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
166  if (node->os_index < maxnode
167  && (mask[node->os_index/sizeof(*mask)/8] & (1UL << (node->os_index % (sizeof(*mask)*8)))))
168  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
169  return -1;
170  return 0;
171 }
172 
202 static __hwloc_inline struct bitmask *
203 hwloc_cpuset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_cpuset_t cpuset) __hwloc_attribute_malloc;
204 static __hwloc_inline struct bitmask *
206 {
207  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
208  hwloc_obj_t node = NULL;
209  struct bitmask *bitmask = numa_allocate_cpumask();
210  if (!bitmask)
211  return NULL;
212  while ((node = hwloc_get_next_obj_covering_cpuset_by_depth(topology, cpuset, depth, node)) != NULL)
213  if (node->attr->numanode.local_memory)
214  numa_bitmask_setbit(bitmask, node->os_index);
215  return bitmask;
216 }
217 
227 static __hwloc_inline struct bitmask *
228 hwloc_nodeset_to_linux_libnuma_bitmask(hwloc_topology_t topology, hwloc_const_nodeset_t nodeset) __hwloc_attribute_malloc;
229 static __hwloc_inline struct bitmask *
231 {
232  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
233  hwloc_obj_t node = NULL;
234  struct bitmask *bitmask = numa_allocate_cpumask();
235  if (!bitmask)
236  return NULL;
237  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
238  if (hwloc_bitmap_isset(nodeset, node->os_index) && node->attr->numanode.local_memory)
239  numa_bitmask_setbit(bitmask, node->os_index);
240  return bitmask;
241 }
242 
251 static __hwloc_inline int
253  const struct bitmask *bitmask)
254 {
255  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
256  hwloc_obj_t node = NULL;
257  hwloc_bitmap_zero(cpuset);
258  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
259  if (numa_bitmask_isbitset(bitmask, node->os_index))
260  if (hwloc_bitmap_or(cpuset, cpuset, node->cpuset) < 0)
261  return -1;
262  return 0;
263 }
264 
273 static __hwloc_inline int
275  const struct bitmask *bitmask)
276 {
277  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_NUMANODE);
278  hwloc_obj_t node = NULL;
279  hwloc_bitmap_zero(nodeset);
280  while ((node = hwloc_get_next_obj_by_depth(topology, depth, node)) != NULL)
281  if (numa_bitmask_isbitset(bitmask, node->os_index))
282  if (hwloc_bitmap_set(nodeset, node->os_index) < 0)
283  return -1;
284  return 0;
285 }
286 
290 #ifdef __cplusplus
291 } /* extern "C" */
292 #endif
293 
294 
295 #endif /* HWLOC_LINUX_NUMA_H */
hwloc_nodeset_from_linux_libnuma_ulongs
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:159
hwloc_nodeset_to_linux_libnuma_bitmask
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:230
HWLOC_OBJ_NUMANODE
@ HWLOC_OBJ_NUMANODE
NUMA node. An object that contains memory that is directly and byte-accessible to the host processors...
Definition: hwloc.h:269
hwloc_const_cpuset_t
hwloc_const_bitmap_t hwloc_const_cpuset_t
A non-modifiable hwloc_cpuset_t.
Definition: hwloc.h:164
hwloc_cpuset_from_linux_libnuma_bitmask
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:252
hwloc_obj
Structure of a topology object.
Definition: hwloc.h:488
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:778
hwloc_obj::os_index
unsigned os_index
OS-provided physical index number. It is not guaranteed unique across the entire machine,...
Definition: hwloc.h:493
hwloc_obj::cpuset
hwloc_cpuset_t cpuset
CPUs covered by this object.
Definition: hwloc.h:604
hwloc_obj::attr
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:507
hwloc_get_next_obj_by_depth
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.
hwloc_get_next_obj_covering_cpuset_by_depth
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_attr_u::hwloc_numanode_attr_s::local_memory
hwloc_uint64_t local_memory
Local memory (in bytes)
Definition: hwloc.h:693
hwloc_nodeset_t
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:179
hwloc_nodeset_to_linux_libnuma_ulongs
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:94
hwloc_cpuset_t
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:162
hwloc_nodeset_from_linux_libnuma_bitmask
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:274
hwloc_bitmap_isset
int hwloc_bitmap_isset(hwloc_const_bitmap_t bitmap, unsigned id)
Test whether index id is part of bitmap bitmap.
hwloc_const_nodeset_t
hwloc_const_bitmap_t hwloc_const_nodeset_t
A non-modifiable hwloc_nodeset_t.
Definition: hwloc.h:182
hwloc_bitmap_or
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.
hwloc_get_type_depth
int hwloc_get_type_depth(hwloc_topology_t topology, hwloc_obj_type_t type)
Returns the depth of objects of type type.
hwloc_obj_attr_u::numanode
struct hwloc_obj_attr_u::hwloc_numanode_attr_s numanode
hwloc_bitmap_zero
void hwloc_bitmap_zero(hwloc_bitmap_t bitmap)
Empty the bitmap bitmap.
hwloc_bitmap_set
int hwloc_bitmap_set(hwloc_bitmap_t bitmap, unsigned id)
Add index id in bitmap bitmap.
hwloc_cpuset_from_linux_libnuma_ulongs
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:132
hwloc_cpuset_to_linux_libnuma_bitmask
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:205
hwloc_cpuset_to_linux_libnuma_ulongs
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:58