|
14 | 14 | #define PLSSVM_MPI_COMMUNICATOR_HPP_ |
15 | 15 | #pragma once |
16 | 16 |
|
| 17 | +#include "plssvm/mpi/detail/mpi_datatype.hpp" // plssvm::mpi::detail::mpi_datatype |
| 18 | +#include "plssvm/mpi/detail/utility.hpp" // PLSSVM_MPI_ERROR_CHECK |
| 19 | + |
17 | 20 | #if defined(PLSSVM_HAS_MPI_ENABLED) |
18 | | - #include "mpi.h" // MPI_Comm, MPI_COMM_WORLD |
| 21 | + #include "mpi.h" // MPI_Comm, MPI_COMM_WORLD, MPI_Gather |
19 | 22 | #endif |
20 | 23 |
|
21 | | -#include <cstddef> // std::size_t |
22 | | -#include <functional> // std::invoke |
| 24 | +#include <algorithm> // std::transform |
| 25 | +#include <cstddef> // std::size_t |
| 26 | +#include <functional> // std::invoke |
| 27 | +#include <type_traits> // std::is_enum_v, std::underlying_type_t |
| 28 | +#include <vector> // std::vector |
23 | 29 |
|
24 | 30 | namespace plssvm::mpi { |
25 | 31 |
|
@@ -95,6 +101,24 @@ class communicator { |
95 | 101 | } |
96 | 102 | } |
97 | 103 |
|
| 104 | + /** |
| 105 | + * @brief Gather the @p value from each MPI rank on the `communicator::main_rank()`. |
| 106 | + * @details If `PLSSVM_HAS_MPI_ENABLED` is undefined, returns the provided @p value wrapped in a `std::vector`. |
| 107 | + * @tparam T the type of the values to gather |
| 108 | + * @param value the value to gather at the main MPI rank |
| 109 | + * @return a `std::vector` containing all gathered values (`[[nodiscard]]`) |
| 110 | + */ |
| 111 | + template <typename T> |
| 112 | + [[nodiscard]] std::vector<T> gather(T value) { |
| 113 | +#if defined(PLSSVM_HAS_MPI_ENABLED) |
| 114 | + std::vector<T> result(this->size()); |
| 115 | + PLSSVM_MPI_ERROR_CHECK(MPI_Gather(&value, 1, detail::mpi_datatype<T>(), result.data(), 1, detail::mpi_datatype<T>(), communicator::main_rank(), comm_)); |
| 116 | + return result; |
| 117 | +#else |
| 118 | + return { value }; |
| 119 | +#endif |
| 120 | + } |
| 121 | + |
98 | 122 | #if defined(PLSSVM_HAS_MPI_ENABLED) |
99 | 123 | /** |
100 | 124 | * @brief Add implicit conversion operator back to a native MPI communicator. |
|
0 commit comments