Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:16

0001 /*
0002  * Copyright © 2021-2023 Inria.  All rights reserved.
0003  * See COPYING in top-level directory.
0004  */
0005 
0006 /** \file
0007  * \brief Macros to help interaction between hwloc and the oneAPI Level Zero interface.
0008  *
0009  * Applications that use both hwloc and Level Zero may want to
0010  * include this file so as to get topology information for L0 devices.
0011  */
0012 
0013 #ifndef HWLOC_LEVELZERO_H
0014 #define HWLOC_LEVELZERO_H
0015 
0016 #include "hwloc.h"
0017 #include "hwloc/autogen/config.h"
0018 #include "hwloc/helper.h"
0019 #ifdef HWLOC_LINUX_SYS
0020 #include "hwloc/linux.h"
0021 #endif
0022 
0023 #include <level_zero/ze_api.h>
0024 #include <level_zero/zes_api.h>
0025 
0026 
0027 #ifdef __cplusplus
0028 extern "C" {
0029 #endif
0030 
0031 
0032 /** \defgroup hwlocality_levelzero Interoperability with the oneAPI Level Zero interface.
0033  *
0034  * This interface offers ways to retrieve topology information about
0035  * devices managed by the Level Zero API.
0036  *
0037  * @{
0038  */
0039 
0040 /** \brief Get the CPU set of logical processors that are physically
0041  * close to the Level Zero device \p device
0042  *
0043  * Store in \p set the CPU-set describing the locality of
0044  * the Level Zero device \p device.
0045  *
0046  * Topology \p topology and device \p device must match the local machine.
0047  * The Level Zero library must have been initialized with Sysman enabled
0048  * (by calling zesInit(0) if supported,
0049  *  or by setting ZES_ENABLE_SYSMAN=1 in the environment).
0050  * I/O devices detection and the Level Zero component are not needed in the
0051  * topology.
0052  *
0053  * The function only returns the locality of the device.
0054  * If more information about the device is needed, OS objects should
0055  * be used instead, see hwloc_levelzero_get_device_osdev().
0056  *
0057  * This function is currently only implemented in a meaningful way for
0058  * Linux; other systems will simply get a full cpuset.
0059  *
0060  * \return 0 on success.
0061  * \return -1 on error, for instance if device information could not be found.
0062  */
0063 static __hwloc_inline int
0064 hwloc_levelzero_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
0065                                   ze_device_handle_t device, hwloc_cpuset_t set)
0066 {
0067 #ifdef HWLOC_LINUX_SYS
0068   /* If we're on Linux, use the sysfs mechanism to get the local cpus */
0069 #define HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX 128
0070   char path[HWLOC_LEVELZERO_DEVICE_SYSFS_PATH_MAX];
0071   zes_pci_properties_t pci;
0072   zes_device_handle_t sdevice = device;
0073   ze_result_t res;
0074 
0075   if (!hwloc_topology_is_thissystem(topology)) {
0076     errno = EINVAL;
0077     return -1;
0078   }
0079 
0080   res = zesDevicePciGetProperties(sdevice, &pci);
0081   if (res != ZE_RESULT_SUCCESS) {
0082     errno = EINVAL;
0083     return -1;
0084   }
0085 
0086   sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus",
0087           pci.address.domain, pci.address.bus, pci.address.device, pci.address.function);
0088   if (hwloc_linux_read_path_as_cpumask(path, set) < 0
0089       || hwloc_bitmap_iszero(set))
0090     hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0091 #else
0092   /* Non-Linux systems simply get a full cpuset */
0093   hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0094 #endif
0095   return 0;
0096 }
0097 
0098 /** \brief Get the hwloc OS device object corresponding to Level Zero device
0099  * \p device.
0100  *
0101  * \return The hwloc OS device object that describes the given Level Zero device \p device.
0102  * \return \c NULL if none could be found.
0103  *
0104  * Topology \p topology and device \p dv_ind must match the local machine.
0105  * I/O devices detection and the Level Zero component must be enabled in the
0106  * topology. If not, the locality of the object may still be found using
0107  * hwloc_levelzero_get_device_cpuset().
0108  *
0109  * \note The corresponding hwloc PCI device may be found by looking
0110  * at the result parent pointer (unless PCI devices are filtered out).
0111  */
0112 static __hwloc_inline hwloc_obj_t
0113 hwloc_levelzero_get_device_osdev(hwloc_topology_t topology, ze_device_handle_t device)
0114 {
0115   zes_device_handle_t sdevice = device;
0116   zes_pci_properties_t pci;
0117   ze_result_t res;
0118   hwloc_obj_t osdev;
0119 
0120   if (!hwloc_topology_is_thissystem(topology)) {
0121     errno = EINVAL;
0122     return NULL;
0123   }
0124 
0125   res = zesDevicePciGetProperties(sdevice, &pci);
0126   if (res != ZE_RESULT_SUCCESS) {
0127     /* L0 was likely initialized without sysman, don't bother */
0128     errno = EINVAL;
0129     return NULL;
0130   }
0131 
0132   osdev = NULL;
0133   while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0134     hwloc_obj_t pcidev = osdev->parent;
0135 
0136     if (strncmp(osdev->name, "ze", 2))
0137       continue;
0138 
0139     if (pcidev
0140       && pcidev->type == HWLOC_OBJ_PCI_DEVICE
0141       && pcidev->attr->pcidev.domain == pci.address.domain
0142       && pcidev->attr->pcidev.bus == pci.address.bus
0143       && pcidev->attr->pcidev.dev == pci.address.device
0144       && pcidev->attr->pcidev.func == pci.address.function)
0145       return osdev;
0146 
0147     /* FIXME: when we'll have serialnumber, try it in case PCI is filtered-out */
0148   }
0149 
0150   return NULL;
0151 }
0152 
0153 /** @} */
0154 
0155 
0156 #ifdef __cplusplus
0157 } /* extern "C" */
0158 #endif
0159 
0160 
0161 #endif /* HWLOC_LEVELZERO_H */