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