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 #include <OpenCL/cl_ext.h>
00027 #else
00028 #include <CL/cl.h>
00029 #include <CL/cl_ext.h>
00030 #endif
00031
00032 #include <stdio.h>
00033
00034
00035 #ifdef __cplusplus
00036 extern "C" {
00037 #endif
00038
00039
00068 static __hwloc_inline int
00069 hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
00070 cl_device_id device __hwloc_attribute_unused,
00071 hwloc_cpuset_t set)
00072 {
00073 #if (defined HWLOC_LINUX_SYS) && (defined CL_DEVICE_TOPOLOGY_AMD)
00074
00075 #define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
00076 char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
00077 FILE *sysfile = NULL;
00078 cl_device_topology_amd amdtopo;
00079 cl_int clret;
00080
00081 if (!hwloc_topology_is_thissystem(topology)) {
00082 errno = EINVAL;
00083 return -1;
00084 }
00085
00086 clret = clGetDeviceInfo(device, CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
00087 if (CL_SUCCESS != clret) {
00088 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00089 return 0;
00090 }
00091 if (CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD != amdtopo.raw.type) {
00092 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00093 return 0;
00094 }
00095
00096 sprintf(path, "/sys/bus/pci/devices/0000:%02x:%02x.%01x/local_cpus",
00097 (unsigned) amdtopo.pcie.bus, (unsigned) amdtopo.pcie.device, (unsigned) amdtopo.pcie.function);
00098 sysfile = fopen(path, "r");
00099 if (!sysfile)
00100 return -1;
00101
00102 if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
00103 || hwloc_bitmap_iszero(set))
00104 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00105
00106 fclose(sysfile);
00107 #else
00108
00109 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00110 #endif
00111 return 0;
00112 }
00113
00129 static __hwloc_inline hwloc_obj_t
00130 hwloc_opencl_get_device_osdev_by_index(hwloc_topology_t topology,
00131 unsigned platform_index, unsigned device_index)
00132 {
00133 unsigned x = (unsigned) -1, y = (unsigned) -1;
00134 hwloc_obj_t osdev = NULL;
00135 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00136 if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
00137 && osdev->name
00138 && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
00139 && platform_index == x && device_index == y)
00140 return osdev;
00141 }
00142 return NULL;
00143 }
00144
00158 static __hwloc_inline hwloc_obj_t
00159 hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
00160 cl_device_id device __hwloc_attribute_unused)
00161 {
00162 #ifdef CL_DEVICE_TOPOLOGY_AMD
00163 hwloc_obj_t osdev;
00164 cl_device_topology_amd amdtopo;
00165 cl_int clret;
00166
00167 clret = clGetDeviceInfo(device, CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
00168 if (CL_SUCCESS != clret) {
00169 errno = EINVAL;
00170 return NULL;
00171 }
00172 if (CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD != amdtopo.raw.type) {
00173 errno = EINVAL;
00174 return NULL;
00175 }
00176
00177 osdev = NULL;
00178 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00179 hwloc_obj_t pcidev = osdev->parent;
00180 if (strncmp(osdev->name, "opencl", 6))
00181 continue;
00182 if (pcidev
00183 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
00184 && pcidev->attr->pcidev.domain == 0
00185 && pcidev->attr->pcidev.bus == amdtopo.pcie.bus
00186 && pcidev->attr->pcidev.dev == amdtopo.pcie.device
00187 && pcidev->attr->pcidev.func == amdtopo.pcie.function)
00188 return osdev;
00189 }
00190
00191 return NULL;
00192 #else
00193 return NULL;
00194 #endif
00195 }
00196
00200 #ifdef __cplusplus
00201 }
00202 #endif
00203
00204
00205 #endif