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--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./utils.hpp"
14#include "../concepts.hpp"
15#include "../declarations.hpp"
16#include "../macros.hpp"
17#include "../traits.hpp"
18
19#include <mpi.h>
20#include <mpi/mpi.hpp>
21
22#include <cstddef>
23#include <functional>
24#include <numeric>
25#include <span>
26#include <tuple>
27#include <type_traits>
28#include <utility>
29
30namespace nda::detail {
31
32 // Helper function to get the shape and total size of the scattered array/view as well as the stride along the first
33 // dimension.
34 template <typename A>
35 requires(is_regular_or_view_v<A> and std::decay_t<A>::is_stride_order_C())
36 auto mpi_scatter_shape_impl(A const &a, mpi::communicator comm, int root) {
37 auto dims = a.shape();
38 mpi::broadcast(dims, comm, root);
39 auto scattered_size = std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<>());
40 auto stride0 = scattered_size / dims[0];
41 dims[0] = mpi::chunk_length(dims[0], comm.size(), comm.rank());
42 return std::make_tuple(dims, scattered_size, stride0);
43 }
44
45} // namespace nda::detail
46
47namespace nda {
48
53
82 template <typename A1, typename A2>
83 requires(is_regular_or_view_v<A1> and std::decay_t<A1>::is_stride_order_C()
84 and is_regular_or_view_v<A2> and std::decay_t<A2>::is_stride_order_C())
85 void mpi_scatter_into(A1 const &a_in, A2 &&a_out, mpi::communicator comm = {}, int root = 0) { // NOLINT
86 // check the ranks of the input arrays/views
87 EXPECTS_WITH_MESSAGE(detail::have_mpi_equal_ranks(a_in, comm), "Error in nda::mpi_scatter_into: Ranks of arrays/views must be equal")
88
89 // check if the input and output arrays/views can be used in the MPI call
90 if (comm.rank() == root) detail::check_layout_mpi_compatible(a_in, "mpi_scatter_into");
91 detail::check_layout_mpi_compatible(a_out, "mpi_scatter_into");
92
93 // get output shape and resize or check the output array/view
94 auto [dims, scattered_size, stride0] = detail::mpi_scatter_shape_impl(a_in, comm, root);
95 resize_or_check_if_view(a_out, dims);
96
97 // scatter the data
98 auto a_out_span = std::span{a_out.data(), static_cast<std::size_t>(a_out.size())};
99 auto a_in_span = std::span{a_in.data(), static_cast<std::size_t>(a_in.size())};
100 mpi::scatter_range(a_in_span, a_out_span, scattered_size, comm, root, stride0);
101 }
102
119 template <typename A>
120 requires(is_regular_or_view_v<A> and std::decay_t<A>::is_stride_order_C())
121 auto mpi_scatter(A const &a, mpi::communicator comm = {}, int root = 0) {
122 using return_t = get_regular_t<A>;
123 return_t a_out;
124 mpi::scatter_into(a, a_out, comm, root);
125 return a_out;
126 }
127
129
130} // namespace nda
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 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:121
void mpi_scatter_into(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 that scatters di...
Definition scatter.hpp:85
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:152
Macros used in the nda library.
Provides various utility functions used by the MPI interface of nda.
Provides type traits for the nda library.