TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
scatter.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
22#pragma once
23
24#include "./utils.hpp"
25#include "../concepts.hpp"
26#include "../declarations.hpp"
27#include "../macros.hpp"
28#include "../traits.hpp"
29
30#include <mpi.h>
31#include <mpi/mpi.hpp>
32
33#include <cstddef>
34#include <functional>
35#include <numeric>
36#include <span>
37#include <tuple>
38#include <type_traits>
39#include <utility>
40
41namespace nda::detail {
42
43 // Helper function to get the shape and total size of the scattered array/view.
44 template <typename A>
45 requires(is_regular_or_view_v<A> and std::decay_t<A>::is_stride_order_C())
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);
53 }
54
55} // namespace nda::detail
56
57namespace nda {
58
91 template <typename A1, typename A2>
92 requires(is_regular_or_view_v<A1> and std::decay_t<A1>::is_stride_order_C()
93 and is_regular_or_view_v<A2> and std::decay_t<A2>::is_stride_order_C())
94 void mpi_scatter_capi(A1 const &a_in, A2 &&a_out, mpi::communicator comm = {}, int root = 0) { // NOLINT
95 // check the ranks of the input arrays/views
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")
97
98 // simply copy if there is no active MPI environment or if the communicator size is < 2
99 if (not mpi::has_env || comm.size() < 2) {
100 a_out = a_in;
101 return;
102 }
103
104 // check if the input and output arrays/views can be used in the MPI call
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");
107
108 // get output shape and resize or check the output array/view
109 auto [dims, scattered_size, stride0] = detail::mpi_scatter_shape_impl(a_in, comm, root);
110 resize_or_check_if_view(a_out, dims);
111
112 // scatter the data
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);
116 }
117
136 template <typename A>
137 requires(is_regular_or_view_v<A> and std::decay_t<A>::is_stride_order_C())
138 auto lazy_mpi_scatter(A &&a, mpi::communicator comm = {}, int root = 0) {
139 return mpi::lazy<mpi::tag::scatter, A>{std::forward<A>(a), comm, root, true};
140 }
141
158 template <typename A>
159 requires(is_regular_or_view_v<A> and std::decay_t<A>::is_stride_order_C())
160 auto mpi_scatter(A const &a, mpi::communicator comm = {}, int root = 0) {
161 using return_t = get_regular_t<A>;
162 return_t a_out;
163 mpi_scatter_capi(a, a_out, comm, root);
164 return a_out;
165 }
166
169} // namespace nda
170
185template <nda::Array A>
186struct mpi::lazy<mpi::tag::scatter, A> {
188 using value_type = typename std::decay_t<A>::value_type;
189
191 using stored_type = A;
192
195
197 mpi::communicator comm;
198
200 const int root{0}; // NOLINT (const is fine here)
201
203 const bool all{false}; // NOLINT (const is fine here)
204
218 [[nodiscard]] auto shape() const { return std::get<0>(nda::detail::mpi_scatter_shape_impl(rhs, comm, root)); }
219
235 template <nda::Array T>
236 requires(std::decay_t<T>::is_stride_order_C())
237 void invoke(T &&target) const { // NOLINT (temporary views are allowed here)
238 nda::mpi_scatter_capi(rhs, target, comm, root);
239 }
240};
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.
Definition scatter.hpp:138
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.
Definition scatter.hpp:160
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 ...
Definition scatter.hpp:94
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.
Definition traits.hpp:163
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.
Definition scatter.hpp:237
mpi::communicator comm
MPI communicator.
Definition scatter.hpp:197
typename std::decay_t< A >::value_type value_type
Value type of the array/view.
Definition scatter.hpp:188
A stored_type
Type of the array/view stored in the lazy object.
Definition scatter.hpp:191
stored_type rhs
Array/View to be scattered.
Definition scatter.hpp:194
auto shape() const
Compute the shape of the nda::ArrayInitializer object.
Definition scatter.hpp:218
Provides type traits for the nda library.