Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.7

  |   Home   |   Support   |   FAQ   |  
opencl.h
1 /*
2  * Copyright ¬© 2012-2013 Inria. All rights reserved.
3  * Copyright ¬© 2013 Universit√© Bordeaux 1. All right reserved.
4  * See COPYING in top-level directory.
5  */
6 
17 #ifndef HWLOC_OPENCL_H
18 #define HWLOC_OPENCL_H
19 
20 #include <hwloc.h>
21 #include <hwloc/autogen/config.h>
22 #include <hwloc/helper.h>
23 #ifdef HWLOC_LINUX_SYS
24 #include <hwloc/linux.h>
25 #endif
26 
27 #include <CL/cl.h>
28 #include <CL/cl_ext.h>
29 
30 #include <stdio.h>
31 
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 
59 static inline int
61  cl_device_id device ,
62  hwloc_cpuset_t set)
63 {
64 #if (defined HWLOC_LINUX_SYS) && (defined CL_DEVICE_TOPOLOGY_AMD)
65  /* If we're on Linux + AMD OpenCL, use the AMD extension + the sysfs mechanism to get the local cpus */
66 #define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
67  char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
68  FILE *sysfile = NULL;
69  cl_device_topology_amd amdtopo;
70  cl_int clret;
71 
72  if (!hwloc_topology_is_thissystem(topology)) {
73  errno = EINVAL;
74  return -1;
75  }
76 
77  clret = clGetDeviceInfo(device, CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
78  if (CL_SUCCESS != clret) {
80  return 0;
81  }
82  if (CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD != amdtopo.raw.type) {
84  return 0;
85  }
86 
87  sprintf(path, "/sys/bus/pci/devices/0000:%02x:%02x.%01x/local_cpus", amdtopo.pcie.bus, amdtopo.pcie.device, amdtopo.pcie.function);
88  sysfile = fopen(path, "r");
89  if (!sysfile)
90  return -1;
91 
92  hwloc_linux_parse_cpumap_file(sysfile, set);
93  if (hwloc_bitmap_iszero(set))
95 
96  fclose(sysfile);
97 #else
98  /* Non-Linux + AMD OpenCL systems simply get a full cpuset */
100 #endif
101  return 0;
102 }
103 
119 static inline hwloc_obj_t
121  unsigned platform_index, unsigned device_index)
122 {
123  unsigned x = (unsigned) -1, y = (unsigned) -1;
124  hwloc_obj_t osdev = NULL;
125  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
126  if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
127  && osdev->name
128  && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
129  && platform_index == x && device_index == y)
130  return osdev;
131  }
132  return NULL;
133 }
134 
148 static inline hwloc_obj_t
150  cl_device_id device )
151 {
152 #ifdef CL_DEVICE_TOPOLOGY_AMD
153  hwloc_obj_t osdev;
154  cl_device_topology_amd amdtopo;
155  cl_int clret;
156 
157  clret = clGetDeviceInfo(device, CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
158  if (CL_SUCCESS != clret) {
159  errno = EINVAL;
160  return NULL;
161  }
162  if (CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD != amdtopo.raw.type) {
163  errno = EINVAL;
164  return NULL;
165  }
166 
167  osdev = NULL;
168  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
169  hwloc_obj_t pcidev = osdev->parent;
170  if (strncmp(osdev->name, "opencl", 6))
171  continue;
172  if (pcidev
173  && pcidev->type == HWLOC_OBJ_PCI_DEVICE
174  && pcidev->attr->pcidev.domain == 0
175  && pcidev->attr->pcidev.bus == amdtopo.pcie.bus
176  && pcidev->attr->pcidev.dev == amdtopo.pcie.device
177  && pcidev->attr->pcidev.func == amdtopo.pcie.function)
178  return osdev;
179  }
180 
181  return NULL;
182 #else
183  return NULL;
184 #endif
185 }
186 
190 #ifdef __cplusplus
191 } /* extern "C" */
192 #endif
193 
194 
195 #endif /* HWLOC_OPENCL_H */