datatype.hpp 13 KB

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