123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #ifndef BOOST_MPI_GATHERV_HPP
- #define BOOST_MPI_GATHERV_HPP
- #include <boost/mpi/exception.hpp>
- #include <boost/mpi/datatype.hpp>
- #include <vector>
- #include <boost/mpi/packed_oarchive.hpp>
- #include <boost/mpi/packed_iarchive.hpp>
- #include <boost/mpi/detail/point_to_point.hpp>
- #include <boost/mpi/communicator.hpp>
- #include <boost/mpi/environment.hpp>
- #include <boost/assert.hpp>
- namespace boost { namespace mpi {
- namespace detail {
-
-
- template<typename T>
- void
- gatherv_impl(const communicator& comm, const T* in_values, int in_size,
- T* out_values, const int* sizes, const int* displs, int root, mpl::true_)
- {
- MPI_Datatype type = get_mpi_datatype<T>(*in_values);
- BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
- (const_cast<T*>(in_values), in_size, type,
- out_values, const_cast<int*>(sizes), const_cast<int*>(displs),
- type, root, comm));
- }
-
-
- template<typename T>
- void
- gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
- mpl::true_)
- {
- MPI_Datatype type = get_mpi_datatype<T>(*in_values);
- BOOST_MPI_CHECK_RESULT(MPI_Gatherv,
- (const_cast<T*>(in_values), in_size, type,
- 0, 0, 0, type, root, comm));
- }
-
-
-
-
-
- template<typename T>
- void
- gatherv_impl(const communicator& comm, const T* in_values, int in_size,
- T* out_values, const int* sizes, const int* displs, int root, mpl::false_)
- {
- int tag = environment::collectives_tag();
- int nprocs = comm.size();
- for (int src = 0; src < nprocs; ++src) {
- if (src == root)
-
- std::copy(in_values, in_values + in_size, out_values + displs[src]);
- else {
-
- packed_iarchive ia(comm);
- MPI_Status status;
- detail::packed_archive_recv(comm, src, tag, ia, status);
- for (int i = 0; i < sizes[src]; ++i)
- ia >> out_values[ displs[src] + i ];
- }
- }
- }
-
-
-
-
-
- template<typename T>
- void
- gatherv_impl(const communicator& comm, const T* in_values, int in_size, int root,
- mpl::false_)
- {
- int tag = environment::collectives_tag();
- packed_oarchive oa(comm);
- for (int i = 0; i < in_size; ++i)
- oa << in_values[i];
- detail::packed_archive_send(comm, root, tag, oa);
- }
- }
- template<typename T>
- void
- gatherv(const communicator& comm, const T* in_values, int in_size,
- T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
- int root)
- {
- if (comm.rank() == root)
- detail::gatherv_impl(comm, in_values, in_size,
- out_values, &sizes[0], &displs[0],
- root, is_mpi_datatype<T>());
- else
- detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
- }
- template<typename T>
- void
- gatherv(const communicator& comm, const std::vector<T>& in_values,
- T* out_values, const std::vector<int>& sizes, const std::vector<int>& displs,
- int root)
- {
- ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, displs, root);
- }
- template<typename T>
- void gatherv(const communicator& comm, const T* in_values, int in_size, int root)
- {
- BOOST_ASSERT(comm.rank() != root);
- detail::gatherv_impl(comm, in_values, in_size, root, is_mpi_datatype<T>());
- }
- template<typename T>
- void gatherv(const communicator& comm, const std::vector<T>& in_values, int root)
- {
- BOOST_ASSERT(comm.rank() != root);
- detail::gatherv_impl(comm, &in_values[0], in_values.size(), root, is_mpi_datatype<T>());
- }
- template<typename T>
- void
- gatherv(const communicator& comm, const T* in_values, int in_size,
- T* out_values, const std::vector<int>& sizes, int root)
- {
- int nprocs = comm.size();
- std::vector<int> displs( nprocs );
- for (int rank = 0, aux = 0; rank < nprocs; ++rank) {
- displs[rank] = aux;
- aux += sizes[rank];
- }
- ::boost::mpi::gatherv(comm, in_values, in_size, out_values, sizes, displs, root);
- }
- template<typename T>
- void
- gatherv(const communicator& comm, const std::vector<T>& in_values,
- T* out_values, const std::vector<int>& sizes, int root)
- {
- ::boost::mpi::gatherv(comm, &in_values[0], in_values.size(), out_values, sizes, root);
- }
- } }
- #endif
|