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