scatterv.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2011 Júlio Hoffimann.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Message Passing Interface 1.1 -- Section 4.6. Scatterv
  6. #ifndef BOOST_MPI_SCATTERV_HPP
  7. #define BOOST_MPI_SCATTERV_HPP
  8. #include <boost/scoped_array.hpp>
  9. #include <boost/mpi/collectives/scatter.hpp>
  10. #include <boost/mpi/detail/offsets.hpp>
  11. #include <boost/mpi/detail/antiques.hpp>
  12. namespace boost { namespace mpi {
  13. namespace detail {
  14. //////////////////////////////////////////////
  15. /// Implementation for MPI primitive types ///
  16. //////////////////////////////////////////////
  17. // We're scattering from the root for a type that has an associated MPI
  18. // datatype, so we'll use MPI_Scatterv to do all of the work.
  19. template<typename T>
  20. void
  21. scatterv_impl(const communicator& comm, const T* in_values, T* out_values, int out_size,
  22. const int* sizes, const int* displs, int root, mpl::true_)
  23. {
  24. assert(!sizes || out_size == sizes[comm.rank()]);
  25. assert(bool(sizes) == bool(in_values));
  26. scoped_array<int> new_offsets_mem(make_offsets(comm, sizes, displs, root));
  27. if (new_offsets_mem) displs = new_offsets_mem.get();
  28. MPI_Datatype type = get_mpi_datatype<T>(*in_values);
  29. BOOST_MPI_CHECK_RESULT(MPI_Scatterv,
  30. (const_cast<T*>(in_values), const_cast<int*>(sizes),
  31. const_cast<int*>(displs), type,
  32. out_values, out_size, type, root, comm));
  33. }
  34. // We're scattering from a non-root for a type that has an associated MPI
  35. // datatype, so we'll use MPI_Scatterv to do all of the work.
  36. template<typename T>
  37. void
  38. scatterv_impl(const communicator& comm, T* out_values, int out_size, int root,
  39. mpl::true_ is_mpi_type)
  40. {
  41. scatterv_impl(comm, (T const*)0, out_values, out_size,
  42. (const int*)0, (const int*)0, root, is_mpi_type);
  43. }
  44. //////////////////////////////////////////////////
  45. /// Implementation for non MPI primitive types ///
  46. //////////////////////////////////////////////////
  47. // We're scattering from the root for a type that does not have an
  48. // associated MPI datatype, so we'll need to serialize it.
  49. template<typename T>
  50. void
  51. scatterv_impl(const communicator& comm, const T* in_values, T* out_values, int out_size,
  52. int const* sizes, int const* displs, int root, mpl::false_)
  53. {
  54. packed_oarchive::buffer_type sendbuf;
  55. bool is_root = comm.rank() == root;
  56. int nproc = comm.size();
  57. std::vector<int> archsizes;
  58. if (is_root) {
  59. assert(out_size == sizes[comm.rank()]);
  60. archsizes.resize(nproc);
  61. std::vector<int> skipped;
  62. if (displs) {
  63. skipped.resize(nproc);
  64. offsets2skipped(sizes, displs, c_data(skipped), nproc);
  65. displs = c_data(skipped);
  66. }
  67. fill_scatter_sendbuf(comm, in_values, sizes, (int const*)0, sendbuf, archsizes);
  68. }
  69. dispatch_scatter_sendbuf(comm, sendbuf, archsizes, (T const*)0, out_values, out_size, root);
  70. }
  71. // We're scattering to a non-root for a type that does not have an
  72. // associated MPI datatype. input data not needed.
  73. // it.
  74. template<typename T>
  75. void
  76. scatterv_impl(const communicator& comm, T* out_values, int n, int root,
  77. mpl::false_ isnt_mpi_type)
  78. {
  79. assert(root != comm.rank());
  80. scatterv_impl(comm, (T const*)0, out_values, n, (int const*)0, (int const*)0, root, isnt_mpi_type);
  81. }
  82. } // end namespace detail
  83. template<typename T>
  84. void
  85. scatterv(const communicator& comm, const T* in_values,
  86. const std::vector<int>& sizes, const std::vector<int>& displs,
  87. T* out_values, int out_size, int root)
  88. {
  89. using detail::c_data;
  90. detail::scatterv_impl(comm, in_values, out_values, out_size, c_data(sizes), c_data(displs),
  91. root, is_mpi_datatype<T>());
  92. }
  93. template<typename T>
  94. void
  95. scatterv(const communicator& comm, const std::vector<T>& in_values,
  96. const std::vector<int>& sizes, const std::vector<int>& displs,
  97. T* out_values, int out_size, int root)
  98. {
  99. using detail::c_data;
  100. ::boost::mpi::scatterv(comm, c_data(in_values), sizes, displs,
  101. out_values, out_size, root);
  102. }
  103. template<typename T>
  104. void scatterv(const communicator& comm, T* out_values, int out_size, int root)
  105. {
  106. BOOST_ASSERT(comm.rank() != root);
  107. detail::scatterv_impl(comm, out_values, out_size, root, is_mpi_datatype<T>());
  108. }
  109. ///////////////////////
  110. // common use versions
  111. ///////////////////////
  112. template<typename T>
  113. void
  114. scatterv(const communicator& comm, const T* in_values,
  115. const std::vector<int>& sizes, T* out_values, int root)
  116. {
  117. using detail::c_data;
  118. detail::scatterv_impl(comm, in_values, out_values, sizes[comm.rank()],
  119. c_data(sizes), (int const*)0,
  120. root, is_mpi_datatype<T>());
  121. }
  122. template<typename T>
  123. void
  124. scatterv(const communicator& comm, const std::vector<T>& in_values,
  125. const std::vector<int>& sizes, T* out_values, int root)
  126. {
  127. ::boost::mpi::scatterv(comm, detail::c_data(in_values), sizes, out_values, root);
  128. }
  129. template<typename T>
  130. void
  131. scatterv(const communicator& comm, const T* in_values,
  132. T* out_values, int n, int root)
  133. {
  134. detail::scatterv_impl(comm, in_values, out_values, n, (int const*)0, (int const*)0,
  135. root, is_mpi_datatype<T>());
  136. }
  137. template<typename T>
  138. void
  139. scatterv(const communicator& comm, const std::vector<T>& in_values,
  140. T* out_values, int out_size, int root)
  141. {
  142. ::boost::mpi::scatterv(comm, detail::c_data(in_values), out_values, out_size, root);
  143. }
  144. } } // end namespace boost::mpi
  145. #endif // BOOST_MPI_SCATTERV_HPP