-
Notifications
You must be signed in to change notification settings - Fork 168
/
nvml.c
105 lines (84 loc) · 2.78 KB
/
nvml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright © 2012-2023 Inria. All rights reserved.
* See COPYING in top-level directory.
*/
#include <stdio.h>
#include <assert.h>
#include <nvml.h>
#include "hwloc.h"
#include "hwloc/nvml.h"
/* check the NVML helpers */
static int check_nvml_backend(hwloc_topology_t topology)
{
struct hwloc_infos_s *infos = hwloc_topology_get_infos(topology);
unsigned i;
for(i=0; i<infos->count; i++)
if (!strcmp(infos->array[i].name, "Backend")
&& !strcmp(infos->array[i].value, "NVML"))
return 1;
return 0;
}
int main(void)
{
hwloc_topology_t topology;
nvmlReturn_t nvres;
nvmlDevice_t device;
unsigned count, i;
int has_nvml_backend;
int err;
nvres = nvmlInit();
if (nvres != NVML_SUCCESS) {
printf("nvmlInit failed %d\n", nvres);
return 0;
}
nvres = nvmlDeviceGetCount(&count);
if (nvres != NVML_SUCCESS) {
printf("nvmlDeviceGetCount failed %d\n", nvres);
return 0;
}
printf("nvmlDeviceGetCount found %u devices\n", count);
hwloc_topology_init(&topology);
hwloc_topology_set_io_types_filter(topology, HWLOC_TYPE_FILTER_KEEP_IMPORTANT);
hwloc_topology_load(topology);
has_nvml_backend = check_nvml_backend(topology);
for(i=0; i<count; i++) {
hwloc_bitmap_t set;
hwloc_obj_t osdev, osdev2, ancestor;
const char *value;
nvres = nvmlDeviceGetHandleByIndex(i, &device);
if (nvres != NVML_SUCCESS) {
printf("nvmlDeviceGetHandleByIndex failed %d\n", nvres);
continue;
}
osdev = hwloc_nvml_get_device_osdev(topology, device);
assert(osdev);
osdev2 = hwloc_nvml_get_device_osdev_by_index(topology, i);
assert(osdev2 == osdev);
ancestor = hwloc_get_non_io_ancestor_obj(topology, osdev);
printf("found OSDev %s\n", osdev->name);
err = strncmp(osdev->name, "nvml", 4);
assert(!err);
assert(atoi(osdev->name+4) == (int) i);
assert(has_nvml_backend);
assert(osdev->attr->osdev.type == (HWLOC_OBJ_OSDEV_COPROC|HWLOC_OBJ_OSDEV_GPU));
value = hwloc_obj_get_info_by_name(osdev, "GPUModel");
printf("found OSDev model %s\n", value);
set = hwloc_bitmap_alloc();
err = hwloc_nvml_get_device_cpuset(topology, device, set);
if (err < 0) {
printf("failed to get cpuset for device %u\n", i);
} else {
char *cpuset_string = NULL;
hwloc_bitmap_asprintf(&cpuset_string, set);
printf("got cpuset %s for device %u\n", cpuset_string, i);
free(cpuset_string);
if (hwloc_bitmap_isequal(hwloc_topology_get_complete_cpuset(topology), hwloc_topology_get_topology_cpuset(topology)))
/* only compare if the topology is complete, otherwise things can be significantly different */
assert(hwloc_bitmap_isequal(set, ancestor->cpuset));
}
hwloc_bitmap_free(set);
}
hwloc_topology_destroy(topology);
nvmlShutdown();
return 0;
}