Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:01:16

0001 /*
0002  * Copyright © 2009 CNRS
0003  * Copyright © 2009-2023 Inria.  All rights reserved.
0004  * Copyright © 2009-2010 Université Bordeaux
0005  * Copyright © 2009-2011 Cisco Systems, Inc.  All rights reserved.
0006  * See COPYING in top-level directory.
0007  */
0008 
0009 /** \file
0010  * \brief Macros to help interaction between hwloc and OpenFabrics
0011  * verbs.
0012  *
0013  * Applications that use both hwloc and OpenFabrics verbs may want to
0014  * include this file so as to get topology information for OpenFabrics
0015  * hardware (InfiniBand, etc).
0016  *
0017  */
0018 
0019 #ifndef HWLOC_OPENFABRICS_VERBS_H
0020 #define HWLOC_OPENFABRICS_VERBS_H
0021 
0022 #include "hwloc.h"
0023 #include "hwloc/autogen/config.h"
0024 #ifdef HWLOC_LINUX_SYS
0025 #include "hwloc/linux.h"
0026 #endif
0027 
0028 #include <infiniband/verbs.h>
0029 
0030 
0031 #ifdef __cplusplus
0032 extern "C" {
0033 #endif
0034 
0035 
0036 /** \defgroup hwlocality_openfabrics Interoperability with OpenFabrics
0037  *
0038  * This interface offers ways to retrieve topology information about
0039  * OpenFabrics devices (InfiniBand, Omni-Path, usNIC, etc).
0040  *
0041  * @{
0042  */
0043 
0044 /** \brief Get the CPU set of processors that are physically
0045  * close to device \p ibdev.
0046  *
0047  * Store in \p set the CPU-set describing the locality of the OpenFabrics
0048  * device \p ibdev (InfiniBand, etc).
0049  *
0050  * Topology \p topology and device \p ibdev must match the local machine.
0051  * I/O devices detection is not needed in the topology.
0052  *
0053  * The function only returns the locality of the device.
0054  * If more information about the device is needed, OS objects should
0055  * be used instead, see hwloc_ibv_get_device_osdev()
0056  * and hwloc_ibv_get_device_osdev_by_name().
0057  *
0058  * This function is currently only implemented in a meaningful way for
0059  * Linux; other systems will simply get a full cpuset.
0060  *
0061  * \return 0 on success.
0062  * \return -1 on error, for instance if device information could not be found.
0063  */
0064 static __hwloc_inline int
0065 hwloc_ibv_get_device_cpuset(hwloc_topology_t topology __hwloc_attribute_unused,
0066                 struct ibv_device *ibdev, hwloc_cpuset_t set)
0067 {
0068 #ifdef HWLOC_LINUX_SYS
0069   /* If we're on Linux, use the verbs-provided sysfs mechanism to
0070      get the local cpus */
0071 #define HWLOC_OPENFABRICS_VERBS_SYSFS_PATH_MAX 128
0072   char path[HWLOC_OPENFABRICS_VERBS_SYSFS_PATH_MAX];
0073 
0074   if (!hwloc_topology_is_thissystem(topology)) {
0075     errno = EINVAL;
0076     return -1;
0077   }
0078 
0079   sprintf(path, "/sys/class/infiniband/%s/device/local_cpus",
0080       ibv_get_device_name(ibdev));
0081   if (hwloc_linux_read_path_as_cpumask(path, set) < 0
0082       || hwloc_bitmap_iszero(set))
0083     hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0084 #else
0085   /* Non-Linux systems simply get a full cpuset */
0086   hwloc_bitmap_copy(set, hwloc_topology_get_complete_cpuset(topology));
0087 #endif
0088   return 0;
0089 }
0090 
0091 /** \brief Get the hwloc OS device object corresponding to the OpenFabrics
0092  * device named \p ibname.
0093  *
0094  * \return The hwloc OS device object describing the OpenFabrics device
0095  * (InfiniBand, Omni-Path, usNIC, etc) whose name is \p ibname
0096  * (mlx5_0, hfi1_0, usnic_0, qib0, etc).
0097  * \return \c NULL if none could be found.
0098  *
0099  * The name \p ibname is usually obtained from ibv_get_device_name().
0100  *
0101  * The topology \p topology does not necessarily have to match the current
0102  * machine. For instance the topology may be an XML import of a remote host.
0103  * I/O devices detection must be enabled in the topology.
0104  *
0105  * \note The corresponding PCI device object can be obtained by looking
0106  * at the OS device parent object.
0107  */
0108 static __hwloc_inline hwloc_obj_t
0109 hwloc_ibv_get_device_osdev_by_name(hwloc_topology_t topology,
0110                    const char *ibname)
0111 {
0112     hwloc_obj_t osdev = NULL;
0113     while ((osdev = hwloc_get_next_osdev(topology, osdev)) != NULL) {
0114         if (HWLOC_OBJ_OSDEV_OPENFABRICS == osdev->attr->osdev.type
0115             && osdev->name && !strcmp(ibname, osdev->name))
0116             return osdev;
0117     }
0118     return NULL;
0119 }
0120 
0121 /** \brief Get the hwloc OS device object corresponding to the OpenFabrics
0122  * device \p ibdev.
0123  *
0124  * \return The hwloc OS device object describing the OpenFabrics
0125  * device \p ibdev (InfiniBand, etc).
0126  * \return \c NULL if none could be found.
0127  *
0128  * Topology \p topology and device \p ibdev must match the local machine.
0129  * I/O devices detection must be enabled in the topology.
0130  * If not, the locality of the object may still be found using
0131  * hwloc_ibv_get_device_cpuset().
0132  *
0133  * \note The corresponding PCI device object can be obtained by looking
0134  * at the OS device parent object.
0135  */
0136 static __hwloc_inline hwloc_obj_t
0137 hwloc_ibv_get_device_osdev(hwloc_topology_t topology,
0138                struct ibv_device *ibdev)
0139 {
0140     if (!hwloc_topology_is_thissystem(topology)) {
0141         errno = EINVAL;
0142         return NULL;
0143     }
0144     return hwloc_ibv_get_device_osdev_by_name(topology, ibv_get_device_name(ibdev));
0145 }
0146 
0147 /** @} */
0148 
0149 
0150 #ifdef __cplusplus
0151 } /* extern "C" */
0152 #endif
0153 
0154 
0155 #endif /* HWLOC_OPENFABRICS_VERBS_H */