KaMPIng 0.2.0
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
builtin_types.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 Mapping of C++ datatypes to builtin MPI types.
16
17#pragma once
18#include <complex>
19#include <cstddef>
20#include <type_traits>
21
22#include <mpi.h>
23
24#include "kamping/kabool.hpp"
25
26namespace kamping {
27
28/// @addtogroup kamping_mpi_utility
29/// @{
30
31/// @brief the members specify which group the datatype belongs to according to the type groups specified in
32/// Section 6.9.2 of the MPI 4.0 standard.
33enum class TypeCategory { integer, floating, complex, logical, byte, character, struct_like, contiguous };
34
35/// @brief Checks if a type of the given \p category has to commited before usage in MPI calls.
37 switch (category) {
38 case TypeCategory::integer:
39 case TypeCategory::floating:
40 case TypeCategory::complex:
41 case TypeCategory::logical:
42 case TypeCategory::byte:
43 case TypeCategory::character:
44 return false;
45 case TypeCategory::struct_like:
46 case TypeCategory::contiguous:
47 return true;
48 }
49}
50
51/// @brief Checks if the type \p T is a builtin MPI type.
52///
53/// Provides a member constant \c value which is equal to \c true if \p T is a builtin type.
54/// If `value` is `true`, the following members are defined, where \c data_type() returns the
55/// corresponding \c MPI_Datatype and \c category the corresponding \ref TypeCategory.
56///
57/// ```cpp
58/// struct builtin_type<T> {
59/// static constexpr bool value = true;
60/// static MPI_Datatype data_type();
61/// static constexpr TypeCategory category;
62/// };
63/// ```
64///
65template <typename T>
66struct builtin_type : std::false_type {};
67
68/// @brief Helper variable template for \ref builtin_type.
69template <typename T>
71
72/// @brief Specialization of \ref builtin_type for \c char.
73template <>
74struct builtin_type<char> : std::true_type {
75 /// @brief Returns the matching \c MPI_Datatype.
77 return MPI_CHAR;
78 }
79 static constexpr TypeCategory category = TypeCategory::character; ///< The types's \ref TypeCategory.
80};
81
82/// @brief Specialization of \ref builtin_type for `signed char`.
83template <>
84struct builtin_type<signed char> : std::true_type {
85 /// @brief Returns the matching \c MPI_Datatype.
87 return MPI_SIGNED_CHAR;
88 }
89 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
90};
91
92/// @brief Specialization of \ref builtin_type for `unsigned char`.
93template <>
94struct builtin_type<unsigned char> : std::true_type {
95 /// @brief Returns the matching \c MPI_Datatype.
97 return MPI_UNSIGNED_CHAR;
98 }
99 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
100};
101
102/// @brief Specialization of \ref builtin_type for `wchar_t`.
103template <>
104struct builtin_type<wchar_t> : std::true_type {
105 /// @brief Returns the matching \c MPI_Datatype.
107 return MPI_WCHAR;
108 }
109 static constexpr TypeCategory category = TypeCategory::character; ///< The types's \ref TypeCategory.
110};
111
112/// @brief Specialization of \ref builtin_type for `short int`.
113template <>
114struct builtin_type<short int> : std::true_type {
115 /// @brief Returns the matching \c MPI_Datatype.
117 return MPI_SHORT;
118 }
119 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
120};
121
122/// @brief Specialization of \ref builtin_type for `unsigned short int`.
123template <>
124struct builtin_type<unsigned short int> : std::true_type {
125 /// @brief Returns the matching \c MPI_Datatype.
127 return MPI_UNSIGNED_SHORT;
128 }
129 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
130};
131
132/// @brief Specialization of \ref builtin_type for `int`.
133template <>
134struct builtin_type<int> : std::true_type {
135 /// @brief Returns the matching \c MPI_Datatype.
137 return MPI_INT;
138 }
139 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
140};
141
142/// @brief Specialization of \ref builtin_type for `unsigned int`.
143template <>
144struct builtin_type<unsigned int> : std::true_type {
145 /// @brief Returns the matching \c MPI_Datatype.
147 return MPI_UNSIGNED;
148 }
149 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
150};
151
152/// @brief Specialization of \ref builtin_type for `long int`.
153template <>
154struct builtin_type<long int> : std::true_type {
155 /// @brief Returns the matching \c MPI_Datatype.
157 return MPI_LONG;
158 }
159 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
160};
161
162/// @brief Specialization of \ref builtin_type for `unsigned long int`.
163template <>
164struct builtin_type<unsigned long int> : std::true_type {
165 /// @brief Returns the matching \c MPI_Datatype.
167 return MPI_UNSIGNED_LONG;
168 }
169 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
170};
171
172/// @brief Specialization of \ref builtin_type for `long long int`.
173template <>
174struct builtin_type<long long int> : std::true_type {
175 /// @brief Returns the matching \c MPI_Datatype.
177 return MPI_LONG_LONG;
178 }
179 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
180};
181
182/// @brief Specialization of \ref builtin_type for `unsigned long long int`.
183template <>
184struct builtin_type<unsigned long long int> : std::true_type {
185 /// @brief Returns the matching \c MPI_Datatype.
188 }
189 static constexpr TypeCategory category = TypeCategory::integer; ///< The types's \ref TypeCategory.
190};
191
192/// @brief Specialization of \ref builtin_type for `float`.
193template <>
194struct builtin_type<float> : std::true_type {
195 /// @brief Returns the matching \c MPI_Datatype.
197 return MPI_FLOAT;
198 }
199 static constexpr TypeCategory category = TypeCategory::floating; ///< The types's \ref TypeCategory.
200};
201
202/// @brief Specialization of \ref builtin_type for `double`.
203template <>
204struct builtin_type<double> : std::true_type {
205 /// @brief Returns the matching \c MPI_Datatype.
207 return MPI_DOUBLE;
208 }
209 static constexpr TypeCategory category = TypeCategory::floating; ///< The types's \ref TypeCategory.
210};
211
212/// @brief Specialization of \ref builtin_type for `long double`.
213template <>
214struct builtin_type<long double> : std::true_type {
215 /// @brief Returns the matching \c MPI_Datatype.
217 return MPI_LONG_DOUBLE;
218 }
219 static constexpr TypeCategory category = TypeCategory::floating; ///< The types's \ref TypeCategory.
220};
221
222/// @brief Specialization of \ref builtin_type for `bool`.
223template <>
224struct builtin_type<bool> : std::true_type {
225 /// @brief Returns the matching \c MPI_Datatype.
227 return MPI_CXX_BOOL;
228 }
229 static constexpr TypeCategory category = TypeCategory::logical; ///< The types's \ref TypeCategory.
230};
231
232/// @brief Specialization of \ref builtin_type for `std::byte`.
233template <>
234struct builtin_type<std::byte> : std::true_type {
235 /// @brief Returns the matching \c MPI_Datatype.
237 return MPI_BYTE;
238 }
239 static constexpr TypeCategory category = TypeCategory::byte; ///< The type's \ref TypeCategory.
240};
241
242/// @brief Specialization of \ref builtin_type for \ref kabool.
243template <>
244struct builtin_type<kabool> : std::true_type {
245 /// @brief Returns the matching \c MPI_Datatype.
247 return MPI_CXX_BOOL;
248 }
249 static constexpr TypeCategory category = TypeCategory::logical; ///< The types's \ref TypeCategory.
250};
251
252/// @brief Specialization of \ref builtin_type for `std::complex<float>`.
253template <>
254struct builtin_type<std::complex<float>> : std::true_type {
255 /// @brief Returns the matching \c MPI_Datatype.
258 }
259 static constexpr TypeCategory category = TypeCategory::complex; ///< The types's \ref TypeCategory.
260};
261
262/// @brief Specialization of \ref builtin_type for `std::complex<double>`.
263template <>
264struct builtin_type<std::complex<double>> : std::true_type {
265 /// @brief Returns the matching \c MPI_Datatype.
268 }
269 static constexpr TypeCategory category = TypeCategory::complex; ///< The types's \ref TypeCategory.
270};
271
272/// @brief Specialization of \ref builtin_type for `std::complex<long double>`.
273template <>
274struct builtin_type<std::complex<long double>> : std::true_type {
275 /// @brief Returns the matching \c MPI_Datatype.
279 static constexpr TypeCategory category = TypeCategory::complex; ///< The types's \ref TypeCategory.
280};
281/// @}
282} // namespace kamping
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
Wrapper around bool to allow handling containers of boolean values.
Definition kabool.hpp:17
constexpr bool is_builtin_type_v
Helper variable template for builtin_type.
Definition builtin_types.hpp:70
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:36
TypeCategory
the members specify which group the datatype belongs to according to the type groups specified in Sec...
Definition builtin_types.hpp:33
STL namespace.
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:226
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:76
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:206
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:196
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:136
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:246
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:216
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:156
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:176
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:116
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:86
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:236
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:266
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:256
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:276
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:96
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:146
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:166
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:186
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:126
static MPI_Datatype data_type()
Returns the matching MPI_Datatype.
Definition builtin_types.hpp:106
Checks if the type T is a builtin MPI type.
Definition builtin_types.hpp:66