Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:56

0001 // Copyright (C) 2005, 2006 Douglas Gregor.
0002 
0003 // Use, modification and distribution is subject to the Boost Software
0004 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0005 // http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // Message Passing Interface 1.1 -- Section 4.5. Gatherv
0008 #ifndef BOOST_MPI_ALLGATHERV_HPP
0009 #define BOOST_MPI_ALLGATHERV_HPP
0010 
0011 #include <cassert>
0012 #include <cstddef>
0013 #include <numeric>
0014 #include <vector>
0015 
0016 #include <boost/mpi/exception.hpp>
0017 #include <boost/mpi/datatype.hpp>
0018 #include <boost/mpi/packed_oarchive.hpp>
0019 #include <boost/mpi/packed_iarchive.hpp>
0020 #include <boost/mpi/detail/point_to_point.hpp>
0021 #include <boost/mpi/communicator.hpp>
0022 #include <boost/mpi/environment.hpp>
0023 #include <boost/mpi/detail/offsets.hpp>
0024 #include <boost/mpi/detail/antiques.hpp>
0025 #include <boost/assert.hpp>
0026 #include <boost/scoped_array.hpp>
0027 
0028 namespace boost { namespace mpi {
0029 
0030 namespace detail {
0031 // We're all-gathering for a type that has an associated MPI
0032 // datatype, so we'll use MPI_Gather to do all of the work.
0033 template<typename T>
0034 void
0035 all_gatherv_impl(const communicator& comm, const T* in_values,
0036                  T* out_values, int const* sizes, int const* displs, mpl::true_)
0037 {
0038   // Make displacements if not provided
0039   scoped_array<int> new_offsets_mem(make_offsets(comm, sizes, displs, -1));
0040   if (new_offsets_mem) displs = new_offsets_mem.get();
0041   MPI_Datatype type = get_mpi_datatype<T>(*in_values);
0042   BOOST_MPI_CHECK_RESULT(MPI_Allgatherv,
0043                          (const_cast<T*>(in_values), sizes[comm.rank()], type,
0044                           out_values,
0045                           const_cast<int*>(sizes),
0046                           const_cast<int*>(displs),
0047                           type, 
0048                           comm));
0049 }
0050 
0051 // We're all-gathering for a type that does not have an
0052 // associated MPI datatype, so we'll need to serialize
0053 // it.
0054 template<typename T>
0055 void
0056 all_gatherv_impl(const communicator& comm, const T* in_values,
0057                  T* out_values, int const* sizes, int const* displs,
0058                  mpl::false_ isnt_mpi_type)
0059 { 
0060   // convert displacement to offsets to skip
0061   scoped_array<int> skipped(make_skipped_slots(comm, sizes, displs));
0062   all_gather_impl(comm, in_values, sizes[comm.rank()], out_values, 
0063                   sizes, skipped.get(), isnt_mpi_type);
0064 }
0065 } // end namespace detail
0066 
0067 template<typename T>
0068 void
0069 all_gatherv(const communicator& comm, const T& in_value, T* out_values,
0070             const std::vector<int>& sizes)
0071 {
0072   using detail::c_data;
0073   assert(sizes.size()   == comm.size());
0074   assert(sizes[comm.rank()] == 1);
0075   detail::all_gatherv_impl(comm, &in_value, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
0076 }
0077 
0078 template<typename T>
0079 void
0080 all_gatherv(const communicator& comm, const T* in_values, T* out_values,
0081             const std::vector<int>& sizes)
0082 {
0083   using detail::c_data;
0084   assert(int(sizes.size()) == comm.size());
0085   detail::all_gatherv_impl(comm, in_values, out_values, c_data(sizes), 0, is_mpi_datatype<T>());
0086 }
0087 
0088 template<typename T>
0089 void
0090 all_gatherv(const communicator& comm, std::vector<T> const& in_values,  std::vector<T>& out_values,
0091            const std::vector<int>& sizes)
0092 {
0093   using detail::c_data;
0094   assert(int(sizes.size()) == comm.size());
0095   assert(int(in_values.size()) == sizes[comm.rank()]);
0096   out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
0097   ::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes);
0098 }
0099 
0100 
0101 template<typename T>
0102 void
0103 all_gatherv(const communicator& comm, const T& in_value, T* out_values,
0104             const std::vector<int>& sizes, const std::vector<int>& displs)
0105 {
0106   using detail::c_data;
0107   assert(sizes.size()   == comm.size());
0108   assert(displs.size() == comm.size());
0109   detail::all_gatherv_impl(comm, &in_value, 1, out_values,
0110                            c_data(sizes), c_data(displs), is_mpi_datatype<T>());
0111 }
0112 
0113 template<typename T>
0114 void
0115 all_gatherv(const communicator& comm, const T* in_values, T* out_values,
0116             const std::vector<int>& sizes, const std::vector<int>& displs)
0117 {
0118   using detail::c_data;
0119   assert(sizes.size()   == comm.size());
0120   assert(displs.size() == comm.size());
0121   detail::all_gatherv_impl(comm, in_values, out_values,
0122                            c_data(sizes), c_data(displs), is_mpi_datatype<T>());
0123 }
0124 
0125 template<typename T>
0126 void
0127 all_gatherv(const communicator& comm, std::vector<T> const& in_values, std::vector<T>& out_values,
0128             const std::vector<int>& sizes, const std::vector<int>& displs)
0129 {
0130   using detail::c_data;
0131   assert(sizes.size()   == comm.size());
0132   assert(displs.size() == comm.size());
0133   assert(in_values.size() == sizes[comm.rank()]);
0134   out_values.resize(std::accumulate(sizes.begin(), sizes.end(), 0));
0135   ::boost::mpi::all_gatherv(comm, c_data(in_values), c_data(out_values), sizes, displs);
0136 }
0137 
0138 } } // end namespace boost::mpi
0139 
0140 #endif // BOOST_MPI_ALL_GATHERV_HPP