KaMPIng 0.2.0
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
mpi_datatype.hpp
Go to the documentation of this file.
1// This file is part of KaMPIng.
2//
3// Copyright 2021-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/// @file
15/// @brief Utility that maps C++ types to types that can be understood by MPI.
16
17#pragma once
18
19#include <type_traits>
20
21#include <mpi.h>
22
23#include "kamping/kassert/kassert.hpp"
24#ifdef KAMPING_ENABLE_REFLECTION
25 #include <boost/pfr.hpp>
26#endif
27
30#include "kamping/noexcept.hpp"
31
32namespace kamping {
33/// @brief Tag used for indicating that a struct is reflectable.
34/// @see struct_type
35struct kamping_tag {};
36} // namespace kamping
37
38namespace kamping {
39/// @addtogroup kamping_mpi_utility
40/// @{
41///
42///
43
44namespace internal {
45/// @brief Helper to check if a type is a `std::pair`.
46template <typename T>
47struct is_std_pair : std::false_type {};
48/// @brief Helper to check if a type is a `std::pair`.
49template <typename T1, typename T2>
50struct is_std_pair<std::pair<T1, T2>> : std::true_type {};
51
52/// @brief Helper to check if a type is a `std::tuple`.
53template <typename T>
54struct is_std_tuple : std::false_type {};
55/// @brief Helper to check if a type is a `std::tuple`.
56template <typename... Ts>
57struct is_std_tuple<std::tuple<Ts...>> : std::true_type {};
58
59/// @brief Helper to check if a type is a `std::array`.
60template <typename A>
61struct is_std_array : std::false_type {};
62
63/// @brief Helper to check if a type is a `std::array`.
64template <typename T, size_t N>
65struct is_std_array<std::array<T, N>> : std::true_type {
66 using value_type = T; ///< The type of the elements in the array.
67 static constexpr size_t size = N; ///< The number of elements in the array.
68};
69} // namespace internal
70
71/// @brief Constructs an contiguous type of size \p N from type \p T using `MPI_Type_contiguous`.
72template <typename T, size_t N>
74 static constexpr TypeCategory category = TypeCategory::contiguous; ///< The category of the type.
75 /// @brief Whether the type has to be committed before it can be used in MPI calls.
77 /// @brief The MPI_Datatype corresponding to the type.
79};
80
81/// @brief Constructs a type that is serialized as a sequence of `sizeof(T)` bytes using `MPI_BYTE`. Note that you must
82/// ensure that this conversion is valid.
83template <typename T>
84struct byte_serialized : contiguous_type<std::byte, sizeof(T)> {};
85
86/// @brief Constructs a MPI_Datatype for a struct-like type.
87/// @tparam T The type to construct the MPI_Datatype for.
88///
89/// This requires that \p T is a `std::pair`, `std::tuple` or a type that is reflectable with
90/// [pfr](https://github.com/boostorg/pfr). If you do not agree with PFR's decision if a type is implicitly
91/// reflectable, you can override it by providing a specialization of \c pfr::is_reflectable with the tag \ref
92/// kamping_tag.
93/// @see https://apolukhin.github.io/pfr_non_boost/pfr/is_reflectable.html
94/// https://www.boost.org/doc/libs/master/doc/html/reference_section_of_pfr.htmlfor details
95/// @note Reflection support for arbitrary struct types is only suppported when KaMPIng is compiled with PFR.
96template <typename T>
98#ifdef KAMPING_ENABLE_REFLECTION
99 static_assert(
101 || boost::pfr::is_implicitly_reflectable<T, kamping_tag>::value,
102 "Type must be a std::pair, std::tuple or reflectable"
103 );
104#else
105 static_assert(
106 internal::is_std_pair<T>::value || internal::is_std_tuple<T>::value, "Type must be a std::pair or std::tuple"
107 );
108#endif
109 /// @brief The category of the type.
110 static constexpr TypeCategory category = TypeCategory::struct_like;
111 /// @brief Whether the type has to be committed before it can be used in MPI calls.
113 /// @brief The MPI_Datatype corresponding to the type.
115};
116
117/// @brief Type tag for indicating that no static type definition exists for a type.
119
120/// @brief The type dispatcher that maps a C++ type \p T to a type trait that can be used to construct an MPI_Datatype.
121///
122/// The mapping is as follows:
123/// - C++ types directly supported by MPI are mapped to the corresponding `MPI_Datatype`.
124/// - Enums are mapped to the underlying type.
125/// - C-style arrays and `std::array` are mapped to contiguous types of the underlying type.
126/// - All other trivially copyable types are mapped to a contiguous type consisting of `sizeof(T)` bytes.
127/// - All other types are not supported directly and require a specialization of `mpi_type_traits`. In this case, the
128/// trait `no_matching_type` is returned.
129///
130/// @returns The corresponding type trait for the type \p T.
131template <typename T>
133 using T_no_const = std::remove_const_t<T>; // remove const from T
134 // we previously also removed volatile here, but interpreting a pointer
135 // to a volatile type as a pointer to a non-volatile type is UB
136
137 static_assert(
138 !std::is_pointer_v<T_no_const>,
139 "MPI does not support pointer types. Why do you want to transfer a pointer over MPI?"
140 );
141
142 static_assert(!std::is_function_v<T_no_const>, "MPI does not support function types.");
143
144 // TODO: this might be a bit too strict. We might want to allow unions in the future.
145 static_assert(!std::is_union_v<T_no_const>, "MPI does not support union types.");
146
147 static_assert(!std::is_void_v<T_no_const>, "There is no MPI datatype corresponding to void.");
148
149 if constexpr (is_builtin_type_v<T_no_const>) {
150 // builtin types are handled by the builtin_type trait
152 } else if constexpr (std::is_enum_v<T_no_const>) {
153 // enums are mapped to the underlying type
155 } else if constexpr (std::is_array_v<T_no_const>) {
156 // arrays are mapped to contiguous types
157 constexpr size_t array_size = std::extent_v<T_no_const>;
158 using underlying_type = std::remove_extent_t<T_no_const>;
160 } else if constexpr (internal::is_std_array<T_no_const>::value) {
161 // std::array is mapped to contiguous types
165 } else if constexpr (std::is_trivially_copyable_v<T_no_const>) {
166 // all other trivially copyable types are mapped to a sequence of bytes
168 } else {
169 return no_matching_type{};
170 }
171}
172
173/// @brief The type trait that maps a C++ type \p T to a type trait that can be used to construct an MPI_Datatype.
174///
175/// The default behavior is controlled by \ref type_dispatcher. If you want to support a type that is not supported by
176/// the default behavior, you can specialize this trait. For example:
177///
178/// ```cpp
179/// struct MyType {
180/// int a;
181/// double b;
182/// char c;
183/// std::array<int, 3> d;
184/// };
185/// namespace kamping {
186/// // using KaMPIng's built-in struct serializer
187/// template <>
188/// struct mpi_type_traits<MyType> : struct_type<MyType> {};
189///
190/// // or using an explicitly constructed type
191/// template <>
192/// struct mpi_type_traits<MyType> {
193/// static constexpr bool has_to_be_committed = true;
194/// static MPI_Datatype data_type() {
195/// MPI_Datatype type;
196/// MPI_Type_create_*(..., &type);
197/// return type;
198/// }
199/// };
200/// } // namespace kamping
201/// ```
202///
203template <typename T, typename Enable = void>
205
206/// @brief The type trait that maps a C++ type \p T to a type trait that can be used to construct an MPI_Datatype.
207///
208/// The default behavior is controlled by \ref type_dispatcher. If you want to support a type that is not supported by
209/// the default behavior, you can specialize this trait. For example:
210///
211/// ```cpp
212/// struct MyType {
213/// int a;
214/// double b;
215/// char c;
216/// std::array<int, 3> d;
217/// };
218/// namespace kamping {
219/// // using KaMPIng's built-in struct serializer
220/// template <>
221/// struct mpi_type_traits<MyType> : struct_type<MyType> {};
222///
223/// // or using an explicitly constructed type
224/// template <>
225/// struct mpi_type_traits<MyType> {
226/// static constexpr bool has_to_be_committed = true;
227/// static MPI_Datatype data_type() {
228/// MPI_Datatype type;
229/// MPI_Type_create_*(..., &type);
230/// return type;
231/// }
232/// };
233/// } // namespace kamping
234/// ```
235///
236template <typename T>
237struct mpi_type_traits<T, std::enable_if_t<!std::is_same_v<decltype(type_dispatcher<T>()), no_matching_type>>> {
238 /// @brief The base type of this trait obtained via \ref type_dispatcher.
239 /// This defines how the data type is constructed in \c mpi_type_traits::data_type().
240 using base = decltype(type_dispatcher<T>());
241 /// @brief The category of the type.
242 static constexpr TypeCategory category = base::category;
243 /// @brief Whether the type has to be committed before it can be used in MPI calls.
244 static constexpr bool has_to_be_committed = category_has_to_be_committed(category);
245 /// @brief The MPI_Datatype corresponding to the type T.
247 return decltype(type_dispatcher<T>())::data_type();
248 }
249};
250
251///@brief Check if the type has a static type definition, i.e. \ref mpi_type_traits is defined.
252template <typename, typename Enable = void>
253struct has_static_type : std::false_type {};
254
255///@brief Check if the type has a static type definition, i.e. \ref mpi_type_traits is defined.
256template <typename T>
257struct has_static_type<T, std::void_t<decltype(mpi_type_traits<T>::data_type())>> : std::true_type {};
258
259///@brief Check if the type has a static type definition, i.e. has a corresponding \c MPI_Datatype defined following the
260/// rules from \ref type_dispatcher.
261template <typename T>
263
264/// @brief Register a new \c MPI_Datatype for \p T with the MPI environment. It will be freed when the environment is
265/// finalized.
266///
267/// The \c MPI_Datatype is created using \c mpi_type_traits<T>::data_type() and committed using \c MPI_Type_commit.
268///
269/// @tparam T The type to register.
270template <typename T>
273 MPI_Type_commit(&type);
275 mpi_env.register_mpi_type(type);
276 return type;
277}
278
279/// @brief Translate template parameter T to an MPI_Datatype. If no corresponding MPI_Datatype exists, we will create
280/// new continuous type.
281/// Based on https://gist.github.com/2b-t/50d85115db8b12ed263f8231abf07fa2
282/// To check if type \c T maps to a builtin \c MPI_Datatype at compile-time, use \c mpi_type_traits.
283/// @tparam T The type to translate into an MPI_Datatype.
284/// @return The tag identifying the corresponding MPI_Datatype or the newly created type.
285/// @see mpi_custom_continuous_type()
286///
287
288/// @brief Translate type \p T to an MPI_Datatype using the type defined via \ref mpi_type_traits.
289///
290/// If the type has not been registered with MPI yet, it will be created and committed and automatically registered with
291/// the MPI environment, such that it will be freed when the environment is finalized.
292///
293/// @tparam T The type to translate into an MPI_Datatype.
294template <typename T>
296 static_assert(
298 "\n --> Type not supported directly by KaMPIng. Please provide a specialization for mpi_type_traits."
299 );
301 // using static initialization to ensure that the type is only committed once
303 return type;
304 } else {
306 }
307}
308
309template <typename T, size_t N>
311 MPI_Datatype type;
313 if constexpr (std::is_same_v<T, std::byte>) {
315 } else {
316 static_assert(
318 "\n --> Type not supported directly by KaMPIng. Please provide a specialization for mpi_type_traits."
319 );
321 }
322 int count = static_cast<int>(N);
323 int err = MPI_Type_contiguous(count, base_type, &type);
325 return type;
326}
327
328namespace internal {
329/// @brief Applies functor \p f to each field of the tuple with an index in index sequence \p Is.
330///
331/// \p f should be a callable that takes a reference to the field and its index.
332template <typename T, typename F, size_t... Is>
333void for_each_tuple_field(T&& t, F&& f, std::index_sequence<Is...>) {
334 (f(std::get<Is>(std::forward<T>(t)), Is), ...);
335}
336
337/// @brief Applies functor \p f to each field of the tuple \p t.
338///
339/// \p f should be a callable that takes a reference to the field and its index.
340template <typename T, typename F>
341void for_each_tuple_field(T& t, F&& f) {
342 for_each_tuple_field(t, std::forward<F>(f), std::make_index_sequence<std::tuple_size_v<T>>{});
343}
344
345/// @brief Applies functor \p f to each field of the tuple-like type \p t.
346/// This works for `std::pair` and `std::tuple`. If KaMPIng's reflection support is enabled, this also works with types
347/// that are reflectable with [pfr](https://github.com/boostorg/pfr).
348///
349/// \p f should be a callable that takes a reference to the field and
350/// its index.
351template <typename T, typename F>
352void for_each_field(T& t, F&& f) {
354 for_each_tuple_field(t, std::forward<F>(f));
355 } else {
356#ifdef KAMPING_ENABLE_REFLECTION
357 boost::pfr::for_each_field(t, std::forward<F>(f));
358#else
359 // should not happen
361#endif
362 }
363}
364
365/// @brief The number of elements in a tuple-like type.
366/// This works for `std::pair` and `std::tuple`.
367/// If KaMPIng's reflection support is enabled, this also works with types that are reflectable with
368/// [pfr](https://github.com/boostorg/pfr).
369template <typename T>
370constexpr size_t tuple_size = [] {
371 if constexpr (internal::is_std_pair<T>::value) {
372 return 2;
373 } else if constexpr (internal::is_std_tuple<T>::value) {
374 return std::tuple_size_v<T>;
375 } else {
376#ifdef KAMPING_ENABLE_REFLECTION
377 return boost::pfr::tuple_size_v<T>;
378#else
379 if constexpr (std::is_arithmetic_v<T>) {
380 return 1;
381 } else {
382 return std::tuple_size_v<T>;
383 }
384#endif
385 }
386}();
387} // namespace internal
388
389template <typename T>
391 T t{};
392 MPI_Aint base;
393 MPI_Get_address(&t, &base);
394 int blocklens[internal::tuple_size<T>];
395 MPI_Datatype types[internal::tuple_size<T>];
396 MPI_Aint disp[internal::tuple_size<T>];
397 internal::for_each_field(t, [&](auto& elem, size_t i) {
399 using elem_type = std::remove_reference_t<decltype(elem)>;
400 static_assert(
402 "\n --> Type not supported directly by KaMPIng. Please provide a specialization for mpi_type_traits."
403 );
405 disp[i] = MPI_Aint_diff(disp[i], base);
406 blocklens[i] = 1;
407 });
408 MPI_Datatype type;
409 int err = MPI_Type_create_struct(static_cast<int>(internal::tuple_size<T>), blocklens, disp, types, &type);
412 err = MPI_Type_create_resized(type, 0, sizeof(T), &resized_type);
414 return resized_type;
415}
416
417/// @brief A scoped MPI_Datatype that commits the type on construction and frees it on destruction.
418/// This is useful for RAII-style management of MPI_Datatype objects.
420 MPI_Datatype _type; ///< The MPI_Datatype.
421public:
422 /// @brief Construct a new scoped MPI_Datatype and commits it. If no type is provided, default to
423 /// `MPI_DATATYPE_NULL` and does not commit or free anything.
425 if (type != MPI_DATATYPE_NULL) {
426 mpi_env.commit(type);
427 }
428 }
429 /// @brief Deleted copy constructor.
431 /// @brief Deleted copy assignment.
433
434 /// @brief Move constructor.
438 /// @brief Move assignment.
440 std::swap(_type, other._type);
441 return *this;
442 }
443 /// @brief Get the MPI_Datatype.
444 MPI_Datatype const& data_type() const {
445 return _type;
446 }
447 /// @brief Free the MPI_Datatype.
449 if (_type != MPI_DATATYPE_NULL) {
450 mpi_env.free(_type);
451 }
452 }
453};
454
455/// @}
456
457} // namespace kamping
Mapping of C++ datatypes to builtin MPI types.
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
A scoped MPI_Datatype that commits the type on construction and frees it on destruction....
Definition mpi_datatype.hpp:419
ScopedDatatype(ScopedDatatype const &)=delete
Deleted copy constructor.
ScopedDatatype & operator=(ScopedDatatype &&other) noexcept
Move assignment.
Definition mpi_datatype.hpp:439
ScopedDatatype(ScopedDatatype &&other) noexcept
Move constructor.
Definition mpi_datatype.hpp:435
ScopedDatatype & operator=(ScopedDatatype const &)=delete
Deleted copy assignment.
ScopedDatatype(MPI_Datatype type=MPI_DATATYPE_NULL)
Construct a new scoped MPI_Datatype and commits it. If no type is provided, default to MPI_DATATYPE_N...
Definition mpi_datatype.hpp:424
~ScopedDatatype()
Free the MPI_Datatype.
Definition mpi_datatype.hpp:448
MPI_Datatype const & data_type() const
Get the MPI_Datatype.
Definition mpi_datatype.hpp:444
Wrapper for MPI functions that don't require a communicator.
Environment< InitMPIMode::NoInitFinalize > const mpi_env
A global environment object to use when you don't want to create a new Environment object.
Definition environment.hpp:323
#define THROW_IF_MPI_ERROR(error_code, function)
Wrapper around THROWING_KAMPING_ASSERT for MPI errors.
Definition error_handling.hpp:34
auto type_dispatcher()
The type dispatcher that maps a C++ type T to a type trait that can be used to construct an MPI_Datat...
Definition mpi_datatype.hpp:132
static constexpr bool has_static_type_v
Check if the type has a static type definition, i.e. has a corresponding MPI_Datatype defined followi...
Definition mpi_datatype.hpp:262
static MPI_Datatype data_type()
The MPI_Datatype corresponding to the type.
Definition mpi_datatype.hpp:390
MPI_Datatype mpi_datatype() KAMPING_NOEXCEPT
Translate template parameter T to an MPI_Datatype. If no corresponding MPI_Datatype exists,...
Definition mpi_datatype.hpp:295
static MPI_Datatype data_type()
The MPI_Datatype corresponding to the type.
Definition mpi_datatype.hpp:310
constexpr bool category_has_to_be_committed(TypeCategory category)
Checks if a type of the given category has to commited before usage in MPI calls.
Definition builtin_types.hpp:35
TypeCategory
the members specify which group the datatype belongs to according to the type groups specified in Sec...
Definition builtin_types.hpp:32
MPI_Datatype construct_and_commit_type()
Register a new MPI_Datatype for T with the MPI environment. It will be freed when the environment is ...
Definition mpi_datatype.hpp:271
void for_each_tuple_field(T &&t, F &&f, std::index_sequence< Is... >)
Applies functor f to each field of the tuple with an index in index sequence Is.
Definition mpi_datatype.hpp:333
void for_each_field(T &t, F &&f)
Applies functor f to each field of the tuple-like type t. This works for std::pair and std::tuple....
Definition mpi_datatype.hpp:352
constexpr size_t tuple_size
The number of elements in a tuple-like type. This works for std::pair and std::tuple....
Definition mpi_datatype.hpp:370
STL namespace.
Defines the macro KAMPING_NOEXCEPT to be used instad of noexcept.
#define KAMPING_NOEXCEPT
noexcept macro.
Definition noexcept.hpp:19
Constructs a type that is serialized as a sequence of sizeof(T) bytes using MPI_BYTE....
Definition mpi_datatype.hpp:84
Constructs an contiguous type of size N from type T using MPI_Type_contiguous.
Definition mpi_datatype.hpp:73
static constexpr bool has_to_be_committed
Whether the type has to be committed before it can be used in MPI calls.
Definition mpi_datatype.hpp:76
static constexpr TypeCategory category
Definition mpi_datatype.hpp:74
Check if the type has a static type definition, i.e. mpi_type_traits is defined.
Definition mpi_datatype.hpp:253
T value_type
The type of the elements in the array.
Definition mpi_datatype.hpp:66
Helper to check if a type is a std::array.
Definition mpi_datatype.hpp:61
Helper to check if a type is a std::pair.
Definition mpi_datatype.hpp:47
Helper to check if a type is a std::tuple.
Definition mpi_datatype.hpp:54
Tag used for indicating that a struct is reflectable.
Definition mpi_datatype.hpp:35
static MPI_Datatype data_type()
The MPI_Datatype corresponding to the type T.
Definition mpi_datatype.hpp:246
The type trait that maps a C++ type T to a type trait that can be used to construct an MPI_Datatype.
Definition mpi_datatype.hpp:204
Type tag for indicating that no static type definition exists for a type.
Definition mpi_datatype.hpp:118
Constructs a MPI_Datatype for a struct-like type.
Definition mpi_datatype.hpp:97
static constexpr TypeCategory category
The category of the type.
Definition mpi_datatype.hpp:110
static constexpr bool has_to_be_committed
Whether the type has to be committed before it can be used in MPI calls.
Definition mpi_datatype.hpp:112