File indexing completed on 2026-06-07 08:28:42
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef HWLOC_NVML_H
0015 #define HWLOC_NVML_H
0016
0017 #include "hwloc.h"
0018 #include "hwloc/autogen/config.h"
0019 #include "hwloc/helper.h"
0020 #ifdef HWLOC_LINUX_SYS
0021 #include "hwloc/linux.h"
0022 #endif
0023
0024 #include <nvml.h>
0025
0026
0027 #ifdef __cplusplus
0028 extern "C" {
0029 #endif
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 static __hwloc_inline int
0060 hwloc_nvml_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
0061 nvmlDevice_t device, hwloc_cpuset_t set)
0062 {
0063 #ifdef HWLOC_LINUX_SYS
0064
0065 #define HWLOC_NVML_DEVICE_SYSFS_PATH_MAX 128
0066 char path[HWLOC_NVML_DEVICE_SYSFS_PATH_MAX];
0067 nvmlReturn_t nvres;
0068 nvmlPciInfo_t pci;
0069
0070 if (!hwloc_topology_is_thissystem(topology)) {
0071 errno = EINVAL;
0072 return -1;
0073 }
0074
0075 nvres = nvmlDeviceGetPciInfo(device, &pci);
0076 if (NVML_SUCCESS != nvres) {
0077 errno = EINVAL;
0078 return -1;
0079 }
0080
0081 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", pci.domain, pci.bus, pci.device);
0082 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
0083 || hwloc_bitmap_iszero(set))
0084 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0085 #else
0086
0087 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0088 #endif
0089 return 0;
0090 }
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 static __hwloc_inline hwloc_obj_t
0106 hwloc_nvml_get_device_osdev_by_index(hwloc_topology_t topology, unsigned idx)
0107 {
0108 hwloc_obj_t osdev = NULL;
0109 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0110 if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type
0111 && osdev->name
0112 && !strncmp("nvml", osdev->name, 4)
0113 && atoi(osdev->name + 4) == (int) idx)
0114 return osdev;
0115 }
0116 return NULL;
0117 }
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 static __hwloc_inline hwloc_obj_t
0133 hwloc_nvml_get_device_osdev(hwloc_topology_t topology, nvmlDevice_t device)
0134 {
0135 hwloc_obj_t osdev;
0136 nvmlReturn_t nvres;
0137 nvmlPciInfo_t pci;
0138 char uuid[64];
0139
0140 if (!hwloc_topology_is_thissystem(topology)) {
0141 errno = EINVAL;
0142 return NULL;
0143 }
0144
0145 nvres = nvmlDeviceGetPciInfo(device, &pci);
0146 if (NVML_SUCCESS != nvres)
0147 return NULL;
0148
0149 nvres = nvmlDeviceGetUUID(device, uuid, sizeof(uuid));
0150 if (NVML_SUCCESS != nvres)
0151 uuid[0] = '\0';
0152
0153 osdev = NULL;
0154 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0155 hwloc_obj_t pcidev = osdev->parent;
0156 const char *info;
0157
0158 if (strncmp(osdev->name, "nvml", 4))
0159 continue;
0160
0161 if (pcidev
0162 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
0163 && pcidev->attr->pcidev.domain == pci.domain
0164 && pcidev->attr->pcidev.bus == pci.bus
0165 && pcidev->attr->pcidev.dev == pci.device
0166 && pcidev->attr->pcidev.func == 0)
0167 return osdev;
0168
0169 info = hwloc_obj_get_info_by_name(osdev, "NVIDIAUUID");
0170 if (info && !strcmp(info, uuid))
0171 return osdev;
0172 }
0173
0174 return NULL;
0175 }
0176
0177
0178
0179
0180 #ifdef __cplusplus
0181 }
0182 #endif
0183
0184
0185 #endif