From 26370f309edb5cc61d07aa2a288594a57a546286 Mon Sep 17 00:00:00 2001 From: Peter Munch Date: Sun, 27 Jun 2021 16:29:49 +0200 Subject: [PATCH] Introduce RepartitioningPolicyTools::CellWeightPolicy --- .../distributed/repartitioning_policy_tools.h | 30 +++ .../repartitioning_policy_tools.cc | 85 ++++++ .../repartitioning_policy_tools.inst.in | 3 + .../repartitioning_03.cc | 10 + .../repartitioning_03.mpirun=5.output | 245 ++++++++++++++++++ 5 files changed, 373 insertions(+) diff --git a/include/deal.II/distributed/repartitioning_policy_tools.h b/include/deal.II/distributed/repartitioning_policy_tools.h index 1c190a25ba..3bdcf77945 100644 --- a/include/deal.II/distributed/repartitioning_policy_tools.h +++ b/include/deal.II/distributed/repartitioning_policy_tools.h @@ -132,6 +132,36 @@ namespace RepartitioningPolicyTools const unsigned int n_min_cells; }; + /** + * A policy that allows to specify a weight of each cell. The underlying + * algorithm will try to distribute the weights equally among the processes. + */ + template + class CellWeightPolicy : public Base + { + public: + /** + * Constructor taking a function that gives a weight to each cell. + */ + CellWeightPolicy( + const std::function::cell_iterator &, + const typename Triangulation::CellStatus)> + &weighting_function); + + virtual LinearAlgebra::distributed::Vector + partition(const Triangulation &tria_in) const override; + + private: + /** + * A function that gives a weight to each cell. + */ + const std::function< + unsigned int(const typename Triangulation::cell_iterator &, + const typename Triangulation::CellStatus)> + weighting_function; + }; + } // namespace RepartitioningPolicyTools DEAL_II_NAMESPACE_CLOSE diff --git a/source/distributed/repartitioning_policy_tools.cc b/source/distributed/repartitioning_policy_tools.cc index a7e35036f4..a76c913f22 100644 --- a/source/distributed/repartitioning_policy_tools.cc +++ b/source/distributed/repartitioning_policy_tools.cc @@ -228,6 +228,91 @@ namespace RepartitioningPolicyTools } + + template + CellWeightPolicy::CellWeightPolicy( + const std::function< + unsigned int(const typename Triangulation::cell_iterator &, + const typename Triangulation::CellStatus)> + &weighting_function) + : weighting_function(weighting_function) + {} + + + + template + LinearAlgebra::distributed::Vector + CellWeightPolicy::partition( + const Triangulation &tria_in) const + { +#ifndef DEAL_II_WITH_MPI + (void)tria_in; + return {}; +#else + + const auto tria = + dynamic_cast *>( + &tria_in); + + Assert(tria, ExcNotImplemented()); + + const auto partitioner = + tria->global_active_cell_index_partitioner().lock(); + + std::vector weights(partitioner->local_size()); + + const auto mpi_communicator = tria_in.get_communicator(); + const auto n_subdomains = Utilities::MPI::n_mpi_processes(mpi_communicator); + + // determine weight of each cell + for (const auto &cell : tria->active_cell_iterators()) + if (cell->is_locally_owned()) + weights[partitioner->global_to_local( + cell->global_active_cell_index())] = + weighting_function( + cell, Triangulation::CellStatus::CELL_PERSIST); + + // determine weight of all the cells locally owned by this process + types::global_cell_index process_local_weight = 0; + for (const auto &weight : weights) + process_local_weight += weight; + + // determine partial sum of weights of this process + types::global_cell_index process_local_weight_offset = 0; + + int ierr = + MPI_Exscan(&process_local_weight, + &process_local_weight_offset, + 1, + Utilities::MPI::internal::mpi_type_id(&process_local_weight), + MPI_SUM, + tria->get_communicator()); + AssertThrowMPI(ierr); + + // total weight of all processes + types::global_cell_index total_weight = + process_local_weight_offset + process_local_weight; + + ierr = MPI_Bcast(&total_weight, + 1, + Utilities::MPI::internal::mpi_type_id(&total_weight), + n_subdomains - 1, + mpi_communicator); + + // setup partition + LinearAlgebra::distributed::Vector partition(partitioner); + + for (types::global_cell_index i = 0, weight = process_local_weight_offset; + i < partition.locally_owned_size(); + weight += weights[i], ++i) + partition.local_element(i) = + static_cast(weight * n_subdomains / total_weight); + + return partition; +#endif + } + + } // namespace RepartitioningPolicyTools diff --git a/source/distributed/repartitioning_policy_tools.inst.in b/source/distributed/repartitioning_policy_tools.inst.in index 7ed65d4b7d..25f159e990 100644 --- a/source/distributed/repartitioning_policy_tools.inst.in +++ b/source/distributed/repartitioning_policy_tools.inst.in @@ -27,5 +27,8 @@ for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS) template class RepartitioningPolicyTools:: MinimalGranularityPolicy; + template class RepartitioningPolicyTools:: + CellWeightPolicy; + #endif } diff --git a/tests/fullydistributed_grids/repartitioning_03.cc b/tests/fullydistributed_grids/repartitioning_03.cc index 765de0a074..66482a5f33 100644 --- a/tests/fullydistributed_grids/repartitioning_03.cc +++ b/tests/fullydistributed_grids/repartitioning_03.cc @@ -133,4 +133,14 @@ main(int argc, char **argv) // first-child policy test(RepartitioningPolicyTools::MinimalGranularityPolicy(4), "grid_policy_minimal"); + + // cell-weight policy + test(RepartitioningPolicyTools::CellWeightPolicy( + [](const auto &cell, const auto) { + return (cell->global_active_cell_index() < + cell->get_triangulation().n_global_active_cells() / 2) ? + 1.0 : + 2.0; + }), + "grid_policy_weight"); } diff --git a/tests/fullydistributed_grids/repartitioning_03.mpirun=5.output b/tests/fullydistributed_grids/repartitioning_03.mpirun=5.output index 469b3b3d2c..c9038e213d 100644 --- a/tests/fullydistributed_grids/repartitioning_03.mpirun=5.output +++ b/tests/fullydistributed_grids/repartitioning_03.mpirun=5.output @@ -146,6 +146,55 @@ DEAL:0:grid_policy_minimal:: DEAL:0:grid_policy_minimal::n_dofs: 4813 DEAL:0:grid_policy_minimal::n_locally_owned_dofs: 993 DEAL:0:grid_policy_minimal:: +DEAL:0:grid_policy_weight::n_levels: 1 +DEAL:0:grid_policy_weight::n_cells: 1 +DEAL:0:grid_policy_weight::n_active_cells: 1 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 9 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 9 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 2 +DEAL:0:grid_policy_weight::n_cells: 5 +DEAL:0:grid_policy_weight::n_active_cells: 4 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 25 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 15 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 3 +DEAL:0:grid_policy_weight::n_cells: 9 +DEAL:0:grid_policy_weight::n_active_cells: 7 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 43 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 21 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 4 +DEAL:0:grid_policy_weight::n_cells: 33 +DEAL:0:grid_policy_weight::n_active_cells: 25 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 141 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 51 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 5 +DEAL:0:grid_policy_weight::n_cells: 93 +DEAL:0:grid_policy_weight::n_active_cells: 70 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 423 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 137 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 6 +DEAL:0:grid_policy_weight::n_cells: 233 +DEAL:0:grid_policy_weight::n_active_cells: 175 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 1353 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 421 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_levels: 7 +DEAL:0:grid_policy_weight::n_cells: 465 +DEAL:0:grid_policy_weight::n_active_cells: 349 +DEAL:0:grid_policy_weight:: +DEAL:0:grid_policy_weight::n_dofs: 4813 +DEAL:0:grid_policy_weight::n_locally_owned_dofs: 993 +DEAL:0:grid_policy_weight:: DEAL:1:grid_policy_default::n_levels: 1 DEAL:1:grid_policy_default::n_cells: 1 @@ -294,6 +343,55 @@ DEAL:1:grid_policy_minimal:: DEAL:1:grid_policy_minimal::n_dofs: 4813 DEAL:1:grid_policy_minimal::n_locally_owned_dofs: 944 DEAL:1:grid_policy_minimal:: +DEAL:1:grid_policy_weight::n_levels: 1 +DEAL:1:grid_policy_weight::n_cells: 1 +DEAL:1:grid_policy_weight::n_active_cells: 1 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 9 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 2 +DEAL:1:grid_policy_weight::n_cells: 5 +DEAL:1:grid_policy_weight::n_active_cells: 4 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 25 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 6 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 3 +DEAL:1:grid_policy_weight::n_cells: 9 +DEAL:1:grid_policy_weight::n_active_cells: 7 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 43 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 4 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 4 +DEAL:1:grid_policy_weight::n_cells: 37 +DEAL:1:grid_policy_weight::n_active_cells: 28 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 141 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 30 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 5 +DEAL:1:grid_policy_weight::n_cells: 105 +DEAL:1:grid_policy_weight::n_active_cells: 79 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 423 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 96 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 6 +DEAL:1:grid_policy_weight::n_cells: 289 +DEAL:1:grid_policy_weight::n_active_cells: 217 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 1353 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 328 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_levels: 7 +DEAL:1:grid_policy_weight::n_cells: 577 +DEAL:1:grid_policy_weight::n_active_cells: 433 +DEAL:1:grid_policy_weight:: +DEAL:1:grid_policy_weight::n_dofs: 4813 +DEAL:1:grid_policy_weight::n_locally_owned_dofs: 944 +DEAL:1:grid_policy_weight:: DEAL:2:grid_policy_default::n_levels: 1 @@ -443,6 +541,55 @@ DEAL:2:grid_policy_minimal:: DEAL:2:grid_policy_minimal::n_dofs: 4813 DEAL:2:grid_policy_minimal::n_locally_owned_dofs: 960 DEAL:2:grid_policy_minimal:: +DEAL:2:grid_policy_weight::n_levels: 1 +DEAL:2:grid_policy_weight::n_cells: 1 +DEAL:2:grid_policy_weight::n_active_cells: 1 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 9 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 1 +DEAL:2:grid_policy_weight::n_cells: 1 +DEAL:2:grid_policy_weight::n_active_cells: 1 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 25 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 3 +DEAL:2:grid_policy_weight::n_cells: 9 +DEAL:2:grid_policy_weight::n_active_cells: 7 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 43 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 7 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 4 +DEAL:2:grid_policy_weight::n_cells: 29 +DEAL:2:grid_policy_weight::n_active_cells: 22 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 141 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 22 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 5 +DEAL:2:grid_policy_weight::n_cells: 93 +DEAL:2:grid_policy_weight::n_active_cells: 70 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 423 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 70 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 6 +DEAL:2:grid_policy_weight::n_cells: 241 +DEAL:2:grid_policy_weight::n_active_cells: 181 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 1353 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 203 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_levels: 7 +DEAL:2:grid_policy_weight::n_cells: 665 +DEAL:2:grid_policy_weight::n_active_cells: 499 +DEAL:2:grid_policy_weight:: +DEAL:2:grid_policy_weight::n_dofs: 4813 +DEAL:2:grid_policy_weight::n_locally_owned_dofs: 960 +DEAL:2:grid_policy_weight:: DEAL:3:grid_policy_default::n_levels: 1 @@ -592,6 +739,55 @@ DEAL:3:grid_policy_minimal:: DEAL:3:grid_policy_minimal::n_dofs: 4813 DEAL:3:grid_policy_minimal::n_locally_owned_dofs: 912 DEAL:3:grid_policy_minimal:: +DEAL:3:grid_policy_weight::n_levels: 1 +DEAL:3:grid_policy_weight::n_cells: 1 +DEAL:3:grid_policy_weight::n_active_cells: 1 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 9 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 2 +DEAL:3:grid_policy_weight::n_cells: 5 +DEAL:3:grid_policy_weight::n_active_cells: 4 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 25 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 4 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 3 +DEAL:3:grid_policy_weight::n_cells: 9 +DEAL:3:grid_policy_weight::n_active_cells: 7 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 43 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 7 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 4 +DEAL:3:grid_policy_weight::n_cells: 29 +DEAL:3:grid_policy_weight::n_active_cells: 22 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 141 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 22 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 5 +DEAL:3:grid_policy_weight::n_cells: 81 +DEAL:3:grid_policy_weight::n_active_cells: 61 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 423 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 64 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 6 +DEAL:3:grid_policy_weight::n_cells: 229 +DEAL:3:grid_policy_weight::n_active_cells: 172 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 1353 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 201 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_levels: 7 +DEAL:3:grid_policy_weight::n_cells: 657 +DEAL:3:grid_policy_weight::n_active_cells: 493 +DEAL:3:grid_policy_weight:: +DEAL:3:grid_policy_weight::n_dofs: 4813 +DEAL:3:grid_policy_weight::n_locally_owned_dofs: 912 +DEAL:3:grid_policy_weight:: DEAL:4:grid_policy_default::n_levels: 1 @@ -741,4 +937,53 @@ DEAL:4:grid_policy_minimal:: DEAL:4:grid_policy_minimal::n_dofs: 4813 DEAL:4:grid_policy_minimal::n_locally_owned_dofs: 1004 DEAL:4:grid_policy_minimal:: +DEAL:4:grid_policy_weight::n_levels: 1 +DEAL:4:grid_policy_weight::n_cells: 1 +DEAL:4:grid_policy_weight::n_active_cells: 1 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 9 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 1 +DEAL:4:grid_policy_weight::n_cells: 1 +DEAL:4:grid_policy_weight::n_active_cells: 1 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 25 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 0 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 3 +DEAL:4:grid_policy_weight::n_cells: 9 +DEAL:4:grid_policy_weight::n_active_cells: 7 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 43 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 4 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 4 +DEAL:4:grid_policy_weight::n_cells: 25 +DEAL:4:grid_policy_weight::n_active_cells: 19 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 141 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 16 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 5 +DEAL:4:grid_policy_weight::n_cells: 61 +DEAL:4:grid_policy_weight::n_active_cells: 46 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 423 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 56 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 6 +DEAL:4:grid_policy_weight::n_cells: 189 +DEAL:4:grid_policy_weight::n_active_cells: 142 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 1353 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 200 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_levels: 7 +DEAL:4:grid_policy_weight::n_cells: 541 +DEAL:4:grid_policy_weight::n_active_cells: 406 +DEAL:4:grid_policy_weight:: +DEAL:4:grid_policy_weight::n_dofs: 4813 +DEAL:4:grid_policy_weight::n_locally_owned_dofs: 1004 +DEAL:4:grid_policy_weight:: -- 2.39.5