00001
00002
00003
00004
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
00039
00040
00041
00042
00043
00044
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
00053 #define HWLOC_CL_DEVICE_PCI_BUS_ID_NV 0x4008
00054 #define HWLOC_CL_DEVICE_PCI_SLOT_ID_NV 0x4009
00055
00056
00072 static __hwloc_inline int
00073 hwloc_opencl_get_device_pci_busid(cl_device_id device,
00074 unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
00075 {
00076 hwloc_cl_device_topology_amd amdtopo;
00077 cl_uint nvbus, nvslot;
00078 cl_int clret;
00079
00080 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
00081 if (CL_SUCCESS == clret
00082 && HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD == amdtopo.raw.type) {
00083 *domain = 0;
00084 *bus = (unsigned) amdtopo.pcie.bus;
00085 *dev = (unsigned) amdtopo.pcie.device;
00086 *func = (unsigned) amdtopo.pcie.function;
00087 return 0;
00088 }
00089
00090 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_ID_NV, sizeof(nvbus), &nvbus, NULL);
00091 if (CL_SUCCESS == clret) {
00092 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_SLOT_ID_NV, sizeof(nvslot), &nvslot, NULL);
00093 if (CL_SUCCESS == clret) {
00094
00095 *domain = nvbus >> 8;
00096 *bus = nvbus & 0xff;
00097
00098 *dev = nvslot >> 3;
00099 *func = nvslot & 0x7;
00100 return 0;
00101 }
00102 }
00103
00104 return -1;
00105 }
00106
00124 static __hwloc_inline int
00125 hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
00126 cl_device_id device __hwloc_attribute_unused,
00127 hwloc_cpuset_t set)
00128 {
00129 #if (defined HWLOC_LINUX_SYS)
00130
00131 #define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
00132 char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
00133 unsigned pcidomain, pcibus, pcidev, pcifunc;
00134
00135 if (!hwloc_topology_is_thissystem(topology)) {
00136 errno = EINVAL;
00137 return -1;
00138 }
00139
00140 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidev, &pcifunc) < 0) {
00141 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00142 return 0;
00143 }
00144
00145 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus", pcidomain, pcibus, pcidev, pcifunc);
00146 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
00147 || hwloc_bitmap_iszero(set))
00148 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00149 #else
00150
00151 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00152 #endif
00153 return 0;
00154 }
00155
00171 static __hwloc_inline hwloc_obj_t
00172 hwloc_opencl_get_device_osdev_by_index(hwloc_topology_t topology,
00173 unsigned platform_index, unsigned device_index)
00174 {
00175 unsigned x = (unsigned) -1, y = (unsigned) -1;
00176 hwloc_obj_t osdev = NULL;
00177 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00178 if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
00179 && osdev->name
00180 && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
00181 && platform_index == x && device_index == y)
00182 return osdev;
00183 }
00184 return NULL;
00185 }
00186
00207 static __hwloc_inline hwloc_obj_t
00208 hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
00209 cl_device_id device __hwloc_attribute_unused)
00210 {
00211 hwloc_obj_t osdev;
00212 unsigned pcidomain, pcibus, pcidevice, pcifunc;
00213
00214 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidevice, &pcifunc) < 0) {
00215 errno = EINVAL;
00216 return NULL;
00217 }
00218
00219 osdev = NULL;
00220 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00221 hwloc_obj_t pcidev = osdev->parent;
00222 if (strncmp(osdev->name, "opencl", 6))
00223 continue;
00224 if (pcidev
00225 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
00226 && pcidev->attr->pcidev.domain == pcidomain
00227 && pcidev->attr->pcidev.bus == pcibus
00228 && pcidev->attr->pcidev.dev == pcidevice
00229 && pcidev->attr->pcidev.func == pcifunc)
00230 return osdev;
00231
00232 }
00233
00234 return NULL;
00235 }
00236
00240 #ifdef __cplusplus
00241 }
00242 #endif
00243
00244
00245 #endif