Hardware Locality (hwloc)  v2.2-20200408.0300.gitad4a86f
opencl.h
00001 /*
00002  * Copyright © 2012-2019 Inria.  All rights reserved.
00003  * Copyright © 2013, 2018 Université Bordeaux.  All right reserved.
00004  * See COPYING in top-level directory.
00005  */
00006 
00014 #ifndef HWLOC_OPENCL_H
00015 #define HWLOC_OPENCL_H
00016 
00017 #include "hwloc.h"
00018 #include "hwloc/autogen/config.h"
00019 #include "hwloc/helper.h"
00020 #ifdef HWLOC_LINUX_SYS
00021 #include "hwloc/linux.h"
00022 #endif
00023 
00024 #ifdef __APPLE__
00025 #include <OpenCL/cl.h>
00026 #else
00027 #include <CL/cl.h>
00028 #endif
00029 
00030 #include <stdio.h>
00031 
00032 
00033 #ifdef __cplusplus
00034 extern "C" {
00035 #endif
00036 
00037 
00038 /* OpenCL extensions aren't always shipped with default headers, and
00039  * they don't always reflect what the installed implementations support.
00040  * Try everything and let the implementation return errors when non supported.
00041  */
00042 /* Copyright (c) 2008-2018 The Khronos Group Inc. */
00043 
00044 /* needs "cl_amd_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
00045 #define HWLOC_CL_DEVICE_TOPOLOGY_AMD 0x4037
00046 typedef union {
00047     struct { cl_uint type; cl_uint data[5]; } raw;
00048     struct { cl_uint type; cl_char unused[17]; cl_char bus; cl_char device; cl_char function; } pcie;
00049 } hwloc_cl_device_topology_amd;
00050 #define HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD 1
00051 
00052 /* needs "cl_nv_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
00053 #define HWLOC_CL_DEVICE_PCI_BUS_ID_NV 0x4008
00054 #define HWLOC_CL_DEVICE_PCI_SLOT_ID_NV 0x4009
00055 #define HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV 0x400A
00056 
00057 
00073 static __hwloc_inline int
00074 hwloc_opencl_get_device_pci_busid(cl_device_id device,
00075                                unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
00076 {
00077         hwloc_cl_device_topology_amd amdtopo;
00078         cl_uint nvbus, nvslot, nvdomain;
00079         cl_int clret;
00080 
00081         clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
00082         if (CL_SUCCESS == clret
00083             && HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD == amdtopo.raw.type) {
00084                 *domain = 0; /* can't do anything better */
00085                 *bus = (unsigned) amdtopo.pcie.bus;
00086                 *dev = (unsigned) amdtopo.pcie.device;
00087                 *func = (unsigned) amdtopo.pcie.function;
00088                 return 0;
00089         }
00090 
00091         clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_ID_NV, sizeof(nvbus), &nvbus, NULL);
00092         if (CL_SUCCESS == clret) {
00093                 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_SLOT_ID_NV, sizeof(nvslot), &nvslot, NULL);
00094                 if (CL_SUCCESS == clret) {
00095                         clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV, sizeof(nvdomain), &nvdomain, NULL);
00096                         if (CL_SUCCESS == clret) { /* available since CUDA 10.2 */
00097                                 *domain = nvdomain;
00098                         } else {
00099                                 *domain = 0;
00100                         }
00101                         *bus = nvbus & 0xff;
00102                         /* non-documented but used in many other projects */
00103                         *dev = nvslot >> 3;
00104                         *func = nvslot & 0x7;
00105                         return 0;
00106                 }
00107         }
00108 
00109         return -1;
00110 }
00111 
00129 static __hwloc_inline int
00130 hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
00131                                cl_device_id device __hwloc_attribute_unused,
00132                                hwloc_cpuset_t set)
00133 {
00134 #if (defined HWLOC_LINUX_SYS)
00135         /* If we're on Linux, try AMD/NVIDIA extensions + the sysfs mechanism to get the local cpus */
00136 #define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
00137         char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
00138         unsigned pcidomain, pcibus, pcidev, pcifunc;
00139 
00140         if (!hwloc_topology_is_thissystem(topology)) {
00141                 errno = EINVAL;
00142                 return -1;
00143         }
00144 
00145         if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidev, &pcifunc) < 0) {
00146                 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00147                 return 0;
00148         }
00149 
00150         sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus", pcidomain, pcibus, pcidev, pcifunc);
00151         if (hwloc_linux_read_path_as_cpumask(path, set) < 0
00152             || hwloc_bitmap_iszero(set))
00153                 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00154 #else
00155         /* Non-Linux systems simply get a full cpuset */
00156         hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00157 #endif
00158   return 0;
00159 }
00160 
00176 static __hwloc_inline hwloc_obj_t
00177 hwloc_opencl_get_device_osdev_by_index(hwloc_topology_t topology,
00178                                        unsigned platform_index, unsigned device_index)
00179 {
00180         unsigned x = (unsigned) -1, y = (unsigned) -1;
00181         hwloc_obj_t osdev = NULL;
00182         while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00183                 if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
00184                     && osdev->name
00185                     && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
00186                     && platform_index == x && device_index == y)
00187                         return osdev;
00188         }
00189         return NULL;
00190 }
00191 
00212 static __hwloc_inline hwloc_obj_t
00213 hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
00214                               cl_device_id device __hwloc_attribute_unused)
00215 {
00216         hwloc_obj_t osdev;
00217         unsigned pcidomain, pcibus, pcidevice, pcifunc;
00218 
00219         if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidevice, &pcifunc) < 0) {
00220                 errno = EINVAL;
00221                 return NULL;
00222         }
00223 
00224         osdev = NULL;
00225         while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00226                 hwloc_obj_t pcidev = osdev->parent;
00227                 if (strncmp(osdev->name, "opencl", 6))
00228                         continue;
00229                 if (pcidev
00230                     && pcidev->type == HWLOC_OBJ_PCI_DEVICE
00231                     && pcidev->attr->pcidev.domain == pcidomain
00232                     && pcidev->attr->pcidev.bus == pcibus
00233                     && pcidev->attr->pcidev.dev == pcidevice
00234                     && pcidev->attr->pcidev.func == pcifunc)
00235                         return osdev;
00236                 /* if PCI are filtered out, we need a info attr to match on */
00237         }
00238 
00239         return NULL;
00240 }
00241 
00245 #ifdef __cplusplus
00246 } /* extern "C" */
00247 #endif
00248 
00249 
00250 #endif /* HWLOC_OPENCL_H */