datatype.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright 2004 The Trustees of Indiana University.
  2. // Copyright 2005 Matthias Troyer.
  3. // Copyright 2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // Authors: Douglas Gregor
  8. // Andrew Lumsdaine
  9. // Matthias Troyer
  10. /** @file datatype.hpp
  11. *
  12. * This header provides the mapping from C++ types to MPI data types.
  13. */
  14. #ifndef BOOST_MPI_DATATYPE_HPP
  15. #define BOOST_MPI_DATATYPE_HPP
  16. #include <boost/mpi/config.hpp>
  17. #include <boost/mpi/datatype_fwd.hpp>
  18. #include <mpi.h>
  19. #include <boost/config.hpp>
  20. #include <boost/mpl/bool.hpp>
  21. #include <boost/mpl/or.hpp>
  22. #include <boost/mpl/and.hpp>
  23. #include <boost/mpi/detail/mpi_datatype_cache.hpp>
  24. #include <boost/mpl/assert.hpp>
  25. #include <boost/archive/basic_archive.hpp>
  26. #include <boost/serialization/item_version_type.hpp>
  27. #include <utility> // for std::pair
  28. #if defined(__cplusplus) && (201103L <= __cplusplus)
  29. #include <array>
  30. #endif
  31. namespace boost { namespace mpi {
  32. /**
  33. * @brief Type trait that determines if there exists a built-in
  34. * integer MPI data type for a given C++ type.
  35. *
  36. * This type trait determines when there is a direct mapping from a
  37. * C++ type to an MPI data type that is classified as an integer data
  38. * type. See @c is_mpi_builtin_datatype for general information about
  39. * built-in MPI data types.
  40. */
  41. template<typename T>
  42. struct is_mpi_integer_datatype
  43. : public boost::mpl::false_ { };
  44. /**
  45. * @brief Type trait that determines if there exists a built-in
  46. * floating point MPI data type for a given C++ type.
  47. *
  48. * This type trait determines when there is a direct mapping from a
  49. * C++ type to an MPI data type that is classified as a floating
  50. * point data type. See @c is_mpi_builtin_datatype for general
  51. * information about built-in MPI data types.
  52. */
  53. template<typename T>
  54. struct is_mpi_floating_point_datatype
  55. : public boost::mpl::false_ { };
  56. /**
  57. * @brief Type trait that determines if there exists a built-in
  58. * logical MPI data type for a given C++ type.
  59. *
  60. * This type trait determines when there is a direct mapping from a
  61. * C++ type to an MPI data type that is classified as an logical data
  62. * type. See @c is_mpi_builtin_datatype for general information about
  63. * built-in MPI data types.
  64. */
  65. template<typename T>
  66. struct is_mpi_logical_datatype
  67. : public boost::mpl::false_ { };
  68. /**
  69. * @brief Type trait that determines if there exists a built-in
  70. * complex MPI data type for a given C++ type.
  71. *
  72. * This type trait determines when there is a direct mapping from a
  73. * C++ type to an MPI data type that is classified as an complex data
  74. * type. See @c is_mpi_builtin_datatype for general information about
  75. * built-in MPI data types.
  76. */
  77. template<typename T>
  78. struct is_mpi_complex_datatype
  79. : public boost::mpl::false_ { };
  80. /**
  81. * @brief Type trait that determines if there exists a built-in
  82. * byte MPI data type for a given C++ type.
  83. *
  84. * This type trait determines when there is a direct mapping from a
  85. * C++ type to an MPI data type that is classified as an byte data
  86. * type. See @c is_mpi_builtin_datatype for general information about
  87. * built-in MPI data types.
  88. */
  89. template<typename T>
  90. struct is_mpi_byte_datatype
  91. : public boost::mpl::false_ { };
  92. /** @brief Type trait that determines if there exists a built-in MPI
  93. * data type for a given C++ type.
  94. *
  95. * This type trait determines when there is a direct mapping from a
  96. * C++ type to an MPI type. For instance, the C++ @c int type maps
  97. * directly to the MPI type @c MPI_INT. When there is a direct
  98. * mapping from the type @c T to an MPI type, @c
  99. * is_mpi_builtin_datatype will derive from @c mpl::true_ and the MPI
  100. * data type will be accessible via @c get_mpi_datatype.
  101. *
  102. * In general, users should not need to specialize this
  103. * trait. However, if you have an additional C++ type that can map
  104. * directly to only of MPI's built-in types, specialize either this
  105. * trait or one of the traits corresponding to categories of MPI data
  106. * types (@c is_mpi_integer_datatype, @c
  107. * is_mpi_floating_point_datatype, @c is_mpi_logical_datatype, @c
  108. * is_mpi_complex_datatype, or @c is_mpi_builtin_datatype). @c
  109. * is_mpi_builtin_datatype derives @c mpl::true_ if any of the traits
  110. * corresponding to MPI data type categories derived @c mpl::true_.
  111. */
  112. template<typename T>
  113. struct is_mpi_builtin_datatype
  114. : boost::mpl::or_<is_mpi_integer_datatype<T>,
  115. is_mpi_floating_point_datatype<T>,
  116. is_mpi_logical_datatype<T>,
  117. is_mpi_complex_datatype<T>,
  118. is_mpi_byte_datatype<T> >
  119. {
  120. };
  121. /** @brief Type trait that determines if a C++ type can be mapped to
  122. * an MPI data type.
  123. *
  124. * This type trait determines if it is possible to build an MPI data
  125. * type that represents a C++ data type. When this is the case, @c
  126. * is_mpi_datatype derives @c mpl::true_ and the MPI data type will
  127. * be accessible via @c get_mpi_datatype.
  128. * For any C++ type that maps to a built-in MPI data type (see @c
  129. * is_mpi_builtin_datatype), @c is_mpi_data_type is trivially
  130. * true. However, any POD ("Plain Old Data") type containing types
  131. * that themselves can be represented by MPI data types can itself be
  132. * represented as an MPI data type. For instance, a @c point3d class
  133. * containing three @c double values can be represented as an MPI
  134. * data type. To do so, first make the data type Serializable (using
  135. * the Boost.Serialization library); then, specialize the @c
  136. * is_mpi_datatype trait for the point type so that it will derive @c
  137. * mpl::true_:
  138. *
  139. * @code
  140. * namespace boost { namespace mpi {
  141. * template<> struct is_mpi_datatype<point>
  142. * : public mpl::true_ { };
  143. * } }
  144. * @endcode
  145. */
  146. template<typename T>
  147. struct is_mpi_datatype
  148. : public is_mpi_builtin_datatype<T>
  149. {
  150. };
  151. /** @brief Returns an MPI data type for a C++ type.
  152. *
  153. * The function creates an MPI data type for the given object @c
  154. * x. The first time it is called for a class @c T, the MPI data type
  155. * is created and cached. Subsequent calls for objects of the same
  156. * type @c T return the cached MPI data type. The type @c T must
  157. * allow creation of an MPI data type. That is, it must be
  158. * Serializable and @c is_mpi_datatype<T> must derive @c mpl::true_.
  159. *
  160. * For fundamental MPI types, a copy of the MPI data type of the MPI
  161. * library is returned.
  162. *
  163. * Note that since the data types are cached, the caller should never
  164. * call @c MPI_Type_free() for the MPI data type returned by this
  165. * call.
  166. *
  167. * @param x for an optimized call, a constructed object of the type
  168. * should be passed; otherwise, an object will be
  169. * default-constructed.
  170. *
  171. * @returns The MPI data type corresponding to type @c T.
  172. */
  173. template<typename T> MPI_Datatype get_mpi_datatype(const T& x)
  174. {
  175. BOOST_MPL_ASSERT((is_mpi_datatype<T>));
  176. return detail::mpi_datatype_cache().datatype(x);
  177. }
  178. // Don't parse this part when we're generating Doxygen documentation.
  179. #ifndef BOOST_MPI_DOXYGEN
  180. /// INTERNAL ONLY
  181. #define BOOST_MPI_DATATYPE(CppType, MPIType, Kind) \
  182. template<> \
  183. inline MPI_Datatype \
  184. get_mpi_datatype< CppType >(const CppType&) { return MPIType; } \
  185. \
  186. template<> \
  187. struct BOOST_JOIN(is_mpi_,BOOST_JOIN(Kind,_datatype))< CppType > \
  188. : boost::mpl::true_ \
  189. {}
  190. /// INTERNAL ONLY
  191. BOOST_MPI_DATATYPE(packed, MPI_PACKED, builtin);
  192. /// INTERNAL ONLY
  193. BOOST_MPI_DATATYPE(char, MPI_CHAR, builtin);
  194. /// INTERNAL ONLY
  195. BOOST_MPI_DATATYPE(short, MPI_SHORT, integer);
  196. /// INTERNAL ONLY
  197. BOOST_MPI_DATATYPE(int, MPI_INT, integer);
  198. /// INTERNAL ONLY
  199. BOOST_MPI_DATATYPE(long, MPI_LONG, integer);
  200. /// INTERNAL ONLY
  201. BOOST_MPI_DATATYPE(float, MPI_FLOAT, floating_point);
  202. /// INTERNAL ONLY
  203. BOOST_MPI_DATATYPE(double, MPI_DOUBLE, floating_point);
  204. /// INTERNAL ONLY
  205. BOOST_MPI_DATATYPE(long double, MPI_LONG_DOUBLE, floating_point);
  206. /// INTERNAL ONLY
  207. BOOST_MPI_DATATYPE(unsigned char, MPI_UNSIGNED_CHAR, builtin);
  208. /// INTERNAL ONLY
  209. BOOST_MPI_DATATYPE(unsigned short, MPI_UNSIGNED_SHORT, integer);
  210. /// INTERNAL ONLY
  211. BOOST_MPI_DATATYPE(unsigned, MPI_UNSIGNED, integer);
  212. /// INTERNAL ONLY
  213. BOOST_MPI_DATATYPE(unsigned long, MPI_UNSIGNED_LONG, integer);
  214. /// INTERNAL ONLY
  215. #define BOOST_MPI_LIST2(A, B) A, B
  216. /// INTERNAL ONLY
  217. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(float, int)>, MPI_FLOAT_INT,
  218. builtin);
  219. /// INTERNAL ONLY
  220. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(double, int)>, MPI_DOUBLE_INT,
  221. builtin);
  222. /// INTERNAL ONLY
  223. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long double, int)>,
  224. MPI_LONG_DOUBLE_INT, builtin);
  225. /// INTERNAL ONLY
  226. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(long, int>), MPI_LONG_INT,
  227. builtin);
  228. /// INTERNAL ONLY
  229. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(short, int>), MPI_SHORT_INT,
  230. builtin);
  231. /// INTERNAL ONLY
  232. BOOST_MPI_DATATYPE(std::pair<BOOST_MPI_LIST2(int, int>), MPI_2INT, builtin);
  233. #undef BOOST_MPI_LIST2
  234. /// specialization of is_mpi_datatype for pairs
  235. template <class T, class U>
  236. struct is_mpi_datatype<std::pair<T,U> >
  237. : public mpl::and_<is_mpi_datatype<T>,is_mpi_datatype<U> >
  238. {
  239. };
  240. /// specialization of is_mpi_datatype for arrays
  241. #if defined(__cplusplus) && (201103L <= __cplusplus)
  242. template<class T, std::size_t N>
  243. struct is_mpi_datatype<std::array<T, N> >
  244. : public is_mpi_datatype<T>
  245. {
  246. };
  247. #endif
  248. // Define wchar_t specialization of is_mpi_datatype, if possible.
  249. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) && \
  250. (defined(MPI_WCHAR) || (BOOST_MPI_VERSION >= 2))
  251. BOOST_MPI_DATATYPE(wchar_t, MPI_WCHAR, builtin);
  252. #endif
  253. // Define long long or __int64 specialization of is_mpi_datatype, if possible.
  254. #if defined(BOOST_HAS_LONG_LONG) && \
  255. (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
  256. BOOST_MPI_DATATYPE(long long, MPI_LONG_LONG_INT, builtin);
  257. #elif defined(BOOST_HAS_MS_INT64) && \
  258. (defined(MPI_LONG_LONG_INT) || (BOOST_MPI_VERSION >= 2))
  259. BOOST_MPI_DATATYPE(__int64, MPI_LONG_LONG_INT, builtin);
  260. #endif
  261. // Define unsigned long long or unsigned __int64 specialization of
  262. // is_mpi_datatype, if possible. We separate this from the check for
  263. // the (signed) long long/__int64 because some MPI implementations
  264. // (e.g., MPICH-MX) have MPI_LONG_LONG_INT but not
  265. // MPI_UNSIGNED_LONG_LONG.
  266. #if defined(BOOST_HAS_LONG_LONG) && \
  267. (defined(MPI_UNSIGNED_LONG_LONG) \
  268. || (BOOST_MPI_VERSION >= 2))
  269. BOOST_MPI_DATATYPE(unsigned long long, MPI_UNSIGNED_LONG_LONG, builtin);
  270. #elif defined(BOOST_HAS_MS_INT64) && \
  271. (defined(MPI_UNSIGNED_LONG_LONG) \
  272. || (BOOST_MPI_VERSION >= 2))
  273. BOOST_MPI_DATATYPE(unsigned __int64, MPI_UNSIGNED_LONG_LONG, builtin);
  274. #endif
  275. // Define signed char specialization of is_mpi_datatype, if possible.
  276. #if defined(MPI_SIGNED_CHAR) || (BOOST_MPI_VERSION >= 2)
  277. BOOST_MPI_DATATYPE(signed char, MPI_SIGNED_CHAR, builtin);
  278. #endif
  279. #endif // Doxygen
  280. namespace detail {
  281. inline MPI_Datatype build_mpi_datatype_for_bool()
  282. {
  283. MPI_Datatype type;
  284. MPI_Type_contiguous(sizeof(bool), MPI_BYTE, &type);
  285. MPI_Type_commit(&type);
  286. return type;
  287. }
  288. }
  289. /// Support for bool. There is no corresponding MPI_BOOL.
  290. /// INTERNAL ONLY
  291. template<>
  292. inline MPI_Datatype get_mpi_datatype<bool>(const bool&)
  293. {
  294. static MPI_Datatype type = detail::build_mpi_datatype_for_bool();
  295. return type;
  296. }
  297. /// INTERNAL ONLY
  298. template<>
  299. struct is_mpi_datatype<bool>
  300. : boost::mpl::bool_<true>
  301. {};
  302. #ifndef BOOST_MPI_DOXYGEN
  303. // direct support for special primitive data types of the serialization library
  304. BOOST_MPI_DATATYPE(boost::archive::library_version_type, get_mpi_datatype(uint_least16_t()), integer);
  305. BOOST_MPI_DATATYPE(boost::archive::version_type, get_mpi_datatype(uint_least8_t()), integer);
  306. BOOST_MPI_DATATYPE(boost::archive::class_id_type, get_mpi_datatype(int_least16_t()), integer);
  307. BOOST_MPI_DATATYPE(boost::archive::class_id_reference_type, get_mpi_datatype(int_least16_t()), integer);
  308. BOOST_MPI_DATATYPE(boost::archive::class_id_optional_type, get_mpi_datatype(int_least16_t()), integer);
  309. BOOST_MPI_DATATYPE(boost::archive::object_id_type, get_mpi_datatype(uint_least32_t()), integer);
  310. BOOST_MPI_DATATYPE(boost::archive::object_reference_type, get_mpi_datatype(uint_least32_t()), integer);
  311. BOOST_MPI_DATATYPE(boost::archive::tracking_type, get_mpi_datatype(bool()), builtin);
  312. BOOST_MPI_DATATYPE(boost::serialization::collection_size_type, get_mpi_datatype(std::size_t()), integer);
  313. BOOST_MPI_DATATYPE(boost::serialization::item_version_type, get_mpi_datatype(uint_least8_t()), integer);
  314. #endif // Doxygen
  315. } } // end namespace boost::mpi
  316. // direct support for special primitive data types of the serialization library
  317. // in the case of homogeneous systems
  318. // define a macro to make explicit designation of this more transparent
  319. #define BOOST_IS_MPI_DATATYPE(T) \
  320. namespace boost { \
  321. namespace mpi { \
  322. template<> \
  323. struct is_mpi_datatype< T > : mpl::true_ {}; \
  324. }} \
  325. /**/
  326. #endif // BOOST_MPI_MPI_DATATYPE_HPP