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