Skip to content
Snippets Groups Projects
Commit 8411edf4 authored by Dmitri Naumov's avatar Dmitri Naumov
Browse files

[BL/MPI] allgatherv for variable size data exchg

parent 4ee86862
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <algorithm> #include <algorithm>
#include "Algorithm.h"
#include "Error.h" #include "Error.h"
#ifdef USE_PETSC #ifdef USE_PETSC
...@@ -136,6 +137,33 @@ static void allreduceInplace(std::vector<T>& vector, ...@@ -136,6 +137,33 @@ static void allreduceInplace(std::vector<T>& vector,
mpi_op, mpi_op,
mpi.communicator); mpi.communicator);
} }
/// Allgather for variable data. Returns offsets in the receive buffer.
/// The receive buffer is resized to accommodate the gathered data.
template <typename T>
static std::vector<int> allgatherv(
std::span<T> const send_buffer,
std::vector<std::remove_const_t<T>>& receive_buffer,
Mpi const& mpi)
{
// Determine the number of elements to send
int const size = static_cast<int>(send_buffer.size());
// Gather sizes from all ranks
std::vector<int> const sizes = allgather(size, mpi);
// Compute offsets based on counts
std::vector<int> const offsets = BaseLib::sizesToOffsets(sizes);
// Resize receive buffer to hold all gathered data
receive_buffer.resize(offsets.back());
MPI_Allgatherv(send_buffer.data(), size, mpiType<T>(),
receive_buffer.data(), sizes.data(), offsets.data(),
mpiType<T>(), mpi.communicator);
return offsets;
}
#endif #endif
/// The reduction is implemented transparently for with and without MPI. In the /// The reduction is implemented transparently for with and without MPI. In the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment