Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.4.3

  |   Home   |   Support   |   FAQ   |  
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
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
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  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domainid, busid, deviceid);
92  sysfile = fopen(path, "r");
93  if (!sysfile)
94  return -1;
95 
96  hwloc_linux_parse_cpumap_file(sysfile, set);
97  if (hwloc_bitmap_iszero(set))
99 
100  fclose(sysfile);
101 #else
102  /* Non-Linux systems simply get a full cpuset */
104 #endif
105  return 0;
106 }
107 
116 static inline hwloc_obj_t
117 hwloc_cuda_get_device_pcidev(hwloc_topology_t topology, CUdevice cudevice)
118 {
119  int domain, bus, dev;
120 
121  if (hwloc_cuda_get_device_pci_ids(topology, cudevice, &domain, &bus, &dev))
122  return NULL;
123 
124  return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0);
125 }
126 
130 #ifdef __cplusplus
131 } /* extern "C" */
132 #endif
133 
134 
135 #endif /* HWLOC_CUDA_H */