This source file includes following definitions.
- hwloc_cudart_get_device_pci_ids
- hwloc_cudart_get_device_cpuset
- hwloc_cudart_get_device_pcidev
- hwloc_cudart_get_device_osdev_by_index
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #ifndef HWLOC_CUDART_H
17 #define HWLOC_CUDART_H
18
19 #include <hwloc.h>
20 #include <hwloc/autogen/config.h>
21 #include <hwloc/helper.h>
22 #ifdef HWLOC_LINUX_SYS
23 #include <hwloc/linux.h>
24 #endif
25
26 #include <cuda.h>
27 #include <cuda_runtime_api.h>
28
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 static __hwloc_inline int
48 hwloc_cudart_get_device_pci_ids(hwloc_topology_t topology __hwloc_attribute_unused,
49 int idx, int *domain, int *bus, int *dev)
50 {
51 cudaError_t cerr;
52 struct cudaDeviceProp prop;
53
54 cerr = cudaGetDeviceProperties(&prop, idx);
55 if (cerr) {
56 errno = ENOSYS;
57 return -1;
58 }
59
60 #if CUDA_VERSION >= 4000
61 *domain = prop.pciDomainID;
62 #else
63 *domain = 0;
64 #endif
65
66 *bus = prop.pciBusID;
67 *dev = prop.pciDeviceID;
68
69 return 0;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 static __hwloc_inline int
89 hwloc_cudart_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
90 int idx, hwloc_cpuset_t set)
91 {
92 #ifdef HWLOC_LINUX_SYS
93
94 #define HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX 128
95 char path[HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX];
96 int domain, bus, dev;
97
98 if (hwloc_cudart_get_device_pci_ids(topology, idx, &domain, &bus, &dev))
99 return -1;
100
101 if (!hwloc_topology_is_thissystem(topology)) {
102 errno = EINVAL;
103 return -1;
104 }
105
106 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", (unsigned) domain, (unsigned) bus, (unsigned) dev);
107 if (hwloc_linux_read_path_as_cpumask(path, set) < 0
108 || hwloc_bitmap_iszero(set))
109 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
110 #else
111
112 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
113 #endif
114 return 0;
115 }
116
117
118
119
120
121
122
123
124
125
126
127 static __hwloc_inline hwloc_obj_t
128 hwloc_cudart_get_device_pcidev(hwloc_topology_t topology, int idx)
129 {
130 int domain, bus, dev;
131
132 if (hwloc_cudart_get_device_pci_ids(topology, idx, &domain, &bus, &dev))
133 return NULL;
134
135 return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0);
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 static __hwloc_inline hwloc_obj_t
156 hwloc_cudart_get_device_osdev_by_index(hwloc_topology_t topology, unsigned idx)
157 {
158 hwloc_obj_t osdev = NULL;
159 while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
160 if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
161 && osdev->name
162 && !strncmp("cuda", osdev->name, 4)
163 && atoi(osdev->name + 4) == (int) idx)
164 return osdev;
165 }
166 return NULL;
167 }
168
169
170
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176
177 #endif