]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Introduce RepartitioningPolicyTools::CellWeightPolicy 12508/head
authorPeter Munch <peterrmuench@gmail.com>
Sun, 27 Jun 2021 14:29:49 +0000 (16:29 +0200)
committerPeter Munch <peterrmuench@gmail.com>
Mon, 28 Jun 2021 20:16:11 +0000 (22:16 +0200)
include/deal.II/distributed/repartitioning_policy_tools.h
source/distributed/repartitioning_policy_tools.cc
source/distributed/repartitioning_policy_tools.inst.in
tests/fullydistributed_grids/repartitioning_03.cc
tests/fullydistributed_grids/repartitioning_03.mpirun=5.output

index 1c190a25ba3874fe5a569503591d6df5b076f300..3bdcf779455830dddea62dfb4a09d5bfb5af7932 100644 (file)
@@ -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 <int dim, int spacedim = dim>
+  class CellWeightPolicy : public Base<dim, spacedim>
+  {
+  public:
+    /**
+     * Constructor taking a function that gives a weight to each cell.
+     */
+    CellWeightPolicy(
+      const std::function<unsigned int(
+        const typename Triangulation<dim, spacedim>::cell_iterator &,
+        const typename Triangulation<dim, spacedim>::CellStatus)>
+        &weighting_function);
+
+    virtual LinearAlgebra::distributed::Vector<double>
+    partition(const Triangulation<dim, spacedim> &tria_in) const override;
+
+  private:
+    /**
+     * A function that gives a weight to each cell.
+     */
+    const std::function<
+      unsigned int(const typename Triangulation<dim, spacedim>::cell_iterator &,
+                   const typename Triangulation<dim, spacedim>::CellStatus)>
+      weighting_function;
+  };
+
 } // namespace RepartitioningPolicyTools
 
 DEAL_II_NAMESPACE_CLOSE
index a7e35036f42772e8ff6a3a16848b3faec8c7c6af..a76c913f22cc2c57504691dccbe7a73195906ac4 100644 (file)
@@ -228,6 +228,91 @@ namespace RepartitioningPolicyTools
   }
 
 
+
+  template <int dim, int spacedim>
+  CellWeightPolicy<dim, spacedim>::CellWeightPolicy(
+    const std::function<
+      unsigned int(const typename Triangulation<dim, spacedim>::cell_iterator &,
+                   const typename Triangulation<dim, spacedim>::CellStatus)>
+      &weighting_function)
+    : weighting_function(weighting_function)
+  {}
+
+
+
+  template <int dim, int spacedim>
+  LinearAlgebra::distributed::Vector<double>
+  CellWeightPolicy<dim, spacedim>::partition(
+    const Triangulation<dim, spacedim> &tria_in) const
+  {
+#ifndef DEAL_II_WITH_MPI
+    (void)tria_in;
+    return {};
+#else
+
+    const auto tria =
+      dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
+        &tria_in);
+
+    Assert(tria, ExcNotImplemented());
+
+    const auto partitioner =
+      tria->global_active_cell_index_partitioner().lock();
+
+    std::vector<unsigned int> 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<dim, spacedim>::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<double> 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<double>(weight * n_subdomains / total_weight);
+
+    return partition;
+#endif
+  }
+
+
 } // namespace RepartitioningPolicyTools
 
 
index 7ed65d4b7d40d808408f7902a43597cc2ea64ca8..25f159e99010c68e208d01fdb35cfd3651abfcfd 100644 (file)
@@ -27,5 +27,8 @@ for (deal_II_dimension : DIMENSIONS; deal_II_space_dimension : SPACE_DIMENSIONS)
     template class RepartitioningPolicyTools::
       MinimalGranularityPolicy<deal_II_dimension, deal_II_space_dimension>;
 
+    template class RepartitioningPolicyTools::
+      CellWeightPolicy<deal_II_dimension, deal_II_space_dimension>;
+
 #endif
   }
index 765de0a0749e1e973bcfd7f6208c7ea6d6a32947..66482a5f33b3ef194c654d8b22231dbe5ec53b90 100644 (file)
@@ -133,4 +133,14 @@ main(int argc, char **argv)
   // first-child policy
   test(RepartitioningPolicyTools::MinimalGranularityPolicy<dim>(4),
        "grid_policy_minimal");
+
+  // cell-weight policy
+  test(RepartitioningPolicyTools::CellWeightPolicy<dim>(
+         [](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");
 }
index 469b3b3d2cab9279274a0b3cc76c3a6e169c52fe..c9038e213d7937c3cbd78acd0e7aff6d078f90a8 100644 (file)
@@ -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::
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.