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) 2024--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
14#include "../traits.hpp"
15
16#include <mpi/mpi.hpp>
17
18#include <cstddef>
19#include <span>
20
21namespace nda {
22
44 template <typename A>
45 void mpi_broadcast(A &&a, mpi::communicator comm = {}, int root = 0) // NOLINT (temporary views are allowed here)
47 {
48 // broadcast the shape of the input array/view on the root process
49 auto dims = a.shape();
50 mpi::broadcast(dims, comm, root);
51
52 // resize or check the output array/view on non-root processes
53 if (comm.rank() != root) resize_or_check_if_view(a, dims);
54
55 // call mpi::broadcast_range with a span if the array/view is contiguous with positive strides
56 if (a.is_contiguous() and a.has_positive_strides()) {
57 auto a_span = std::span{a.data(), static_cast<std::size_t>(a.size())};
58 mpi::broadcast_range(a_span, comm, root);
59 } else {
60 mpi::broadcast_range(a, comm, root);
61 }
62 }
63
64} // namespace nda
Provides basic functions to create and manipulate arrays and views.
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.
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:45
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:153
Provides type traits for the nda library.