gatherv.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.5. Gatherv
  6. #ifndef BOOST_MPI_GATHERV_HPP
  7. #define BOOST_MPI_GATHERV_HPP
  8. #include <vector>
  9. #include <boost/mpi/exception.hpp>
  10. #include <boost/mpi/datatype.hpp>
  11. #include <boost/mpi/packed_oarchive.hpp>
  12. #include <boost/mpi/packed_iarchive.hpp>
  13. #include <boost/mpi/detail/point_to_point.hpp>
  14. #include <boost/mpi/communicator.hpp>
  15. #include <boost/mpi/environment.hpp>
  16. #include <boost/mpi/detail/offsets.hpp>
  17. #include <boost/assert.hpp>
  18. #include <boost/scoped_array.hpp>
  19. namespace boost { namespace mpi {
  20. namespace detail {
  21. // We're gathering at the root for a type that has an associated MPI
  22. // datatype, so we'll use MPI_Gatherv to do all of the work.
  23. template<typename T>
  24. void
  25. gatherv_impl(const communicator& comm, const T* in_values, int in_size,
  26. T* out_values, const int* sizes, const int* displs, int root, mpl::true_)
  27. {
  28. MPI_Datatype type = get_mpi_datatype<T>(*in_values);
  29. BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
  30. (const_cast<T*>(in_values), in_size, type,
  31. out_values, const_cast<int*>(sizes), const_cast<int*>(displs),
  32. type, root, comm));
  33. }
  34. // We're gathering from a non-root for a type that has an associated MPI
  35. // datatype, so we'll use MPI_Gatherv to do all of the work.
  36. template<typename T>
  37. void
  38. gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
  39. mpl::true_)
  40. {
  41. MPI_Datatype type = get_mpi_datatype<T>(*in_values);
  42. BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
  43. (const_cast<T*>(in_values), in_size, type,
  44. 0, 0, 0, type, root, comm));
  45. }
  46. // We're gathering at the root for a type that does not have an
  47. // associated MPI datatype, so we'll need to serialize
  48. // it. Unfortunately, this means that we cannot use MPI_Gatherv, so
  49. // we'll just have all of the non-root nodes send individual
  50. // messages to the root.
  51. template<typename T>
  52. void
  53. gatherv_impl(const communicator& comm, const T* in_values, int in_size,
  54. T* out_values, const int* sizes, const int* displs, int root, mpl::false_)
  55. {
  56. // convert displacement to offsets to skip
  57. scoped_array<int> skipped(make_skipped_slots(comm, sizes, displs, root));
  58. gather_impl(comm, in_values, in_size, out_values, sizes, skipped.get(), root, mpl::false_());
  59. }
  60. // We're gathering at a non-root for a type that does not have an
  61. // associated MPI datatype, so we'll need to serialize
  62. // it.
  63. template<typename T>
  64. void
  65. gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
  66. mpl::false_)
  67. {
  68. gather_impl(comm, in_values, in_size, (T*)0,(int const*)0,(int const*)0, root,
  69. mpl::false_());
  70. }
  71. } // end namespace detail
  72. template<typename T>
  73. void
  74. gatherv(const communicator& comm, const T* in_values, int in_size,
  75. T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
  76. int root)
  77. {
  78. if (comm.rank() == root)
  79. detail::gatherv_impl(comm, in_values, in_size,
  80. out_values, &sizes[0], &displs[0],
  81. root, is_mpi_datatype<T>());
  82. else
  83. detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
  84. }
  85. template<typename T>
  86. void
  87. gatherv(const communicator& comm, const std::vector<T>& in_values,
  88. T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
  89. int root)
  90. {
  91. ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
  92. }
  93. template<typename T>
  94. void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
  95. {
  96. BOOST_ASSERT(comm.rank() != root);
  97. detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
  98. }
  99. template<typename T>
  100. void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
  101. {
  102. BOOST_ASSERT(comm.rank() != root);
  103. detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
  104. }
  105. ///////////////////////
  106. // common use versions
  107. ///////////////////////
  108. template<typename T>
  109. void
  110. gatherv(const communicator& comm, const T* in_values, int in_size,
  111. T* out_values, const std::vector<int>& sizes, int root)
  112. {
  113. int nprocs = comm.size();
  114. std::vector<int> displs( nprocs );
  115. for (int rank = 0, aux = 0; rank < nprocs; ++rank) {
  116. displs[rank] = aux;
  117. aux += sizes[rank];
  118. }
  119. ::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
  120. }
  121. template<typename T>
  122. void
  123. gatherv(const communicator& comm, const std::vector<T>& in_values,
  124. T* out_values, const std::vector<int>& sizes, int root)
  125. {
  126. ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, root);
  127. }
  128. } } // end namespace boost::mpi
  129. #endif // BOOST_MPI_GATHERV_HPP