File indexing completed on 2026-09-17 09:16:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef HWLOC_LEVELZERO_H
0015 #define HWLOC_LEVELZERO_H
0016
0017 #include "hwloc.h"
0018 #include "hwloc/autogen/config.h"
0019 #include "hwloc/helper.h"
0020 #ifdef HWLOC_LINUX_SYS
0021 #include "hwloc/linux.h"
0022 #endif
0023
0024 #include <level_zero/ze_api.h>
0025 #include <level_zero/zes_api.h>
0026
0027
0028 #ifdef __cplusplus
0029 extern "C" {
0030 #endif
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066 static __hwloc_inline int
0067 hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
0068 ze_device_handle_t device, hwloc_cpuset_t set)
0069 {
0070 #ifdef HWLOC_LINUX_SYS
0071
0072 #define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
0073 char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
0074 ze_pci_ext_properties_t pci;
0075 ze_result_t res;
0076
0077 if (!hwloc_topology_is_thissystem(topology)) {
0078 errno = EINVAL;
0079 return -1;
0080 }
0081
0082 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
0083 pci.pNext = NULL;
0084 res = zeDevicePciGetPropertiesExt(device, &pci);
0085 if (res != ZE_RESULT_SUCCESS) {
0086 errno = EINVAL;
0087 return -1;
0088 }
0089
0090 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
0091 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
0092 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
0093 || hwloc_bitmap_iszero(set))
0094 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0095 #else
0096
0097 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0098 #endif
0099 return 0;
0100 }
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 static __hwloc_inline int
0125 hwloc_levelzero_get_sysman_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
0126 zes_device_handle_t device, hwloc_cpuset_t set)
0127 {
0128 #ifdef HWLOC_LINUX_SYS
0129
0130 #define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
0131 char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
0132 zes_pci_properties_t pci;
0133 ze_result_t res;
0134
0135 if (!hwloc_topology_is_thissystem(topology)) {
0136 errno = EINVAL;
0137 return -1;
0138 }
0139
0140 res = zesDevicePciGetProperties(device, &pci);
0141 if (res != ZE_RESULT_SUCCESS) {
0142 errno = EINVAL;
0143 return -1;
0144 }
0145
0146 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
0147 pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
0148 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
0149 || hwloc_bitmap_iszero(set))
0150 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0151 #else
0152
0153 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0154 #endif
0155 return 0;
0156 }
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 static __hwloc_inline hwloc_obj_t
0180 hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
0181 {
0182 ze_pci_ext_properties_t pci;
0183 ze_result_t res;
0184 hwloc_obj_t osdev;
0185
0186 if (!hwloc_topology_is_thissystem(topology)) {
0187 errno = EINVAL;
0188 return NULL;
0189 }
0190
0191 pci.stype = ZE_STRUCTURE_TYPE_PCI_EXT_PROPERTIES;
0192 pci.pNext = NULL;
0193 res = zeDevicePciGetPropertiesExt(device, &pci);
0194 if (res != ZE_RESULT_SUCCESS) {
0195 errno = EINVAL;
0196 return NULL;
0197 }
0198
0199 osdev = NULL;
0200 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0201 hwloc_obj_t pcidev;
0202
0203 if (strncmp(osdev->name, "ze", 2))
0204 continue;
0205
0206 pcidev = osdev;
0207 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
0208 pcidev = pcidev->parent;
0209 if (!pcidev)
0210 continue;
0211
0212 if (pcidev
0213 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
0214 && pcidev->attr->pcidev.domain == pci.address.domain
0215 && pcidev->attr->pcidev.bus == pci.address.bus
0216 && pcidev->attr->pcidev.dev == pci.address.device
0217 && pcidev->attr->pcidev.func == pci.address.function)
0218 return osdev;
0219
0220
0221 }
0222
0223 return NULL;
0224 }
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246 static __hwloc_inline hwloc_obj_t
0247 hwloc_levelzero_get_sysman_device_osdev(hwloc_topology_t topology, zes_device_handle_t device)
0248 {
0249 zes_pci_properties_t pci;
0250 ze_result_t res;
0251 hwloc_obj_t osdev;
0252
0253 if (!hwloc_topology_is_thissystem(topology)) {
0254 errno = EINVAL;
0255 return NULL;
0256 }
0257
0258 res = zesDevicePciGetProperties(device, &pci);
0259 if (res != ZE_RESULT_SUCCESS) {
0260 errno = EINVAL;
0261 return NULL;
0262 }
0263
0264 osdev = NULL;
0265 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0266 hwloc_obj_t pcidev;
0267
0268 if (strncmp(osdev->name, "ze", 2))
0269 continue;
0270
0271 pcidev = osdev;
0272 while (pcidev && pcidev->type != HWLOC_OBJ_PCI_DEVICE)
0273 pcidev = pcidev->parent;
0274 if (!pcidev)
0275 continue;
0276
0277 if (pcidev
0278 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
0279 && pcidev->attr->pcidev.domain == pci.address.domain
0280 && pcidev->attr->pcidev.bus == pci.address.bus
0281 && pcidev->attr->pcidev.dev == pci.address.device
0282 && pcidev->attr->pcidev.func == pci.address.function)
0283 return osdev;
0284
0285
0286 }
0287
0288 return NULL;
0289 }
0290
0291
0292
0293
0294 #ifdef __cplusplus
0295 }
0296 #endif
0297
0298
0299 #endif