KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
operation_builder.hpp
Go to the documentation of this file.
1// This file is part of KaMPIng.
2//
3// Copyright 2021-2022 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.
13
14/// @file
15/// The class defined in this file serve as wrapper for functions passed to \c MPI calls wrapped by KaMPIng.
16
17#pragma once
18
19#include "kamping/assertion_levels.hpp"
20#include "kamping/mpi_ops.hpp"
22#include "kassert/kassert.hpp"
23
24namespace kamping {
25/// @addtogroup kamping_mpi_utility
26/// @{
27
28namespace internal {
29
30/// @brief Parameter wrapping an operation passed to reduce-like MPI collectives.
31/// This wraps an MPI operation without the argument of the operation specified. This enables the user to construct
32/// such wrapper using the parameter factory \c kamping::op without passing the type of the operation. The library
33/// developer may then construct the actual operation wrapper with a given type later.
34///
35/// @tparam Op type of the operation (may be a function object or a lambda)
36/// @tparam Commutative tag specifying if the operation is commutative
37template <typename Op, typename Commutative>
39public:
40 static constexpr ParameterType parameter_type =
41 ParameterType::op; ///< The type of parameter this object encapsulates.
42
43 /// @brief constructs an Operation builder
44 /// @param op the operation
45 /// @param commutative_tag tag indicating if the operation is commutative (see \c kamping::op for details)
46 OperationBuilder(Op&& op, Commutative commutative_tag [[maybe_unused]]) : _op(op) {}
47
48 /// @brief Move constructor for OperationsBuilder.
50
51 /// @brief Move assignment operator for OperationsBuilder.
53
54 /// @brief Copy constructor is deleted as buffers should only be moved.
56 // redundant as defaulted move constructor implies the deletion
57
58 /// @brief Copy assignment operator is deleted as buffers should only be moved.
60 // redundant as defaulted move constructor implies the deletion
61
62 /// @brief constructs an operation for the given type T
63 /// @tparam T argument type of the reduction operation
64 template <typename T>
66 if constexpr (std::is_same_v<std::remove_reference_t<std::remove_const_t<Op>>, MPI_Op>) {
67#if KASSERT_ENABLED(KAMPING_ASSERTION_LEVEL_NORMAL)
68 // mapping a MPI_Op to the corresponding function object requires a scan over all builtin operations
69 with_operation_functor(_op, [](auto operation) {
70 if constexpr (!std::is_same_v<decltype(operation), ops::null<>>) {
71 // the user passed a builtin datatype, so we can do some checking
72 KASSERT(
73 (mpi_operation_traits<decltype(operation), T>::is_builtin),
74 "The provided builtin operation is not compatible with datatype T."
75 );
76 }
77 });
78#endif
80 } else {
81 static_assert(std::is_invocable_r_v<T, Op, T const&, T const&>, "Type of custom operation does not match.");
82 return ReduceOperation<T, Op, Commutative>(std::forward<Op>(_op), Commutative{});
83 }
84 }
85
86private:
87 Op _op; ///< the operation which is encapsulated
88};
89
90} // namespace internal
91
92/// @}
93
94} // namespace kamping
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
Parameter wrapping an operation passed to reduce-like MPI collectives. This wraps an MPI operation wi...
Definition operation_builder.hpp:38
OperationBuilder & operator=(OperationBuilder &&)=default
Move assignment operator for OperationsBuilder.
static constexpr ParameterType parameter_type
The type of parameter this object encapsulates.
Definition operation_builder.hpp:40
OperationBuilder(OperationBuilder const &)=delete
Copy constructor is deleted as buffers should only be moved.
auto build_operation()
constructs an operation for the given type T
Definition operation_builder.hpp:65
OperationBuilder & operator=(OperationBuilder const &)=delete
Copy assignment operator is deleted as buffers should only be moved.
OperationBuilder(Op &&op, Commutative commutative_tag)
constructs an Operation builder
Definition operation_builder.hpp:46
OperationBuilder(OperationBuilder &&)=default
Move constructor for OperationsBuilder.
ParameterType
Each input parameter to one of the MPI calls wrapped by KaMPIng needs to has one of the following tag...
Definition named_parameter_types.hpp:33
@ op
Tag used to represent a reduce operation in a MPI call.
Definitions for builtin MPI operations.
File containing the parameter types used by the KaMPIng library.
auto with_operation_functor(MPI_Op op, Functor &&func)
Helper function that maps an MPI_Op to the matching functor from kamping::ops. In case no function ma...
Definition mpi_ops.hpp:403
Type trait for checking whether a functor is a builtin MPI reduction operation and query correspondin...
Definition mpi_ops.hpp:221
builtin null operation (aka MPI_OP_NULL)
Definition mpi_ops.hpp:185