OpenMPI  0.1.1
cuda.h
Go to the documentation of this file.
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 
8 /** \file
9  * \brief Macros to help interaction between hwloc and the CUDA Driver API.
10  *
11  * Applications that use both hwloc and the CUDA Driver API may want to
12  * include this file so as to get topology information for CUDA devices.
13  *
14  */
15 
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 
31 /** \defgroup hwlocality_cuda CUDA Driver API Specific Functions
32  * @{
33  */
34 
35 /** \brief Get the CPU set of logical processors that are physically
36  * close to device \p cudevice.
37  *
38  * For the given CUDA Driver API device \p cudevice, read the corresponding
39  * kernel-provided cpumap file and return the corresponding CPU set.
40  * This function is currently only implemented in a meaningful way for
41  * Linux; other systems will simply get a full cpuset.
42  */
43 static __hwloc_inline int
44 hwloc_cuda_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
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 
90 /** @} */
91 
92 
93 #ifdef __cplusplus
94 } /* extern "C" */
95 #endif
96 
97 
98 #endif /* HWLOC_CUDA_H */
HWLOC_DECLSPEC void hwloc_bitmap_copy(hwloc_bitmap_t dst, hwloc_const_bitmap_t src)
Copy the contents of bitmap src into the already allocated bitmap dst.
static __hwloc_inline hwloc_const_cpuset_t __hwloc_attribute_pure hwloc_topology_get_complete_cpuset(hwloc_topology_t topology)
Get complete CPU set.
Definition: helper.h:729
Macros to help interaction between hwloc and Linux.
HWLOC_DECLSPEC int hwloc_linux_parse_cpumap_file(FILE *file, hwloc_cpuset_t set)
Convert a linux kernel cpumap file file into hwloc CPU set.
Definition: topology-linux.c:1332
Macros to help interaction between hwloc and the CUDA Driver API.
static __hwloc_inline int hwloc_cuda_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused, CUdevice cudevice, hwloc_cpuset_t set)
Get the CPU set of logical processors that are physically close to device cudevice.
Definition: cuda.h:44
Definition: private.h:56
Definition: cpuset.c:38