]> https://gitweb.dealii.org/ - dealii.git/commitdiff
p::f::T: allow repartitioning
authorPeter Munch <peterrmuench@gmail.com>
Mon, 6 Dec 2021 21:26:29 +0000 (22:26 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Mon, 6 Dec 2021 21:26:29 +0000 (22:26 +0100)
include/deal.II/distributed/fully_distributed_tria.h
include/deal.II/distributed/repartitioning_policy_tools.h
source/distributed/fully_distributed_tria.cc
source/grid/tria_description.cc

index 2de0ba3d75f85148939cc0ae4141b45554ad711b..318e99f84e4f1495410766bba4e8482834224ca9 100644 (file)
@@ -39,6 +39,12 @@ namespace GridTools
   template <typename CellIterator>
   struct PeriodicFacePair;
 }
+
+namespace RepartitioningPolicyTools
+{
+  template <int dim, int spacedim>
+  class Base;
+}
 #endif
 
 namespace parallel
@@ -196,6 +202,20 @@ namespace parallel
                                  const unsigned int)> &partitioner,
         const TriangulationDescription::Settings &     settings);
 
+      /**
+       * TODO
+       */
+      void
+      set_partitioner(
+        const RepartitioningPolicyTools::Base<dim, spacedim> &partitioner,
+        const TriangulationDescription::Settings &            settings);
+
+      /**
+       * TODO
+       */
+      void
+      repartition();
+
       /**
        * Coarsen and refine the mesh according to refinement and coarsening
        * flags set.
@@ -301,6 +321,12 @@ namespace parallel
                          const unsigned int)>
         partitioner;
 
+      /**
+       * TODO
+       */
+      SmartPointer<const RepartitioningPolicyTools::Base<dim, spacedim>>
+        partitioner_distributed;
+
       /**
        * Sorted list of pairs of coarse-cell ids and their indices.
        */
index 81d412c45cccd7d3c90eff57909cfbb268f17c0a..8240a7208662f2e481599f4a827dc54b50b96dfe 100644 (file)
@@ -46,7 +46,7 @@ namespace RepartitioningPolicyTools
    * See the description of RepartitioningPolicyTools for more information.
    */
   template <int dim, int spacedim = dim>
