Hardware Locality (hwloc)  PR-737-20250925.0822.gite8f69c77f
rsmi.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright © 2012-2024 Inria. All rights reserved.
4  * Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
5  * Written by Advanced Micro Devices,
6  * See COPYING in top-level directory.
7  */
8 
16 #ifndef HWLOC_RSMI_H
17 #define HWLOC_RSMI_H
18 
19 #include "hwloc.h"
20 #include "hwloc/autogen/config.h"
21 #include "hwloc/helper.h"
22 #ifdef HWLOC_LINUX_SYS
23 #include "hwloc/linux.h"
24 #endif
25 
26 #include <rocm_smi/rocm_smi.h>
27 
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
63 static __hwloc_inline int
64 hwloc_rsmi_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
65  uint32_t dv_ind, hwloc_cpuset_t set)
66 {
67 #ifdef HWLOC_LINUX_SYS
68  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
69 #define HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX 128
70  char path[HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX];
71  rsmi_status_t ret;
72  uint64_t bdfid = 0;
73  unsigned domain, device, bus;
74 
75  if (!hwloc_topology_is_thissystem(topology)) {
76  errno = EINVAL;
77  return -1;
78  }
79 
80  ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
81  if (RSMI_STATUS_SUCCESS != ret) {
82  errno = EINVAL;
83  return -1;
84  }
85  domain = (bdfid>>32) & 0xffffffff;
86  bus = ((bdfid & 0xffff)>>8) & 0xff;
87  device = ((bdfid & 0xff)>>3) & 0x1f;
88 
89  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, device);
90  if (hwloc_linux_read_path_as_cpumask(path, set) < 0
91  || hwloc_bitmap_iszero(set))
93 #else
94  /* Non-Linux systems simply get a full cpuset */
96 #endif
97  return 0;
98 }
99 
115 static __hwloc_inline hwloc_obj_t
117 {
118  hwloc_obj_t osdev = NULL;
119  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
120  if ((osdev->attr->osdev.types & (HWLOC_OBJ_OSDEV_GPU|HWLOC_OBJ_OSDEV_COPROC)) /* assume future RSMI devices will be at least GPU or COPROC */
121  && osdev->name
122  && !strncmp("rsmi", osdev->name, 4)
123  && atoi(osdev->name + 4) == (int) dv_ind)
124  return osdev;
125  }
126  return NULL;
127 }
128 
144 static __hwloc_inline hwloc_obj_t
146 {
147  hwloc_obj_t osdev;
148  rsmi_status_t ret;
149  uint64_t bdfid = 0;
150  unsigned domain, device, bus, func;
151  uint64_t id;
152  char uuid[64];
153 
154  if (!hwloc_topology_is_thissystem(topology)) {
155  errno = EINVAL;
156  return NULL;
157  }
158 
159  ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
160  if (RSMI_STATUS_SUCCESS != ret) {
161  errno = EINVAL;
162  return NULL;
163  }
164  domain = (bdfid>>32) & 0xffffffff;
165  bus = ((bdfid & 0xffff)>>8) & 0xff;
166  device = ((bdfid & 0xff)>>3) & 0x1f;
167  func = bdfid & 0x7;
168 
169  ret = rsmi_dev_unique_id_get(dv_ind, &id);
170  if (RSMI_STATUS_SUCCESS != ret)
171  uuid[0] = '\0';
172  else
173  sprintf(uuid, "%lx", id);
174 
175  osdev = NULL;
176  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
177  hwloc_obj_t pcidev = osdev->parent;
178  const char *info;
179 
180  if (strncmp(osdev->name, "rsmi", 4))
181  continue;
182 
183  if (pcidev
184  && pcidev->type == HWLOC_OBJ_PCI_DEVICE
185  && pcidev->attr->pcidev.domain == domain
186  && pcidev->attr->pcidev.bus == bus
187  && pcidev->attr->pcidev.dev == device
188  && pcidev->attr->pcidev.func == func)
189  return osdev;
190 
191  info = hwloc_obj_get_info_by_name(osdev, "AMDUUID");
192  if (info && !strcmp(info, uuid))
193  return osdev;
194  }
195 
196  return NULL;
197 }
198 
202 #ifdef __cplusplus
203 } /* extern "C" */
204 #endif
205 
206 
207 #endif /* HWLOC_RSMI_H */
HWLOC_OBJ_OSDEV_COPROC
@ HWLOC_OBJ_OSDEV_COPROC
Operating system co-processor device. For instance "opencl0d0" for a OpenCL device,...
Definition: hwloc.h:396
hwloc_bitmap_iszero
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
hwloc_obj
Structure of a topology object.
Definition: hwloc.h:488
HWLOC_OBJ_OSDEV_GPU
@ HWLOC_OBJ_OSDEV_GPU
Operating system GPU device. For instance ":0.0" for a GL display, "card0" for a Linux DRM device,...
Definition: hwloc.h:389
hwloc_topology_t
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:778
hwloc_linux_read_path_as_cpumask
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_rsmi_get_device_osdev
hwloc_obj_t hwloc_rsmi_get_device_osdev(hwloc_topology_t topology, uint32_t dv_ind)
Get the hwloc OS device object corresponding to AMD GPU device, whose index is dv_ind.
Definition: rsmi.h:145
hwloc_obj_attr_u::hwloc_pcidev_attr_s::dev
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:727
hwloc_obj_attr_u::hwloc_pcidev_attr_s::bus
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:726
hwloc_obj::parent
struct hwloc_obj * parent
Parent, NULL if root (Machine object)
Definition: hwloc.h:538
hwloc_obj_attr_u::osdev
struct hwloc_obj_attr_u::hwloc_osdev_attr_s osdev
hwloc_topology_get_complete_cpuset
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
hwloc_obj::attr
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:507
hwloc_obj_attr_u::hwloc_osdev_attr_s::types
hwloc_obj_osdev_types_t types
OR'ed set of at least one hwloc_obj_osdev_type_e.
Definition: hwloc.h:761
hwloc_obj::name
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition: hwloc.h:500
hwloc_rsmi_get_device_osdev_by_index
hwloc_obj_t hwloc_rsmi_get_device_osdev_by_index(hwloc_topology_t topology, uint32_t dv_ind)
Get the hwloc OS device object corresponding to the AMD GPU device whose index is dv_ind.
Definition: rsmi.h:116
hwloc_cpuset_t
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:162
hwloc_obj_attr_u::hwloc_pcidev_attr_s::domain
unsigned int domain
Domain number (xxxx in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:725
HWLOC_OBJ_PCI_DEVICE
@ HWLOC_OBJ_PCI_DEVICE
PCI device (filtered out by default).
Definition: hwloc.h:320
hwloc_bitmap_copy
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.
hwloc_obj_attr_u::hwloc_pcidev_attr_s::func
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:728
hwloc_obj_attr_u::pcidev
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
hwloc_obj::type
hwloc_obj_type_t type
Type of object.
Definition: hwloc.h:490
hwloc_get_next_osdev
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:1291
hwloc_obj_get_info_by_name
const char * hwloc_obj_get_info_by_name(hwloc_obj_t obj, const char *name)
Search the given name in the object array of infos and return the corresponding value.
hwloc_rsmi_get_device_cpuset
int hwloc_rsmi_get_device_cpuset(hwloc_topology_t topology, uint32_t dv_ind, hwloc_cpuset_t set)
Get the CPU set of logical processors that are physically close to AMD GPU device whose index is dv_i...
Definition: rsmi.h:64
hwloc_topology_is_thissystem
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?