TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
reduce.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"
15#include "../concepts.hpp"
16#include "../declarations.hpp"
17#include "../exceptions.hpp"
18#include "../macros.hpp"
19#include "../map.hpp"
20#include "../traits.hpp"
21
22#include <mpi.h>
23#include <mpi/mpi.hpp>
24
25#include <cstddef>
26#include <span>
27#include <type_traits>
28#include <utility>
29
30namespace nda {
31
36
67 template <typename A1, typename A2>
69 void mpi_reduce_into(A1 const &a_in, A2 &&a_out, mpi::communicator comm = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) { // NOLINT
70 // check the shape of the input arrays/views
71 EXPECTS_WITH_MESSAGE(detail::have_mpi_equal_shapes(a_in, comm), "Error in nda::mpi_reduce_into: Shapes of arrays/views must be equal");
72
73 // resize or check the output array/view on receiving ranks
74 bool const receives = (all || (comm.rank() == root));
75 if (receives) resize_or_check_if_view(a_out, a_in.shape());
76
77 // call mpi::reduce_range with a span if the input and output arrays/views are contiguous with positive strides
78 // (on non-receiving ranks, the output should always be contiguous since it is not used)
79 if (a_in.is_contiguous() and a_in.has_positive_strides()) {
80 auto a_in_span = std::span{a_in.data(), static_cast<std::size_t>(a_in.size())};
81 if ((a_out.is_contiguous() and a_out.has_positive_strides()) || !receives) {
82 auto a_out_span = std::span{a_out.data(), static_cast<std::size_t>(a_out.size())};
83 mpi::reduce_range(a_in_span, a_out_span, comm, root, all, op);
84 } else {
85 mpi::reduce_range(a_in_span, a_out, comm, root, all, op);
86 }
87 } else {
88 if ((a_out.is_contiguous() and a_out.has_positive_strides()) || !receives) {
89 auto a_out_span = std::span{a_out.data(), static_cast<std::size_t>(a_out.size())};
90 mpi::reduce_range(a_in, a_out_span, comm, root, all, op);
91 } else {
92 mpi::reduce_range(a_in, a_out, comm, root, all, op);
93 }
94 }
95 }
96
127 template <typename A>
129 auto mpi_reduce(A const &a, mpi::communicator comm = {}, int root = 0, bool all = false, MPI_Op op = MPI_SUM) {
130 using value_t = std::remove_cvref_t<decltype(mpi::reduce(std::declval<get_value_t<A>>()))>;
131 using return_t = basic_array<value_t, get_rank<A>, typename A::layout_policy_t::contiguous_t, get_algebra<A>, heap<>>;
132 return_t a_out;
133 mpi_reduce_into(a, a_out, comm, root, all, op);
134 return a_out;
135 }
136
138
139} // namespace nda
Provides basic functions to create and manipulate arrays and views.
Provides concepts for the nda library.
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
bool all(A const &a)
Do all elements of the array evaluate to true?
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_reduce(A const &a, mpi::communicator comm={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for nda::basic_array or nda::basic_array_view types.
Definition reduce.hpp:129
void mpi_reduce_into(A1 const &a_in, A2 &&a_out, mpi::communicator comm={}, int root=0, bool all=false, MPI_Op op=MPI_SUM)
Implementation of an MPI reduce for nda::basic_array or nda::basic_array_view types that reduces dire...
Definition reduce.hpp:69
std::decay_t< decltype(get_first_element(std::declval< A const >()))> get_value_t
Get the value type of an array/view or a scalar type.
Definition traits.hpp:181
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
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition policies.hpp:52
Macros used in the nda library.
Provides lazy function calls on arrays/views.
Provides various utility functions used by the MPI interface of nda.
Provides type traits for the nda library.