Hardware Locality (hwloc) 2.13.0rc1
levelzero.h
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2021-2024 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))
95#else
96 /* Non-Linux systems simply get a full cpuset */
98#endif
99 return 0;
100}
101
124static __hwloc_inline int
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 res = zesDevicePciGetProperties(device, &pci);
141 if (res != ZE_RESULT_SUCCESS) {
142 errno = EINVAL;
143 return -1;
144 }
145
146 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
147 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
148 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
149 || hwloc_bitmap_iszero(set))
151#else
152 /* Non-Linux systems simply get a full cpuset */
154#endif
155 return 0;
156}
157
179static __hwloc_inline hwloc_obj_t
180hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
181{
182 ze_pci_ext_properties_t pci;
183 ze_result_t res;
184 hwloc_obj_t osdev;
185
186 if (!hwloc_topology_is_thissystem(topology)) {
187 errno = EINVAL;
188 return NULL;
189 }
190
191 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
192 pci.pNext = NULL;
193 res = zeDevicePciGetPropertiesExt(device, &pci);
194 if (res != ZE_RESULT_SUCCESS) {
195 errno = EINVAL;
196 return NULL;
197 }
198
199 osdev = NULL;
200 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
201 hwloc_obj_t pcidev;
202
203 if (strncmp(osdev->name, "ze", 2))
204 continue;
205
206 pcidev = osdev;
207 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
208 pcidev = pcidev->parent;
209 if (!pcidev)
210 continue;
211
212 if (pcidev
213 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
214 && pcidev->attr->pcidev.domain == pci.address.domain
215 && pcidev->attr->pcidev.bus == pci.address.bus
216 && pcidev->attr->pcidev.dev == pci.address.device
217 && pcidev->attr->pcidev.func == pci.address.function)
218 return osdev;
219
220 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
221 }
222
223 return NULL;
224}
225
246static __hwloc_inline hwloc_obj_t
248{
249 zes_pci_properties_t pci;
250 ze_result_t res;
251 hwloc_obj_t osdev;
252
253 if (!hwloc_topology_is_thissystem(topology)) {
254 errno = EINVAL;
255 return NULL;
256 }
257
258 res = zesDevicePciGetProperties(device, &pci);
259 if (res != ZE_RESULT_SUCCESS) {
260 errno = EINVAL;
261 return NULL;
262 }
263
264 osdev = NULL;
265 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
266 hwloc_obj_t pcidev;
267
268 if (strncmp(osdev->name, "ze", 2))
269 continue;
270
271 pcidev = osdev;
272 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
273 pcidev = pcidev->parent;
274 if (!pcidev)
275 continue;
276
277 if (pcidev
278 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
279 && pcidev->attr->pcidev.domain == pci.address.domain
280 && pcidev->attr->pcidev.bus == pci.address.bus
281 && pcidev->attr->pcidev.dev == pci.address.device
282 && pcidev->attr->pcidev.func == pci.address.function)
283 return osdev;
284
285 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
286 }
287
288 return NULL;
289}
290
292
293
294#ifdef __cplusplus
295} /* extern "C" */
296#endif
297
298
299#endif /* HWLOC_LEVELZERO_H */
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
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
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.
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition hwloc.h:748
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
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:247
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:180
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
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
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_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
PCI device (filtered out by default).
Definition hwloc.h:300
struct hwloc_obj * hwloc_obj_t
Convenience typedef; a pointer to a struct hwloc_obj.
Definition hwloc.h:637
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:683
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:684
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
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:682
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_t type
Type of object.
Definition hwloc.h:439
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition hwloc.h:456
struct hwloc_obj * parent
Parent, NULL if root (Machine object).
Definition hwloc.h:487
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev