48template <nda::Array A>
49struct mpi::lazy<mpi::tag::scatter, A> {
66 const bool all{
false};
78 [[nodiscard]]
auto shape()
const {
79 auto dims =
rhs.shape();
82 dims[0] = mpi::chunk_length(dim0,
comm.size(),
comm.rank());
92 template <nda::Array T>
94 if (not target.is_contiguous()) NDA_RUNTIME_ERROR <<
"Error in MPI scatter for nda::Array: Target array needs to be contiguous";
95 static_assert(std::decay_t<A>::layout_t::stride_order_encoded == std::decay_t<T>::layout_t::stride_order_encoded,
96 "Error in MPI scatter for nda::Array: Incompatible stride orders");
99 if (not mpi::has_env) {
106 resize_or_check_if_view(target, dims);
109 auto dim0 =
rhs.extent(0);
110 auto stride0 =
rhs.indexmap().strides()[0];
111 auto sendcounts = std::vector<int>(
comm.size());
112 auto displs = std::vector<int>(
comm.size() + 1, 0);
113 int recvcount = mpi::chunk_length(dim0,
comm.size(),
comm.rank()) * stride0;
114 for (
int r = 0; r <
comm.size(); ++r) {
115 sendcounts[r] = mpi::chunk_length(dim0,
comm.size(), r) * stride0;
116 displs[r + 1] = sendcounts[r] + displs[r];
120 auto mpi_value_type = mpi::mpi_type<value_type>::get();
121 MPI_Scatterv((
void *)
rhs.data(), &sendcounts[0], &displs[0], mpi_value_type, (
void *)target.data(), recvcount, mpi_value_type,
root,
comm.get());
155 template <
typename A>
159 if (not a.is_contiguous()) NDA_RUNTIME_ERROR <<
"Error in MPI scatter for nda::Array: Array needs to be contiguous";
160 return mpi::lazy<mpi::tag::scatter, A>{std::forward<A>(a), comm, root,
all};
Provides basic functions to create and manipulate arrays and views.
Check if a given type satisfies the array initializer concept for a given nda::MemoryArray type.
Provides concepts for the nda library.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
bool all(A const &a)
Do all elements of the array evaluate to true?
ArrayInitializer< std::remove_reference_t< A > > auto mpi_scatter(A &&a, mpi::communicator comm={}, int root=0, bool all=false)
Implementation of an MPI scatter for nda::basic_array or nda::basic_array_view types.
constexpr bool is_regular_or_view_v
Constexpr variable that is true if type A is either a regular array or a view.
void invoke(T &&target) const
Execute the lazy MPI operation and write the result to a target array/view.
decltype(std::declval< const A >()()) const_view_type
Const view type of the array/view stored in the lazy object.
const_view_type rhs
View of the array/view to be scattered.
mpi::communicator comm
MPI communicator.
const bool all
Should all processes receive the result. (doesn't make sense for scatter)
typename std::decay_t< A >::value_type value_type
Value type of the array/view.
const int root
MPI root process.
auto shape() const
Compute the shape of the target array.
Provides type traits for the nda library.