Hardware Locality (hwloc)  2.10.0
rsmi.h
1 /*
2  * Copyright © 2012-2023 Inria. All rights reserved.
3  * Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
4  * Written by Advanced Micro Devices,
5  * See COPYING in top-level directory.
6  */
7 
15 #ifndef HWLOC_RSMI_H
16 #define HWLOC_RSMI_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 #include <rocm_smi/rocm_smi.h>
26 
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 
62 static __hwloc_inline int
63 hwloc_rsmi_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
64  uint32_t dv_ind, hwloc_cpuset_t set)
65 {
66 #ifdef HWLOC_LINUX_SYS
67  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
68 #define HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX 128
69  char path[HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX];
70  rsmi_status_t ret;
71  uint64_t bdfid = 0;
72  unsigned domain, device, bus;
73 
74  if (!hwloc_topology_is_thissystem(topology)) {
75  errno = EINVAL;
76  return -1;
77  }
78 
79  ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
80  if (RSMI_STATUS_SUCCESS != ret) {
81  errno = EINVAL;
82  return -1;
83  }
84  domain = (bdfid>>32) & 0xffffffff;
85  bus = ((bdfid & 0xffff)>>8) & 0xff;
86  device = ((bdfid & 0xff)>>3) & 0x1f;
87 
88  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, device);
89  if (hwloc_linux_read_path_as_cpumask(path, set) < 0
90  || hwloc_bitmap_iszero(set))
92 #else
93  /* Non-Linux systems simply get a full cpuset */
95 #endif
96  return 0;
97 }
98 
114 static __hwloc_inline hwloc_obj_t
116 {
117  hwloc_obj_t osdev = NULL;
118  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
119  if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type
120  && osdev->name
121  && !strncmp("rsmi", osdev->name, 4)
122  && atoi(osdev->name + 4) == (int) dv_ind)
123  return osdev;
124  }
125  return NULL;
126 }
127 
143 static __hwloc_inline hwloc_obj_t
145 {
146  hwloc_obj_t osdev;
147  rsmi_status_t ret;
148  uint64_t bdfid = 0;
149  unsigned domain, device, bus, func;
150  uint64_t id;
151  char uuid[64];
152 
153  if (!hwloc_topology_is_thissystem(topology)) {
154  errno = EINVAL;
155  return NULL;
156  }
157 
158  ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
159  if (RSMI_STATUS_SUCCESS != ret) {
160  errno = EINVAL;
161  return NULL;
162  }
163  domain = (bdfid>>32) & 0xffffffff;
164  bus = ((bdfid & 0xffff)>>8) & 0xff;
165  device = ((bdfid & 0xff)>>3) & 0x1f;
166  func = bdfid & 0x7;
167 
168  ret = rsmi_dev_unique_id_get(dv_ind, &id);
169  if (RSMI_STATUS_SUCCESS != ret)
170  uuid[0] = '\0';
171  else
172  sprintf(uuid, "%lx", id);
173 
174  osdev = NULL;
175  while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
176  hwloc_obj_t pcidev = osdev->parent;
177  const char *info;
178 
179  if (strncmp(osdev->name, "rsmi", 4))
180  continue;
181 
182  if (pcidev
183  && pcidev->type == HWLOC_OBJ_PCI_DEVICE
184  && pcidev->attr->pcidev.domain == domain
185  && pcidev->attr->pcidev.bus == bus
186  && pcidev->attr->pcidev.dev == device
187  && pcidev->attr->pcidev.func == func)
188  return osdev;
189 
190  info = hwloc_obj_get_info_by_name(osdev, "AMDUUID");
191  if (info && !strcmp(info, uuid))
192  return osdev;
193  }
194 
195  return NULL;
196 }
197 
201 #ifdef __cplusplus
202 } /* extern "C" */
203 #endif
204 
205 
206 #endif /* HWLOC_RSMI_H */
unsigned char func
Function number (t in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:678
Structure of a topology object.
Definition: hwloc.h:431
static 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:1282
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.
struct hwloc_obj_attr_u::hwloc_pcidev_attr_s pcidev
static 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:115
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
int hwloc_bitmap_iszero(hwloc_const_bitmap_t bitmap)
Test whether bitmap bitmap is empty.
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:670
int hwloc_topology_is_thissystem(hwloc_topology_t restrict topology)
Does the topology context come from this system?
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:161
static 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:63
Operating system GPU device. For instance ":0.0" for a GL display, "card0" for a Linux DRM device...
Definition: hwloc.h:374
PCI device (filtered out by default).
Definition: hwloc.h:295
union hwloc_obj_attr_u * attr
Object type-specific Attributes, may be NULL if no attribute value was found.
Definition: hwloc.h:450
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_obj_type_t type
Type of object.
Definition: hwloc.h:433
unsigned char dev
Device number (zz in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:677
struct hwloc_obj * parent
Parent, NULL if root (Machine object)
Definition: hwloc.h:481
static const char * hwloc_obj_get_info_by_name(hwloc_obj_t obj, const char *name)
Search the given name in object infos and return the corresponding value.
struct hwloc_topology * hwloc_topology_t
Topology context.
Definition: hwloc.h:742
unsigned char bus
Bus number (yy in the PCI BDF notation xxxx:yy:zz.t).
Definition: hwloc.h:676
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition: hwloc.h:443
struct hwloc_obj_attr_u::hwloc_osdev_attr_s osdev
hwloc_obj_osdev_type_t type
Definition: hwloc.h:716
static 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:144