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