DOLFIN-X
DOLFIN-X C++ interface
Partitioning.h
1 // Copyright (C) 2020 Garth N. Wells
2 //
3 // This file is part of DOLFINX (https://www.fenicsproject.org)
4 //
5 // SPDX-License-Identifier: LGPL-3.0-or-later
6 
7 #pragma once
8 
9 #include <cstdint>
10 #include <dolfinx/common/IndexMap.h>
11 #include <dolfinx/common/Timer.h>
12 #include <dolfinx/graph/AdjacencyList.h>
13 #include <mpi.h>
14 #include <set>
15 #include <utility>
16 #include <vector>
17 
18 namespace dolfinx::graph
19 {
20 
24 
25 namespace Partitioning
26 {
48 std::tuple<std::vector<std::int32_t>, std::vector<std::int64_t>,
49  std::vector<int>>
50 reorder_global_indices(MPI_Comm comm,
51  const std::vector<std::int64_t>& global_indices,
52  const std::vector<bool>& shared_indices);
53 
62 std::pair<graph::AdjacencyList<std::int32_t>, std::vector<std::int64_t>>
64 
75 std::tuple<graph::AdjacencyList<std::int32_t>, common::IndexMap>
77  MPI_Comm comm, const graph::AdjacencyList<std::int32_t>& list_local,
78  const std::vector<std::int64_t>& local_to_global_links,
79  const std::vector<bool>& shared_links);
80 
92 std::tuple<graph::AdjacencyList<std::int64_t>, std::vector<int>,
93  std::vector<std::int64_t>, std::vector<int>>
94 distribute(MPI_Comm comm, const graph::AdjacencyList<std::int64_t>& list,
95  const graph::AdjacencyList<std::int32_t>& destinations);
96 
104 std::vector<std::int64_t>
105 compute_ghost_indices(MPI_Comm comm,
106  const std::vector<std::int64_t>& global_indices,
107  const std::vector<int>& ghost_owners);
108 
118 template <typename T>
119 Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
120 distribute_data(MPI_Comm comm, const std::vector<std::int64_t>& indices,
121  const Eigen::Ref<const Eigen::Array<
122  T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& x);
123 
135 std::vector<std::int64_t>
138 
147 std::vector<std::int32_t>
148 compute_local_to_local(const std::vector<std::int64_t>& local0_to_global,
149  const std::vector<std::int64_t>& local1_to_global);
150 } // namespace Partitioning
151 
152 //---------------------------------------------------------------------------
153 // Implementation
154 //---------------------------------------------------------------------------
155 template <typename T>
156 Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
158  MPI_Comm comm, const std::vector<std::int64_t>& indices,
159  const Eigen::Ref<const Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic,
160  Eigen::RowMajor>>& x)
161 {
162  common::Timer timer("Fetch float data from remote processes");
163 
164  const std::int64_t num_points_local = x.rows();
165  const int size = dolfinx::MPI::size(comm);
166  const int rank = dolfinx::MPI::rank(comm);
167  std::vector<std::int64_t> global_sizes(size);
168  MPI_Allgather(&num_points_local, 1, MPI_INT64_T, global_sizes.data(), 1,
169  MPI_INT64_T, comm);
170  std::vector<std::int64_t> global_offsets(size + 1, 0);
171  std::partial_sum(global_sizes.begin(), global_sizes.end(),
172  global_offsets.begin() + 1);
173 
174  // Build index data requests
175  std::vector<int> number_index_send(size, 0);
176  std::vector<int> index_owner(indices.size());
177  std::vector<int> index_order(indices.size());
178  std::iota(index_order.begin(), index_order.end(), 0);
179  std::sort(index_order.begin(), index_order.end(),
180  [&indices](int a, int b) { return (indices[a] < indices[b]); });
181 
182  int p = 0;
183  for (std::size_t i = 0; i < index_order.size(); ++i)
184  {
185  int j = index_order[i];
186  while (indices[j] >= global_offsets[p + 1])
187  ++p;
188  index_owner[j] = p;
189  number_index_send[p]++;
190  }
191 
192  // Compute send displacements
193  std::vector<int> disp_index_send(size + 1, 0);
194  std::partial_sum(number_index_send.begin(), number_index_send.end(),
195  disp_index_send.begin() + 1);
196 
197  // Pack global index send data
198  std::vector<std::int64_t> indices_send(disp_index_send.back());
199  std::vector<int> disp_tmp = disp_index_send;
200  for (std::size_t i = 0; i < indices.size(); ++i)
201  {
202  const int owner = index_owner[i];
203  indices_send[disp_tmp[owner]++] = indices[i];
204  }
205 
206  // Send/receive number of indices to communicate to each process
207  std::vector<int> number_index_recv(size);
208  MPI_Alltoall(number_index_send.data(), 1, MPI_INT, number_index_recv.data(),
209  1, MPI_INT, comm);
210 
211  // Compute receive displacements
212  std::vector<int> disp_index_recv(size + 1, 0);
213  std::partial_sum(number_index_recv.begin(), number_index_recv.end(),
214  disp_index_recv.begin() + 1);
215 
216  // Send/receive global indices
217  std::vector<std::int64_t> indices_recv(disp_index_recv.back());
218  MPI_Alltoallv(indices_send.data(), number_index_send.data(),
219  disp_index_send.data(), MPI_INT64_T, indices_recv.data(),
220  number_index_recv.data(), disp_index_recv.data(), MPI_INT64_T,
221  comm);
222 
223  const int item_size = x.cols();
224  assert(item_size != 0);
225  // Pack point data to send back (transpose)
226  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> x_return(
227  indices_recv.size(), item_size);
228  for (int p = 0; p < size; ++p)
229  {
230  for (int i = disp_index_recv[p]; i < disp_index_recv[p + 1]; ++i)
231  {
232  const std::int32_t index_local = indices_recv[i] - global_offsets[rank];
233  assert(index_local >= 0);
234  x_return.row(i) = x.row(index_local);
235  }
236  }
237 
238  MPI_Datatype compound_type;
239  MPI_Type_contiguous(item_size, dolfinx::MPI::mpi_type<T>(), &compound_type);
240  MPI_Type_commit(&compound_type);
241 
242  // Send back point data
243  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> my_x(
244  disp_index_send.back(), item_size);
245  MPI_Alltoallv(x_return.data(), number_index_recv.data(),
246  disp_index_recv.data(), compound_type, my_x.data(),
247  number_index_send.data(), disp_index_send.data(), compound_type,
248  comm);
249 
250  return my_x;
251 }
252 
253 } // namespace dolfinx::graph
dolfinx::common::IndexMap
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:45
dolfinx::graph
Graph data structures and algorithms.
Definition: AdjacencyList.h:18
dolfinx::graph::Partitioning::distribute
std::tuple< graph::AdjacencyList< std::int64_t >, std::vector< int >, std::vector< std::int64_t >, std::vector< int > > distribute(MPI_Comm comm, const graph::AdjacencyList< std::int64_t > &list, const graph::AdjacencyList< std::int32_t > &destinations)
Distribute adjacency list nodes to destination ranks. The global index of each node is assumed to be ...
Definition: Partitioning.cpp:420
dolfinx::graph::AdjacencyList
This class provides a static adjacency list data structure. It is commonly used to store directed gra...
Definition: AdjacencyList.h:28
dolfinx::MPI::rank
static int rank(MPI_Comm comm)
Return process rank for the communicator.
Definition: MPI.cpp:79
dolfinx::MPI::size
static int size(MPI_Comm comm)
Return size of the group (number of processes) associated with the communicator.
Definition: MPI.cpp:87
dolfinx::graph::Partitioning::compute_local_to_local
std::vector< std::int32_t > compute_local_to_local(const std::vector< std::int64_t > &local0_to_global, const std::vector< std::int64_t > &local1_to_global)
Compute a local0-to-local1 map from two local-to-global maps with common global indices.
Definition: Partitioning.cpp:685
dolfinx::graph::Partitioning::create_distributed_adjacency_list
std::tuple< graph::AdjacencyList< std::int32_t >, common::IndexMap > create_distributed_adjacency_list(MPI_Comm comm, const graph::AdjacencyList< std::int32_t > &list_local, const std::vector< std::int64_t > &local_to_global_links, const std::vector< bool > &shared_links)
Build a distributed AdjacencyList list with re-numbered links from an AdjacencyList that may have non...
Definition: Partitioning.cpp:391
dolfinx::graph::Partitioning::distribute_data
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > distribute_data(MPI_Comm comm, const std::vector< std::int64_t > &indices, const Eigen::Ref< const Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor >> &x)
Distribute data to process ranks where it it required.
Definition: Partitioning.h:157
dolfinx::graph::Partitioning::create_local_adjacency_list
std::pair< graph::AdjacencyList< std::int32_t >, std::vector< std::int64_t > > create_local_adjacency_list(const graph::AdjacencyList< std::int64_t > &list)
Compute a local AdjacencyList list with contiguous indices from an AdjacencyList that may have non-co...
Definition: Partitioning.cpp:359
dolfinx::graph::Partitioning::reorder_global_indices
std::tuple< std::vector< std::int32_t >, std::vector< std::int64_t >, std::vector< int > > reorder_global_indices(MPI_Comm comm, const std::vector< std::int64_t > &global_indices, const std::vector< bool > &shared_indices)
Definition: Partitioning.cpp:22
dolfinx::graph::Partitioning::compute_ghost_indices
std::vector< std::int64_t > compute_ghost_indices(MPI_Comm comm, const std::vector< std::int64_t > &global_indices, const std::vector< int > &ghost_owners)
Compute ghost indices in a global IndexMap space, from a list of arbitrary global indices,...
Definition: Partitioning.cpp:535
dolfinx::common::Timer
A timer can be used for timing tasks. The basic usage is.
Definition: Timer.h:31
dolfinx::graph::Partitioning::compute_local_to_global_links
std::vector< std::int64_t > compute_local_to_global_links(const graph::AdjacencyList< std::int64_t > &global, const graph::AdjacencyList< std::int32_t > &local)
Given an adjacency list with global, possibly non-contiguous, link indices and a local adjacency list...
Definition: Partitioning.cpp:651