Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.4.3

  |   Home   |   Support   |   FAQ   |  
cudart.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_CUDART_H
17 #define HWLOC_CUDART_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_runtime_api.h>
25 
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 
38 static inline int
40  int device, int *domain, int *bus, int *dev)
41 {
42  cudaError_t cerr;
43  struct cudaDeviceProp prop;
44 
45  cerr = cudaGetDeviceProperties(&prop, device);
46  if (cerr) {
47  errno = ENOSYS;
48  return -1;
49  }
50 
51 #if CUDART_VERSION >= 4000
52  *domain = prop.pciDomainID;
53 #else
54  *domain = 0;
55 #endif
56 
57  *bus = prop.pciBusID;
58  *dev = prop.pciDeviceID;
59 
60  return 0;
61 }
62 
73 static inline int
75  int device, hwloc_cpuset_t set)
76 {
77 #ifdef HWLOC_LINUX_SYS
78  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
79 #define HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX 128
80  char path[HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX];
81  FILE *sysfile = NULL;
82  int domain, bus, dev;
83 
84  if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev))
85  return -1;
86 
87  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, dev);
88  sysfile = fopen(path, "r");
89  if (!sysfile)
90  return -1;
91 
92  hwloc_linux_parse_cpumap_file(sysfile, set);
93  if (hwloc_bitmap_iszero(set))
95 
96  fclose(sysfile);
97 #else
98  /* Non-Linux systems simply get a full cpuset */
100 #endif
101  return 0;
102 }
103 
112 static inline hwloc_obj_t
114 {
115  int domain, bus, dev;
116 
117  if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev))
118  return NULL;
119 
120  return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0);
121 }
122 
126 #ifdef __cplusplus
127 } /* extern "C" */
128 #endif
129 
130 
131 #endif /* HWLOC_CUDART_H */