Hardware Locality (hwloc)  2.15.0a1-git
amdsmi.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright © 2012-2026 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_AMDSMI_H
17 #define HWLOC_AMDSMI_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 <amd_smi/amdsmi.h>
27 
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
63 static __hwloc_inline int
65  amdsmi_processor_handle proc, 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  amdsmi_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 = amdsmi_get_gpu_bdf_id(proc, &bdfid);
81  if (AMDSMI_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 (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type
121  && osdev->name
122  && !strncmp("rsmi", osdev->name, 4)
123  && atoi(osdev->name + 4) == (int) hip_id)
124  return osdev;
125  }
126  return NULL;
127 }
128 
144 static __hwloc_inline hwloc_obj_t
145 hwloc_amdsmi_get_device_osdev(hwloc_topology_t topology, amdsmi_processor_handle proc)
146 {
147  amdsmi_status_t ret;
148  amdsmi_enumeration_info_t enum_info;
149 
150  ret = amdsmi_get_gpu_enumeration_info(proc, &enum_info);
151  if (AMDSMI_STATUS_SUCCESS != ret)
152  return NULL;
153 
154  return hwloc_amdsmi_get_device_osdev_by_index(topology, enum_info.hip_id);
155 }
156 
160 #ifdef __cplusplus
161 } /* extern "C" */
162 #endif
163 
164 
165 #endif /* HWLOC_AMDSMI_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:1291
hwloc_obj_t hwloc_amdsmi_get_device_osdev_by_index(hwloc_topology_t topology, uint32_t hip_id)
Get the hwloc OS device object corresponding to the AMD GPU device whose HIP index is hip_id.
Definition: amdsmi.h:116
int hwloc_amdsmi_get_device_cpuset(hwloc_topology_t topology, amdsmi_processor_handle proc, 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: amdsmi.h:64
hwloc_obj_t hwloc_amdsmi_get_device_osdev(hwloc_topology_t topology, amdsmi_processor_handle proc)
Get the hwloc OS device object corresponding to AMD GPU device, whose processor handle is proc.
Definition: amdsmi.h:145
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:748
hwloc_const_cpuset_t hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get 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_GPU
Operating system GPU device. For instance ":0.0" for a GL display, "card0" for a Linux DRM device.
Definition: hwloc.h:380
hwloc_obj_osdev_type_t type
Definition: hwloc.h:722
Structure of a topology object.
Definition: hwloc.h:437
char * name
Object-specific name if any. Mostly used for identifying OS devices and Misc objects where a name str...
Definition: hwloc.h:449
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_attr_u::hwloc_osdev_attr_s osdev