00001
00002
00003
00004
00005
00013 #ifndef HWLOC_NVML_H
00014 #define HWLOC_NVML_H
00015
00016 #include <hwloc.h>
00017 #include <hwloc/autogen/config.h>
00018 #include <hwloc/helper.h>
00019 #ifdef HWLOC_LINUX_SYS
00020 #include <hwloc/linux.h>
00021 #endif
00022
00023 #include <nvml.h>
00024
00025
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif
00029
00030
00055 static __hwloc_inline int
00056 hwloc_nvml_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
00057 nvmlDevice_t device, hwloc_cpuset_t set)
00058 {
00059 #ifdef HWLOC_LINUX_SYS
00060
00061 #define HWLOC_NVML_DEVICE_SYSFS_PATH_MAX 128
00062 char path[HWLOC_NVML_DEVICE_SYSFS_PATH_MAX];
00063 FILE *sysfile = NULL;
00064 nvmlReturn_t nvres;
00065 nvmlPciInfo_t pci;
00066
00067 if (!hwloc_topology_is_thissystem(topology)) {
00068 errno = EINVAL;
00069 return -1;
00070 }
00071
00072 nvres = nvmlDeviceGetPciInfo(device, &pci);
00073 if (NVML_SUCCESS != nvres) {
00074 errno = EINVAL;
00075 return -1;
00076 }
00077
00078 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", pci.domain, pci.bus, pci.device);
00079 sysfile = fopen(path, "r");
00080 if (!sysfile)
00081 return -1;
00082
00083 if (hwloc_linux_parse_cpumap_file(sysfile, set) < 0
00084 || hwloc_bitmap_iszero(set))
00085 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00086
00087 fclose(sysfile);
00088 #else
00089
00090 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00091 #endif
00092 return 0;
00093 }
00094
00108 static __hwloc_inline hwloc_obj_t
00109 hwloc_nvml_get_device_osdev_by_index(hwloc_topology_t topology, unsigned idx)
00110 {
00111 hwloc_obj_t osdev = NULL;
00112 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00113 if (HWLOC_OBJ_OSDEV_GPU == osdev->attr->osdev.type
00114 && osdev->name
00115 && !strncmp("nvml", osdev->name, 4)
00116 && atoi(osdev->name + 4) == (int) idx)
00117 return osdev;
00118 }
00119 return NULL;
00120 }
00121
00135 static __hwloc_inline hwloc_obj_t
00136 hwloc_nvml_get_device_osdev(hwloc_topology_t topology, nvmlDevice_t device)
00137 {
00138 hwloc_obj_t osdev;
00139 nvmlReturn_t nvres;
00140 nvmlPciInfo_t pci;
00141
00142 if (!hwloc_topology_is_thissystem(topology)) {
00143 errno = EINVAL;
00144 return NULL;
00145 }
00146
00147 nvres = nvmlDeviceGetPciInfo(device, &pci);
00148 if (NVML_SUCCESS != nvres)
00149 return NULL;
00150
00151 osdev = NULL;
00152 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00153 hwloc_obj_t pcidev = osdev->parent;
00154 if (strncmp(osdev->name, "nvml", 4))
00155 continue;
00156 if (pcidev
00157 && pcidev->type == HWLOC_OBJ_PCI_DEVICE
00158 && pcidev->attr->pcidev.domain == pci.domain
00159 && pcidev->attr->pcidev.bus == pci.bus
00160 && pcidev->attr->pcidev.dev == pci.device
00161 && pcidev->attr->pcidev.func == 0)
00162 return osdev;
00163 }
00164
00165 return NULL;
00166 }
00167
00171 #ifdef __cplusplus
00172 }
00173 #endif
00174
00175
00176 #endif