18#include <deal.II/base/partitioner.templates.h>
20#include <boost/serialization/utility.hpp>
35 , n_ghost_indices_data(0)
36 , n_import_indices_data(0)
37 , n_ghost_indices_in_larger_set(0)
40 , communicator(MPI_COMM_SELF)
41 , have_ghost_indices(false)
46 Partitioner::Partitioner(
const unsigned int size)
48 , locally_owned_range_data(
size)
51 , n_ghost_indices_data(0)
52 , n_import_indices_data(0)
53 , n_ghost_indices_in_larger_set(0)
56 , communicator(MPI_COMM_SELF)
57 , have_ghost_indices(false)
59 locally_owned_range_data.add_range(0,
size);
60 locally_owned_range_data.compress();
61 ghost_indices_data.set_size(
size);
71 , locally_owned_range_data(global_size)
72 , local_range_data{0, local_size}
73 , n_ghost_indices_data(ghost_size)
74 , n_import_indices_data(0)
75 , n_ghost_indices_in_larger_set(0)
78 , communicator(communicator)
79 , have_ghost_indices(true)
83# ifdef DEAL_II_WITH_MPI
85 MPI_Exscan(&local_size,
94 local_range_data = {prefix_sum, prefix_sum + local_size};
96 locally_owned_range_data.add_range(prefix_sum, prefix_sum + local_size);
97 locally_owned_range_data.compress();
102 Partitioner::Partitioner(
const IndexSet &locally_owned_indices,
107 , n_ghost_indices_data(0)
108 , n_import_indices_data(0)
109 , n_ghost_indices_in_larger_set(0)
112 , communicator(communicator_in)
113 , have_ghost_indices(false)
115 set_owned_indices(locally_owned_indices);
116 set_ghost_indices(ghost_indices_in);
121 Partitioner::Partitioner(
const IndexSet &locally_owned_indices,
125 , n_ghost_indices_data(0)
126 , n_import_indices_data(0)
127 , n_ghost_indices_in_larger_set(0)
130 , communicator(communicator_in)
131 , have_ghost_indices(false)
133 set_owned_indices(locally_owned_indices);
139 Partitioner::reinit(
const IndexSet &locally_owned_indices,
143 have_ghost_indices =
false;
144 communicator = communicator_in;
145 set_owned_indices(locally_owned_indices);
146 set_ghost_indices(ghost_indices);
152 Partitioner::set_owned_indices(
const IndexSet &locally_owned_indices)
159 ExcMessage(
"The index set specified in locally_owned_indices "
160 "is not contiguous."));
164 std::pair<types::global_dof_index, types::global_dof_index>(
169 local_range_data.second - local_range_data.first <
171 std::numeric_limits<unsigned int>::max()),
173 "Index overflow: This class supports at most 2^32-1 locally owned vector entries"));
174 locally_owned_range_data.set_size(locally_owned_indices.
size());
175 locally_owned_range_data.add_range(local_range_data.first,
176 local_range_data.second);
177 locally_owned_range_data.compress();
179 ghost_indices_data.set_size(locally_owned_indices.
size());
185 Partitioner::set_ghost_indices(
const IndexSet &ghost_indices_in,
186 const IndexSet &larger_ghost_index_set)
192 ghost_indices_in.
size() == locally_owned_range_data.size(),
194 locally_owned_range_data.size()));
196 ghost_indices_data = ghost_indices_in;
197 if (ghost_indices_data.size() != locally_owned_range_data.size())
198 ghost_indices_data.
set_size(locally_owned_range_data.size());
199 ghost_indices_data.subtract_set(locally_owned_range_data);
200 ghost_indices_data.compress();
202 ghost_indices_data.n_elements() <
204 std::numeric_limits<unsigned int>::max()),
206 "Index overflow: This class supports at most 2^32-1 ghost elements"));
207 n_ghost_indices_data = ghost_indices_data.n_elements();
220# ifdef DEAL_II_WITH_MPI
235 my_size += local_range_data.first;
239 const int ierr = MPI_Exscan(&my_size,
254 my_shift = local_range_data.first;
258 if (local_range_data.first == 0 && my_shift != 0)
262 local_range_data.first = my_shift;
263 local_range_data.second = my_shift + old_locally_owned_size;
266 const auto [owning_ranks_of_ghosts, import_data] =
268 locally_owned_range_data, ghost_indices_data, communicator);
272 ghost_targets_data = {};
274 if (owning_ranks_of_ghosts.size() > 0)
276 ghost_targets_data.emplace_back(owning_ranks_of_ghosts[0], 0);
277 for (
auto i : owning_ranks_of_ghosts)
279 Assert(i >= ghost_targets_data.back().first,
281 "Expect result of ConsensusAlgorithms::Process to be "
283 if (i == ghost_targets_data.back().first)
284 ghost_targets_data.back().second++;
286 ghost_targets_data.emplace_back(i, 1);
292 n_import_indices_data = 0;
293 import_targets_data = {};
294 import_targets_data.reserve(import_data.size());
295 import_indices_chunks_by_rank_data = {};
296 import_indices_chunks_by_rank_data.reserve(import_data.size());
297 import_indices_chunks_by_rank_data.resize(1);
298 for (
const auto &i : import_data)
299 if (i.
second.n_elements() > 0)
301 import_targets_data.emplace_back(i.first, i.second.n_elements());
302 n_import_indices_data += i.second.n_elements();
303 import_indices_chunks_by_rank_data.push_back(
304 import_indices_chunks_by_rank_data.back() +
305 i.second.n_intervals());
309 import_indices_data = {};
310 import_indices_data.reserve(import_indices_chunks_by_rank_data.back());
311 for (
const auto &i : import_data)
313 Assert((i.second & locally_owned_range_data) == i.second,
315 for (
auto interval = i.second.begin_intervals();
316 interval != i.second.end_intervals();
318 import_indices_data.emplace_back(*interval->begin() -
319 local_range_data.first,
320 interval->last() + 1 -
321 local_range_data.first);
342 const std::vector<types::global_dof_index> ghost_indices_ref =
343 ghost_indices_data.get_index_vector();
345 std::vector<types::global_dof_index> indices_to_send(n_import_indices());
346 std::vector<types::global_dof_index> ghost_indices(n_ghost_indices());
348 const std::vector<types::global_dof_index> my_indices =
349 locally_owned_range_data.get_index_vector();
350 std::vector<MPI_Request> requests;
351 n_ghost_indices_in_larger_set = n_ghost_indices_data;
352 export_to_ghosted_array_start(127,
354 my_indices.data(), my_indices.size()),
358 export_to_ghosted_array_finish(
make_array_view(ghost_indices), requests);
360 const int ierr = MPI_Testall(requests.size(),
363 MPI_STATUSES_IGNORE);
367 "MPI found unfinished requests. Check communication setup"));
369 for (
unsigned int i = 0; i < ghost_indices.size(); ++i)
376 if (larger_ghost_index_set.
size() == 0)
378 ghost_indices_subset_chunks_by_rank_data.clear();
379 ghost_indices_subset_data.emplace_back(0, n_ghost_indices());
380 n_ghost_indices_in_larger_set = n_ghost_indices_data;
385 ghost_indices_data.size());
387 (larger_ghost_index_set & locally_owned_range_data).n_elements() ==
389 ExcMessage(
"Ghost index set should not overlap with owned set."));
390 Assert((larger_ghost_index_set & ghost_indices_data) ==
392 ExcMessage(
"Larger ghost index set must contain the tight "
393 "ghost index set."));
395 n_ghost_indices_in_larger_set = larger_ghost_index_set.
n_elements();
399 std::vector<unsigned int> expanded_numbering;
400 for (const ::IndexSet::size_type index : ghost_indices_data)
403 ExcMessage(
"The given larger ghost index set must contain "
404 "all indices in the actual index set."));
408 std::numeric_limits<unsigned int>::max()),
410 "Index overflow: This class supports at most 2^32-1 ghost elements"));
411 expanded_numbering.push_back(
416 std::vector<std::pair<unsigned int, unsigned int>>
417 ghost_indices_subset;
418 ghost_indices_subset_chunks_by_rank_data.resize(
419 ghost_targets_data.size() + 1);
421 ghost_indices_subset_chunks_by_rank_data[0] = 0;
422 unsigned int shift = 0;
423 for (
unsigned int p = 0; p < ghost_targets_data.size(); ++p)
426 for (
unsigned int ii = 0; ii < ghost_targets_data[p].second; ++ii)
428 const unsigned int i =
shift + ii;
429 if (expanded_numbering[i] == last_index + 1)
431 ghost_indices_subset.back().second++;
434 ghost_indices_subset.emplace_back(expanded_numbering[i],
435 expanded_numbering[i] +
437 last_index = expanded_numbering[i];
439 shift += ghost_targets_data[p].second;
440 ghost_indices_subset_chunks_by_rank_data[p + 1] =
441 ghost_indices_subset.size();
443 ghost_indices_subset_data = ghost_indices_subset;
450 Partitioner::is_compatible(
const Partitioner &part)
const
456# ifdef DEAL_II_WITH_MPI
459 int communicators_same = 0;
460 const int ierr = MPI_Comm_compare(part.communicator,
462 &communicators_same);
464 if (!(communicators_same == MPI_IDENT ||
465 communicators_same == MPI_CONGRUENT))
469 return (global_size == part.global_size &&
470 local_range_data == part.local_range_data &&
471 ghost_indices_data == part.ghost_indices_data);
477 Partitioner::is_globally_compatible(
const Partitioner &part)
const
486 Partitioner::memory_consumption()
const
489 memory += locally_owned_range_data.memory_consumption();
491 memory += ghost_indices_data.memory_consumption();
492 memory +=
sizeof(n_ghost_indices_data);
495 memory +=
sizeof(import_indices_plain_dev) +
496 sizeof(*import_indices_plain_dev.begin()) *
497 import_indices_plain_dev.capacity();
501 import_indices_chunks_by_rank_data);
505 ghost_indices_subset_chunks_by_rank_data);
518 Partitioner::initialize_import_indices_plain_dev()
const
520 const unsigned int n_import_targets = import_targets_data.size();
521 import_indices_plain_dev.reserve(n_import_targets);
522 for (
unsigned int i = 0; i < n_import_targets; ++i)
525 std::vector<std::pair<unsigned int, unsigned int>>::const_iterator
526 my_imports = import_indices_data.begin() +
527 import_indices_chunks_by_rank_data[i],
528 end_my_imports = import_indices_data.begin() +
529 import_indices_chunks_by_rank_data[i + 1];
530 std::vector<unsigned int> import_indices_plain_host;
531 for (; my_imports != end_my_imports; ++my_imports)
533 const unsigned int chunk_size =
534 my_imports->second - my_imports->first;
535 for (
unsigned int j = 0; j < chunk_size; ++j)
536 import_indices_plain_host.push_back(my_imports->first + j);
540 const auto chunk_size = import_indices_plain_host.size();
541 import_indices_plain_dev.emplace_back(
"import_indices_plain_dev" +
544 Kokkos::deep_copy(import_indices_plain_dev.back(),
545 Kokkos::View<unsigned int *, Kokkos::HostSpace>(
546 import_indices_plain_host.data(), chunk_size));
557#include "partitioner.inst"
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, 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)
types::global_dof_index locally_owned_size
const unsigned int n_procs
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
std::pair< std::vector< unsigned int >, std::map< unsigned int, IndexSet > > compute_index_owner_and_requesters(const IndexSet &owned_indices, const IndexSet &indices_to_look_up, const MPI_Comm &comm)
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
const MPI_Datatype mpi_type_id_for_type
static const unsigned int invalid_unsigned_int
unsigned int global_dof_index
#define DEAL_II_DOF_INDEX_MPI_TYPE