35 MPI_Allgather( sendTo.data(), comm_size, MPI_INT,
91 const std::map<int,MessageType>& messages,
96 MPI_Comm_rank( comm, &rank);
97 MPI_Comm_size( comm, &comm_size);
98 thrust::host_vector<int> sendTo( comm_size, 0 ), recvFrom( comm_size, 0);
99 thrust::host_vector<int> global( comm_size*comm_size);
100 auto flat_vals = detail::flatten_values( messages);
101 for(
auto& send : flat_vals)
103 sendTo[send.first] = send.second.size();
106 MPI_Allgather( sendTo.data(), comm_size, MPI_INT,
107 global.data(), comm_size, MPI_INT,
109 for(
int i=0; i<comm_size; i++)
110 recvFrom[i] = global[i*comm_size+rank];
112 thrust::host_vector<int> accS( comm_size), accR(comm_size);
113 thrust::exclusive_scan( sendTo.begin(), sendTo.end(), accS.begin());
114 thrust::exclusive_scan( recvFrom.begin(), recvFrom.end(), accR.begin());
116 auto send = detail::flatten_map( flat_vals);
118 thrust::host_vector<value_type> recv(
119 thrust::reduce( recvFrom.begin(), recvFrom.end()));
120 void * send_ptr = thrust::raw_pointer_cast( send.data());
121 const int * sendTo_ptr = thrust::raw_pointer_cast( sendTo.data());
122 const int * accS_ptr = thrust::raw_pointer_cast( accS.data());
123 void * recv_ptr = thrust::raw_pointer_cast( recv.data());
124 const int * recvFrom_ptr = thrust::raw_pointer_cast( recvFrom.data());
125 const int * accR_ptr = thrust::raw_pointer_cast( accR.data());
126 MPI_Datatype type = dg::getMPIDataType<value_type>();
127 MPI_Alltoallv( send_ptr, sendTo_ptr, accS_ptr, type,
128 recv_ptr, recvFrom_ptr, accR_ptr, type,
130 return detail::make_map_t<MessageType>( recv, detail::make_size_map( recvFrom) );
148void mpi_gather(
const thrust::host_vector<std::array<int,2>>& gather_map,
149 const ContainerType& gatherFrom, ContainerType& result, MPI_Comm comm)
151 thrust::host_vector<int> bufferIdx;
152 auto gather_m = gIdx2unique_idx( gather_map, bufferIdx);
154 auto gather = detail::flatten_map( send_map);
155 auto size_map = detail::get_size_map( send_map);
156 ContainerType res( gather.size());
157 thrust::gather( gather.begin(), gather.end(), gatherFrom.begin(), res.begin());
158 std::map<int,ContainerType> result_map = detail::make_map_t<ContainerType>( res, size_map);
159 std::map<int,ContainerType> recv =
mpi_permute( result_map, comm);
160 ContainerType flat = detail::flatten_map( recv);
161 result.resize( bufferIdx.size());
162 thrust::gather( bufferIdx.begin(), bufferIdx.end(), flat.begin(), result.begin());
184void mpi_scatter(
const thrust::host_vector<std::array<int,2>>& scatter_map,
185 const ContainerType& toScatter, ContainerType& result,
187 bool resize_result =
false
190 thrust::host_vector<int> bufferIdx;
192 auto scatter_m = gIdx2unique_idx( scatter_map, bufferIdx);
194 auto scatter = detail::flatten_map( recv_map);
196 auto size_map = detail::get_size_map( scatter_m);
198 ContainerType to_send( toScatter);
199 thrust::scatter( toScatter.begin(), toScatter.end(), bufferIdx.begin(), to_send.begin());
201 auto send = detail::make_map_t<ContainerType>( to_send, size_map);
203 auto res = detail::flatten_map( result_map);
205 result.resize( res.size());
206 thrust::scatter( res.begin(), res.end(), scatter.begin(), result.begin());
226 thrust::host_vector<Integer> seq( p.size());
227 thrust::host_vector<std::array<Integer,2>> seq_arr( p.size());
228 thrust::sequence( seq.begin(), seq.end());
230 MPI_Comm_rank( comm, &rank);
232 for(
unsigned u=0; u<seq.size(); u++)
233 seq_arr[u] = {rank, seq[u]};
234 thrust::host_vector<std::array<Integer,2>> sort_map;
void mpi_gather(const thrust::host_vector< std::array< int, 2 > > &gather_map, const ContainerType &gatherFrom, ContainerType &result, MPI_Comm comm)
Un-optimized distributed gather operation.
Definition mpi_permutation.h:148
thrust::host_vector< std::array< Integer, 2 > > mpi_invert_permutation(const thrust::host_vector< std::array< Integer, 2 > > &p, MPI_Comm comm)
Invert a globally bijective index map.
Definition mpi_permutation.h:223
void mpi_scatter(const thrust::host_vector< std::array< int, 2 > > &scatter_map, const ContainerType &toScatter, ContainerType &result, MPI_Comm comm, bool resize_result=false)
Un-optimized distributed scatter operation.
Definition mpi_permutation.h:184
std::map< int, MessageType > mpi_permute(const std::map< int, MessageType > &messages, MPI_Comm comm)
Exchange messages between processes in a communicator.
Definition mpi_permutation.h:90