Hardware Locality (hwloc) 3.0.0a1-git
opencl.h
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 * Copyright © 2012-2026 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
22#ifdef __APPLE__
23#include <OpenCL/cl.h>
24#else
25#include <CL/cl.h>
26#endif
27
28#include <stdio.h>
29
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35
36/* OpenCL extensions aren't always shipped with default headers, and
37 * they don't always reflect what the installed implementations support.
38 * Try everything and let the implementation return errors when non supported.
39 */
40/* Copyright (c) 2008-2018 The Khronos Group Inc. */
41
42/* needs "cl_khr_pci_bus_info" device extension, but not strictly required for clGetDeviceInfo() */
43typedef struct {
44 cl_uint pci_domain;
45 cl_uint pci_bus;
46 cl_uint pci_device;
47 cl_uint pci_function;
49#define HWLOC_CL_DEVICE_PCI_BUS_INFO_KHR 0x410F
50
51/* needs "cl_amd_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
52#define HWLOC_CL_DEVICE_TOPOLOGY_AMD 0x4037
53typedef union {
54 struct { cl_uint type; cl_uint data[5]; } raw;
55 struct { cl_uint type; cl_char unused[17]; cl_char bus; cl_char device; cl_char function; } pcie;
57#define HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD 1
58
59/* needs "cl_nv_device_attribute_query" device extension, but not strictly required for clGetDeviceInfo() */
60#define HWLOC_CL_DEVICE_PCI_BUS_ID_NV 0x4008
61#define HWLOC_CL_DEVICE_PCI_SLOT_ID_NV 0x4009
62#define HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV 0x400A
63
64
75
83static __hwloc_inline int
85 unsigned *domain, unsigned *bus, unsigned *dev, unsigned *func)
86{
89 cl_uint nvbus, nvslot, nvdomain;
90 cl_int clret;
91
92 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_INFO_KHR, sizeof(khrbusinfo), &khrbusinfo, NULL);
93 if (CL_SUCCESS == clret) {
94 *domain = (unsigned) khrbusinfo.pci_domain;
95 *bus = (unsigned) khrbusinfo.pci_bus;
96 *dev = (unsigned) khrbusinfo.pci_device;
97 *func = (unsigned) khrbusinfo.pci_function;
98 return 0;
99 }
100
101 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_TOPOLOGY_AMD, sizeof(amdtopo), &amdtopo, NULL);
102 if (CL_SUCCESS == clret
103 && HWLOC_CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD == amdtopo.raw.type) {
104 *domain = 0; /* can't do anything better */
105 /* cl_device_topology_amd stores bus ID in cl_char, dont convert those signed char directly to unsigned int */
106 *bus = (unsigned) (unsigned char) amdtopo.pcie.bus;
107 *dev = (unsigned) (unsigned char) amdtopo.pcie.device;
108 *func = (unsigned) (unsigned char) amdtopo.pcie.function;
109 return 0;
110 }
111
112 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_BUS_ID_NV, sizeof(nvbus), &nvbus, NULL);
113 if (CL_SUCCESS == clret) {
114 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_SLOT_ID_NV, sizeof(nvslot), &nvslot, NULL);
115 if (CL_SUCCESS == clret) {
116 clret = clGetDeviceInfo(device, HWLOC_CL_DEVICE_PCI_DOMAIN_ID_NV, sizeof(nvdomain), &nvdomain, NULL);
117 if (CL_SUCCESS == clret) { /* available since CUDA 10.2 */
118 *domain = nvdomain;
119 } else {
120 *domain = 0;
121 }
122 *bus = nvbus & 0xff;
123 /* non-documented but used in many other projects */
124 *dev = nvslot >> 3;
125 *func = nvslot & 0x7;
126 return 0;
127 }
128 }
129
130 return -1;
131}
132
152static __hwloc_inline int
153hwloc_opencl_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
154 cl_device_id device __hwloc_attribute_unused,
155 hwloc_cpuset_t set)
156{
157 unsigned pcidomain, pcibus, pcidev, pcifunc;
158
159 if (!hwloc_topology_is_thissystem(topology)) {
160 errno = EINVAL;
161 return -1;
162 }
163
164 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidev, &pcifunc) < 0) {
166 return 0;
167 }
168
169 return hwloc_get_pci_busid_cpuset(topology, set, pcidomain, pcibus, pcidev, pcifunc);
170}
171
187static __hwloc_inline hwloc_obj_t
189 unsigned platform_index, unsigned device_index)
190{
191 unsigned x = (unsigned) -1, y = (unsigned) -1;
192 hwloc_obj_t osdev = NULL;
193 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
194 if ((osdev->attr->osdev.types & HWLOC_OBJ_OSDEV_COPROC) /* assume future OpenCL devices will be at least COPROC */
195 && osdev->name
196 && sscanf(osdev->name, "opencl%ud%u", &x, &y) == 2
197 && platform_index == x && device_index == y)
198 return osdev;
199 }
200 return NULL;
201}
202
224static __hwloc_inline hwloc_obj_t
225hwloc_opencl_get_device_osdev(hwloc_topology_t topology __hwloc_attribute_unused,
226 cl_device_id device __hwloc_attribute_unused)
227{
228 hwloc_obj_t osdev;
229 unsigned pcidomain, pcibus, pcidevice, pcifunc;
230
231 if (hwloc_opencl_get_device_pci_busid(device, &pcidomain, &pcibus, &pcidevice, &pcifunc) < 0) {
232 errno = EINVAL;
233 return NULL;
234 }
235
236 osdev = NULL;
237 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
238 hwloc_obj_t pcidev = osdev->parent;
239 if (strncmp(osdev->name, "opencl", 6))
240 continue;
241 if (pcidev
242 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
243 && pcidev->attr->pcidev.domain == pcidomain
244 && pcidev->attr->pcidev.bus == pcibus
245 && pcidev->attr->pcidev.dev == pcidevice
246 && pcidev->attr->pcidev.func == pcifunc)
247 return osdev;
248 /* if PCI are filtered out, we need a info attr to match on */
249 }
250
251 return NULL;
252}
253
255
256
257#ifdef __cplusplus
258} /* extern "C" */
259#endif
260
261
262#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:1310
int hwloc_get_pci_busid_cpuset(hwloc_topology_t topology, hwloc_cpuset_t cpuset, unsigned domain, unsigned bus, unsigned dev, unsigned func)
Find the locality of a given PCI bus id.
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:788
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU 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:400
@ HWLOC_OBJ_PCI_DEVICE
PCI device (filtered out by default).
Definition hwloc.h:324
struct hwloc_obj * hwloc_obj_t
Convenience typedef; a pointer to a struct hwloc_obj.
Definition hwloc.h:691
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:153
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:84
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:225
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:188
Definition opencl.h:43
cl_uint pci_bus
Definition opencl.h:45
cl_uint pci_device
Definition opencl.h:46
cl_uint pci_domain
Definition opencl.h:44
cl_uint pci_function
Definition opencl.h:47
hwloc_obj_osdev_types_t types
OR'ed set of at least one hwloc_obj_osdev_type_e.
Definition hwloc.h:771
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:737
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:738
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:736
unsigned int domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t).
Definition hwloc.h:735
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition hwloc.h:504
hwloc_obj_type_t type
Type of object.
Definition hwloc.h:494
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition hwloc.h:511
struct hwloc_obj * parent
Parent, NULL if root (Machine object).
Definition hwloc.h:542
Definition opencl.h:53
cl_char device
Definition opencl.h:55
cl_char bus
Definition opencl.h:55
cl_char function
Definition opencl.h:55
struct hwloc_cl_device_topology_amd::@071162130142265310326120313235175131267326125102 pcie
cl_uint type
Definition opencl.h:54
cl_char unused[17]
Definition opencl.h:55
struct hwloc_cl_device_topology_amd::@367043162050367207255353304342340012354032217026 raw
cl_uint data[5]
Definition opencl.h:54
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
struct hwloc_obj_attr_u::hwloc_osdev_attr_s osdev