41namespace nda::detail {
46 auto mpi_scatter_shape_impl(A
const &a, mpi::communicator comm,
int root) {
47 auto dims = a.shape();
48 mpi::broadcast(dims, comm, root);
49 auto scattered_size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<>());
50 auto stride0 = scattered_size / dims[0];
51 dims[0] = mpi::chunk_length(dims[0], comm.size(), comm.rank());
52 return std::make_tuple(dims, scattered_size, stride0);
91 template <
typename A1,
typename A2>
94 void mpi_scatter_capi(A1 const &a_in, A2 &&a_out, mpi::communicator comm = {},
int root = 0) {
96 EXPECTS_WITH_MESSAGE(detail::have_mpi_equal_ranks(a_in, comm),
"Error in nda::mpi_scatter_capi: Ranks of arrays/views must be equal")
99 if (not mpi::has_env || comm.size() < 2) {
105 if (comm.rank() == root) detail::check_layout_mpi_compatible(a_in,
"mpi_scatter_capi");
106 detail::check_layout_mpi_compatible(a_out,
"mpi_scatter_capi");
109 auto [dims, scattered_size, stride0] = detail::mpi_scatter_shape_impl(a_in, comm, root);
113 auto a_out_span = std::span{a_out.data(),
static_cast<std::size_t
>(a_out.size())};
114 auto a_in_span = std::span{a_in.data(),
static_cast<std::size_t
>(a_in.size())};
115 mpi::scatter_range(a_in_span, a_out_span, scattered_size, comm, root, stride0);
136 template <
typename A>
139 return mpi::lazy<mpi::tag::scatter, A>{std::forward<A>(a), comm, root,
true};
158 template <
typename A>
160 auto mpi_scatter(A
const &a, mpi::communicator comm = {},
int root = 0) {
185template <nda::Array A>
186struct mpi::lazy<mpi::tag::scatter, A> {
203 const bool all{
false};
218 [[nodiscard]]
auto shape()
const {
return std::get<0>(nda::detail::mpi_scatter_shape_impl(rhs, comm, root)); }
235 template <nda::Array T>
236 requires(std::decay_t<T>::is_stride_order_C())
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
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_scatter(A &&a, mpi::communicator comm={}, int root=0)
Implementation of a lazy MPI scatter for nda::basic_array or nda::basic_array_view types.
auto mpi_scatter(A const &a, mpi::communicator comm={}, int root=0)
Implementation of an MPI scatter for nda::basic_array or nda::basic_array_view types.
void mpi_scatter_capi(A1 const &a_in, A2 &&a_out, mpi::communicator comm={}, int root=0)
Implementation of an MPI scatter for nda::basic_array or nda::basic_array_view types using a C-style ...
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.
Macros used in the nda library.
Provides various utility functions used by the MPI interface of nda.
void invoke(T &&target) const
Execute the lazy MPI operation and write the result to a target array/view.
mpi::communicator comm
MPI communicator.
typename std::decay_t< A >::value_type value_type
Value type of the array/view.
A stored_type
Type of the array/view stored in the lazy object.
stored_type rhs
Array/View to be scattered.
auto shape() const
Compute the shape of the nda::ArrayInitializer object.
Provides type traits for the nda library.