scan.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor@gmail.com>.
  2. // Copyright (C) 2004 The Trustees of Indiana University
  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. // Message Passing Interface 1.1 -- Section 4.9.1. Scan
  9. #ifndef BOOST_MPI_SCAN_HPP
  10. #define BOOST_MPI_SCAN_HPP
  11. #include <boost/mpi/exception.hpp>
  12. #include <boost/mpi/datatype.hpp>
  13. // For (de-)serializing sends and receives
  14. #include <boost/mpi/packed_oarchive.hpp>
  15. #include <boost/mpi/packed_iarchive.hpp>
  16. // For packed_[io]archive sends and receives
  17. #include <boost/mpi/detail/point_to_point.hpp>
  18. #include <boost/mpi/communicator.hpp>
  19. #include <boost/mpi/environment.hpp>
  20. #include <boost/mpi/detail/computation_tree.hpp>
  21. #include <boost/mpi/operations.hpp>
  22. #include <algorithm>
  23. #include <exception>
  24. #include <boost/assert.hpp>
  25. namespace boost { namespace mpi {
  26. /************************************************************************
  27. * Implementation details *
  28. ************************************************************************/
  29. namespace detail {
  30. /**********************************************************************
  31. * Simple prefix reduction with MPI_Scan *
  32. **********************************************************************/
  33. // We are performing prefix reduction for a type that has an
  34. // associated MPI datatype and operation, so we'll use MPI_Scan
  35. // directly.
  36. template<typename T, typename Op>
  37. void
  38. scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
  39. Op /*op*/, mpl::true_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
  40. {
  41. BOOST_MPI_CHECK_RESULT(MPI_Scan,
  42. (const_cast<T*>(in_values), out_values, n,
  43. boost::mpi::get_mpi_datatype<T>(*in_values),
  44. (is_mpi_op<Op, T>::op()), comm));
  45. }
  46. /**********************************************************************
  47. * User-defined prefix reduction with MPI_Scan *
  48. **********************************************************************/
  49. // We are performing prefix reduction for a type that has an
  50. // associated MPI datatype but with a custom operation. We'll use
  51. // MPI_Scan directly, but we'll need to create an MPI_Op manually.
  52. template<typename T, typename Op>
  53. void
  54. scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
  55. Op op, mpl::false_ /*is_mpi_op*/, mpl::true_ /*is_mpi_datatype*/)
  56. {
  57. user_op<Op, T> mpi_op;
  58. BOOST_MPI_CHECK_RESULT(MPI_Scan,
  59. (const_cast<T*>(in_values), out_values, n,
  60. boost::mpi::get_mpi_datatype<T>(*in_values),
  61. mpi_op.get_mpi_op(), comm));
  62. }
  63. /**********************************************************************
  64. * User-defined, tree-based reduction for non-MPI data types *
  65. **********************************************************************/
  66. template<typename T, typename Op>
  67. void
  68. upper_lower_scan(const communicator& comm, const T* in_values, int n,
  69. T* out_values, Op& op, int lower, int upper)
  70. {
  71. int tag = environment::collectives_tag();
  72. int rank = comm.rank();
  73. if (lower + 1 == upper) {
  74. std::copy(in_values, in_values + n, out_values);
  75. } else {
  76. int middle = (lower + upper) / 2;
  77. if (rank < middle) {
  78. // Lower half
  79. upper_lower_scan(comm, in_values, n, out_values, op, lower, middle);
  80. // If we're the last process in the lower half, send our values
  81. // to everyone in the upper half.
  82. if (rank == middle - 1) {
  83. packed_oarchive oa(comm);
  84. for (int i = 0; i < n; ++i)
  85. oa << out_values[i];
  86. for (int p = middle; p < upper; ++p)
  87. comm.send(p, tag, oa);
  88. }
  89. } else {
  90. // Upper half
  91. upper_lower_scan(comm, in_values, n, out_values, op, middle, upper);
  92. // Receive value from the last process in the lower half.
  93. packed_iarchive ia(comm);
  94. comm.recv(middle - 1, tag, ia);
  95. // Combine value that came from the left with our value
  96. T left_value;
  97. for (int i = 0; i < n; ++i)
  98. {
  99. ia >> left_value;
  100. out_values[i] = op(left_value, out_values[i]);
  101. }
  102. }
  103. }
  104. }
  105. // We are performing prefix reduction for a type that has no
  106. // associated MPI datatype and operation, so we'll use a simple
  107. // upper/lower algorithm.
  108. template<typename T, typename Op>
  109. inline void
  110. scan_impl(const communicator& comm, const T* in_values, int n, T* out_values,
  111. Op op, mpl::false_ /*is_mpi_op*/, mpl::false_/*is_mpi_datatype*/)
  112. {
  113. upper_lower_scan(comm, in_values, n, out_values, op, 0, comm.size());
  114. }
  115. } // end namespace detail
  116. template<typename T, typename Op>
  117. inline void
  118. scan(const communicator& comm, const T& in_value, T& out_value, Op op)
  119. {
  120. detail::scan_impl(comm, &in_value, 1, &out_value, op,
  121. is_mpi_op<Op, T>(), is_mpi_datatype<T>());
  122. }
  123. template<typename T, typename Op>
  124. inline void
  125. scan(const communicator& comm, const T* in_values, int n, T* out_values, Op op)
  126. {
  127. detail::scan_impl(comm, in_values, n, out_values, op,
  128. is_mpi_op<Op, T>(), is_mpi_datatype<T>());
  129. }
  130. template<typename T, typename Op>
  131. inline T
  132. scan(const communicator& comm, const T& in_value, Op op)
  133. {
  134. T out_value;
  135. detail::scan_impl(comm, &in_value, 1, &out_value, op,
  136. is_mpi_op<Op, T>(), is_mpi_datatype<T>());
  137. return out_value;
  138. }
  139. } } // end namespace boost::mpi
  140. #endif // BOOST_MPI_SCAN_HPP