14 #include <dolfinx/graph/AdjacencyList.h>
18 #include <type_traits>
22 #define MPICH_IGNORE_CXX_SEEK 1
39 explicit Comm(MPI_Comm
comm,
bool duplicate =
true);
57 MPI_Comm
comm()
const;
65 static int rank(MPI_Comm comm);
69 static int size(MPI_Comm comm);
90 const std::set<int>& edges);
98 const std::vector<int>& send_offsets,
99 const std::vector<T>& send_data);
106 static std::tuple<std::vector<int>, std::vector<int>>
111 static std::size_t
global_offset(MPI_Comm comm, std::size_t range,
116 static std::array<std::int64_t, 2>
local_range(
int process, std::int64_t N,
126 template <
typename T>
132 template <
typename T>
136 throw std::runtime_error(
"MPI data type unknown");
145 inline MPI_Datatype MPI::mpi_type<float>()
150 inline MPI_Datatype MPI::mpi_type<double>()
155 inline MPI_Datatype MPI::mpi_type<std::complex<double>>()
157 return MPI_DOUBLE_COMPLEX;
160 inline MPI_Datatype MPI::mpi_type<short int>()
165 inline MPI_Datatype MPI::mpi_type<int>()
170 inline MPI_Datatype MPI::mpi_type<unsigned int>()
175 inline MPI_Datatype MPI::mpi_type<long int>()
180 inline MPI_Datatype MPI::mpi_type<unsigned long>()
182 return MPI_UNSIGNED_LONG;
185 inline MPI_Datatype MPI::mpi_type<long long>()
187 return MPI_LONG_LONG;
190 inline MPI_Datatype MPI::mpi_type<unsigned long long>()
192 return MPI_UNSIGNED_LONG_LONG;
195 inline MPI_Datatype MPI::mpi_type<bool>()
201 template <
typename T>
202 graph::AdjacencyList<T>
206 const Eigen::Array<std::int32_t, Eigen::Dynamic, 1>& send_offsets
208 const Eigen::Array<T, Eigen::Dynamic, 1>& values_in = send_data.
array();
211 assert(send_data.
num_nodes() == comm_size);
214 std::vector<int> send_size(comm_size);
215 std::adjacent_difference(send_offsets.data() + 1,
216 send_offsets.data() + send_offsets.rows(),
220 std::vector<int> recv_size(comm_size);
221 MPI_Alltoall(send_size.data(), 1, mpi_type<int>(), recv_size.data(), 1,
222 mpi_type<int>(), comm);
225 Eigen::Array<std::int32_t, Eigen::Dynamic, 1> recv_offset(comm_size + 1);
227 std::partial_sum(recv_size.begin(), recv_size.end(), recv_offset.data() + 1);
230 Eigen::Array<T, Eigen::Dynamic, 1> recv_values(recv_offset(comm_size));
231 MPI_Alltoallv(values_in.data(), send_size.data(), send_offsets.data(),
232 mpi_type<T>(), recv_values.data(), recv_size.data(),
233 recv_offset.data(), mpi_type<T>(), comm);
236 std::move(recv_offset));
239 template <
typename T>
242 const std::vector<int>& send_offsets,
243 const std::vector<T>& send_data)
246 int indegree(-1), outdegree(-2), weighted(-1);
247 MPI_Dist_graph_neighbors_count(neighbor_comm, &indegree, &outdegree,
250 assert((
int)send_data.size() == send_offsets.back());
251 assert(send_offsets[0] == 0);
254 std::vector<int> send_sizes(outdegree, 0);
255 std::vector<int> recv_sizes(indegree);
256 std::adjacent_difference(send_offsets.begin() + 1, send_offsets.end(),
258 MPI_Neighbor_alltoall(send_sizes.data(), 1, MPI::mpi_type<int>(),
259 recv_sizes.data(), 1, MPI::mpi_type<int>(),
263 Eigen::Array<int, Eigen::Dynamic, 1> recv_offsets(recv_sizes.size() + 1);
265 std::partial_sum(recv_sizes.begin(), recv_sizes.end(),
266 recv_offsets.data() + 1);
268 Eigen::Array<T, Eigen::Dynamic, 1> recv_data(
269 recv_offsets(recv_offsets.rows() - 1));
270 MPI_Neighbor_alltoallv(
271 send_data.data(), send_sizes.data(), send_offsets.data(),
272 MPI::mpi_type<T>(), recv_data.data(), recv_sizes.data(),
273 recv_offsets.data(), MPI::mpi_type<T>(), neighbor_comm);