Hardware Locality (hwloc)  master-20250612.1317.gitd03ae8e67
levelzero.h
1 /*
2  * Copyright © 2021-2024 Inria. All rights reserved.
3  * See COPYING in top-level directory.
4  */
5 
13 #ifndef HWLOC_LEVELZERO_H
14 #define HWLOC_LEVELZERO_H
15 
16 #include "hwloc.h"
17 #include "hwloc/autogen/config.h"
18 #include "hwloc/helper.h"
19 #ifdef HWLOC_LINUX_SYS
20 #include "hwloc/linux.h"
21 #endif
22 
23 #include <level_zero/ze_api.h>
24 #include <level_zero/zes_api.h>
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 
65 static __hwloc_inline int
66 hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
67  ze_device_handle_t device, hwloc_cpuset_t set)
68 {
69 #ifdef HWLOC_LINUX_SYS
70  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
71 #define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
72  char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
73  ze_pci_ext_properties_t pci;
74  ze_result_t res;
75 
76  if (!hwloc_topology_is_thissystem(topology)) {
77  errno = EINVAL;
78  return -1;
79  }
80 
81  pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
82  pci.pNext = NULL;
83  res = zeDevicePciGetPropertiesExt(device, &pci);
84  if (res != ZE_RESULT_SUCCESS) {
85  errno = EINVAL;
86  return -1;
87  }
88 
89  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
90  pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
91  if (hwloc_linux_read_path_as_cpumask(path, set) < 0
92  || hwloc_bitmap_iszero(set))
94 #else
95  /* Non-Linux systems simply get a full cpuset */
97 #endif
98  return 0;
99 }
100 
123 static __hwloc_inline int
125  zes_device_handle_t device, hwloc_cpuset_t set)
126 {
127 #ifdef HWLOC_LINUX_SYS
128  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
129 #define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
130  char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
131  zes_pci_properties_t pci;
132  ze_result_t res;
133 
134  if (!hwloc_topology_is_thissystem(topology)) {
135  errno = EINVAL;
136  return -1;
137  }
138 
139  res = zesDevicePciGetProperties(device, &pci);
140  if (res != ZE_RESULT_SUCCESS) {
141  errno = EINVAL;
142  return -1;
143  }
144 
145  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
146  pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
147  if (hwloc_linux_read_path_as_cpumask(path, set) < 0
148  || hwloc_bitmap_iszero(set))
150 #else
151  /* Non-Linux systems simply get a full cpuset */
153 #endif
154  return 0;
155 }
156 
178 static __hwloc_inline hwloc_obj_t
179 hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
180 {
181  ze_pci_ext_properties_t pci;
182  ze_result_t res;
183  hwloc_obj_t osdev;
184 
185  if (!hwloc_topology_is_thissystem(topology)) {
186  errno = EINVAL;
187  return NULL;
188  }
189 
190  pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
191  pci.pNext = NULL;
192  res = zeDevicePciGetPropertiesExt(device, &pci);
193  if (res != ZE_RESULT_SUCCESS) {
194  errno = EINVAL;
195  return NULL;
196  }
197 
198  osdev = NULL;
199  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
200  hwloc_obj_t pcidev;
201 
202  if (strncmp(osdev->name, "ze", 2))
203  continue;
204 
205  pcidev = osdev;
206  while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
207  pcidev = pcidev->parent;
208  if (!pcidev)
209  continue;
210 
211  if (pcidev
212  && pcidev->type == HWLOC_OBJ_PCI_DEVICE
213  && pcidev->attr->pcidev.domain == pci.address.domain
214  && pcidev->attr->pcidev.bus == pci.address.bus
215  && pcidev->attr->pcidev.dev == pci.address.device
216  && pcidev->attr->pcidev.func == pci.address.function)
217  return osdev;
218 
219  /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
220  }
221 
222  return NULL;
223 }
224 
245 static __hwloc_inline hwloc_obj_t
246 hwloc_levelzero_get_sysman_device_osdev(hwloc_topology_t topology, zes_device_handle_t device)
247 {
248  zes_pci_properties_t pci;
249  ze_result_t res;
250  hwloc_obj_t osdev;
251 
252  if (!hwloc_topology_is_thissystem(topology)) {
253  errno = EINVAL;
254  return NULL;
255  }
256 
257  res = zesDevicePciGetProperties(device, &pci);
258  if (res != ZE_RESULT_SUCCESS) {
259  errno = EINVAL;
260  return NULL;
261  }
262 
263  osdev = NULL;
264  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
265  hwloc_obj_t pcidev;
266 
267  if (strncmp(osdev->name, "ze", 2))
268  continue;
269 
270  pcidev = osdev;
271  while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
272  pcidev = pcidev->parent;
273  if (!pcidev)
274  continue;
275 
276  if (pcidev
277  && pcidev->type == HWLOC_OBJ_PCI_DEVICE
278  && pcidev->attr->pcidev.domain == pci.address.domain
279  && pcidev->attr->pcidev.bus == pci.address.bus
280  && pcidev->attr->pcidev.dev == pci.address.device
281  && pcidev->attr->pcidev.func == pci.address.function)
282  return osdev;
283 
284  /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
285  }
286 
287  return NULL;
288 }
289 
293 #ifdef __cplusplus
294 } /* extern "C" */
295 #endif
296 
297 
298 #endif /* HWLOC_LEVELZERO_H */
hwloc_bitmap_iszero
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
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:66
hwloc_topology_get_complete_cpuset
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
hwloc_topology_is_thissystem
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?
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:246
hwloc_obj::type
hwloc_obj_type_t type
Type of object.
Definition: hwloc.h:489
HWLOC_OBJ_PCI_DEVICE
@ HWLOC_OBJ_PCI_DEVICE
PCI device (filtered out by default).
Definition: hwloc.h:319
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:506
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:727
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:777
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:179
hwloc_obj
Structure of a topology object.
Definition: hwloc.h:487
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:1290
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:499
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:725
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_obj::parent
struct hwloc_obj * parent
Parent, NULL if root (Machine object)
Definition: hwloc.h:537
hwloc_obj_attr_u::pcidev
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
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_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:161
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:124
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:726
hwloc_obj_attr_u::hwloc_pcidev_attr_s::domain
unsigned int domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:724