Hardware Locality (hwloc)  2.14.0rc2-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 
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
29 extern "C" {
30 #endif
31 
32 
66 static __hwloc_inline int
67 hwloc_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 
124 static __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  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))
153 #else
154  /* Non-Linux systems simply get a full cpuset */
156 #endif
157  return 0;
158 }
159 
181 static __hwloc_inline hwloc_obj_t
182 hwloc_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 
248 static __hwloc_inline hwloc_obj_t
249 hwloc_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 
298 #ifdef __cplusplus
299 } /* extern "C" */
300 #endif
301 
302 
303 #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:249
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
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
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
Structure of a topology object.
Definition: hwloc.h:437
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