Hardware Locality (hwloc) 1.6.2

- Main Page
- Related Pages
- Modules
- Data Structures

cuda.h
1 /*
2 * Copyright © 2010-2012 inria. All rights reserved.
3 * Copyright © 2010-2011 Université Bordeaux 1
4 * Copyright © 2011 Cisco Systems, Inc. All rights reserved.
5 * See COPYING in top-level directory.
6 */
7
16 #ifndef HWLOC_CUDA_H
17 #define HWLOC_CUDA_H
18
19 #include <hwloc.h>
20 #include <hwloc/autogen/config.h>
21 #include <hwloc/linux.h>
22 #include <hwloc/helper.h>
23
24 #include <cuda.h>
25
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31
38 static inline int
39 hwloc_cuda_get_device_pci_ids(hwloc_topology_t topology ,
40 CUdevice cudevice, int *domain, int *bus, int *dev)
41 {
42 CUresult cres;
43
44 #if CUDA_VERSION >= 4000
45 cres = cuDeviceGetAttribute(domain, CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID, cudevice);
46 if (cres != CUDA_SUCCESS) {
47 errno = ENOSYS;
48 return -1;
49 }
50 #else
51 *domain = 0;
52 #endif
53 cres = cuDeviceGetAttribute(bus, CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, cudevice);
54 if (cres != CUDA_SUCCESS) {
55 errno = ENOSYS;
56 return -1;
57 }
58 cres = cuDeviceGetAttribute(dev, CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, cudevice);
59 if (cres != CUDA_SUCCESS) {
60 errno = ENOSYS;
61 return -1;
62 }
63
64 return 0;
65 }
66
77 static inline int
78 hwloc_cuda_get_device_cpuset(hwloc_topology_t topology ,
79 CUdevice cudevice, hwloc_cpuset_t set)
80 {
81 #ifdef HWLOC_LINUX_SYS
82 /* If we're on Linux, use the sysfs mechanism to get the local cpus */
83 #define HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX 128
84 char path[HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX];
85 FILE *sysfile = NULL;
86 int domainid, busid, deviceid;
87
88 if (hwloc_cuda_get_device_pci_ids(topology, cudevice, &domainid, &busid, &deviceid))
89 return -1;
90
91 if (!hwloc_topology_is_thissystem(topology)) {
92 errno = EINVAL;
93 return -1;
94 }
95
96 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domainid, busid, deviceid);
97 sysfile = fopen(path, "r");
98 if (!sysfile)
99 return -1;
100
101 hwloc_linux_parse_cpumap_file(sysfile, set);
102 if (hwloc_bitmap_iszero(set))
103 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
104
105 fclose(sysfile);
106 #else
107 /* Non-Linux systems simply get a full cpuset */
108 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
109 #endif
110 return 0;
111 }
112
121 static inline hwloc_obj_t
122 hwloc_cuda_get_device_pcidev(hwloc_topology_t topology, CUdevice cudevice)
123 {
124 int domain, bus, dev;
125
126 if (hwloc_cuda_get_device_pci_ids(topology, cudevice, &domain, &bus, &dev))
127 return NULL;
128
129 return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0);
130 }
131
135 #ifdef __cplusplus
136 } /* extern "C" */
137 #endif
138
139
140 #endif /* HWLOC_CUDA_H */