operations.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Copyright (C) 2004 The Trustees of Indiana University.
  2. // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>
  3. // Use, modification and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // Authors: Douglas Gregor
  7. // Andrew Lumsdaine
  8. /** @file operations.hpp
  9. *
  10. * This header provides a mapping from function objects to @c MPI_Op
  11. * constants used in MPI collective operations. It also provides
  12. * several new function object types not present in the standard @c
  13. * <functional> header that have direct mappings to @c MPI_Op.
  14. */
  15. #ifndef BOOST_MPI_IS_MPI_OP_HPP
  16. #define BOOST_MPI_IS_MPI_OP_HPP
  17. #include <boost/mpi/config.hpp>
  18. #include <boost/mpl/bool.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/mpl/and.hpp>
  21. #include <boost/mpi/datatype.hpp>
  22. #include <boost/core/enable_if.hpp>
  23. #include <boost/core/uncaught_exceptions.hpp>
  24. #include <functional>
  25. namespace boost { namespace mpi {
  26. template<typename Op, typename T> struct is_mpi_op;
  27. /**
  28. * @brief Determine if a function object type is commutative.
  29. *
  30. * This trait determines if an operation @c Op is commutative when
  31. * applied to values of type @c T. Parallel operations such as @c
  32. * reduce and @c prefix_sum can be implemented more efficiently with
  33. * commutative operations. To mark an operation as commutative, users
  34. * should specialize @c is_commutative and derive from the class @c
  35. * mpl::true_.
  36. */
  37. template<typename Op, typename T>
  38. struct is_commutative : public mpl::false_ { };
  39. /**************************************************************************
  40. * Function objects for MPI operations not in <functional> header *
  41. **************************************************************************/
  42. /**
  43. * @brief Compute the maximum of two values.
  44. *
  45. * This binary function object computes the maximum of the two values
  46. * it is given. When used with MPI and a type @c T that has an
  47. * associated, built-in MPI data type, translates to @c MPI_MAX.
  48. */
  49. template<typename T>
  50. struct maximum
  51. {
  52. typedef T first_argument_type;
  53. typedef T second_argument_type;
  54. typedef T result_type;
  55. /** @returns the maximum of x and y. */
  56. const T& operator()(const T& x, const T& y) const
  57. {
  58. return x < y? y : x;
  59. }
  60. };
  61. /**
  62. * @brief Compute the minimum of two values.
  63. *
  64. * This binary function object computes the minimum of the two values
  65. * it is given. When used with MPI and a type @c T that has an
  66. * associated, built-in MPI data type, translates to @c MPI_MIN.
  67. */
  68. template<typename T>
  69. struct minimum
  70. {
  71. typedef T first_argument_type;
  72. typedef T second_argument_type;
  73. typedef T result_type;
  74. /** @returns the minimum of x and y. */
  75. const T& operator()(const T& x, const T& y) const
  76. {
  77. return x < y? x : y;
  78. }
  79. };
  80. /**
  81. * @brief Compute the bitwise AND of two integral values.
  82. *
  83. * This binary function object computes the bitwise AND of the two
  84. * values it is given. When used with MPI and a type @c T that has an
  85. * associated, built-in MPI data type, translates to @c MPI_BAND.
  86. */
  87. template<typename T>
  88. struct bitwise_and
  89. {
  90. typedef T first_argument_type;
  91. typedef T second_argument_type;
  92. typedef T result_type;
  93. /** @returns @c x & y. */
  94. T operator()(const T& x, const T& y) const
  95. {
  96. return x & y;
  97. }
  98. };
  99. /**
  100. * @brief Compute the bitwise OR of two integral values.
  101. *
  102. * This binary function object computes the bitwise OR of the two
  103. * values it is given. When used with MPI and a type @c T that has an
  104. * associated, built-in MPI data type, translates to @c MPI_BOR.
  105. */
  106. template<typename T>
  107. struct bitwise_or
  108. {
  109. typedef T first_argument_type;
  110. typedef T second_argument_type;
  111. typedef T result_type;
  112. /** @returns the @c x | y. */
  113. T operator()(const T& x, const T& y) const
  114. {
  115. return x | y;
  116. }
  117. };
  118. /**
  119. * @brief Compute the logical exclusive OR of two integral values.
  120. *
  121. * This binary function object computes the logical exclusive of the
  122. * two values it is given. When used with MPI and a type @c T that has
  123. * an associated, built-in MPI data type, translates to @c MPI_LXOR.
  124. */
  125. template<typename T>
  126. struct logical_xor
  127. {
  128. typedef T first_argument_type;
  129. typedef T second_argument_type;
  130. typedef T result_type;
  131. /** @returns the logical exclusive OR of x and y. */
  132. T operator()(const T& x, const T& y) const
  133. {
  134. return (x || y) && !(x && y);
  135. }
  136. };
  137. /**
  138. * @brief Compute the bitwise exclusive OR of two integral values.
  139. *
  140. * This binary function object computes the bitwise exclusive OR of
  141. * the two values it is given. When used with MPI and a type @c T that
  142. * has an associated, built-in MPI data type, translates to @c
  143. * MPI_BXOR.
  144. */
  145. template<typename T>
  146. struct bitwise_xor
  147. {
  148. typedef T first_argument_type;
  149. typedef T second_argument_type;
  150. typedef T result_type;
  151. /** @returns @c x ^ y. */
  152. T operator()(const T& x, const T& y) const
  153. {
  154. return x ^ y;
  155. }
  156. };
  157. /**************************************************************************
  158. * MPI_Op queries *
  159. **************************************************************************/
  160. /**
  161. * @brief Determine if a function object has an associated @c MPI_Op.
  162. *
  163. * This trait determines if a function object type @c Op, when used
  164. * with argument type @c T, has an associated @c MPI_Op. If so, @c
  165. * is_mpi_op<Op,T> will derive from @c mpl::false_ and will
  166. * contain a static member function @c op that takes no arguments but
  167. * returns the associated @c MPI_Op value. For instance, @c
  168. * is_mpi_op<std::plus<int>,int>::op() returns @c MPI_SUM.
  169. *
  170. * Users may specialize @c is_mpi_op for any other class templates
  171. * that map onto operations that have @c MPI_Op equivalences, such as
  172. * bitwise OR, logical and, or maximum. However, users are encouraged
  173. * to use the standard function objects in the @c functional and @c
  174. * boost/mpi/operations.hpp headers whenever possible. For
  175. * function objects that are class templates with a single template
  176. * parameter, it may be easier to specialize @c is_builtin_mpi_op.
  177. */
  178. template<typename Op, typename T>
  179. struct is_mpi_op : public mpl::false_ { };
  180. /// INTERNAL ONLY
  181. template<typename T>
  182. struct is_mpi_op<maximum<T>, T>
  183. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  184. is_mpi_floating_point_datatype<T> >
  185. {
  186. static MPI_Op op() { return MPI_MAX; }
  187. };
  188. /// INTERNAL ONLY
  189. template<typename T>
  190. struct is_mpi_op<minimum<T>, T>
  191. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  192. is_mpi_floating_point_datatype<T> >
  193. {
  194. static MPI_Op op() { return MPI_MIN; }
  195. };
  196. /// INTERNAL ONLY
  197. template<typename T>
  198. struct is_mpi_op<std::plus<T>, T>
  199. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  200. is_mpi_floating_point_datatype<T>,
  201. is_mpi_complex_datatype<T> >
  202. {
  203. static MPI_Op op() { return MPI_SUM; }
  204. };
  205. /// INTERNAL ONLY
  206. template<typename T>
  207. struct is_mpi_op<std::multiplies<T>, T>
  208. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  209. is_mpi_floating_point_datatype<T>,
  210. is_mpi_complex_datatype<T> >
  211. {
  212. static MPI_Op op() { return MPI_PROD; }
  213. };
  214. /// INTERNAL ONLY
  215. template<typename T>
  216. struct is_mpi_op<std::logical_and<T>, T>
  217. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  218. is_mpi_logical_datatype<T> >
  219. {
  220. static MPI_Op op() { return MPI_LAND; }
  221. };
  222. /// INTERNAL ONLY
  223. template<typename T>
  224. struct is_mpi_op<std::logical_or<T>, T>
  225. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  226. is_mpi_logical_datatype<T> >
  227. {
  228. static MPI_Op op() { return MPI_LOR; }
  229. };
  230. /// INTERNAL ONLY
  231. template<typename T>
  232. struct is_mpi_op<logical_xor<T>, T>
  233. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  234. is_mpi_logical_datatype<T> >
  235. {
  236. static MPI_Op op() { return MPI_LXOR; }
  237. };
  238. /// INTERNAL ONLY
  239. template<typename T>
  240. struct is_mpi_op<bitwise_and<T>, T>
  241. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  242. is_mpi_byte_datatype<T> >
  243. {
  244. static MPI_Op op() { return MPI_BAND; }
  245. };
  246. /// INTERNAL ONLY
  247. template<typename T>
  248. struct is_mpi_op<bitwise_or<T>, T>
  249. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  250. is_mpi_byte_datatype<T> >
  251. {
  252. static MPI_Op op() { return MPI_BOR; }
  253. };
  254. /// INTERNAL ONLY
  255. template<typename T>
  256. struct is_mpi_op<bitwise_xor<T>, T>
  257. : public boost::mpl::or_<is_mpi_integer_datatype<T>,
  258. is_mpi_byte_datatype<T> >
  259. {
  260. static MPI_Op op() { return MPI_BXOR; }
  261. };
  262. namespace detail {
  263. // A helper class used to create user-defined MPI_Ops
  264. template<typename Op, typename T>
  265. class user_op
  266. {
  267. public:
  268. user_op()
  269. {
  270. BOOST_MPI_CHECK_RESULT(MPI_Op_create,
  271. (&user_op<Op, T>::perform,
  272. is_commutative<Op, T>::value,
  273. &mpi_op));
  274. }
  275. ~user_op()
  276. {
  277. if (boost::core::uncaught_exceptions() > 0) {
  278. // Ignore failure cases: there are obviously other problems
  279. // already, and we don't want to cause program termination if
  280. // MPI_Op_free fails.
  281. MPI_Op_free(&mpi_op);
  282. } else {
  283. BOOST_MPI_CHECK_RESULT(MPI_Op_free, (&mpi_op));
  284. }
  285. }
  286. MPI_Op& get_mpi_op()
  287. {
  288. return mpi_op;
  289. }
  290. private:
  291. MPI_Op mpi_op;
  292. static void BOOST_MPI_CALLING_CONVENTION perform(void* vinvec, void* voutvec, int* plen, MPI_Datatype*)
  293. {
  294. T* invec = static_cast<T*>(vinvec);
  295. T* outvec = static_cast<T*>(voutvec);
  296. Op op;
  297. std::transform(invec, invec + *plen, outvec, outvec, op);
  298. }
  299. };
  300. } // end namespace detail
  301. } } // end namespace boost::mpi
  302. #endif // BOOST_MPI_GET_MPI_OP_HPP