File indexing completed on 2025-01-30 09:46:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef BOOST_MPI_DATATYPE_HPP
0018 #define BOOST_MPI_DATATYPE_HPP
0019
0020 #include <boost/mpi/config.hpp>
0021 #include <boost/mpi/datatype_fwd.hpp>
0022 #include <mpi.h>
0023 #include <boost/config.hpp>
0024 #include <boost/mpl/bool.hpp>
0025 #include <boost/mpl/or.hpp>
0026 #include <boost/mpl/and.hpp>
0027 #include <boost/mpi/detail/mpi_datatype_cache.hpp>
0028 #include <boost/mpl/assert.hpp>
0029 #include <boost/archive/basic_archive.hpp>
0030 #include <boost/serialization/library_version_type.hpp>
0031 #include <boost/serialization/item_version_type.hpp>
0032 #include <utility> // for std::pair
0033
0034 #if defined(__cplusplus) && (201103L <= __cplusplus)
0035 #include <array>
0036 #endif
0037
0038 namespace boost { namespace mpi {
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 template<typename T>
0050 struct is_mpi_integer_datatype
0051 : public boost::mpl::false_ { };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 template<typename T>
0063 struct is_mpi_floating_point_datatype
0064 : public boost::mpl::false_ { };
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 template<typename T>
0076 struct is_mpi_logical_datatype
0077 : public boost::mpl::false_ { };
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 template<typename T>
0089 struct is_mpi_complex_datatype
0090 : public boost::mpl::false_ { };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 template<typename T>
0102 struct is_mpi_byte_datatype
0103 : public boost::mpl::false_ { };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 template<typename T>
0126 struct is_mpi_builtin_datatype
0127 : boost::mpl::or_<is_mpi_integer_datatype<T>,
0128 is_mpi_floating_point_datatype<T>,
0129 is_mpi_logical_datatype<T>,
0130 is_mpi_complex_datatype<T>,
0131 is_mpi_byte_datatype<T> >
0132 {
0133 };
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 template<typename T>
0162 struct is_mpi_datatype
0163 : public is_mpi_builtin_datatype<T>
0164 {
0165 };
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189 template<typename T> MPI_Datatype get_mpi_datatype(const T& x)
0190 {
0191 BOOST_MPL_ASSERT((is_mpi_datatype<T>));
0192 return detail::mpi_datatype_cache().datatype(x);
0193 }
0194
0195
0196 #ifndef BOOST_MPI_DOXYGEN
0197
0198
0199 #define BOOST_MPI_DATATYPE(CppType, MPIType, Kind) \
0200 template<> \
0201 inline MPI_Datatype \
0202 get_mpi_datatype< CppType >(const CppType&) { return MPIType; } \
0203 \
0204 template<> \
0205 struct BOOST_JOIN(is_mpi_,BOOST_JOIN(Kind,_datatype))< CppType > \
0206 : boost::mpl::true_ \
0207 {}
0208
0209
0210 BOOST_MPI_DATATYPE(packed, MPI_PACKED, builtin);
0211
0212
0213 BOOST_MPI_DATATYPE(char, MPI_CHAR, builtin);
0214
0215
0216 BOOST_MPI_DATATYPE(short, MPI_SHORT, integer);
0217
0218
0219 BOOST_MPI_DATATYPE(int, MPI_INT, integer);
0220
0221
0222 BOOST_MPI_DATATYPE(long, MPI_LONG, integer);
0223
0224
0225 BOOST_MPI_DATATYPE(float, MPI_FLOAT, floating_point);
0226
0227
0228 BOOST_MPI_DATATYPE(double, MPI_DOUBLE, floating_point);
0229
0230
0231 BOOST_MPI_DATATYPE(long double, MPI_LONG_DOUBLE, floating_point);
0232
0233
0234 BOOST_MPI_DATATYPE(unsigned char, MPI_UNSIGNED_CHAR, builtin);
0235
0236
0237 BOOST_MPI_DATATYPE(unsigned short, MPI_UNSIGNED_SHORT, integer);
0238
0239
0240 BOOST_MPI_DATATYPE(unsigned, MPI_UNSIGNED, integer);
0241
0242
0243 BOOST_MPI_DATATYPE(unsigned long, MPI_UNSIGNED_LONG, integer);
0244
0245
0246 #define BOOST_MPI_LIST2(A, B) A, B
0247
0248 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(float, int)>, MPI_FLOAT_INT,
0249 builtin);
0250
0251 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(double, int)>, MPI_DOUBLE_INT,
0252 builtin);
0253
0254 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long double, int)>,
0255 MPI_LONG_DOUBLE_INT, builtin);
0256
0257 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long, int>), MPI_LONG_INT,
0258 builtin);
0259
0260 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(short, int>), MPI_SHORT_INT,
0261 builtin);
0262
0263 BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(int, int>), MPI_2INT, builtin);
0264 #undef BOOST_MPI_LIST2
0265
0266
0267 template <class T, class U>
0268 struct is_mpi_datatype<std::pair<T,U> >
0269 : public mpl::and_<is_mpi_datatype<T>,is_mpi_datatype<U> >
0270 {
0271 };
0272
0273
0274 #if defined(__cplusplus) && (201103L <= __cplusplus)
0275 template<class T, std::size_t N>
0276 struct is_mpi_datatype<std::array<T, N> >
0277 : public is_mpi_datatype<T>
0278 {
0279 };
0280 #endif
0281
0282
0283 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && \
0284 (defined(MPI_WCHAR) || (BOOST_MPI_VERSION >= 2))
0285 BOOST_MPI_DATATYPE(wchar_t, MPI_WCHAR, builtin);
0286 #endif
0287
0288
0289 #if defined(BOOST_HAS_LONG_LONG) && \
0290 (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
0291 BOOST_MPI_DATATYPE(long long, MPI_LONG_LONG_INT, builtin);
0292 #elif defined(BOOST_HAS_MS_INT64) && \
0293 (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
0294 BOOST_MPI_DATATYPE(__int64, MPI_LONG_LONG_INT, builtin);
0295 #endif
0296
0297
0298
0299
0300
0301
0302 #if defined(BOOST_HAS_LONG_LONG) && \
0303 (defined(MPI_UNSIGNED_LONG_LONG) \
0304 || (BOOST_MPI_VERSION >= 2))
0305 BOOST_MPI_DATATYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG, builtin);
0306 #elif defined(BOOST_HAS_MS_INT64) && \
0307 (defined(MPI_UNSIGNED_LONG_LONG) \
0308 || (BOOST_MPI_VERSION >= 2))
0309 BOOST_MPI_DATATYPE(unsigned __int64, MPI_UNSIGNED_LONG_LONG, builtin);
0310 #endif
0311
0312
0313 #if defined(MPI_SIGNED_CHAR) || (BOOST_MPI_VERSION >= 2)
0314 BOOST_MPI_DATATYPE(signed char, MPI_SIGNED_CHAR, builtin);
0315 #endif
0316
0317
0318 #endif
0319
0320 namespace detail {
0321 inline MPI_Datatype build_mpi_datatype_for_bool()
0322 {
0323
0324 MPI_Datatype type;
0325 MPI_Type_contiguous(sizeof(bool), MPI_BYTE, &type);
0326 MPI_Type_commit(&type);
0327 return type;
0328 }
0329 }
0330
0331
0332
0333 template<>
0334 inline MPI_Datatype get_mpi_datatype<bool>(const bool&)
0335 {
0336 static MPI_Datatype type = detail::build_mpi_datatype_for_bool();
0337 return type;
0338 }
0339
0340
0341 template<>
0342 struct is_mpi_datatype<bool>
0343 : boost::mpl::bool_<true>
0344 {};
0345
0346
0347 #ifndef BOOST_MPI_DOXYGEN
0348
0349 BOOST_MPI_DATATYPE(boost::serialization::library_version_type, get_mpi_datatype(uint_least16_t()), integer);
0350 BOOST_MPI_DATATYPE(boost::archive::version_type, get_mpi_datatype(uint_least8_t()), integer);
0351 BOOST_MPI_DATATYPE(boost::archive::class_id_type, get_mpi_datatype(int_least16_t()), integer);
0352 BOOST_MPI_DATATYPE(boost::archive::class_id_reference_type, get_mpi_datatype(int_least16_t()), integer);
0353 BOOST_MPI_DATATYPE(boost::archive::class_id_optional_type, get_mpi_datatype(int_least16_t()), integer);
0354 BOOST_MPI_DATATYPE(boost::archive::object_id_type, get_mpi_datatype(uint_least32_t()), integer);
0355 BOOST_MPI_DATATYPE(boost::archive::object_reference_type, get_mpi_datatype(uint_least32_t()), integer);
0356 BOOST_MPI_DATATYPE(boost::archive::tracking_type, get_mpi_datatype(bool()), builtin);
0357 BOOST_MPI_DATATYPE(boost::serialization::collection_size_type, get_mpi_datatype(std::size_t()), integer);
0358 BOOST_MPI_DATATYPE(boost::serialization::item_version_type, get_mpi_datatype(uint_least8_t()), integer);
0359 #endif
0360
0361
0362 } }
0363
0364
0365
0366
0367 #define BOOST_IS_MPI_DATATYPE(T) \
0368 namespace boost { \
0369 namespace mpi { \
0370 template<> \
0371 struct is_mpi_datatype< T > : mpl::true_ {}; \
0372 }} \
0373
0374
0375
0376 #endif