42namespace nda::detail {
47 auto mpi_gather_shape_impl(A
const &a, mpi::communicator comm,
int root,
bool all) {
48 auto dims = a.shape();
49 dims[0] = mpi::all_reduce(dims[0], comm);
50 auto gathered_size = std::accumulate(dims.begin(), dims.end(), 1l, std::multiplies<>());
52 return std::make_pair(dims, gathered_size);
96 template <
typename A1,
typename A2>
99 void mpi_gather_capi(A1 const &a_in, A2 &&a_out, mpi::communicator comm = {},
int root = 0,
bool all =
false) {
101 EXPECTS_WITH_MESSAGE(detail::have_mpi_equal_shapes(a_in(nda::range(1),
nda::ellipsis{}), comm),
102 "Error in nda::mpi_gather_capi: Shapes of arrays/views must be equal save the first one");
105 if (not mpi::has_env || comm.size() < 2) {
111 detail::check_layout_mpi_compatible(a_in,
"mpi_gather_capi");
114 auto [dims, gathered_size] = detail::mpi_gather_shape_impl(a_in, comm, root,
all);
115 auto a_out_span = std::span{a_out.data(), 0};
116 if (
all || (comm.rank() == root)) {
118 detail::check_layout_mpi_compatible(a_out,
"mpi_gather_capi");
124 a_out_span = std::span{a_out.data(),
static_cast<std::size_t
>(a_out.size())};
128 auto a_in_span = std::span{a_in.data(),
static_cast<std::size_t
>(a_in.size())};
129 mpi::gather_range(a_in_span, a_out_span, gathered_size, comm, root,
all);
151 template <
typename A>
154 return mpi::lazy<mpi::tag::gather, A>{std::forward<A>(a), comm, root,
all};
175 template <
typename A>
177 auto mpi_gather(A
const &a, mpi::communicator comm = {},
int root = 0,
bool all =
false) {
202template <nda::Array A>
203struct mpi::lazy<mpi::tag::gather, A> {
220 const bool all{
false};
235 [[nodiscard]]
auto shape()
const {
return nda::detail::mpi_gather_shape_impl(rhs, comm, root, all).first; }
252 template <nda::Array T>
253 requires(std::decay_t<T>::is_stride_order_C())
Provides utility functions for std::array.
Provides basic functions to create and manipulate arrays and views.
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
bool all(A const &a)
Do all elements of the array evaluate to true?
void resize_or_check_if_view(A &a, std::array< long, A::rank > const &sha)
Resize a given regular array to the given shape or check if a given view as the correct shape.
auto lazy_mpi_gather(A &&a, mpi::communicator comm={}, int root=0, bool all=false)
Implementation of a lazy MPI gather for nda::basic_array or nda::basic_array_view types.
auto mpi_gather(A const &a, mpi::communicator comm={}, int root=0, bool all=false)
Implementation of an MPI gather for nda::basic_array or nda::basic_array_view types.
void mpi_gather_capi(A1 const &a_in, A2 &&a_out, mpi::communicator comm={}, int root=0, bool all=false)
Implementation of an MPI gather for nda::basic_array or nda::basic_array_view types using a C-style A...
decltype(basic_array{std::declval< T >()}) get_regular_t
Get the type of the nda::basic_array that would be obtained by constructing an array from a given typ...
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
constexpr std::array< T, R > make_initialized_array(T v)
Create a new std::array object initialized with a specific value.
Macros used in the nda library.
Provides various utility functions used by the MPI interface of nda.
Includes the itertools header and provides some additional utilities.
void invoke(T &&target) const
Execute the lazy MPI operation and write the result to a target array/view.
auto shape() const
Compute the shape of the nda::ArrayInitializer object.
A stored_type
Type of the array/view stored in the lazy object.
typename std::decay_t< A >::value_type value_type
Value type of the array/view.
mpi::communicator comm
MPI communicator.
stored_type rhs
Array/View to be gathered.
Mimics Python's ... syntax.
Provides type traits for the nda library.