11 #include <dolfinx/common/MPI.h>
12 #include <dolfinx/graph/AdjacencyList.h>
18 #include <xtl/xspan.hpp>
45 std::tuple<std::int64_t, std::vector<std::int32_t>,
46 std::vector<std::vector<std::int64_t>>,
47 std::vector<std::vector<int>>>
50 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
98 const xtl::span<const int>& dest_ranks,
99 const xtl::span<const std::int64_t>&
ghosts,
100 const xtl::span<const int>& src_ranks);
103 template <
typename U,
typename V,
typename W,
typename X>
105 MPI_Comm
comm, U&& comm_owner_to_ghost, U&& comm_ghost_to_owner,
106 V&& displs_recv_fwd, V&& ghost_pos_recv_fwd, W&&
ghosts,
109 _comm_owner_to_ghost(std::forward<U>(comm_owner_to_ghost)),
110 _comm_ghost_to_owner(std::forward<U>(comm_ghost_to_owner)),
111 _displs_recv_fwd(std::forward<V>(displs_recv_fwd)),
112 _ghost_pos_recv_fwd(std::forward<V>(ghost_pos_recv_fwd)),
113 _ghosts(std::forward<W>(
ghosts)),
114 _shared_indices(std::forward<X>(shared_indices))
116 _sizes_recv_fwd.resize(_displs_recv_fwd.size() - 1, 0);
117 std::adjacent_difference(_displs_recv_fwd.cbegin() + 1,
118 _displs_recv_fwd.cend(), _sizes_recv_fwd.begin());
120 const std::vector<int32_t>& displs_send = _shared_indices->offsets();
121 _sizes_send_fwd.resize(_shared_indices->num_nodes(), 0);
122 std::adjacent_difference(displs_send.cbegin() + 1, displs_send.cend(),
123 _sizes_send_fwd.begin());
143 std::array<std::int64_t, 2>
local_range() const noexcept;
156 const std::vector<std::int64_t>&
ghosts() const noexcept;
160 MPI_Comm
comm() const;
172 const xtl::span<std::int64_t>& global) const;
179 const xtl::span<std::int32_t>& local) const;
199 const graph::AdjacencyList<std::int32_t>&
242 std::pair<
IndexMap, std::vector<std::int32_t>>
243 create_submap(const xtl::span<const std::int32_t>& indices) const;
264 template <typename T>
266 MPI_Datatype& data_type, MPI_Request& request,
267 const xtl::span<T>& recv_buffer)
const
270 const std::vector<int32_t>& displs_send_fwd = _shared_indices->offsets();
273 if (_displs_recv_fwd.size() == 1 and displs_send_fwd.size() == 1)
278 MPI_Type_size(data_type, &n);
280 if (
static_cast<int>(send_buffer.size()) != n * displs_send_fwd.back())
281 throw std::runtime_error(
"Incompatible send buffer size.");
282 if (
static_cast<int>(recv_buffer.size()) != n * _displs_recv_fwd.back())
283 throw std::runtime_error(
"Incompatible receive buffer size..");
286 MPI_Ineighbor_alltoallv(send_buffer.data(), _sizes_send_fwd.data(),
287 displs_send_fwd.data(), data_type,
288 recv_buffer.data(), _sizes_recv_fwd.data(),
289 _displs_recv_fwd.data(), data_type,
290 _comm_owner_to_ghost.comm(), &request);
302 const std::vector<int32_t>& displs_send_fwd = _shared_indices->offsets();
303 if (_displs_recv_fwd.size() == 1 and displs_send_fwd.size() == 1)
307 MPI_Wait(&request, MPI_STATUS_IGNORE);
320 template <
typename T>
322 xtl::span<T> remote_data,
int n)
const
324 MPI_Datatype data_type;
326 data_type = dolfinx::MPI::mpi_type<T>();
329 MPI_Type_contiguous(n, dolfinx::MPI::mpi_type<T>(), &data_type);
330 MPI_Type_commit(&data_type);
333 const std::vector<std::int32_t>& indices = _shared_indices->array();
334 std::vector<T> send_buffer(n * indices.size());
335 for (std::size_t i = 0; i < indices.size(); ++i)
337 std::copy_n(std::next(local_data.cbegin(), n * indices[i]), n,
338 std::next(send_buffer.begin(), n * i));
342 std::vector<T> buffer_recv(n * _displs_recv_fwd.back());
344 xtl::span<T>(buffer_recv));
348 assert(remote_data.size() == n * _ghost_pos_recv_fwd.size());
349 for (std::size_t i = 0; i < _ghost_pos_recv_fwd.size(); ++i)
351 std::copy_n(std::next(buffer_recv.cbegin(), n * _ghost_pos_recv_fwd[i]),
352 n, std::next(remote_data.begin(), n * i));
356 MPI_Type_free(&data_type);
379 template <
typename T>
381 MPI_Datatype& data_type, MPI_Request& request,
382 const xtl::span<T>& recv_buffer)
const
385 const std::vector<int32_t>& displs_send_fwd = _shared_indices->offsets();
388 if (_displs_recv_fwd.size() == 1 and displs_send_fwd.size() == 1)
393 MPI_Type_size(data_type, &n);
395 if (
static_cast<int>(send_buffer.size()) != n * _ghosts.size())
396 throw std::runtime_error(
"Inconsistent send buffer size.");
397 if (
static_cast<int>(recv_buffer.size()) != n * displs_send_fwd.back())
398 throw std::runtime_error(
"Inconsistent receive buffer size.");
401 MPI_Ineighbor_alltoallv(send_buffer.data(), _sizes_recv_fwd.data(),
402 _displs_recv_fwd.data(), data_type,
403 recv_buffer.data(), _sizes_send_fwd.data(),
404 displs_send_fwd.data(), data_type,
405 _comm_ghost_to_owner.comm(), &request);
417 const std::vector<int32_t>& displs_send_fwd = _shared_indices->offsets();
418 if (_displs_recv_fwd.size() == 1 and displs_send_fwd.size() == 1)
422 MPI_Wait(&request, MPI_STATUS_IGNORE);
434 template <
typename T>
436 const xtl::span<const T>& remote_data,
int n,
439 MPI_Datatype data_type;
441 data_type = dolfinx::MPI::mpi_type<T>();
444 MPI_Type_contiguous(n, dolfinx::MPI::mpi_type<T>(), &data_type);
445 MPI_Type_commit(&data_type);
449 std::vector<T> buffer_send;
450 buffer_send.resize(n * _displs_recv_fwd.back());
451 for (std::size_t i = 0; i < _ghost_pos_recv_fwd.size(); ++i)
453 std::copy_n(std::next(remote_data.cbegin(), n * i), n,
454 std::next(buffer_send.begin(), n * _ghost_pos_recv_fwd[i]));
459 std::vector<T> buffer_recv(n * _shared_indices->array().size());
461 xtl::span<T>(buffer_recv));
465 assert(local_data.size() == n * this->size_local());
466 const std::vector<std::int32_t>& shared_indices = _shared_indices->array();
470 for (std::size_t i = 0; i < shared_indices.size(); ++i)
472 std::copy_n(std::next(buffer_recv.cbegin(), n * i), n,
473 std::next(local_data.begin(), n * shared_indices[i]));
477 for (std::size_t i = 0; i < shared_indices.size(); ++i)
479 for (
int j = 0; j < n; ++j)
480 local_data[shared_indices[i] * n + j] += buffer_recv[i * n + j];
486 MPI_Type_free(&data_type);
491 std::array<std::int64_t, 2> _local_range;
494 std::int64_t _size_global;
515 std::vector<std::int32_t> _sizes_send_fwd, _sizes_recv_fwd, _displs_recv_fwd;
519 std::vector<std::int32_t> _ghost_pos_recv_fwd;
522 std::vector<std::int64_t> _ghosts;
530 std::unique_ptr<graph::AdjacencyList<std::int32_t>> _shared_indices;
A duplicate MPI communicator and manage lifetime of the communicator.
Definition: MPI.h:40
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:60
~IndexMap()=default
Destructor.
Direction
Edge directions of neighborhood communicator.
Definition: IndexMap.h:71
std::vector< int > ghost_owners() const
Compute the owner on the neighborhood communicator of each ghost index.
Definition: IndexMap.cpp:627
const std::vector< std::int32_t > & scatter_fwd_ghost_positions() const noexcept
Position of ghost entries in the receive buffer after a forward scatter, e.g. for a receive buffer b ...
Definition: IndexMap.cpp:622
std::int32_t num_ghosts() const noexcept
Number of ghost indices on this process.
Definition: IndexMap.cpp:542
Mode
Mode for reverse scatter operation.
Definition: IndexMap.h:64
void scatter_rev_end(MPI_Request &request) const
Complete a non-blocking send of ghost values to the owning rank. This function complete the communica...
Definition: IndexMap.h:414
std::array< std::int64_t, 2 > local_range() const noexcept
Range of indices (global) owned by this process.
Definition: IndexMap.cpp:537
std::int32_t size_local() const noexcept
Number of indices owned by on this process.
Definition: IndexMap.cpp:544
std::int64_t size_global() const noexcept
Number indices across communicator.
Definition: IndexMap.cpp:549
IndexMap(IndexMap &&map)=default
Move constructor.
const graph::AdjacencyList< std::int32_t > & scatter_fwd_indices() const noexcept
Local (owned) indices shared with neighbor processes, i.e. are ghosts on other processes,...
Definition: IndexMap.cpp:615
IndexMap & operator=(IndexMap &&map)=default
Move assignment.
void scatter_fwd(const xtl::span< const T > &local_data, xtl::span< T > remote_data, int n) const
Send n values for each index that is owned to processes that have the index as a ghost....
Definition: IndexMap.h:321
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:551
void global_to_local(const xtl::span< const std::int64_t > &global, const xtl::span< std::int32_t > &local) const
Compute local indices for array of global indices.
Definition: IndexMap.cpp:575
std::pair< IndexMap, std::vector< std::int32_t > > create_submap(const xtl::span< const std::int32_t > &indices) const
Create new index map from a subset of indices in this index map. The order of the indices is preserve...
Definition: IndexMap.cpp:785
void scatter_fwd_end(MPI_Request &request) const
Complete a non-blocking send from the local owner of to process ranks that have the index as a ghost....
Definition: IndexMap.h:299
std::map< std::int32_t, std::set< int > > compute_shared_indices() const
Definition: IndexMap.cpp:656
void local_to_global(const xtl::span< const std::int32_t > &local, const xtl::span< std::int64_t > &global) const
Compute global indices for array of local indices.
Definition: IndexMap.cpp:556
IndexMap(MPI_Comm comm, std::int32_t local_size)
Create an non-overlapping index map with local_size owned on this process.
Definition: IndexMap.cpp:371
void scatter_fwd_begin(const xtl::span< const T > &send_buffer, MPI_Datatype &data_type, MPI_Request &request, const xtl::span< T > &recv_buffer) const
Start a non-blocking send of owned data to ranks that ghost the data. The communication is completed ...
Definition: IndexMap.h:265
MPI_Comm comm() const
Return the MPI communicator used to create the index map.
Definition: IndexMap.cpp:641
std::vector< std::int64_t > global_indices() const
Global indices.
Definition: IndexMap.cpp:601
void scatter_rev_begin(const xtl::span< const T > &send_buffer, MPI_Datatype &data_type, MPI_Request &request, const xtl::span< T > &recv_buffer) const
Start a non-blocking send of ghost values to the owning rank. The non-blocking communication is compl...
Definition: IndexMap.h:380
void scatter_rev(xtl::span< T > local_data, const xtl::span< const T > &remote_data, int n, IndexMap::Mode op) const
Send n values for each ghost index to owning to the process.
Definition: IndexMap.h:435
Miscellaneous classes, functions and types.
std::vector< int32_t > compute_owned_indices(const xtl::span< const std::int32_t > &indices, const IndexMap &map)
Given a vector of indices (local numbering, owned or ghost) and an index map, this function returns t...
Definition: IndexMap.cpp:132
std::tuple< std::int64_t, std::vector< std::int32_t >, std::vector< std::vector< std::int64_t > >, std::vector< std::vector< int > > > stack_index_maps(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int >> &maps)
Compute layout data and ghost indices for a stacked (concatenated) index map, i.e....
Definition: IndexMap.cpp:243