-  class Base
+  class Base : public Subscriptor
   {
   public:
     /**
index 098f7951fc4eec75d97d9eb3ada53f6a3de63c08..2a7496d5ec07eac3744ee6365c46867174f6c758 100644 (file)
@@ -18,6 +18,7 @@
 #include <deal.II/base/mpi.h>
 
 #include <deal.II/distributed/fully_distributed_tria.h>
+#include <deal.II/distributed/repartitioning_policy_tools.h>
 
 #include <deal.II/grid/grid_tools.h>
 
@@ -310,6 +311,37 @@ namespace parallel
 
 
 
+    template <int dim, int spacedim>
+    void
+    Triangulation<dim, spacedim>::set_partitioner(
+      const RepartitioningPolicyTools::Base<dim, spacedim> &partitioner,
+      const TriangulationDescription::Settings &            settings)
+    {
+      this->partitioner_distributed = &partitioner;
+      this->settings                = settings;
+    }
+
+
+
+    template <int dim, int spacedim>
+    void
+    Triangulation<dim, spacedim>::repartition()
+    {
+      this->signals.pre_distributed_repartition();
+
+      const auto construction_data = TriangulationDescription::Utilities::
+        create_description_from_triangulation(
+          *this,
+          this->partitioner_distributed->partition(*this),
+          this->settings);
+
+      this->create_triangulation(construction_data);
+
+      this->signals.post_distributed_repartition();
+    }
+
+
+
     template <int dim, int spacedim>
     void
     Triangulation<dim, spacedim>::execute_coarsening_and_refinement()
index 3bb3e28a01904ad73279bb33aa170eac4778b62e..1606c0df6481e7451510f013e7c93acb132d8aa3 100644 (file)
@@ -17,6 +17,7 @@
 #include <deal.II/base/mpi.h>
 #include <deal.II/base/mpi_consensus_algorithms.h>
 
+#include <deal.II/distributed/fully_distributed_tria.h>
 #include <deal.II/distributed/tria.h>
 
 #include <deal.II/dofs/dof_accessor.h>
@@ -109,7 +110,8 @@ namespace TriangulationDescription
         collect(
           const std::vector<unsigned int> &                  relevant_processes,
           const std::vector<DescriptionTemp<dim, spacedim>> &description_temp,
-          const MPI_Comm &                                   comm)
+          const MPI_Comm &                                   comm,
+          const bool vertices_have_unique_ids)
         {
           dealii::Utilities::MPI::ConsensusAlgorithms::AnonymousProcess<char,
                                                                         char>
@@ -135,7 +137,8 @@ namespace TriangulationDescription
                   std::vector<char> &) {
                 this->merge(
                   dealii::Utilities::unpack<DescriptionTemp<dim, spacedim>>(
-                    recv_buffer, false));
+                    recv_buffer, false),
+                  vertices_have_unique_ids);
               });
 
           dealii::Utilities::MPI::ConsensusAlgorithms::Selector<char, char>(
@@ -149,17 +152,66 @@ namespace TriangulationDescription
          * This done by the reduce() function.
          */
         void
-        merge(const DescriptionTemp<dim, spacedim> &other)
+        merge(const DescriptionTemp<dim, spacedim> &other,
+              const bool                            vertices_have_unique_ids)
         {
           this->cell_infos.resize(
             std::max(other.cell_infos.size(), this->cell_infos.size()));
 
-          this->coarse_cells.insert(this->coarse_cells.end(),
-                                    other.coarse_cells.begin(),
-                                    other.coarse_cells.end());
-          this->coarse_cell_vertices.insert(this->coarse_cell_vertices.end(),
-                                            other.coarse_cell_vertices.begin(),
-                                            other.coarse_cell_vertices.end());
+          if (vertices_have_unique_ids == false)
+            {
+              const auto comp = [](const auto &a, const auto &b) {
+                const double tolerance = 1e-10;
+
+                if (a.distance(b) < tolerance)
+                  return false;
+
+                for (unsigned int i = 0; i < spacedim; ++i)
+                  if (std::abs(a[i] - b[i]) > tolerance)
+                    return a[i] < b[i];
+
+                return false;
+              };
+
+              std::map<Point<spacedim>, unsigned int, decltype(comp)> map(comp);
+              std::map<unsigned int, unsigned int>                    map_i;
+
+              for (unsigned int i = 0; i < this->coarse_cell_vertices.size();
+                   ++i)
+                map[coarse_cell_vertices[i].second] = i;
+
+              unsigned int counter = coarse_cell_vertices.size();
+
+              for (const auto p : other.coarse_cell_vertices)
+                if (map.find(p.second) == map.end())
+                  {
+                    this->coarse_cell_vertices.emplace_back(counter, p.second);
+                    map[p.second] = map_i[p.first] = counter++;
+                  }
+                else
+                  map_i[p.first] = map[p.second];
+
+              for (auto cell : other.coarse_cells)
+                {
+                  for (auto &v : cell.vertices)
+                    v = map_i[v];
+                }
+
+              this->coarse_cells.insert(this->coarse_cells.end(),
+                                        other.coarse_cells.begin(),
+                                        other.coarse_cells.end());
+            }
+          else
+            {
+              this->coarse_cells.insert(this->coarse_cells.end(),
+                                        other.coarse_cells.begin(),
+                                        other.coarse_cells.end());
+              this->coarse_cell_vertices.insert(
+                this->coarse_cell_vertices.end(),
+                other.coarse_cell_vertices.begin(),
+                other.coarse_cell_vertices.end());
+            }
+
           this->coarse_cell_index_to_coarse_cell_id.insert(
             this->coarse_cell_index_to_coarse_cell_id.end(),
             other.coarse_cell_index_to_coarse_cell_id.begin(),
@@ -1025,9 +1077,13 @@ namespace TriangulationDescription
       // collect description from all processes that used to own locally-owned
       // active cells of this process in a single description
       DescriptionTemp<dim, spacedim> description_merged;
-      description_merged.collect(relevant_processes,
-                                 descriptions_per_rank,
-                                 partition.get_mpi_communicator());
+      description_merged.collect(
+        relevant_processes,
+        descriptions_per_rank,
+        partition.get_mpi_communicator(),
+        dynamic_cast<
+          const parallel::fullydistributed::Triangulation<dim, spacedim> *>(
+          &tria) != nullptr);
 
       // remove redundant entries
       description_merged.reduce();

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.