Blame src/hwloc/include/hwloc/intel-mic.h

Packit Service c5cf8c
/*
Packit Service c5cf8c
 * Copyright © 2013-2016 Inria.  All rights reserved.
Packit Service c5cf8c
 * See COPYING in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
/** \file
Packit Service c5cf8c
 * \brief Macros to help interaction between hwloc and Intel Xeon Phi (MIC).
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Applications that use both hwloc and Intel Xeon Phi (MIC) may want to
Packit Service c5cf8c
 * include this file so as to get topology information for MIC devices.
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
#ifndef HWLOC_INTEL_MIC_H
Packit Service c5cf8c
#define HWLOC_INTEL_MIC_H
Packit Service c5cf8c
Packit Service c5cf8c
#include <hwloc.h>
Packit Service c5cf8c
#include <hwloc/autogen/config.h>
Packit Service c5cf8c
#include <hwloc/helper.h>
Packit Service c5cf8c
#ifdef HWLOC_LINUX_SYS
Packit Service c5cf8c
#include <hwloc/linux.h>
Packit Service c5cf8c
#include <dirent.h>
Packit Service c5cf8c
#include <string.h>
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
#include <stdio.h>
Packit Service c5cf8c
#include <stdlib.h>
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#ifdef __cplusplus
Packit Service c5cf8c
extern "C" {
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
/** \defgroup hwlocality_intel_mic Interoperability with Intel Xeon Phi (MIC)
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * This interface offers ways to retrieve topology information about
Packit Service c5cf8c
 * Intel Xeon Phi (MIC) devices.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * @{
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
/** \brief Get the CPU set of logical processors that are physically
Packit Service c5cf8c
 * close to MIC device whose index is \p idx.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Return the CPU set describing the locality of the MIC device whose index is \p idx.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Topology \p topology and device index \p idx must match the local machine.
Packit Service c5cf8c
 * I/O devices detection is not needed in the topology.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * The function only returns the locality of the device.
Packit Service c5cf8c
 * If more information about the device is needed, OS objects should
Packit Service c5cf8c
 * be used instead, see hwloc_intel_mic_get_device_osdev_by_index().
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * This function is currently only implemented in a meaningful way for
Packit Service c5cf8c
 * Linux; other systems will simply get a full cpuset.
Packit Service c5cf8c
 */
Packit Service c5cf8c
static __hwloc_inline int
Packit Service c5cf8c
hwloc_intel_mic_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
Packit Service c5cf8c
				  int idx __hwloc_attribute_unused,
Packit Service c5cf8c
				  hwloc_cpuset_t set)
Packit Service c5cf8c
{
Packit Service c5cf8c
#ifdef HWLOC_LINUX_SYS
Packit Service c5cf8c
	/* If we're on Linux, use the sysfs mechanism to get the local cpus */
Packit Service c5cf8c
#define HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX 128
Packit Service c5cf8c
	char path[HWLOC_INTEL_MIC_DEVICE_SYSFS_PATH_MAX];
Packit Service c5cf8c
	DIR *sysdir = NULL;
Packit Service c5cf8c
	struct dirent *dirent;
Packit Service c5cf8c
	unsigned pcibus, pcidev, pcifunc;
Packit Service c5cf8c
Packit Service c5cf8c
	if (!hwloc_topology_is_thissystem(topology)) {
Packit Service c5cf8c
		errno = EINVAL;
Packit Service c5cf8c
		return -1;
Packit Service c5cf8c
	}
Packit Service c5cf8c
Packit Service c5cf8c
	sprintf(path, "/sys/class/mic/mic%d", idx);
Packit Service c5cf8c
	sysdir = opendir(path);
Packit Service c5cf8c
	if (!sysdir)
Packit Service c5cf8c
		return -1;
Packit Service c5cf8c
Packit Service c5cf8c
	while ((dirent = readdir(sysdir)) != NULL) {
Packit Service c5cf8c
		if (sscanf(dirent->d_name, "pci_%02x:%02x.%02x", &pcibus, &pcidev, &pcifunc) == 3) {
Packit Service c5cf8c
			sprintf(path, "/sys/class/mic/mic%d/pci_%02x:%02x.%02x/local_cpus", idx, pcibus, pcidev, pcifunc);
Packit Service c5cf8c
			if (hwloc_linux_read_path_as_cpumask(path, set) < 0
Packit Service c5cf8c
			    || hwloc_bitmap_iszero(set))
Packit Service c5cf8c
				hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
Packit Service c5cf8c
			break;
Packit Service c5cf8c
		}
Packit Service c5cf8c
	}
Packit Service c5cf8c
Packit Service c5cf8c
	closedir(sysdir);
Packit Service c5cf8c
#else
Packit Service c5cf8c
	/* Non-Linux systems simply get a full cpuset */
Packit Service c5cf8c
	hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
Packit Service c5cf8c
#endif
Packit Service c5cf8c
	return 0;
Packit Service c5cf8c
}
Packit Service c5cf8c
Packit Service c5cf8c
/** \brief Get the hwloc OS device object corresponding to the
Packit Service c5cf8c
 * MIC device for the given index.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * Return the OS device object describing the MIC device whose index is \p idx.
Packit Service c5cf8c
 * Return NULL if there is none.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * The topology \p topology does not necessarily have to match the current
Packit Service c5cf8c
 * machine. For instance the topology may be an XML import of a remote host.
Packit Service c5cf8c
 * I/O devices detection must be enabled in the topology.
Packit Service c5cf8c
 *
Packit Service c5cf8c
 * \note The corresponding PCI device object can be obtained by looking
Packit Service c5cf8c
 * at the OS device parent object.
Packit Service c5cf8c
 */
Packit Service c5cf8c
static __hwloc_inline hwloc_obj_t
Packit Service c5cf8c
hwloc_intel_mic_get_device_osdev_by_index(hwloc_topology_t topology,
Packit Service c5cf8c
					  unsigned idx)
Packit Service c5cf8c
{
Packit Service c5cf8c
	hwloc_obj_t osdev = NULL;
Packit Service c5cf8c
	while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
Packit Service c5cf8c
		if (HWLOC_OBJ_OSDEV_COPROC == osdev->attr->osdev.type
Packit Service c5cf8c
                    && osdev->name
Packit Service c5cf8c
		    && !strncmp("mic", osdev->name, 3)
Packit Service c5cf8c
		    && atoi(osdev->name + 3) == (int) idx)
Packit Service c5cf8c
                        return osdev;
Packit Service c5cf8c
        }
Packit Service c5cf8c
        return NULL;
Packit Service c5cf8c
}
Packit Service c5cf8c
Packit Service c5cf8c
/** @} */
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#ifdef __cplusplus
Packit Service c5cf8c
} /* extern "C" */
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
#endif /* HWLOC_INTEL_MIC_H */