Hardware Locality (hwloc)  v2.1-20200123.0330.git0a8b367
intel-mic.h
00001 /*
00002  * Copyright © 2013-2016 Inria.  All rights reserved.
00003  * See COPYING in top-level directory.
00004  */
00005 
00013 #ifndef HWLOC_INTEL_MIC_H
00014 #define HWLOC_INTEL_MIC_H
00015 
00016 #include "hwloc.h"
00017 #include "hwloc/autogen/config.h"
00018 #include "hwloc/helper.h"
00019 
00020 #ifdef HWLOC_LINUX_SYS
00021 #include "hwloc/linux.h"
00022 
00023 #include <dirent.h>
00024 #include <string.h>
00025 #endif
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 
00030 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 
00059 static __hwloc_inline int
00060 hwloc_intel_mic_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
00061                                   int idx __hwloc_attribute_unused,
00062                                   hwloc_cpuset_t set)
00063 {
00064 #ifdef HWLOC_LINUX_SYS
00065         /* If we're on Linux, use the sysfs mechanism to get the local cpus */
00066 #define HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX 128
00067         char path[HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX];
00068         DIR *sysdir = NULL;
00069         struct dirent *dirent;
00070         unsigned pcibus, pcidev, pcifunc;
00071 
00072         if (!hwloc_topology_is_thissystem(topology)) {
00073                 errno = EINVAL;
00074                 return -1;
00075         }
00076 
00077         sprintf(path, "/sys/class/mic/mic%d", idx);
00078         sysdir = opendir(path);
00079         if (!sysdir)
00080                 return -1;
00081 
00082         while ((dirent = readdir(sysdir)) != NULL) {
00083                 if (sscanf(dirent->d_name, "pci_%02x:%02x.%02x", &pcibus, &pcidev, &pcifunc) == 3) {
00084                         sprintf(path, "/sys/class/mic/mic%d/pci_%02x:%02x.%02x/local_cpus", idx, pcibus, pcidev, pcifunc);
00085                         if (hwloc_linux_read_path_as_cpumask(path, set) < 0
00086                             || hwloc_bitmap_iszero(set))
00087                                 hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00088                         break;
00089                 }
00090         }
00091 
00092         closedir(sysdir);
00093 #else
00094         /* Non-Linux systems simply get a full cpuset */
00095         hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
00096 #endif
00097         return 0;
00098 }
00099 
00113 static __hwloc_inline hwloc_obj_t
00114 hwloc_intel_mic_get_device_osdev_by_index(hwloc_topology_t topology,
00115                                           unsigned idx)
00116 {
00117         hwloc_obj_t osdev = NULL;
00118         while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
00119                 if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
00120                     && osdev->name
00121                     && !strncmp("mic", osdev->name, 3)
00122                     && atoi(osdev->name + 3) == (int) idx)
00123                         return osdev;
00124         }
00125         return NULL;
00126 }
00127 
00131 #ifdef __cplusplus
00132 } /* extern "C" */
00133 #endif
00134 
00135 
00136 #endif /* HWLOC_INTEL_MIC_H */