Hardware Locality (hwloc)
1.4.1
|
00001 /* 00002 * Copyright © 2010 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 00071 static inline int 00072 hwloc_cudart_get_device_cpuset(hwloc_topology_t topology , 00073 int device, hwloc_cpuset_t set) 00074 { 00075 #ifdef HWLOC_LINUX_SYS 00076 /* If we're on Linux, use the sysfs mechanism to get the local cpus */ 00077 #define HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX 128 00078 char path[HWLOC_CUDART_DEVICE_SYSFS_PATH_MAX]; 00079 FILE *sysfile = NULL; 00080 int domain, bus, dev; 00081 if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev)) 00082 return -1; 00083 00084 sprintf(path, "/sys/bus/pci/devices/%04x:%02x:%02x.0/local_cpus", domain, bus, dev); 00085 sysfile = fopen(path, "r"); 00086 if (!sysfile) 00087 return -1; 00088 00089 hwloc_linux_parse_cpumap_file(sysfile, set); 00090 if (hwloc_bitmap_iszero(set)) 00091 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology)); 00092 00093 fclose(sysfile); 00094 #else 00095 /* Non-Linux systems simply get a full cpuset */ 00096 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology)); 00097 #endif 00098 return 0; 00099 } 00100 00107 static inline hwloc_obj_t 00108 hwloc_cudart_get_device_pcidev(hwloc_topology_t topology, int device) 00109 { 00110 int domain, bus, dev; 00111 00112 if (hwloc_cudart_get_device_pci_ids(topology, device, &domain, &bus, &dev)) 00113 return NULL; 00114 00115 return hwloc_get_pcidev_by_busid(topology, domain, bus, dev, 0); 00116 } 00117 00121 #ifdef __cplusplus 00122 } /* extern "C" */ 00123 #endif 00124 00125 00126 #endif /* HWLOC_CUDART_H */