Hardware Locality (hwloc): levelzero.h Source File

Hardware Locality (hwloc) 2.14.0rc1

levelzero.h
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2021-2026 Inria. All rights reserved.
4 * See COPYING in top-level directory.
5 */
6
13
14#ifndef HWLOC_LEVELZERO_H
15#define HWLOC_LEVELZERO_H
16
17#include "hwloc.h"
18#include "hwloc/autogen/config.h"
19#include "hwloc/helper.h"
20#ifdef HWLOC_LINUX_SYS
21#include "hwloc/linux.h"
22#endif
23
24#include <level_zero/ze_api.h>
25#include <level_zero/zes_api.h>
26
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32
41
66static __hwloc_inline int
67hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
68 ze_device_handle_t device, hwloc_cpuset_t set)
69{
70#ifdef HWLOC_LINUX_SYS
71 /* If we're on Linux, use the sysfs mechanism to get the local cpus */
72#define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
73 char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
74 ze_pci_ext_properties_t pci;
75 ze_result_t res;
76
77 if (!hwloc_topology_is_thissystem(topology)) {
78 errno = EINVAL;
79 return -1;
80 }
81
82 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
83 pci.pNext = NULL;
84 res = zeDevicePciGetPropertiesExt(device, &pci);
85 if (res != ZE_RESULT_SUCCESS) {
86 errno = EINVAL;
87 return -1;
88 }
89
90 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
91 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
92 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
93 || hwloc_bitmap_iszero(set))
94 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
95#else
96 /* Non-Linux systems simply get a full cpuset */
97 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
98#endif
99 return 0;
100}
101
124static __hwloc_inline int
125hwloc_levelzero_get_sysman_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
126 zes_device_handle_t device, hwloc_cpuset_t set)
127{
128#ifdef HWLOC_LINUX_SYS
129 /* If we're on Linux, use the sysfs mechanism to get the local cpus */
130#define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
131 char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
132 zes_pci_properties_t pci;
133 ze_result_t res;
134
135 if (!hwloc_topology_is_thissystem(topology)) {
136 errno = EINVAL;
137 return -1;
138 }
139
140 pci.stype = ZES_STRUCTURE_TYPE_PCI_PROPERTIES;
141 pci.pNext = NULL;
142 res = zesDevicePciGetProperties(device, &pci);
143 if (res != ZE_RESULT_SUCCESS) {
144 errno = EINVAL;
145 return -1;
146 }
147
148 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
149 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
150 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
151 || hwloc_bitmap_iszero(set))
152 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
153#else
154 /* Non-Linux systems simply get a full cpuset */
155 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
156#endif
157 return 0;
158}
159
181static __hwloc_inline hwloc_obj_t
182hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
183{
184 ze_pci_ext_properties_t pci;
185 ze_result_t res;
186 hwloc_obj_t osdev;
187
188 if (!hwloc_topology_is_thissystem(topology)) {
189 errno = EINVAL;
190 return NULL;
191 }
192
193 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
194 pci.pNext = NULL;
195 res = zeDevicePciGetPropertiesExt(device, &pci);
196 if (res != ZE_RESULT_SUCCESS) {
197 errno = EINVAL;
198 return NULL;
199 }
200
201 osdev = NULL;
202 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
203 hwloc_obj_t pcidev;
204
205 if (strncmp(osdev->name, "ze", 2))
206 continue;
207
208 pcidev = osdev;
209 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
210 pcidev = pcidev->parent;
211 if (!pcidev)
212 continue;
213
214 if (pcidev
215 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
216 && pcidev->attr->pcidev.domain == pci.address.domain
217 && pcidev->attr->pcidev.bus == pci.address.bus
218 && pcidev->attr->pcidev.dev == pci.address.device
219 && pcidev->attr->pcidev.func == pci.address.function)
220 return osdev;
221
222 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
223 }
224
225 return NULL;
226}
227
248static __hwloc_inline hwloc_obj_t
249hwloc_levelzero_get_sysman_device_osdev(hwloc_topology_t topology, zes_device_handle_t device)
250{
251 zes_pci_properties_t pci;
252 ze_result_t res;
253 hwloc_obj_t osdev;
254
255 if (!hwloc_topology_is_thissystem(topology)) {
256 errno = EINVAL;
257 return NULL;
258 }
259
260 pci.stype = ZES_STRUCTURE_TYPE_PCI_PROPERTIES;
261 pci.pNext = NULL;
262 res = zesDevicePciGetProperties(device, &pci);
263 if (res != ZE_RESULT_SUCCESS) {
264 errno = EINVAL;
265 return NULL;
266 }
267
268 osdev = NULL;
269 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
270 hwloc_obj_t pcidev;
271
272 if (strncmp(osdev->name, "ze", 2))
273 continue;
274
275 pcidev = osdev;
276 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
277 pcidev = pcidev->parent;
278 if (!pcidev)
279 continue;
280
281 if (pcidev
282 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
283 && pcidev->attr->pcidev.domain == pci.address.domain
284 && pcidev->attr->pcidev.bus == pci.address.bus
285 && pcidev->attr->pcidev.dev == pci.address.device
286 && pcidev->attr->pcidev.func == pci.address.function)
287 return osdev;
288
289 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
290 }
291
292 return NULL;
293}
294
296
297
298#ifdef __cplusplus
299} /* extern "C" */
300#endif
301
302
303#endif /* HWLOC_LEVELZERO_H */
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:165
HWLOC_OBJ_PCI_DEVICE
@ HWLOC_OBJ_PCI_DEVICE
PCI device (filtered out by default).
Definition hwloc.h:300
hwloc_obj_t
struct hwloc_obj * hwloc_obj_t
Convenience typedef; a pointer to a struct hwloc_obj.
Definition hwloc.h:637
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition hwloc.h:748
hwloc_topology_is_thissystem
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?
hwloc_topology_get_complete_cpuset
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
hwloc_get_next_osdev
hwloc_obj_t hwloc_get_next_osdev(hwloc_topology_t topology, hwloc_obj_t prev)
Get the next OS device in the system.
Definition helper.h:1291
hwloc_bitmap_iszero
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
hwloc_bitmap_copy
int hwloc_bitmap_copy(hwloc_bitmap_t dst, hwloc_const_bitmap_t src)
Copy the contents of bitmap src into the already allocated bitmap dst.
hwloc_linux_read_path_as_cpumask
int hwloc_linux_read_path_as_cpumask(const char *path, hwloc_bitmap_t set)
Convert a linux kernel cpumask file path into a hwloc bitmap set.
hwloc_levelzero_get_sysman_device_osdev
hwloc_obj_t hwloc_levelzero_get_sysman_device_osdev(hwloc_topology_t topology, zes_device_handle_t device)
Get the hwloc OS device object corresponding to Level Zero Sysman device device.
Definition levelzero.h:249
hwloc_levelzero_get_device_osdev
hwloc_obj_t hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
Get the hwloc OS device object corresponding to Level Zero device device.
Definition levelzero.h:182
hwloc_levelzero_get_device_cpuset
int hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology, ze_device_handle_t device, hwloc_cpuset_t set)
Get the CPU set of logical processors that are physically close to the Level Zero device device.
Definition levelzero.h:67
hwloc_levelzero_get_sysman_device_cpuset
int hwloc_levelzero_get_sysman_device_cpuset(hwloc_topology_t topology, zes_device_handle_t device, hwloc_cpuset_t set)
Get the CPU set of logical processors that are physically close to the Level Zero Sysman device devic...
Definition levelzero.h:125
hwloc_obj::name
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition hwloc.h:449
hwloc_obj::type
hwloc_obj_type_t type
Type of object.
Definition hwloc.h:439
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:456
hwloc_obj::parent
struct hwloc_obj * parent
Parent, NULL if root (Machine object).
Definition hwloc.h:487
hwloc_obj_attr_u::pcidev
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
hwloc_obj_attr_u::hwloc_pcidev_attr_s::dev
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:683
hwloc_obj_attr_u::hwloc_pcidev_attr_s::func
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:684
hwloc_obj_attr_u::hwloc_pcidev_attr_s::domain
unsigned short domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t). Only 16bits PCI domains are supported by d...
Definition hwloc.h:676
hwloc_obj_attr_u::hwloc_pcidev_attr_s::bus
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:682