rsmi.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /** \file
  8. * \brief Macros to help interaction between hwloc and the ROCm SMI Management Library.
  9. *
  10. * Applications that use both hwloc and the ROCm SMI Management Library may want to
  11. * include this file so as to get topology information for AMD GPU devices.
  12. */
  13. #ifndef HWLOC_RSMI_H
  14. #define HWLOC_RSMI_H
  15. #include "hwloc.h"
  16. #include "hwloc/autogen/config.h"
  17. #include "hwloc/helper.h"
  18. #ifdef HWLOC_LINUX_SYS
  19. #include "hwloc/linux.h"
  20. #endif
  21. #include <rocm_smi/rocm_smi.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /** \defgroup hwlocality_rsmi Interoperability with the ROCm SMI Management Library
  26. *
  27. * This interface offers ways to retrieve topology information about
  28. * devices managed by the ROCm SMI Management Library.
  29. *
  30. * @{
  31. */
  32. /** \brief Get the CPU set of logical processors that are physically
  33. * close to AMD GPU device whose index is \p dv_ind.
  34. *
  35. * Store in \p set the CPU-set describing the locality of the AMD GPU device
  36. * whose index is \p dv_ind.
  37. *
  38. * Topology \p topology and device \p dv_ind must match the local machine.
  39. * I/O devices detection and the ROCm SMI component are not needed in the
  40. * topology.
  41. *
  42. * The function only returns the locality of the device.
  43. * If more information about the device is needed, OS objects should
  44. * be used instead, see hwloc_rsmi_get_device_osdev()
  45. * and hwloc_rsmi_get_device_osdev_by_index().
  46. *
  47. * This function is currently only implemented in a meaningful way for
  48. * Linux; other systems will simply get a full cpuset.
  49. *
  50. * \return 0 on success.
  51. * \return -1 on error, for instance if device information could not be found.
  52. */
  53. static __hwloc_inline int
  54. hwloc_rsmi_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
  55. uint32_t dv_ind, hwloc_cpuset_t set)
  56. {
  57. #ifdef HWLOC_LINUX_SYS
  58. /* If we're on Linux, use the sysfs mechanism to get the local cpus */
  59. #define HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX 128
  60. char path[HWLOC_RSMI_DEVICE_SYSFS_PATH_MAX];
  61. rsmi_status_t ret;
  62. uint64_t bdfid = 0;
  63. unsigned domain, device, bus;
  64. if (!hwloc_topology_is_thissystem(topology)) {
  65. errno = EINVAL;
  66. return -1;
  67. }
  68. ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
  69. if (RSMI_STATUS_SUCCESS != ret) {
  70. errno = EINVAL;
  71. return -1;
  72. }
  73. domain = (bdfid>>32) & 0xffffffff;
  74. bus = ((bdfid & 0xffff)>>8) & 0xff;
  75. device = ((bdfid & 0xff)>>3) & 0x1f;
  76. sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, device);
  77. if (hwloc_linux_read_path_as_cpumask(path, set) < 0
  78. || hwloc_bitmap_iszero(set))
  79. hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
  80. #else
  81. /* Non-Linux systems simply get a full cpuset */
  82. hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
  83. #endif
  84. return 0;
  85. }
  86. /** \brief Get the hwloc OS device object corresponding to the
  87. * AMD GPU device whose index is \p dv_ind.
  88. *
  89. * \return The hwloc OS device object describing the AMD GPU device whose
  90. * index is \p dv_ind.
  91. * \return \c NULL if none could be found.
  92. *
  93. * The topology \p topology does not necessarily have to match the current
  94. * machine. For instance the topology may be an XML import of a remote host.
  95. * I/O devices detection and the ROCm SMI component must be enabled in the
  96. * topology.
  97. *
  98. * \note The corresponding PCI device object can be obtained by looking
  99. * at the OS device parent object (unless PCI devices are filtered out).
  100. */
  101. static __hwloc_inline hwloc_obj_t
  102. hwloc_rsmi_get_device_osdev_by_index(hwloc_topology_t topology, uint32_t dv_ind)
  103. {
  104. hwloc_obj_t osdev = NULL;
  105. while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
  106. if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type
  107. && osdev->name
  108. && !strncmp("rsmi", osdev->name, 4)
  109. && atoi(osdev->name + 4) == (int) dv_ind)
  110. return osdev;
  111. }
  112. return NULL;
  113. }
  114. /** \brief Get the hwloc OS device object corresponding to AMD GPU device,
  115. * whose index is \p dv_ind.
  116. *
  117. * \return The hwloc OS device object that describes the given
  118. * AMD GPU, whose index is \p dv_ind.
  119. * \return \c NULL if none could be found.
  120. *
  121. * Topology \p topology and device \p dv_ind must match the local machine.
  122. * I/O devices detection and the ROCm SMI component must be enabled in the
  123. * topology. If not, the locality of the object may still be found using
  124. * hwloc_rsmi_get_device_cpuset().
  125. *
  126. * \note The corresponding hwloc PCI device may be found by looking
  127. * at the result parent pointer (unless PCI devices are filtered out).
  128. */
  129. static __hwloc_inline hwloc_obj_t
  130. hwloc_rsmi_get_device_osdev(hwloc_topology_t topology, uint32_t dv_ind)
  131. {
  132. hwloc_obj_t osdev;
  133. rsmi_status_t ret;
  134. uint64_t bdfid = 0;
  135. unsigned domain, device, bus, func;
  136. uint64_t id;
  137. char uuid[64];
  138. if (!hwloc_topology_is_thissystem(topology)) {
  139. errno = EINVAL;
  140. return NULL;
  141. }
  142. ret = rsmi_dev_pci_id_get(dv_ind, &bdfid);
  143. if (RSMI_STATUS_SUCCESS != ret) {
  144. errno = EINVAL;
  145. return NULL;
  146. }
  147. domain = (bdfid>>32) & 0xffffffff;
  148. bus = ((bdfid & 0xffff)>>8) & 0xff;
  149. device = ((bdfid & 0xff)>>3) & 0x1f;
  150. func = bdfid & 0x7;
  151. ret = rsmi_dev_unique_id_get(dv_ind, &id);
  152. if (RSMI_STATUS_SUCCESS != ret)
  153. uuid[0] = '\0';
  154. else
  155. sprintf(uuid, "%lx", id);
  156. osdev = NULL;
  157. while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
  158. hwloc_obj_t pcidev = osdev->parent;
  159. const char *info;
  160. if (strncmp(osdev->name, "rsmi", 4))
  161. continue;
  162. if (pcidev
  163. && pcidev->type == HWLOC_OBJ_PCI_DEVICE
  164. && pcidev->attr->pcidev.domain == domain
  165. && pcidev->attr->pcidev.bus == bus
  166. && pcidev->attr->pcidev.dev == device
  167. && pcidev->attr->pcidev.func == func)
  168. return osdev;
  169. info = hwloc_obj_get_info_by_name(osdev, "AMDUUID");
  170. if (info && !strcmp(info, uuid))
  171. return osdev;
  172. }
  173. return NULL;
  174. }
  175. /** @} */
  176. #ifdef __cplusplus
  177. } /* extern "C" */
  178. #endif
  179. #endif /* HWLOC_RSMI_H */