Hardware Locality (hwloc)
1.4.2
|
00001 /* 00002 * Copyright © 2010-2012 inria. All rights reserved. 00003 * Copyright © 2010-2011 Université Bordeaux 1 00004 * Copyright © 2011 Cisco Systems, Inc. All rights reserved. 00005 * See COPYING in top-level directory. 00006 */ 00007 00016 #ifndef HWLOC_CUDART_H 00017 #define HWLOC_CUDART_H 00018 00019 #include <hwloc.h> 00020 #include <hwloc/autogen/config.h> 00021 #include <hwloc/linux.h> 00022 #include <hwloc/helper.h> 00023 00024 #include <cuda_runtime_api.h> 00025 00026 00027 #ifdef __cplusplus 00028 extern "C" { 00029 #endif 00030 00031 00038 static inline int 00039 hwloc_cudart_get_device_pci_ids(hwloc_topology_t topology , 00040 int device, int *domain, int *bus, int *dev) 00041 { 00042 cudaError_t cerr; 00043 struct cudaDeviceProp prop; 00044 00045 cerr = cudaGetDeviceProperties(&prop, device); 00046 if (cerr) { 00047 errno = ENOSYS; 00048 return -1; 00049 } 00050 00051 #if CUDART_VERSION >= 4000 00052 *domain = prop.pciDomainID; 00053 #else 00054 *domain = 0; 00055 #endif 00056 00057 *bus = prop.pciBusID; 00058 *dev = prop.pciDeviceID; 00059 00060 return 0; 00061 } 00062 00073 static inline int 00074 hwloc_cudart_get_device_cpuset(hwloc_topology_t topology , 00075 int device, hwloc_cpuset_t set) 00076 { 00077 #ifdef HWLOC_LINUX_SYS 00078 /* If we're on Linux, use the sysfs mechanism to get the local cpus */ 00079 #define HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX 128 00080 char path[HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX]; 00081 FILE *sysfile = NULL; 00082 int domain, bus, dev; 00083 00084 if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev)) 00085 return -1; 00086 00087 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, dev); 00088 sysfile = fopen(path, "r"); 00089 if (!sysfile) 00090 return -1; 00091 00092 hwloc_linux_parse_cpumap_file(sysfile, set); 00093 if (hwloc_bitmap_iszero(set)) 00094 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology)); 00095 00096 fclose(sysfile); 00097 #else 00098 /* Non-Linux systems simply get a full cpuset */ 00099 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology)); 00100 #endif 00101 return 0; 00102 } 00103 00112 static inline hwloc_obj_t 00113 hwloc_cudart_get_device_pcidev(hwloc_topology_t topology, int device) 00114 { 00115 int domain, bus, dev; 00116 00117 if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev)) 00118 return NULL; 00119 00120 return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0); 00121 } 00122 00126 #ifdef __cplusplus 00127 } /* extern "C" */ 00128 #endif 00129 00130 00131 #endif /* HWLOC_CUDART_H */