18#include <deal.II/base/partitioner.templates.h>
20#include <boost/serialization/utility.hpp>
33 , n_ghost_indices_data(0)
34 , n_import_indices_data(0)
35 , n_ghost_indices_in_larger_set(0)
38 , communicator(MPI_COMM_SELF)
39 , have_ghost_indices(false)
44 Partitioner::Partitioner(
const unsigned int size)
46 , locally_owned_range_data(size)
49 , n_ghost_indices_data(0)
50 , n_import_indices_data(0)
51 , n_ghost_indices_in_larger_set(0)
54 , communicator(MPI_COMM_SELF)
55 , have_ghost_indices(false)
57 locally_owned_range_data.add_range(0, size);
58 locally_owned_range_data.compress();
59 ghost_indices_data.set_size(size);
69 , locally_owned_range_data(global_size)
70 , local_range_data{0, local_size}
71 , n_ghost_indices_data(ghost_size)
72 , n_import_indices_data(0)
73 , n_ghost_indices_in_larger_set(0)
76 , communicator(communicator)
77 , have_ghost_indices(true)
81#ifdef DEAL_II_WITH_MPI
83 MPI_Exscan(&local_size,
92 local_range_data = {prefix_sum, prefix_sum + local_size};
94 locally_owned_range_data.add_range(prefix_sum, prefix_sum + local_size);
95 locally_owned_range_data.compress();
100 Partitioner::Partitioner(
const IndexSet &locally_owned_indices,
105 , n_ghost_indices_data(0)
106 , n_import_indices_data(0)
107 , n_ghost_indices_in_larger_set(0)
110 , communicator(communicator_in)
111 , have_ghost_indices(false)
113 set_owned_indices(locally_owned_indices);
114 set_ghost_indices(ghost_indices_in);
119 Partitioner::Partitioner(
const IndexSet &locally_owned_indices,
123 , n_ghost_indices_data(0)
124 , n_import_indices_data(0)
125 , n_ghost_indices_in_larger_set(0)
128 , communicator(communicator_in)
129 , have_ghost_indices(false)
131 set_owned_indices(locally_owned_indices);
137 Partitioner::reinit(
const IndexSet &vector_space_vector_index_set,
138 const IndexSet &read_write_vector_index_set,
141 have_ghost_indices =
false;
142 communicator = communicator_in;
143 set_owned_indices(vector_space_vector_index_set);
144 set_ghost_indices(read_write_vector_index_set);
150 Partitioner::set_owned_indices(
const IndexSet &locally_owned_indices)
165 ExcMessage(
"The index set specified in locally_owned_indices "
166 "is not contiguous."));
170 std::pair<types::global_dof_index, types::global_dof_index>(
175 local_range_data.second - local_range_data.first <
177 std::numeric_limits<unsigned int>::max()),
179 "Index overflow: This class supports at most 2^32-1 locally owned vector entries"));
180 locally_owned_range_data.set_size(locally_owned_indices.
size());
181 locally_owned_range_data.add_range(local_range_data.first,
182 local_range_data.second);
183 locally_owned_range_data.compress();
185 ghost_indices_data.set_size(locally_owned_indices.
size());
191 Partitioner::set_ghost_indices(
const IndexSet &ghost_indices_in,
192 const IndexSet &larger_ghost_index_set)
198 ghost_indices_in.
size() == locally_owned_range_data.size(),
200 locally_owned_range_data.size()));
202 ghost_indices_data = ghost_indices_in;
203 if (ghost_indices_data.size() != locally_owned_range_data.size())
204 ghost_indices_data.
set_size(locally_owned_range_data.size());
205 ghost_indices_data.subtract_set(locally_owned_range_data);
206 ghost_indices_data.compress();
208 ghost_indices_data.n_elements() <
210 std::numeric_limits<unsigned int>::max()),
212 "Index overflow: This class supports at most 2^32-1 ghost elements"));
213 n_ghost_indices_data = ghost_indices_data.n_elements();
226#ifdef DEAL_II_WITH_MPI
241 my_size += local_range_data.first;
245 const int ierr = MPI_Exscan(&my_size,
260 my_shift = local_range_data.first;
264 if (local_range_data.first == 0 && my_shift != 0)
267 locally_owned_size();
268 local_range_data.first = my_shift;
269 local_range_data.second = my_shift + old_locally_owned_size;
272 std::vector<unsigned int> owning_ranks_of_ghosts(
273 ghost_indices_data.n_elements());
276 internal::ComputeIndexOwner::ConsensusAlgorithmsPayload process(
277 locally_owned_range_data,
280 owning_ranks_of_ghosts,
286 ConsensusAlgorithms::Selector<
288 std::pair<types::global_dof_index, types::global_dof_index>>,
289 std::vector<unsigned int>>
290 consensus_algorithm(process, communicator);
291 consensus_algorithm.run();
294 ghost_targets_data = {};
296 if (owning_ranks_of_ghosts.size() > 0)
298 ghost_targets_data.emplace_back(owning_ranks_of_ghosts[0], 0);
299 for (
auto i : owning_ranks_of_ghosts)
301 Assert(i >= ghost_targets_data.back().first,
303 "Expect result of ConsensusAlgorithms::Process to be "
305 if (i == ghost_targets_data.back().first)
306 ghost_targets_data.back().second++;
308 ghost_targets_data.emplace_back(i, 1);
314 std::map<unsigned int, IndexSet> import_data = process.get_requesters();
317 n_import_indices_data = 0;
318 import_targets_data = {};
319 import_targets_data.reserve(import_data.size());
320 import_indices_chunks_by_rank_data = {};
321 import_indices_chunks_by_rank_data.reserve(import_data.size());
322 import_indices_chunks_by_rank_data.resize(1);
323 for (
const auto &i : import_data)
324 if (i.second.n_elements() > 0)
326 import_targets_data.emplace_back(i.first, i.second.n_elements());
327 n_import_indices_data += i.second.n_elements();
328 import_indices_chunks_by_rank_data.push_back(
329 import_indices_chunks_by_rank_data.back() +
330 i.second.n_intervals());
334 import_indices_data = {};
335 import_indices_data.reserve(import_indices_chunks_by_rank_data.back());
336 for (
const auto &i : import_data)
338 Assert((i.second & locally_owned_range_data) == i.second,
340 for (
auto interval = i.second.begin_intervals();
341 interval != i.second.end_intervals();
343 import_indices_data.emplace_back(*interval->begin() -
344 local_range_data.first,
345 interval->last() + 1 -
346 local_range_data.first);
367 std::vector<types::global_dof_index> ghost_indices_ref;
368 ghost_indices_data.fill_index_vector(ghost_indices_ref);
370 std::vector<types::global_dof_index> indices_to_send(n_import_indices());
371 std::vector<types::global_dof_index> ghost_indices(n_ghost_indices());
372 std::vector<types::global_dof_index> my_indices;
373 locally_owned_range_data.fill_index_vector(my_indices);
374 std::vector<MPI_Request> requests;
375 n_ghost_indices_in_larger_set = n_ghost_indices_data;
376 export_to_ghosted_array_start(127,
378 my_indices.data(), my_indices.size()),
382 export_to_ghosted_array_finish(
make_array_view(ghost_indices), requests);
384 const int ierr = MPI_Testall(requests.size(),
387 MPI_STATUSES_IGNORE);
391 "MPI found unfinished requests. Check communication setup"));
393 for (
unsigned int i = 0; i < ghost_indices.size(); ++i)
400 if (larger_ghost_index_set.
size() == 0)
402 ghost_indices_subset_chunks_by_rank_data.clear();
403 ghost_indices_subset_data.emplace_back(0, n_ghost_indices());
404 n_ghost_indices_in_larger_set = n_ghost_indices_data;
409 ghost_indices_data.size());
411 (larger_ghost_index_set & locally_owned_range_data).n_elements() ==
413 ExcMessage(
"Ghost index set should not overlap with owned set."));
414 Assert((larger_ghost_index_set & ghost_indices_data) ==
416 ExcMessage(
"Larger ghost index set must contain the tight "
417 "ghost index set."));
419 n_ghost_indices_in_larger_set = larger_ghost_index_set.
n_elements();
423 std::vector<unsigned int> expanded_numbering;
427 ExcMessage(
"The given larger ghost index set must contain "
428 "all indices in the actual index set."));
432 std::numeric_limits<unsigned int>::max()),
434 "Index overflow: This class supports at most 2^32-1 ghost elements"));
435 expanded_numbering.push_back(
440 std::vector<std::pair<unsigned int, unsigned int>>
441 ghost_indices_subset;
442 ghost_indices_subset_chunks_by_rank_data.resize(
443 ghost_targets_data.size() + 1);
445 ghost_indices_subset_chunks_by_rank_data[0] = 0;
446 unsigned int shift = 0;
447 for (
unsigned int p = 0; p < ghost_targets_data.size(); ++p)
450 for (
unsigned int ii = 0; ii < ghost_targets_data[p].second; ++ii)
452 const unsigned int i =
shift + ii;
453 if (expanded_numbering[i] == last_index + 1)
455 ghost_indices_subset.back().second++;
458 ghost_indices_subset.emplace_back(expanded_numbering[i],
459 expanded_numbering[i] +
461 last_index = expanded_numbering[i];
463 shift += ghost_targets_data[p].second;
464 ghost_indices_subset_chunks_by_rank_data[p + 1] =
465 ghost_indices_subset.size();
467 ghost_indices_subset_data = ghost_indices_subset;
474 Partitioner::is_compatible(
const Partitioner &part)
const
480#ifdef DEAL_II_WITH_MPI
483 int communicators_same = 0;
484 const int ierr = MPI_Comm_compare(part.communicator,
486 &communicators_same);
488 if (!(communicators_same == MPI_IDENT ||
489 communicators_same == MPI_CONGRUENT))
493 return (global_size == part.global_size &&
494 local_range_data == part.local_range_data &&
495 ghost_indices_data == part.ghost_indices_data);
501 Partitioner::is_globally_compatible(
const Partitioner &part)
const
510 Partitioner::memory_consumption()
const
513 4 *
sizeof(
unsigned int) +
sizeof(
MPI_Comm));
519 import_indices_chunks_by_rank_data);
521 ghost_indices_subset_chunks_by_rank_data);
535#include "partitioner.inst"
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
bool is_contiguous() const
size_type index_within_set(const size_type global_index) const
size_type n_elements() const
bool is_element(const size_type index) const
void set_size(const size_type size)
size_type nth_index_in_set(const size_type local_index) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
T min(const T &t, const MPI_Comm &mpi_communicator)
T sum(const T &t, const MPI_Comm &mpi_communicator)
const MPI_Datatype mpi_type_id_for_type
unsigned int n_mpi_processes(const MPI_Comm &mpi_communicator)
T max(const T &t, const MPI_Comm &mpi_communicator)
static const unsigned int invalid_unsigned_int
unsigned int global_dof_index
#define DEAL_II_DOF_INDEX_MPI_TYPE