TRIQS/nda
2.0.0
Multi-dimensional array library for C++
Toggle main menu visibility
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
"
14
#include "
../basic_functions.hpp
"
15
#include "
../declarations.hpp
"
16
#include "
../macros.hpp
"
17
#include "
../map.hpp
"
18
#include "
../traits.hpp
"
19
20
#include <mpi.h>
21
#include <mpi/mpi.hpp>
22
23
#include <cstddef>
24
#include <span>
25
#include <type_traits>
26
#include <utility>
27
28
namespace
nda {
29
34
65
template
<
typename
A1,
typename
A2>
66
requires
(
is_regular_or_view_v<A1>
&&
is_regular_or_view_v<A2>
)
67
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
68
// check the shape of the input arrays/views
69
EXPECTS_WITH_MESSAGE(detail::have_mpi_equal_shapes(a_in, comm),
"Error in nda::mpi_reduce_into: Shapes of arrays/views must be equal"
);
70
71
// resize or check the output array/view on receiving ranks
72
bool
const
receives = (
all
|| (comm.rank() == root));
73
if
(receives)
resize_or_check_if_view
(a_out, a_in.shape());
74
75
// call mpi::reduce_range with a span if the input and output arrays/views are contiguous with positive strides
76
// (on non-receiving ranks, the output should always be contiguous since it is not used)
77
if
(a_in.is_contiguous() and a_in.has_positive_strides()) {
78
auto
a_in_span = std::span{a_in.data(),
static_cast<
std::size_t
>
(a_in.size())};
79
if
((a_out.is_contiguous() and a_out.has_positive_strides()) || !receives) {
80
auto
a_out_span = std::span{a_out.data(),
static_cast<
std::size_t
>
(a_out.size())};
81
mpi::reduce_range(a_in_span, a_out_span, comm, root,
all
, op);
82
}
else
{
83
mpi::reduce_range(a_in_span, a_out, comm, root,
all
, op);
84
}
85
}
else
{
86
if
((a_out.is_contiguous() and a_out.has_positive_strides()) || !receives) {
87
auto
a_out_span = std::span{a_out.data(),
static_cast<
std::size_t
>
(a_out.size())};
88
mpi::reduce_range(a_in, a_out_span, comm, root,
all
, op);
89
}
else
{
90
mpi::reduce_range(a_in, a_out, comm, root,
all
, op);
91
}
92
}
93
}
94
125
template
<
typename
A>
126
requires
(
is_regular_or_view_v<A>
)
127
auto
mpi_reduce
(A
const
&a, mpi::communicator comm = {},
int
root = 0,
bool
all
=
false
, MPI_Op op = MPI_SUM) {
128
using
value_t = std::remove_cvref_t<
decltype
(mpi::reduce(std::declval<
get_value_t<A>
>()))>;
129
using
return_t = basic_array<value_t, get_rank<A>,
typename
A::layout_policy_t::contiguous_t, get_algebra<A>,
heap<>
>;
130
return_t a_out;
131
mpi_reduce_into
(a, a_out, comm, root,
all
, op);
132
return
a_out;
133
}
134
136
137
}
// namespace nda
basic_functions.hpp
Provides basic functions to create and manipulate arrays and views.
declarations.hpp
Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_vi...
nda::all
bool all(A const &a)
Do all elements of the array evaluate to true?
Definition
algorithms.hpp:110
nda::resize_or_check_if_view
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.
Definition
basic_functions.hpp:305
nda::mpi_reduce
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:127
nda::mpi_reduce_into
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:67
nda::get_value_t
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:191
nda::is_regular_or_view_v
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:162
nda::heap
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition
policies.hpp:52
macros.hpp
Macros used in the nda library.
map.hpp
Provides lazy function calls on arrays/views.
utils.hpp
Provides various utility functions used by the MPI interface of nda.
traits.hpp
Provides type traits for the nda library.
nda
mpi
reduce.hpp
Generated by
1.17.0