Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.6

  |   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  if (!hwloc_topology_is_thissystem(topology)) {
88  errno = EINVAL;
89  return -1;
90  }
91 
92  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, dev);
93  sysfile = fopen(path, "r");
94  if (!sysfile)
95  return -1;
96 
97  hwloc_linux_parse_cpumap_file(sysfile, set);
98  if (hwloc_bitmap_iszero(set))
100 
101  fclose(sysfile);
102 #else
103  /* Non-Linux systems simply get a full cpuset */
105 #endif
106  return 0;
107 }
108 
117 static inline hwloc_obj_t
119 {
120  int domain, bus, dev;
121 
122  if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev))
123  return NULL;
124 
125  return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0);
126 }
127 
131 #ifdef __cplusplus
132 } /* extern "C" */
133 #endif
134 
135 
136 #endif /* HWLOC_CUDART_H */