Hardware Locality (hwloc) 2.16.0
opencl.h
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2012-2023 Inria. All rights reserved.
4 * Copyright © 2013, 2018 Université Bordeaux. All right reserved.
5 * See COPYING in top-level directory.
6 */
7
14
15#ifndef HWLOC_OPENCL_H
16#define HWLOC_OPENCL_H
17
18#include "hwloc.h"
19#include "hwloc/autogen/config.h"
20#include "hwloc/helper.h"
21#ifdef HWLOC_LINUX_SYS
22#include "hwloc/linux.h"
23#endif
24
25#ifdef __APPLE__
26#include <OpenCL/cl.h>
27#else
28#include <CL/cl.h>
29#endif
30
31#include <stdio.h>
32
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38
39/* \cond 0
40 * Don't document these internal structures.
41 */
42
43/* OpenCL extensions aren't always shipped with default headers, and
44 * they don't always reflect what the installed implementations support.
45 * Try everything and let the implementation return errors when non supported.
46 */
47/* Copyright (c) 2008-2018 The Khronos Group Inc. */
48
49/* needs "cl_khr_pci_bus_info" device extension, but not strictly required for clGetDeviceInfo() */
50typedef struct {
51 cl_uint pci_domain;
52 cl_uint pci_bus;
53 cl_uint pci_device;
54 cl_uint pci_function;
55} hwloc_cl_device_pci_bus_info_khr;
56#define HWLOC_CL_DEVICE_PCI_BUS_INFO_KHR 0x410F
57
58/* needs "cl_amd_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
59#define HWLOC_CL_DEVICE_TOPOLOGY_AMD 0x4037
60typedef union {
61 struct { cl_uint type; cl_uint data[5]; } raw;
62 struct { cl_uint type; cl_char unused[17]; cl_char bus; cl_char device; cl_char function; } pcie;
63} hwloc_cl_device_topology_amd;
64#define HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD 1
65
66/* needs "cl_nv_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
67#define HWLOC_CL_DEVICE_PCI_BUS_ID_NV 0x4008
68#define HWLOC_CL_DEVICE_PCI_SLOT_ID_NV 0x4009
69#define HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV 0x400A
70
71/* End of Don't document these internal structures.
72 * \endcond
73 */
74
75
76
87
95static __hwloc_inline int
97 unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
98{
99 hwloc_cl_device_topology_amd amdtopo;
100 hwloc_cl_device_pci_bus_info_khr khrbusinfo;
101 cl_uint nvbus, nvslot, nvdomain;
102 cl_int clret;
103
104 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_INFO_KHR, sizeof(khrbusinfo), &khrbusinfo, NULL);
105 if (CL_SUCCESS == clret) {
106 *domain = (unsigned) khrbusinfo.pci_domain;
107 *bus = (unsigned) khrbusinfo.pci_bus;
108 *dev = (unsigned) khrbusinfo.pci_device;
109 *func = (unsigned) khrbusinfo.pci_function;
110 return 0;
111 }
112
113 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
114 if (CL_SUCCESS == clret
115 && HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD == amdtopo.raw.type) {
116 *domain = 0; /* can't do anything better */
117 /* cl_device_topology_amd stores bus ID in cl_char, dont convert those signed char directly to unsigned int */
118 *bus = (unsigned) (unsigned char) amdtopo.pcie.bus;
119 *dev = (unsigned) (unsigned char) amdtopo.pcie.device;
120 *func = (unsigned) (unsigned char) amdtopo.pcie.function;
121 return 0;
122 }
123
124 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_ID_NV, sizeof(nvbus), &nvbus, NULL);
125 if (CL_SUCCESS == clret) {
126 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_SLOT_ID_NV, sizeof(nvslot), &nvslot, NULL);
127 if (CL_SUCCESS == clret) {
128 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV, sizeof(nvdomain), &nvdomain, NULL);
129 if (CL_SUCCESS == clret) { /* available since CUDA 10.2 */
130 *domain = nvdomain;
131 } else {
132 *domain = 0;
133 }
134 *bus = nvbus & 0xff;
135 /* non-documented but used in many other projects */
136 *dev = nvslot >> 3;
137 *func = nvslot & 0x7;
138 return 0;
139 }
140 }
141
142 return -1;
143}
144
165static __hwloc_inline int
166hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
167 cl_device_id device __hwloc_attribute_unused,
168 hwloc_cpuset_t set)
169{
170#if (defined HWLOC_LINUX_SYS)
171 /* If we're on Linux, try AMD/NVIDIA extensions + the sysfs mechanism to get the local cpus */
172#define HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX 128
173 char path[HWLOC_OPENCL_DEVICE_SYSFS_PATH_MAX];
174 unsigned pcidomain, pcibus, pcidev, pcifunc;
175
176 if (!hwloc_topology_is_thissystem(topology)) {
177 errno = EINVAL;
178 return -1;
179 }
180
181 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidev, &pcifunc) < 0) {
183 return 0;
184 }
185
186 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/local_cpus", pcidomain, pcibus, pcidev, pcifunc);
187 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
188 || hwloc_bitmap_iszero(set))
190#else
191 /* Non-Linux systems simply get a full cpuset */
193#endif
194 return 0;
195}
196
212static __hwloc_inline hwloc_obj_t
214 unsigned platform_index, unsigned device_index)
215{
216 unsigned x = (unsigned) -1, y = (unsigned) -1;
217 hwloc_obj_t osdev = NULL;
218 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
220 && osdev->name
221 && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
222 && platform_index == x && device_index == y)
223 return osdev;
224 }
225 return NULL;
226}
227
249static __hwloc_inline hwloc_obj_t
250hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
251 cl_device_id device __hwloc_attribute_unused)
252{
253 hwloc_obj_t osdev;
254 unsigned pcidomain, pcibus, pcidevice, pcifunc;
255
256 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidevice, &pcifunc) < 0) {
257 errno = EINVAL;
258 return NULL;
259 }
260
261 osdev = NULL;
262 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
263 hwloc_obj_t pcidev = osdev->parent;
264 if (strncmp(osdev->name, "opencl", 6))
265 continue;
266 if (pcidev
267 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
268 && pcidev->attr->pcidev.domain == pcidomain
269 && pcidev->attr->pcidev.bus == pcibus
270 && pcidev->attr->pcidev.dev == pcidevice
271 && pcidev->attr->pcidev.func == pcifunc)
272 return osdev;
273 /* if PCI are filtered out, we need a info attr to match on */
274 }
275
276 return NULL;
277}
278
280
281
282#ifdef __cplusplus
283} /* extern "C" */
284#endif
285
286
287#endif /* HWLOC_OPENCL_H */
hwloc_obj_t hwloc_get_next_osdev(hwloc_topology_t topology, hwloc_obj_t prev)
Get the next OS device in the system.
Definition helper.h:1328
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
int hwloc_bitmap_copy(hwloc_bitmap_t dst, hwloc_const_bitmap_t src)
Copy the contents of bitmap src into the already allocated bitmap dst.
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition hwloc.h:760
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get the complete CPU set.
int hwloc_linux_read_path_as_cpumask(const char *path, hwloc_bitmap_t set)
Convert a linux kernel cpumask file path into a hwloc bitmap set.
hwloc_bitmap_t hwloc_cpuset_t
A CPU set is a bitmap whose bits are set according to CPU physical OS indexes.
Definition hwloc.h:165
@ HWLOC_OBJ_OSDEV_COPROC
Operating system co-processor device. For instance "opencl0d0" for a OpenCL device,...
Definition hwloc.h:391
@ HWLOC_OBJ_PCI_DEVICE
PCI device (filtered out by default).
Definition hwloc.h:300
struct hwloc_obj * hwloc_obj_t
Convenience typedef; a pointer to a struct hwloc_obj.
Definition hwloc.h:645
int hwloc_opencl_get_device_cpuset(hwloc_topology_t topology, cl_device_id device, hwloc_cpuset_t set)
Get the CPU set of processors that are physically close to OpenCL device device.
Definition opencl.h:166
int hwloc_opencl_get_device_pci_busid(cl_device_id device, unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
Return the domain, bus and device IDs of the OpenCL device device.
Definition opencl.h:96
hwloc_obj_t hwloc_opencl_get_device_osdev(hwloc_topology_t topology, cl_device_id device)
Get the hwloc OS device object corresponding to OpenCL device deviceX.
Definition opencl.h:250
hwloc_obj_t hwloc_opencl_get_device_osdev_by_index(hwloc_topology_t topology, unsigned platform_index, unsigned device_index)
Get the hwloc OS device object corresponding to the OpenCL device for the given indexes.
Definition opencl.h:213
hwloc_obj_osdev_type_t type
Definition hwloc.h:734
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:693
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:694
unsigned short domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t). Only 16bits PCI domains are supported by d...
Definition hwloc.h:686
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:692
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition hwloc.h:449
hwloc_obj_type_t type
Type of object.
Definition hwloc.h:439
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition hwloc.h:456
struct hwloc_obj * parent
Parent, NULL if root (Machine object).
Definition hwloc.h:487
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
struct hwloc_obj_attr_u::hwloc_osdev_attr_s osdev