Hardware Locality (hwloc) 3.0.0a1-git
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
21#include <level_zero/ze_api.h>
22#include <level_zero/zes_api.h>
23
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29
38
62static __hwloc_inline int
63hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
64 ze_device_handle_t device, hwloc_cpuset_t set)
65{
66 ze_pci_ext_properties_t pci;
67 ze_result_t res;
68
69 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
70 pci.pNext = NULL;
71 res = zeDevicePciGetPropertiesExt(device, &pci);
72 if (res != ZE_RESULT_SUCCESS) {
73 errno = EINVAL;
74 return -1;
75 }
76
77 return hwloc_get_pci_busid_cpuset(topology, set,
78 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
79}
80
102static __hwloc_inline int
104 zes_device_handle_t device, hwloc_cpuset_t set)
105{
106 hwloc_obj_t parent;
107 zes_pci_properties_t pci;
108 ze_result_t res;
109
110 pci.stype = ZES_STRUCTURE_TYPE_PCI_PROPERTIES;
111 pci.pNext = NULL;
112 res = zesDevicePciGetProperties(device, &pci);
113 if (res != ZE_RESULT_SUCCESS) {
114 errno = EINVAL;
115 return -1;
116 }
117
118 return hwloc_get_pci_busid_cpuset(topology,
119 set,
120 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
121}
122
144static __hwloc_inline hwloc_obj_t
145hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
146{
147 ze_pci_ext_properties_t pci;
148 ze_result_t res;
149 hwloc_obj_t osdev;
150
151 if (!hwloc_topology_is_thissystem(topology)) {
152 errno = EINVAL;
153 return NULL;
154 }
155
156 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
157 pci.pNext = NULL;
158 res = zeDevicePciGetPropertiesExt(device, &pci);
159 if (res != ZE_RESULT_SUCCESS) {
160 errno = EINVAL;
161 return NULL;
162 }
163
164 osdev = NULL;
165 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
166 hwloc_obj_t pcidev;
167
168 if (strncmp(osdev->name, "ze", 2))
169 continue;
170
171 pcidev = osdev;
172 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
173 pcidev = pcidev->parent;
174 if (!pcidev)
175 continue;
176
177 if (pcidev
178 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
179 && pcidev->attr->pcidev.domain == pci.address.domain
180 && pcidev->attr->pcidev.bus == pci.address.bus
181 && pcidev->attr->pcidev.dev == pci.address.device
182 && pcidev->attr->pcidev.func == pci.address.function)
183 return osdev;
184
185 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
186 }
187
188 return NULL;
189}
190
211static __hwloc_inline hwloc_obj_t
213{
214 zes_pci_properties_t pci;
215 ze_result_t res;
216 hwloc_obj_t osdev;
217
218 if (!hwloc_topology_is_thissystem(topology)) {
219 errno = EINVAL;
220 return NULL;
221 }
222
223 pci.stype = ZES_STRUCTURE_TYPE_PCI_PROPERTIES;
224 pci.pNext = NULL;
225 res = zesDevicePciGetProperties(device, &pci);
226 if (res != ZE_RESULT_SUCCESS) {
227 errno = EINVAL;
228 return NULL;
229 }
230
231 osdev = NULL;
232 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
233 hwloc_obj_t pcidev;
234
235 if (strncmp(osdev->name, "ze", 2))
236 continue;
237
238 pcidev = osdev;
239 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
240 pcidev = pcidev->parent;
241 if (!pcidev)
242 continue;
243
244 if (pcidev
245 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
246 && pcidev->attr->pcidev.domain == pci.address.domain
247 && pcidev->attr->pcidev.bus == pci.address.bus
248 && pcidev->attr->pcidev.dev == pci.address.device
249 && pcidev->attr->pcidev.func == pci.address.function)
250 return osdev;
251
252 /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
253 }
254
255 return NULL;
256}
257
259
260
261#ifdef __cplusplus
262} /* extern "C" */
263#endif
264
265
266#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:1310
int hwloc_get_pci_busid_cpuset(hwloc_topology_t topology, hwloc_cpuset_t cpuset, unsigned domain, unsigned bus, unsigned dev, unsigned func)
Find the locality of a given PCI bus id.
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:788
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:212
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:145
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:63
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:103
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:324
struct hwloc_obj * hwloc_obj_t
Convenience typedef; a pointer to a struct hwloc_obj.
Definition hwloc.h:691
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:737
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:738
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:736
unsigned int domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:735
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition hwloc.h:504
hwloc_obj_type_t type
Type of object.
Definition hwloc.h:494
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 * parent
Parent, NULL if root (Machine object).
Definition hwloc.h:542
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev