Skip to content

Commit 37390c4

Browse files
committed
Add simple gather function.
1 parent b5b0422 commit 37390c4

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

include/plssvm/mpi/communicator.hpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
#define PLSSVM_MPI_COMMUNICATOR_HPP_
1515
#pragma once
1616

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+
1720
#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
1922
#endif
2023

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
2329

2430
namespace plssvm::mpi {
2531

@@ -95,6 +101,24 @@ class communicator {
95101
}
96102
}
97103

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+
98122
#if defined(PLSSVM_HAS_MPI_ENABLED)
99123
/**
100124
* @brief Add implicit conversion operator back to a native MPI communicator.

0 commit comments

Comments
 (0)