TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
broadcast.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2023 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: Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides an MPI broadcast function for nda::Array types.
20 */
21
22#pragma once
23
24#include "../basic_functions.hpp"
25#include "../concepts.hpp"
26#include "../exceptions.hpp"
27#include "../traits.hpp"
28
29#include <mpi/mpi.hpp>
30
31namespace nda {
32
33 /**
34 * @ingroup av_mpi
35 * @brief Implementation of an MPI broadcast for nda::basic_array or nda::basic_array_view types.
36 *
37 * @details For the root process, the array/view is broadcasted to all other processes. For non-root processes, the
38 * array/view is resized/checked to match the broadcasted dimensions and the data is written into the given
39 * array/view.
40 *
41 * Throws an exception, if a given view does not have the correct shape.
42 *
43 * @code{.cpp}
44 * // create an array on all processes
45 * nda::array<int, 2> arr(3, 4);
46 *
47 * // ...
48 * // fill array on root process
49 * // ...
50 *
51 * // broadcast the array to all processes
52 * mpi::broadcast(arr);
53 * @endcode
54 *
55 * @tparam A nda::basic_array or nda::basic_array_view type.
56 * @param a Array or view to be broadcasted from/into.
57 * @param comm `mpi::communicator` object.
58 * @param root Rank of the root process.
59 */
60 template <typename A>
61 void mpi_broadcast(A &a, mpi::communicator comm = {}, int root = 0)
62 requires(is_regular_or_view_v<A>)
63 {
64 static_assert(has_contiguous_layout<A>, "Error in MPI broadcast for nda::Array: Array needs to be contiguous");
65 auto dims = a.shape();
66 MPI_Bcast(&dims[0], dims.size(), mpi::mpi_type<typename decltype(dims)::value_type>::get(), root, comm.get());
67 if (comm.rank() != root) { resize_or_check_if_view(a, dims); }
68 MPI_Bcast(a.data(), a.size(), mpi::mpi_type<typename A::value_type>::get(), root, comm.get());
69 }
70
71} // namespace nda
void mpi_broadcast(A &a, mpi::communicator comm={}, int root=0)
Implementation of an MPI broadcast for nda::basic_array or nda::basic_array_view types.
Definition broadcast.hpp:61