|
| 1 | +////////////////////////////////////////////////////////////////////////////// |
| 2 | +// Copyright (c) 2016-26, Lawrence Livermore National Security, LLC and Umpire |
| 3 | +// project contributors. See the COPYRIGHT file for details. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: (MIT) |
| 6 | +////////////////////////////////////////////////////////////////////////////// |
| 7 | + |
| 8 | +#include "umpire/util/mpi_shared.hpp" |
| 9 | + |
| 10 | +#if defined(UMPIRE_ENABLE_MPI) |
| 11 | + |
| 12 | +#include <fstream> |
| 13 | +#include <string> |
| 14 | + |
| 15 | +#if defined(__linux__) |
| 16 | +#include <sched.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include "umpire/util/Macros.hpp" |
| 20 | +#if defined(UMPIRE_ENABLE_NUMA) |
| 21 | +#include "umpire/util/numa.hpp" |
| 22 | +#endif |
| 23 | +#include "umpire/util/error.hpp" |
| 24 | + |
| 25 | +namespace umpire { |
| 26 | +namespace util { |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +constexpr int IGNORE_KEY{0}; |
| 31 | + |
| 32 | +bool get_socket_color_from_package_id(int& color) |
| 33 | +{ |
| 34 | +#if defined(__linux__) |
| 35 | + const int cpu = sched_getcpu(); |
| 36 | + if (cpu < 0) { |
| 37 | + UMPIRE_LOG(Debug, "sched_getcpu failed while determining socket color"); |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + std::ifstream package_id_file{"/sys/devices/system/cpu/cpu" + std::to_string(cpu) + |
| 42 | + "/topology/physical_package_id"}; |
| 43 | + if (!package_id_file) { |
| 44 | + UMPIRE_LOG(Debug, "Could not open physical_package_id for cpu " << cpu); |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + package_id_file >> color; |
| 49 | + if (!package_id_file) { |
| 50 | + UMPIRE_LOG(Debug, "Could not read physical_package_id for cpu " << cpu); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + UMPIRE_LOG(Debug, "Using Linux physical_package_id " << color << " for cpu " << cpu); |
| 55 | + return true; |
| 56 | +#else |
| 57 | + UMPIRE_USE_VAR(color); |
| 58 | + return false; |
| 59 | +#endif |
| 60 | +} |
| 61 | + |
| 62 | +bool get_socket_color_from_numa(int& color) |
| 63 | +{ |
| 64 | +#if defined(UMPIRE_ENABLE_NUMA) && defined(__linux__) |
| 65 | + const int cpu = sched_getcpu(); |
| 66 | + if (cpu < 0) { |
| 67 | + UMPIRE_LOG(Debug, "sched_getcpu failed while determining NUMA fallback color"); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + color = numa::node_of_cpu(cpu); |
| 73 | + UMPIRE_LOG(Debug, "Using NUMA node " << color << " as socket color fallback for cpu " << cpu); |
| 74 | + return true; |
| 75 | + } catch (const std::exception& e) { |
| 76 | + UMPIRE_LOG(Debug, "NUMA fallback failed for cpu " << cpu << ": " << e.what()); |
| 77 | + return false; |
| 78 | + } |
| 79 | +#else |
| 80 | + UMPIRE_USE_VAR(color); |
| 81 | + return false; |
| 82 | +#endif |
| 83 | +} |
| 84 | + |
| 85 | +} // end anonymous namespace |
| 86 | + |
| 87 | +MPI_Comm create_shared_communicator(MPI_Comm comm, MemoryResourceTraits::shared_scope scope) |
| 88 | +{ |
| 89 | + MPI_Comm shared_comm{MPI_COMM_NULL}; |
| 90 | + |
| 91 | + if (scope == MemoryResourceTraits::shared_scope::node) { |
| 92 | + MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, IGNORE_KEY, MPI_INFO_NULL, &shared_comm); |
| 93 | + return shared_comm; |
| 94 | + } |
| 95 | + |
| 96 | + if (scope == MemoryResourceTraits::shared_scope::socket) { |
| 97 | + MPI_Comm node_comm{MPI_COMM_NULL}; |
| 98 | + MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, IGNORE_KEY, MPI_INFO_NULL, &node_comm); |
| 99 | + |
| 100 | + int color{-1}; |
| 101 | + if (!get_socket_color_from_package_id(color) && !get_socket_color_from_numa(color)) { |
| 102 | + MPI_Comm_free(&node_comm); |
| 103 | + UMPIRE_ERROR(runtime_error, |
| 104 | + "Unable to determine a socket color for shared_scope::socket. " |
| 105 | + "Expected Linux CPU topology data or a NUMA fallback."); |
| 106 | + } |
| 107 | + |
| 108 | + UMPIRE_LOG(Debug, "Creating socket-scoped shared communicator with color " << color); |
| 109 | + MPI_Comm_split(node_comm, color, IGNORE_KEY, &shared_comm); |
| 110 | + MPI_Comm_free(&node_comm); |
| 111 | + return shared_comm; |
| 112 | + } |
| 113 | + |
| 114 | + UMPIRE_ERROR(runtime_error, |
| 115 | + fmt::format("Unsupported shared communicator scope: {}", to_string(scope))); |
| 116 | +} |
| 117 | + |
| 118 | +} // end of namespace util |
| 119 | +} // end of namespace umpire |
| 120 | + |
| 121 | +#endif |
0 commit comments