Open MPI logo

Portable Hardware Locality (hwloc) Documentation: v1.3.3

  |   Home   |   Support   |   FAQ   |  
cuda.h
1 /*
2  * Copyright © 2010 inria. All rights reserved.
3  * Copyright © 2010 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 
23 #include <cuda.h>
24 
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 
43 static inline int
45  CUdevice cudevice, hwloc_cpuset_t set)
46 {
47 #ifdef HWLOC_LINUX_SYS
48  /* If we're on Linux, use the sysfs mechanism to get the local cpus */
49 #define HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX 128
50  CUresult cres;
51  int domainid = 0;
52  int deviceid;
53  int busid;
54  char path[HWLOC_CUDA_DEVICE_SYSFS_PATH_MAX];
55  FILE *sysfile = NULL;
56 
57 #if CUDA_VERSION >= 4000
58  cres = cuDeviceGetAttribute(&domainid, CU_DEVICE_ATTRIBUTE_PCI_DOMAIN_ID, cudevice);
59  if (cres != CUDA_SUCCESS) {
60  errno = ENOSYS;
61  return -1;
62  }
63 #endif
64  cres = cuDeviceGetAttribute(&busid, CU_DEVICE_ATTRIBUTE_PCI_BUS_ID, cudevice);
65  if (cres != CUDA_SUCCESS) {
66  errno = ENOSYS;
67  return -1;
68  }
69  cres = cuDeviceGetAttribute(&deviceid, CU_DEVICE_ATTRIBUTE_PCI_DEVICE_ID, cudevice);
70  if (cres != CUDA_SUCCESS) {
71  errno = ENOSYS;
72  return -1;
73  }
74 
75  sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domainid, busid, deviceid);
76  sysfile = fopen(path, "r");
77  if (!sysfile)
78  return -1;
79 
80  hwloc_linux_parse_cpumap_file(sysfile, set);
81 
82  fclose(sysfile);
83 #else
84  /* Non-Linux systems simply get a full cpuset */
86 #endif
87  return 0;
88 }
89 
93 #ifdef __cplusplus
94 } /* extern "C" */
95 #endif
96 
97 
98 #endif /* HWLOC_CUDA_H */