| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2021 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_distributed_repartitioning_policy_tools_h |
| 16 |
#define dealii_distributed_repartitioning_policy_tools_h |
| 17 |
|
| 18 |
#include <deal.II/grid/tria.h> |
| 19 |
|
| 20 |
#include <deal.II/lac/la_parallel_vector.h> |
| 21 |
|
| 22 |
DEAL_II_NAMESPACE_OPEN |
| 23 |
|
| 24 |
/** |
| 25 |
* A namespace with repartitioning policies. These classes return vectors |
| 26 |
* of the new owners of the active locally owned and ghost cells of a |
| 27 |
* Triangulation object. The returned vectors can be used, e.g., in |
| 28 |
* TriangulationDescription::Utilities::create_description_from_triangulation() |
| 29 |
* to create a TriangulationDescription::Description based on a given |
| 30 |
* Triangulation and the predescribed partition, which can be used to |
| 31 |
* set up a parallel::fullydistributed::Triangulation objects. |
| 32 |
* |
| 33 |
* These policies can be also used in context of |
| 34 |
* MGTransferGlobalCoarseningTools::create_geometric_coarsening_sequence() to |
| 35 |
* prescribe arbitrary partitioning in multgrid levels of global coarsening |
| 36 |
* multigrid schmeme. |
| 37 |
*/ |
| 38 |
namespace RepartitioningPolicyTools |
| 39 |
{ |
| 40 |
/** |
| 41 |
* The base class for repartitioning policies. |
| 42 |
* |
| 43 |
* Used in |
| 44 |
* MGTransferGlobalCoarseningTools::create_geometric_coarsening_sequence(). |
| 45 |
* See the description of RepartitioningPolicyTools for more information. |
| 46 |
*/ |
| 47 |
template <int dim, int spacedim = dim> |
| 48 |
class Base : public EnableObserverPointer |
| 49 |
{ |
| 50 |
public: |
| 51 |
/** |
| 52 |
* Return a vector of the new owners of the active locally owned and ghost |
| 53 |
* cells. |
| 54 |
*/ |
| 55 |
virtual LinearAlgebra::distributed::Vector<double> |
| 56 |
partition(const Triangulation<dim, spacedim> &tria_coarse_in) const = 0; |
| 57 |
|
| 58 |
/** |
| 59 |
* Destructor. |
| 60 |
*/ |
| 61 |
virtual ~Base() = default; |
| 62 |
}; |
| 63 |
|
| 64 |
/** |
| 65 |
* A dummy policy that simply returns an empty vector, which is interpreted |
| 66 |
* in MGTransferGlobalCoarseningTools::create_geometric_coarsening_sequence() |
| 67 |
* in a way that the triangulation is not repartitioned. |
| 68 |
*/ |
| 69 |
template <int dim, int spacedim = dim> |
| 70 |
class DefaultPolicy : public Base<dim, spacedim> |
| 71 |
{ |
| 72 |
public: |
| 73 |
/** |
| 74 |
* Constructor. |
| 75 |
* |
| 76 |
* @param tighten allows to renumber of subdomains so that empty ranks are |
| 77 |
* positioned at the end. |
| 78 |
*/ |
| 79 |
DefaultPolicy(const bool tighten = false); |
| 80 |
|
| 81 |
virtual LinearAlgebra::distributed::Vector<double> |
| 82 |
partition( |
| 83 |
const Triangulation<dim, spacedim> &tria_coarse_in) const override; |
| 84 |
|
| 85 |
private: |
| 86 |
const bool tighten; |
| 87 |
}; |
| 88 |
|
| 89 |
/** |
| 90 |
* A policy that partitions coarse grids based on a base triangulation |
| 91 |
* according to a first-child policy. The triangulation to be partitioned |
| 92 |
* should be able to be obtained by a sequence of (global) coarsening steps. |
| 93 |
*/ |
| 94 |
template <int dim, int spacedim = dim> |
| 95 |
class FirstChildPolicy : public Base<dim, spacedim> |
| 96 |
{ |
| 97 |
public: |
| 98 |
/** |
| 99 |
* Constructor taking the base (fine) triangulation. |
| 100 |
*/ |
| 101 |
FirstChildPolicy(const Triangulation<dim, spacedim> &tria_fine); |
| 102 |
|
| 103 |
virtual LinearAlgebra::distributed::Vector<double> |
| 104 |
partition( |
| 105 |
const Triangulation<dim, spacedim> &tria_coarse_in) const override; |
| 106 |
|
| 107 |
private: |
| 108 |
/** |
| 109 |
* Number of coarse cells. |
| 110 |
*/ |
| 111 |
const unsigned int n_coarse_cells; |
| 112 |
|
| 113 |
/** |
| 114 |
* Number of global levels. |
| 115 |
*/ |
| 116 |
const unsigned int n_global_levels; |
| 117 |
|
| 118 |
/** |
| 119 |
* Index set constructed from the triangulation passed to the constructor. |
| 120 |
* It contains all the cells that would be owned by the current process |
| 121 |
* if the levels would be partitioned according to a first-child policy. |
| 122 |
*/ |
| 123 |
IndexSet is_level_partitions; |
| 124 |
}; |
| 125 |
|
| 126 |
/** |
| 127 |
* A policy that allows to specify a minimal number of cells per |
| 128 |
* process. If a threshold is reached, processes might be left |
| 129 |
* without cells. The cells will be distributed evenly among the |
| 130 |
* remaining processes. |
| 131 |
*/ |
| 132 |
template <int dim, int spacedim = dim> |
| 133 |
class MinimalGranularityPolicy : public Base<dim, spacedim> |
| 134 |
{ |
| 135 |
public: |
| 136 |
/** |
| 137 |
* Constructor taking the minimum number of cells per process. |
| 138 |
*/ |
| 139 |
MinimalGranularityPolicy(const unsigned int n_min_cells); |
| 140 |
|
| 141 |
virtual LinearAlgebra::distributed::Vector<double> |
| 142 |
partition(const Triangulation<dim, spacedim> &tria_in) const override; |
| 143 |
|
| 144 |
private: |
| 145 |
/** |
| 146 |
* Minimum number of cells per process. |
| 147 |
*/ |
| 148 |
const unsigned int n_min_cells; |
| 149 |
}; |
| 150 |
|
| 151 |
/** |
| 152 |
* A policy that allows to specify a weight of each cell. The underlying |
| 153 |
* algorithm will try to distribute the weights equally among the processes. |
| 154 |
*/ |
| 155 |
template <int dim, int spacedim = dim> |
| 156 |
class CellWeightPolicy : public Base<dim, spacedim> |
| 157 |
{ |
| 158 |
public: |
| 159 |
/** |
| 160 |
* Constructor taking a function that gives a weight to each cell. |
| 161 |
*/ |
| 162 |
CellWeightPolicy( |
| 163 |
const std::function<unsigned int( |
| 164 |
const typename Triangulation<dim, spacedim>::cell_iterator &, |
| 165 |
const CellStatus)> &weighting_function); |
| 166 |
|
| 167 |
virtual LinearAlgebra::distributed::Vector<double> |
| 168 |
partition(const Triangulation<dim, spacedim> &tria_in) const override; |
| 169 |
|
| 170 |
private: |
| 171 |
/** |
| 172 |
* A function that gives a weight to each cell. |
| 173 |
*/ |
| 174 |
const std::function< |
| 175 |
unsigned int(const typename Triangulation<dim, spacedim>::cell_iterator &, |
| 176 |
const CellStatus)> |
| 177 |
weighting_function; |
| 178 |
}; |
| 179 |
|
| 180 |
} // namespace RepartitioningPolicyTools |
| 181 |
|
| 182 |
DEAL_II_NAMESPACE_CLOSE |
| 183 |
|
| 184 |
#endif |