29template <
class Allocator = std::allocator<std::
int32_t>>
50 : _bs(
bs), _remote_inds(0, alloc), _local_inds(0, alloc), _src(map.src()),
56 assert(std::is_sorted(_src.begin(), _src.end()));
57 assert(std::is_sorted(_dest.begin(), _dest.end()));
63 MPI_Dist_graph_create_adjacent(
64 map.
comm(), _src.size(), _src.data(), MPI_UNWEIGHTED, _dest.size(),
65 _dest.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm0);
69 MPI_Dist_graph_create_adjacent(
70 map.
comm(), _dest.size(), _dest.data(), MPI_UNWEIGHTED, _src.size(),
71 _src.data(), MPI_UNWEIGHTED, MPI_INFO_NULL,
false, &comm1);
75 const std::vector<int>& owners = map.
owners();
76 std::vector<std::int32_t> perm(owners.size());
77 std::iota(perm.begin(), perm.end(), 0);
78 dolfinx::argsort_radix<std::int32_t>(owners, perm);
82 const std::vector<std::int64_t>& ghosts = map.
ghosts();
83 std::vector<int> owners_sorted(owners.size());
84 std::vector<std::int64_t> ghosts_sorted(owners.size());
85 std::transform(perm.begin(), perm.end(), owners_sorted.begin(),
86 [&owners](
auto idx) { return owners[idx]; });
87 std::transform(perm.begin(), perm.end(), ghosts_sorted.begin(),
88 [&ghosts](
auto idx) { return ghosts[idx]; });
96 _sizes_remote.resize(_src.size(), 0);
97 _displs_remote.resize(_src.size() + 1, 0);
98 std::vector<std::int32_t>::iterator begin = owners_sorted.begin();
99 for (std::size_t i = 0; i < _src.size(); i++)
101 auto upper = std::upper_bound(begin, owners_sorted.end(), _src[i]);
102 int num_ind = std::distance(begin, upper);
103 _displs_remote[i + 1] = _displs_remote[i] + num_ind;
104 _sizes_remote[i] = num_ind;
115 _sizes_local.resize(_dest.size());
116 _displs_local.resize(_sizes_local.size() + 1);
117 _sizes_remote.reserve(1);
118 _sizes_local.reserve(1);
119 MPI_Neighbor_alltoall(_sizes_remote.data(), 1, MPI_INT32_T,
120 _sizes_local.data(), 1, MPI_INT32_T, _comm1.comm());
121 std::partial_sum(_sizes_local.begin(), _sizes_local.end(),
122 std::next(_displs_local.begin()));
124 assert((std::int32_t)ghosts_sorted.size() == _displs_remote.back());
125 assert((std::int32_t)ghosts_sorted.size() == _displs_remote.back());
129 std::vector<std::int64_t> recv_buffer(_displs_local.back(), 0);
130 MPI_Neighbor_alltoallv(ghosts_sorted.data(), _sizes_remote.data(),
131 _displs_remote.data(), MPI_INT64_T,
132 recv_buffer.data(), _sizes_local.data(),
133 _displs_local.data(), MPI_INT64_T, _comm1.comm());
135 const std::array<std::int64_t, 2> range = map.
local_range();
138 std::for_each(recv_buffer.begin(), recv_buffer.end(),
140 { assert(idx >= range[0] and idx < range[1]); });
145 auto rescale = [](
auto& x,
int bs)
147 std::transform(x.begin(), x.end(), x.begin(),
148 [
bs](
auto e) { return e *= bs; });
150 rescale(_sizes_local,
bs);
151 rescale(_displs_local,
bs);
152 rescale(_sizes_remote,
bs);
153 rescale(_displs_remote,
bs);
158 _local_inds = std::vector<std::int32_t, allocator_type>(
159 recv_buffer.size() * _bs, alloc);
160 std::int64_t offset = range[0] * _bs;
161 for (std::size_t i = 0; i < recv_buffer.size(); i++)
162 for (
int j = 0; j < _bs; j++)
163 _local_inds[i * _bs + j] = (recv_buffer[i] * _bs + j) - offset;
167 = std::vector<std::int32_t, allocator_type>(perm.size() * _bs, alloc);
168 for (std::size_t i = 0; i < perm.size(); i++)
169 for (
int j = 0; j < _bs; j++)
170 _remote_inds[i * _bs + j] = perm[i] * _bs + j;
194 template <
typename T>
196 std::span<T> recv_buffer,
197 std::span<MPI_Request> requests,
201 if (_sizes_local.empty() and _sizes_remote.empty())
208 assert(requests.size() == std::size_t(1));
209 MPI_Ineighbor_alltoallv(
210 send_buffer.data(), _sizes_local.data(), _displs_local.data(),
211 dolfinx::MPI::mpi_type<T>(), recv_buffer.data(), _sizes_remote.data(),
212 _displs_remote.data(), dolfinx::MPI::mpi_type<T>(), _comm0.comm(),
218 assert(requests.size() == _dest.size() + _src.size());
219 for (std::size_t i = 0; i < _src.size(); i++)
221 MPI_Irecv(recv_buffer.data() + _displs_remote[i], _sizes_remote[i],
222 dolfinx::MPI::mpi_type<T>(), _src[i], MPI_ANY_TAG,
223 _comm0.comm(), &requests[i]);
226 for (std::size_t i = 0; i < _dest.size(); i++)
228 MPI_Isend(send_buffer.data() + _displs_local[i], _sizes_local[i],
229 dolfinx::MPI::mpi_type<T>(), _dest[i], 0, _comm0.comm(),
230 &requests[i + _src.size()]);
235 throw std::runtime_error(
"Scatter::type not recognized");
250 if (_sizes_local.empty() and _sizes_remote.empty())
254 MPI_Waitall(requests.size(), requests.data(), MPI_STATUS_IGNORE);
277 template <
typename T,
typename Functor>
279 std::span<T> local_buffer, std::span<T> remote_buffer,
280 Functor pack_fn, std::span<MPI_Request> requests,
283 assert(local_buffer.size() == _local_inds.size());
284 assert(remote_buffer.size() == _remote_inds.size());
285 pack_fn(local_data, _local_inds, local_buffer);
309 template <
typename T,
typename Functor>
311 std::span<T> remote_data, Functor unpack_fn,
312 std::span<MPI_Request> requests)
const
314 assert(remote_buffer.size() == _remote_inds.size());
315 assert(remote_data.size() == _remote_inds.size());
317 unpack_fn(remote_buffer, _remote_inds, remote_data,
318 [](T , T b) {
return b; });
332 template <
typename T>
334 std::span<T> remote_data)
const
336 std::vector<MPI_Request> requests(1, MPI_REQUEST_NULL);
339 auto pack_fn = [](
const auto& in,
const auto& idx,
auto& out)
341 for (std::size_t i = 0; i < idx.size(); ++i)
345 std::span<T>(remote_buffer), pack_fn,
346 std::span<MPI_Request>(requests));
348 auto unpack_fn = [](
const auto& in,
const auto& idx,
auto& out,
auto op)
350 for (std::size_t i = 0; i < idx.size(); ++i)
351 out[idx[i]] = op(out[idx[i]], in[i]);
354 scatter_fwd_end(std::span<const T>(remote_buffer), remote_data, unpack_fn,
355 std::span<MPI_Request>(requests));
384 template <
typename T>
386 std::span<T> recv_buffer,
387 std::span<MPI_Request> requests,
391 if (_sizes_local.empty() and _sizes_remote.empty())
400 assert(requests.size() == 1);
401 MPI_Ineighbor_alltoallv(send_buffer.data(), _sizes_remote.data(),
402 _displs_remote.data(), MPI::mpi_type<T>(),
403 recv_buffer.data(), _sizes_local.data(),
404 _displs_local.data(), MPI::mpi_type<T>(),
405 _comm1.comm(), &requests[0]);
410 assert(requests.size() == _dest.size() + _src.size());
412 for (std::size_t i = 0; i < _dest.size(); i++)
414 MPI_Irecv(recv_buffer.data() + _displs_local[i], _sizes_local[i],
415 dolfinx::MPI::mpi_type<T>(), _dest[i], MPI_ANY_TAG,
416 _comm0.comm(), &requests[i]);
421 for (std::size_t i = 0; i < _src.size(); i++)
423 MPI_Isend(send_buffer.data() + _displs_remote[i], _sizes_remote[i],
424 dolfinx::MPI::mpi_type<T>(), _src[i], 0, _comm0.comm(),
425 &requests[i + _dest.size()]);
430 throw std::runtime_error(
"Scatter::type not recognized");
445 if (_sizes_local.empty() and _sizes_remote.empty())
449 MPI_Waitall(request.size(), request.data(), MPI_STATUS_IGNORE);
480 template <
typename T,
typename Functor>
482 std::span<T> remote_buffer, std::span<T> local_buffer,
483 Functor pack_fn, std::span<MPI_Request> request,
486 assert(local_buffer.size() == _local_inds.size());
487 assert(remote_buffer.size() == _remote_inds.size());
488 pack_fn(remote_data, _remote_inds, remote_buffer);
512 template <
typename T,
typename Functor,
typename BinaryOp>
514 Functor unpack_fn, BinaryOp op,
515 std::span<MPI_Request> request)
517 assert(local_buffer.size() == _local_inds.size());
518 if (_local_inds.size() > 0)
519 assert(*std::max_element(_local_inds.begin(), _local_inds.end())
520 < std::int32_t(local_data.size()));
522 unpack_fn(local_buffer, _local_inds, local_data, op);
527 template <
typename T,
typename BinaryOp>
528 void scatter_rev(std::span<T> local_data, std::span<const T> remote_data,
533 auto pack_fn = [](
const auto& in,
const auto& idx,
auto& out)
535 for (std::size_t i = 0; i < idx.size(); ++i)
538 auto unpack_fn = [](
const auto& in,
const auto& idx,
auto& out,
auto op)
540 for (std::size_t i = 0; i < idx.size(); ++i)
541 out[idx[i]] = op(out[idx[i]], in[i]);
543 std::vector<MPI_Request> request(1, MPI_REQUEST_NULL);
545 std::span<T>(local_buffer), pack_fn,
546 std::span<MPI_Request>(request));
547 scatter_rev_end(std::span<const T>(local_buffer), local_data, unpack_fn, op,
548 std::span<MPI_Request>(request));
561 return _remote_inds.size();
582 int bs() const noexcept {
return _bs; }
589 std::vector<MPI_Request> requests;
593 requests = {MPI_REQUEST_NULL};
596 requests.resize(_dest.size() + _src.size(), MPI_REQUEST_NULL);
599 throw std::runtime_error(
"Scatter::type not recognized");
623 std::vector<std::int32_t, allocator_type> _remote_inds;
626 std::vector<int> _sizes_remote;
629 std::vector<int> _displs_remote;
634 std::vector<std::int32_t, allocator_type> _local_inds;
637 std::vector<int> _sizes_local;
640 std::vector<int> _displs_local;
644 std::vector<int> _src;
648 std::vector<int> _dest;
A duplicate MPI communicator and manage lifetime of the communicator.
Definition MPI.h:43
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition IndexMap.h:64
bool overlapped() const noexcept
Check if index map has overlaps (ghosts on any rank).
Definition IndexMap.cpp:948
const std::vector< int > & owners() const
The ranks that own each ghost index.
Definition IndexMap.h:174
std::array< std::int64_t, 2 > local_range() const noexcept
Range of indices (global) owned by this process.
Definition IndexMap.cpp:384
const std::vector< std::int64_t > & ghosts() const noexcept
Local-to-global map for ghosts (local indexing beyond end of local range)
Definition IndexMap.cpp:398
MPI_Comm comm() const
Return the MPI communicator that the map is defined on.
Definition IndexMap.cpp:461
A Scatterer supports the MPI scattering and gathering of data that is associated with a common::Index...
Definition Scatterer.h:31
std::vector< MPI_Request > create_request_vector(Scatterer::type type=type::neighbor)
Create a vector of MPI_Requests for a given Scatterer::type.
Definition Scatterer.h:586
Allocator allocator_type
The allocator type.
Definition Scatterer.h:34
void scatter_fwd_end(std::span< MPI_Request > requests) const
Complete a non-blocking send from the local owner to process ranks that have the index as a ghost.
Definition Scatterer.h:247
void scatter_fwd(std::span< const T > local_data, std::span< T > remote_data) const
Scatter data associated with owned indices to ghosting ranks.
Definition Scatterer.h:333
Scatterer(const IndexMap &map, int bs, const Allocator &alloc=Allocator())
Create a scatterer.
Definition Scatterer.h:49
std::int32_t remote_buffer_size() const noexcept
Buffer size for remote data (ghosts) used in forward and reverse communication.
Definition Scatterer.h:559
std::int32_t local_buffer_size() const noexcept
Size of buffer for local data (owned and shared) used in forward and reverse communication.
Definition Scatterer.h:554
void scatter_fwd_end(std::span< const T > remote_buffer, std::span< T > remote_data, Functor unpack_fn, std::span< MPI_Request > requests) const
Complete a non-blocking send from the local owner to process ranks that have the index as a ghost,...
Definition Scatterer.h:310
type
Types of MPI communication pattern used by the Scatterer.
Definition Scatterer.h:38
void scatter_rev_end(std::span< MPI_Request > request) const
End the reverse scatter communication.
Definition Scatterer.h:442
void scatter_fwd_begin(std::span< const T > send_buffer, std::span< T > recv_buffer, std::span< MPI_Request > requests, Scatterer::type type=type::neighbor) const
Start a non-blocking send of owned data to ranks that ghost the data.
Definition Scatterer.h:195
void scatter_rev_begin(std::span< const T > remote_data, std::span< T > remote_buffer, std::span< T > local_buffer, Functor pack_fn, std::span< MPI_Request > request, Scatterer::type type=type::neighbor) const
Scatter data associated with ghost indices to owning ranks.
Definition Scatterer.h:481
const std::vector< std::int32_t > & local_indices() const noexcept
Return a vector of local indices (owned) used to pack/unpack local data. These indices are grouped by...
Definition Scatterer.h:567
void scatter_rev_end(std::span< const T > local_buffer, std::span< T > local_data, Functor unpack_fn, BinaryOp op, std::span< MPI_Request > request)
End the reverse scatter communication, and unpack the received local buffer into local data.
Definition Scatterer.h:513
void scatter_fwd_begin(std::span< const T > local_data, std::span< T > local_buffer, std::span< T > remote_buffer, Functor pack_fn, std::span< MPI_Request > requests, Scatterer::type type=type::neighbor) const
Scatter data associated with owned indices to ghosting ranks.
Definition Scatterer.h:278
const std::vector< std::int32_t > & remote_indices() const noexcept
Return a vector of remote indices (ghosts) used to pack/unpack ghost data. These indices are grouped ...
Definition Scatterer.h:574
void scatter_rev(std::span< T > local_data, std::span< const T > remote_data, BinaryOp op)
Scatter data associated with ghost indices to ranks that own the indices.
Definition Scatterer.h:528
int bs() const noexcept
The number values (block size) to send per index in the common::IndexMap use to create the scatterer.
Definition Scatterer.h:582
void scatter_rev_begin(std::span< const T > send_buffer, std::span< T > recv_buffer, std::span< MPI_Request > requests, Scatterer::type type=type::neighbor) const
Start a non-blocking send of ghost data to ranks that own the data.
Definition Scatterer.h:385
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
Top-level namespace.
Definition defines.h:12