34#include "kamping/kassert/kassert.hpp"
41namespace kamping::ops::internal {
113 template <
typename T,
typename S>
128namespace kamping::ops {
131template <
typename T =
void>
135template <
typename T =
void>
139template <
typename T =
void>
143template <
typename T =
void>
147template <
typename T =
void>
151template <
typename T =
void>
155template <
typename T =
void>
159template <
typename T =
void>
163template <
typename T =
void>
167template <
typename T =
void>
171template <
typename T =
void>
183namespace kamping::types {
185#ifdef KAMPING_DOXYGEN_ONLY
201template <
typename Op,
typename T>
218template <
typename Op,
typename T,
typename Enable =
void>
223template <
typename T,
typename S>
224struct mpi_operation_traits<
227 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
228 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::floating
231 static constexpr T
identity = std::numeric_limits<T>::lowest();
237template <
typename T,
typename S>
238struct mpi_operation_traits<
239 kamping::ops::min<S>,
241 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
242 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::floating
245 static constexpr T
identity = std::numeric_limits<T>::max();
251template <
typename T,
typename S>
252struct mpi_operation_traits<
253 kamping::ops::plus<S>,
255 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
256 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::floating
257 || builtin_type<T>::category == TypeCategory::complex
266template <
typename T,
typename S>
267struct mpi_operation_traits<
268 kamping::ops::multiplies<S>,
270 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
271 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::floating
272 || builtin_type<T>::category == TypeCategory::complex
281template <
typename T,
typename S>
282struct mpi_operation_traits<
283 kamping::ops::logical_and<S>,
285 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
286 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::logical
295template <
typename T,
typename S>
296struct mpi_operation_traits<
297 kamping::ops::logical_or<S>,
299 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
300 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::logical
303 static constexpr T
identity =
false;
309template <
typename T,
typename S>
310struct mpi_operation_traits<
311 kamping::ops::logical_xor<S>,
313 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
314 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::logical
317 static constexpr T
identity =
false;
323template <
typename T,
typename S>
324struct mpi_operation_traits<
325 kamping::ops::bit_and<S>,
327 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
328 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::byte
331 static constexpr T
identity = ~(T{0});
337template <
typename T,
typename S>
338struct mpi_operation_traits<
339 kamping::ops::bit_or<S>,
341 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
342 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::byte
351template <
typename T,
typename S>
352struct mpi_operation_traits<
353 kamping::ops::bit_xor<S>,
355 std::enable_if_t<(std::is_same_v<S, void> || std::is_same_v<T, S>)&&(
356 builtin_type<T>::category == TypeCategory::integer || builtin_type<T>::category == TypeCategory::byte
397 if (
this != &
other) {
441template <
bool is_commutative,
typename T,
typename Op>
444 std::is_default_constructible_v<Op>,
445 "ScopedFunctorOp requires a default-constructible functor. Use ScopedCallbackOp for lambdas."
447 static_assert(std::is_invocable_r_v<T, Op, T const&, T const&>,
"Op must be callable as T(T const&, T const&).");
465 return _functor(
lhs,
rhs);
471 T*
in =
static_cast<T*
>(
invec);
476 static ScopedOp _make_scoped_op() {
478 MPI_Op_create(_execute,
static_cast<int>(is_commutative), &raw);
479 return ScopedOp{raw,
true};
500template <
bool is_commutative>
548template <
typename Functor>
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
RAII handle that creates an MPI_Op from a raw MPI callback function pointer.
Definition reduce_ops.hpp:501
MPI_Op get() const noexcept
Definition reduce_ops.hpp:524
void(*)(void *, void *, int *, MPI_Datatype *) callback_type
The MPI callback signature expected by MPI_Op_create.
Definition reduce_ops.hpp:504
ScopedCallbackOp(ScopedCallbackOp &&) noexcept=default
Move constructor. The moved-from handle becomes empty.
ScopedCallbackOp() noexcept=default
Constructs an empty, non-owning handle (MPI_OP_NULL).
RAII handle that creates an MPI_Op from a default-constructible C++ functor.
Definition reduce_ops.hpp:442
T operator()(T const &lhs, T const &rhs) const
Applies the functor to two values.
Definition reduce_ops.hpp:464
MPI_Op get() const noexcept
Definition reduce_ops.hpp:459
ScopedFunctorOp(Op op)
Creates an MPI_Op for the given functor.
Definition reduce_ops.hpp:451
RAII wrapper for an MPI_Op.
Definition reduce_ops.hpp:378
ScopedOp & operator=(ScopedOp &&other) noexcept
Move assignment. Frees any currently owned op, then transfers ownership.
Definition reduce_ops.hpp:396
MPI_Op get() const noexcept
Definition reduce_ops.hpp:411
ScopedOp(MPI_Op op, bool owns) noexcept
Wrap an existing MPI_Op.
Definition reduce_ops.hpp:386
ScopedOp() noexcept
Constructs an empty, non-owning handle (MPI_OP_NULL).
Definition reduce_ops.hpp:381
ScopedOp(ScopedOp &&other) noexcept
Move constructor. Transfers ownership; the moved-from handle no longer frees the op.
Definition reduce_ops.hpp:392
internal::OperationBuilder< Op, Commutative > op(Op &&op, Commutative commute=ops::internal::undefined_commutative_tag{})
Passes a reduction operation to ther underlying call. Accepts function objects, lambdas,...
Definition named_parameters.hpp:1219
Mapping of C++ datatypes to builtin MPI types.
std::logical_and< T > logical_and
Builtin logical AND (MPI_LAND).
Definition reduce_ops.hpp:148
constexpr internal::commutative_tag commutative
Tag: operation is commutative.
Definition reduce_ops.hpp:174
std::bit_or< T > bit_or
Builtin bitwise OR (MPI_BOR).
Definition reduce_ops.hpp:160
std::logical_or< T > logical_or
Builtin logical OR (MPI_LOR).
Definition reduce_ops.hpp:156
std::bit_and< T > bit_and
Builtin bitwise AND (MPI_BAND).
Definition reduce_ops.hpp:152
std::bit_xor< T > bit_xor
Builtin bitwise XOR (MPI_BXOR).
Definition reduce_ops.hpp:168
std::plus< T > plus
Builtin summation (MPI_SUM).
Definition reduce_ops.hpp:140
constexpr internal::non_commutative_tag non_commutative
Tag: operation is non-commutative.
Definition reduce_ops.hpp:175
std::multiplies< T > multiplies
Builtin multiplication (MPI_PROD).
Definition reduce_ops.hpp:144
Tag for a commutative user-defined reduce operation.
Definition reduce_ops.hpp:120
constexpr bool operator()(T const &lhs, S const &rhs) const
Returns the logical XOR of the two parameters.
Definition reduce_ops.hpp:114
Logical XOR function object (no STL equivalent).
Definition reduce_ops.hpp:97
constexpr bool operator()(T const &lhs, T const &rhs) const
Returns the logical XOR of the two parameters.
Definition reduce_ops.hpp:101
constexpr auto operator()(T const &lhs, T const &rhs) const
Returns the maximum of the two parameters.
Definition reduce_ops.hpp:65
Wrapper struct for std::max.
Definition reduce_ops.hpp:49
constexpr T operator()(T const &lhs, T const &rhs) const
Returns the maximum of the two parameters.
Definition reduce_ops.hpp:53
constexpr auto operator()(T const &lhs, T const &rhs) const
Returns the minimum of the two parameters.
Definition reduce_ops.hpp:89
Wrapper struct for std::min (same rationale as max_impl).
Definition reduce_ops.hpp:73
constexpr T operator()(T const &lhs, T const &rhs) const
Returns the minimum of the two parameters.
Definition reduce_ops.hpp:77
Tag for a non-commutative user-defined reduce operation.
Definition reduce_ops.hpp:122
Tag for a reduce operation without a manually declared commutativity (builtin ops only).
Definition reduce_ops.hpp:124
Null operation (MPI_OP_NULL).
Definition reduce_ops.hpp:172
Type trait that maps a (functor type, element type) pair to its builtin MPI_Op.
Definition reduce_ops.hpp:202
static constexpr bool is_builtin
true if Op applied to T corresponds to a predefined MPI operation constant.
Definition reduce_ops.hpp:204
static MPI_Op op()
Returns the predefined MPI_Op constant for this operation.
static constexpr T identity
The identity element for this operation and data type.
Definition reduce_ops.hpp:209