KaMPIng 0.1.0
(Near) zero-overhead C++ MPI bindings.
Loading...
Searching...
No Matches
named_parameter_filtering.hpp
1// This file is part of KaMPIng.
2//
3// Copyright 2024 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#pragma once
15
18
19namespace kamping::internal {
20
21/// @brief Wrapper class to store an enum entry (\ref kamping::internal::ParameterType) in a separate type (so that it
22/// can be used in a compile time list)
23///
24/// @tparam ptype ParameterType to store as a type
25template <ParameterType ptype>
27 static constexpr ParameterType parameter_type = ptype; ///< ParameterType to be stored in this type.
28};
29/// @brief Base template used to concatenate a type to a given std::tuple.
30/// based on https://stackoverflow.com/a/18366475
31template <typename, typename>
32struct PrependType {};
33
34/// @brief Specialization of a class template used to preprend a type to a given std::tuple.
35///
36/// @tparam Head Type to prepend to the std::tuple.
37/// @tparam Tail Types contained in the std::tuple.
38template <typename Head, typename... Tail>
39struct PrependType<Head, std::tuple<Tail...>> {
40 using type = std::tuple<Head, Tail...>; ///< tuple with prepended Head type.
41};
42
43/// @brief Base template used to filter a list of types and only keep those whose types meet specified criteria.
44/// See the following specialisations for more information.
45template <typename...>
46struct FilterOut;
47
48/// @brief Specialisation of template class used to filter a list of types and only keep the those whose types meet
49/// the specified criteria.
50template <typename Predicate>
52 using type = std::tuple<>; ///< Tuple of types meeting the specified criteria.
53};
54
55/// @brief Specialization of template class used to filter a list of (buffer-)types and discard those for which
56/// The template is recursively instantiated to check one type after the other and "insert" it into a
57/// std::tuple if it meets the criteria .
58/// based on https://stackoverflow.com/a/18366475
59///
60/// @tparam Predicate Predicate function which has a constexpr static member function `discard()` taking \tparam Head as
61/// template parameter and returning a bool indiciating whether head shall be discard (filtered out).
62/// @tparam Head Type for which it is checked whether it meets the predicate.
63/// @tparam Tail Types that are checked later on during the recursive instantiation.
64template <typename Predicate, typename Head, typename... Tail>
66 using non_ref_first = std::remove_reference_t<Head>; ///< Remove potential reference from Head.
67 static constexpr bool discard_elem =
68 Predicate::template discard<non_ref_first>(); ///< Predicate which Head has to fulfill to be kept.
69 static constexpr auto ptype = parameter_type_v<Head>; ///< ParameterType stored as a static variable in Head.
70 using type = std::conditional_t<
71 discard_elem,
72 typename FilterOut<Predicate, Tail...>::type,
73 typename PrependType<
74 std::integral_constant<parameter_type_t<Head>, ptype>,
75 typename FilterOut<Predicate, Tail...>::type>::type>; ///< A std::tuple<T1, ..., Tn> where T1, ..., Tn are
76 ///< those types among Head, Tail... which fulfill the
77 ///< predicate.
78};
79
80/// @brief Specialisation of template class for types stored in a std::tuple<...> that is used to filter these types and
81/// only keep those which meet certain criteria (see above).
82///
83/// @tparam Types Types to check.
84template <typename Predicate, typename... Types>
85struct FilterOut<Predicate, std::tuple<Types...>> {
86 using type =
87 typename FilterOut<Predicate, Types...>::type; ///< A std::tuple<T1, ..., Tn> where T1, ..., Tn are those
88 ///< types among Types... which match the criteria.
89};
90
91/// @brief Retrieve the buffer with requested ParameterType from the std::tuple containg all buffers.
92///
93/// @tparam ptype ParameterType of the buffer to retrieve.
94/// @tparam Buffers Types of the data buffers.
95/// @param buffers Data buffers out of which the one with requested parameter type is retrieved.
96/// @return Reference to the buffer which the requested ParameterType.
97template <internal::ParameterType ptype, typename... Buffers>
98auto& retrieve_buffer(std::tuple<Buffers...>& buffers) {
99 return internal::select_parameter_type_in_tuple<ptype>(buffers);
100}
101
102/// @brief Construct tuple containing all buffers specified in \tparam ParamterTypeTuple.
103///
104/// @tparam ParameterTypeTuple Tuple containing ParameterTypeEntries specifing the entries to be
105/// retrieved from the BufferTuple buffers.
106/// @tparam Buffers Types of the data buffers.
107/// @tparam i Integer sequence.
108/// @param buffers Data buffers out of which the ones with parameter types contained in ParameterTypeTuple are
109/// retrieved.
110/// @return std::tuple containing all requested data buffers.
111template <typename ParameterTypeTuple, typename... Buffers, std::size_t... i>
113 std::tuple<Buffers...>& buffers, std::index_sequence<i...> /*index_sequence*/
114) {
115 return std::make_tuple(std::move(retrieve_buffer<std::tuple_element_t<i, ParameterTypeTuple>::value>(buffers))...);
116}
117
118/// @brief Construct tuple containing all buffers specified in \tparam ParamterTypeTuple.
119///
120/// @tparam ParameterTypeTuple Tuple containing ParameterTypeEntries specifing the entries to be
121/// retrieved from the BufferTuple buffers.
122/// @tparam Buffers Types of the data buffers.
123/// @param buffers Data buffers out of which the ones with parameter types contained in ParameterTypeTuple are
124/// retrieved.
125/// @return std::tuple containing all requested data buffers.
126template <typename ParameterTypeTuple, typename... Buffers>
128 // number of buffers that will be contained in the result object (including the receive buffer if it is an out
129 // parameter)
130 constexpr std::size_t num_output_parameters = std::tuple_size_v<ParameterTypeTuple>;
131 auto buffers_tuple = std::tie(buffers...);
132
135 std::make_index_sequence<num_output_parameters>{}
136 );
137}
138} // namespace kamping::internal
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
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
Template magic to implement named parameters in cpp.
File containing the parameter types used by the KaMPIng library.
Internal namespace marking the code that is not user-facing.
Definition collectives_helpers.hpp:20
auto construct_buffer_tuple_impl(std::tuple< Buffers... > &buffers, std::index_sequence< i... >)
Construct tuple containing all buffers specified in.
Definition named_parameter_filtering.hpp:112
auto & retrieve_buffer(std::tuple< Buffers... > &buffers)
Retrieve the buffer with requested ParameterType from the std::tuple containg all buffers.
Definition named_parameter_filtering.hpp:98
auto construct_buffer_tuple(Buffers &&... buffers)
Construct tuple containing all buffers specified in.
Definition named_parameter_filtering.hpp:127
STL namespace.
std::conditional_t< discard_elem, typename FilterOut< Predicate, Tail... >::type, typename PrependType< std::integral_constant< parameter_type_t< Head >, ptype >, typename FilterOut< Predicate, Tail... >::type >::type > type
Definition named_parameter_filtering.hpp:70
std::remove_reference_t< Head > non_ref_first
Remove potential reference from Head.
Definition named_parameter_filtering.hpp:66
typename FilterOut< Predicate, Types... >::type type
Definition named_parameter_filtering.hpp:86
std::tuple<> type
Tuple of types meeting the specified criteria.
Definition named_parameter_filtering.hpp:52
Base template used to filter a list of types and only keep those whose types meet specified criteria....
Definition named_parameter_filtering.hpp:46
Wrapper class to store an enum entry (kamping::internal::ParameterType) in a separate type (so that i...
Definition named_parameter_filtering.hpp:26
static constexpr ParameterType parameter_type
ParameterType to be stored in this type.
Definition named_parameter_filtering.hpp:27
std::tuple< Head, Tail... > type
tuple with prepended Head type.
Definition named_parameter_filtering.hpp:40
Base template used to concatenate a type to a given std::tuple. based on https://stackoverflow....
Definition named_parameter_filtering.hpp:32