opencl.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright © 2012-2023 Inria. All rights reserved.
  3. * Copyright © 2013, 2018 Université Bordeaux. All right reserved.
  4. * See COPYING in top-level directory.
  5. */
  6. /** \file
  7. * \brief Macros to help interaction between hwloc and the OpenCL interface.
  8. *
  9. * Applications that use both hwloc and OpenCL may want to
  10. * include this file so as to get topology information for OpenCL devices.
  11. */
  12. #ifndef HWLOC_OPENCL_H
  13. #define HWLOC_OPENCL_H
  14. #include "hwloc.h"
  15. #include "hwloc/autogen/config.h"
  16. #include "hwloc/helper.h"
  17. #ifdef HWLOC_LINUX_SYS
  18. #include "hwloc/linux.h"
  19. #endif
  20. #ifdef __APPLE__
  21. #include <OpenCL/cl.h>
  22. #else
  23. #include <CL/cl.h>
  24. #endif
  25. #include <stdio.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* OpenCL extensions aren't always shipped with default headers, and
  30. * they don't always reflect what the installed implementations support.
  31. * Try everything and let the implementation return errors when non supported.
  32. */
  33. /* Copyright (c) 2008-2018 The Khronos Group Inc. */
  34. /* needs "cl_amd_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
  35. #define HWLOC_CL_DEVICE_TOPOLOGY_AMD 0x4037
  36. typedef union {
  37. struct { cl_uint type; cl_uint data[5]; } raw;
  38. struct { cl_uint type; cl_char unused[17]; cl_char bus; cl_char device; cl_char function; } pcie;
  39. } hwloc_cl_device_topology_amd;
  40. #define HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD 1
  41. /* needs "cl_nv_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
  42. #define HWLOC_CL_DEVICE_PCI_BUS_ID_NV 0x4008
  43. #define HWLOC_CL_DEVICE_PCI_SLOT_ID_NV 0x4009
  44. #define HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV 0x400A
  45. /** \defgroup hwlocality_opencl Interoperability with OpenCL
  46. *
  47. * This interface offers ways to retrieve topology information about
  48. * OpenCL devices.
  49. *
  50. * Only AMD and NVIDIA OpenCL implementations currently offer useful locality
  51. * information about their devices.
  52. *
  53. * @{
  54. */
  55. /** \brief Return the domain, bus and device IDs of the OpenCL device \p device.
  56. *
  57. * Device \p device must match the local machine.
  58. *
  59. * \return 0 on success.
  60. * \return -1 on error, for instance if device information could not be found.
  61. */
  62. static __hwloc_inline int
  63. hwloc_opencl_get_device_pci_busid(cl_device_id device,
  64. unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
  65. {
  66. hwloc_cl_device_topology_amd amdtopo;
  67. cl_uint nvbus, nvslot, nvdomain;
  68. cl_int clret;
  69. clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
  70. if (CL_SUCCESS == clret
  71. && HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD == amdtopo.raw.type) {
  72. *domain = 0; /* can't do anything better */
  73. /* cl_device_topology_amd stores bus ID in cl_char, dont convert those signed char directly to unsigned int */
  74. *bus = (unsigned) (unsigned char) amdtopo.pcie.bus;
  75. *dev = (unsigned) (unsigned char) amdtopo.pcie.device;
  76. *func = (unsigned) (unsigned char) amdtopo.pcie.function;
  77. return 0;
  78. }
  79. clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_ID_NV, sizeof(nvbus), &nvbus, NULL);
  80. if (CL_SUCCESS == clret) {
  81. clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_SLOT_ID_NV, sizeof(nvslot), &nvslot, NULL);
  82. if (CL_SUCCESS == clret) {
  83. clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV, sizeof(nvdomain), &nvdomain, NULL);
  84. if (CL_SUCCESS == clret) { /* available since CUDA 10.2 */
  85. *domain = nvdomain;
  86. } else {
  87. *domain = 0;
  88. }
  89. *bus = nvbus & 0xff;
  90. /* non-documented but used in many other projects */
  91. *dev = nvslot >> 3;
  92. *func = nvslot & 0x7;
  93. return 0;
  94. }
  95. }
  96. return -1;
  97. }
  98. /** \brief Get the CPU set of processors that are physically
  99. * close to OpenCL device \p device.
  100. *
  101. * Store in \p set the CPU-set describing the locality of the OpenCL device \p device.
  102. *
  103. * Topology \p topology and device \p device must match the local machine.
  104. * I/O devices detection and the OpenCL component are not needed in the topology.
  105. *
  106. * The function only returns the locality of the device.
  107. * If more information about the device is needed, OS objects should
  108. * be used instead, see hwloc_opencl_get_device_osdev()
  109. * and hwloc_opencl_get_device_osdev_by_index().
  110. *
  111. * This function is currently only implemented in a meaningful way for
  112. * Linux with the AMD or NVIDIA OpenCL implementation; other systems will simply
  113. * get a full cpuset.
  114. *
  115. * \return 0 on success.
  116. * \return -1 on error, for instance if the device could not be found.
  117. */
  118. static __hwloc_inline int
  119. hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
  120. cl_device_id device __hwloc_attribute_unused,
  121. hwloc_cpuset_t set)
  122. {
  123. #if (defined HWLOC_LINUX_SYS)
  124. /* If we're on Linux, try AMD/NVIDIA extensions + the sysfs mechanism to get the local cpus */
  125. #define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
  126. char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
  127. unsigned pcidomain, pcibus, pcidev, pcifunc;
  128. if (!hwloc_topology_is_thissystem(topology)) {
  129. errno = EINVAL;
  130. return -1;
  131. }
  132. if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidev, &pcifunc) < 0) {
  133. hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
  134. return 0;
  135. }
  136. sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus", pcidomain, pcibus, pcidev, pcifunc);
  137. if (hwloc_linux_read_path_as_cpumask(path, set) < 0
  138. || hwloc_bitmap_iszero(set))
  139. hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
  140. #else
  141. /* Non-Linux systems simply get a full cpuset */
  142. hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
  143. #endif
  144. return 0;
  145. }
  146. /** \brief Get the hwloc OS device object corresponding to the
  147. * OpenCL device for the given indexes.
  148. *
  149. * \return The hwloc OS device object describing the OpenCL device
  150. * whose platform index is \p platform_index,
  151. * and whose device index within this platform if \p device_index.
  152. * \return \c NULL if there is none.
  153. *
  154. * The topology \p topology does not necessarily have to match the current
  155. * machine. For instance the topology may be an XML import of a remote host.
  156. * I/O devices detection and the OpenCL component must be enabled in the topology.
  157. *
  158. * \note The corresponding PCI device object can be obtained by looking
  159. * at the OS device parent object (unless PCI devices are filtered out).
  160. */
  161. static __hwloc_inline hwloc_obj_t
  162. hwloc_opencl_get_device_osdev_by_index(hwloc_topology_t topology,
  163. unsigned platform_index, unsigned device_index)
  164. {
  165. unsigned x = (unsigned) -1, y = (unsigned) -1;
  166. hwloc_obj_t osdev = NULL;
  167. while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
  168. if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
  169. && osdev->name
  170. && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
  171. && platform_index == x && device_index == y)
  172. return osdev;
  173. }
  174. return NULL;
  175. }
  176. /** \brief Get the hwloc OS device object corresponding to OpenCL device \p deviceX.
  177. *
  178. * \return The hwloc OS device object corresponding to the given OpenCL device \p device.
  179. * \return \c NULL if none could be found, for instance
  180. * if required OpenCL attributes are not available.
  181. *
  182. * This function currently only works on AMD and NVIDIA OpenCL devices that support
  183. * relevant OpenCL extensions. hwloc_opencl_get_device_osdev_by_index()
  184. * should be preferred whenever possible, i.e. when platform and device index
  185. * are known.
  186. *
  187. * Topology \p topology and device \p device must match the local machine.
  188. * I/O devices detection and the OpenCL component must be enabled in the topology.
  189. * If not, the locality of the object may still be found using
  190. * hwloc_opencl_get_device_cpuset().
  191. *
  192. * \note This function cannot work if PCI devices are filtered out.
  193. *
  194. * \note The corresponding hwloc PCI device may be found by looking
  195. * at the result parent pointer (unless PCI devices are filtered out).
  196. */
  197. static __hwloc_inline hwloc_obj_t
  198. hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
  199. cl_device_id device __hwloc_attribute_unused)
  200. {
  201. hwloc_obj_t osdev;
  202. unsigned pcidomain, pcibus, pcidevice, pcifunc;
  203. if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidevice, &pcifunc) < 0) {
  204. errno = EINVAL;
  205. return NULL;
  206. }
  207. osdev = NULL;
  208. while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
  209. hwloc_obj_t pcidev = osdev->parent;
  210. if (strncmp(osdev->name, "opencl", 6))
  211. continue;
  212. if (pcidev
  213. && pcidev->type == HWLOC_OBJ_PCI_DEVICE
  214. && pcidev->attr->pcidev.domain == pcidomain
  215. && pcidev->attr->pcidev.bus == pcibus
  216. && pcidev->attr->pcidev.dev == pcidevice
  217. && pcidev->attr->pcidev.func == pcifunc)
  218. return osdev;
  219. /* if PCI are filtered out, we need a info attr to match on */
  220. }
  221. return NULL;
  222. }
  223. /** @} */
  224. #ifdef __cplusplus
  225. } /* extern "C" */
  226. #endif
  227. #endif /* HWLOC_OPENCL_H */