SpECTRE Documentation Coverage Report
Current view: top level - Domain - ElementDistribution.hpp Coverage Total Hit
Commit: 0d1d717af84a81fb1284a20c4013708fe85cc2f0 Lines: 38.5 % 13 5
Test Date: 2026-10-22 17:29:42
Legend: Lines:     hit not hit

            Line data    Source code
       1            0 : // Distributed under the MIT License.
       2              : // See LICENSE.txt for details.
       3              : 
       4              : #pragma once
       5              : 
       6              : #include <array>
       7              : #include <cstddef>
       8              : #include <iosfwd>
       9              : #include <optional>
      10              : #include <unordered_map>
      11              : #include <unordered_set>
      12              : #include <utility>
      13              : #include <vector>
      14              : 
      15              : /// \cond
      16              : template <size_t Dim>
      17              : class Block;
      18              : 
      19              : template <size_t Dim>
      20              : class ElementId;
      21              : 
      22              : namespace Options {
      23              : class Option;
      24              : template <typename T>
      25              : struct create_from_yaml;
      26              : }  // namespace Options
      27              : 
      28              : namespace Spectral {
      29              : enum class Basis : uint8_t;
      30              : enum class Quadrature : uint8_t;
      31              : }  // namespace Spectral
      32              : /// \endcond
      33              : 
      34              : namespace domain {
      35              : /// The weighting scheme for assigning computational costs to `Element`s for
      36              : /// distributing balanced compuational costs per processor (see
      37              : /// `BlockZCurveProcDistribution`)
      38            1 : enum class ElementWeight {
      39              :   /// A weighting scheme where each `Element` is assigned the same computational
      40              :   /// cost
      41              :   Uniform,
      42              :   /// A weighting scheme where each `Element`'s computational cost is equal to
      43              :   /// the number of grid points in that `Element`
      44              :   NumGridPoints,
      45              :   /// A weighting scheme where each `Element`'s computational cost is weighted
      46              :   /// by both the number of grid points and minimum spacing between grid points
      47              :   /// in that `Element` (see `get_num_points_and_grid_spacing_cost()` for
      48              :   /// details)
      49              :   NumGridPointsAndGridSpacing
      50              : };
      51              : 
      52            0 : std::ostream& operator<<(std::ostream& os, ElementWeight weight);
      53              : 
      54              : /// \brief Get the cost of each `Element` in a list of `Block`s where
      55              : /// `element_weight` specifies which weight distribution scheme to use
      56              : ///
      57              : /// \details It is only necessary to pass in a value for `i1_basis` and
      58              : /// `i1_quadrature` if the value for `element_weight` is
      59              : /// `ElementWeight::NumGridPointsAndGridSpacing`. Otherwise, the argument isn't
      60              : /// needed and will have no effect if it does have a value.
      61              : template <size_t Dim>
      62            1 : std::unordered_map<ElementId<Dim>, double> get_element_costs(
      63              :     const std::vector<Block<Dim>>& blocks,
      64              :     const std::vector<std::array<size_t, Dim>>& initial_refinement_levels,
      65              :     const std::vector<std::array<size_t, Dim>>& initial_extents,
      66              :     ElementWeight element_weight,
      67              :     const std::optional<Spectral::Basis>& i1_basis,
      68              :     const std::optional<Spectral::Quadrature>& i1_quadrature);
      69              : 
      70              : /*!
      71              :  * \brief Distribution strategy for assigning elements to CPUs using a
      72              :  * Morton ('Z-order') space-filling curve to determine placement within each
      73              :  * block, where `Element`s are distributed across CPUs
      74              :  *
      75              :  * \details The element distribution attempts to assign a balanced total
      76              :  * computational cost to each processor that is allowed to have `Element`s.
      77              :  * First, each `Block`'s `Element`s are ordered by their Z-curve index (see more
      78              :  * below). `Element`s are traversed in this order and assigned to CPUs in order,
      79              :  * moving onto the next CPU once the target cost per CPU is met. The target cost
      80              :  * per CPU is defined as the remaining cost to distribute divided by the
      81              :  * remaining number of CPUs to distribute to. This is an important distinction
      82              :  * from simply having one constant target cost per CPU defined as the total cost
      83              :  * divided by the total number of CPUs with elements. Since the total cost of
      84              :  * `Element`s on a processor will nearly never add up to be exactly the average
      85              :  * cost per CPU, this means that we would either have to decide to overshoot or
      86              :  * undershoot the average as we iterate over the CPUs and assign `Element`s. If
      87              :  * we overshoot the average on each processor, the final processor could have a
      88              :  * much lower cost than the rest of the processors and we run the risk of
      89              :  * overshooting so much that one or more of the requested processors don't get
      90              :  * assigned any `Element`s at all. If we undershoot the average on each
      91              :  * processor, the final processor could have a much higher cost than the others
      92              :  * due to remainder cost piling up. This algorithm avoids these risks by instead
      93              :  * adjusting the target cost per CPU as we finish assigning cost to previous
      94              :  * CPUs.
      95              :  *
      96              :  * Morton curves are a simple and easily-computed space-filling curve that
      97              :  * (unlike Hilbert curves) permit diagonal traversal. See, for instance,
      98              :  * \cite Borrell2018 for a discussion of mesh partitioning using space-filling
      99              :  * curves.
     100              :  * A concrete example of the use of a Morton curve in 2d is given below.
     101              :  *
     102              :  * A sketch of a 2D block with 4x2 elements, with each element labeled according
     103              :  * to the order on the Morton curve:
     104              :  * ```
     105              :  *          x-->
     106              :  *          0   1   2   3
     107              :  *        ----------------
     108              :  *  y  0 |  0   2   4   6
     109              :  *  |    |  | / | / | / |
     110              :  *  v  1 |  1   3   5   7
     111              :  * ```
     112              :  * (forming a zig-zag path, that under some rotation/reflection has a 'Z'
     113              :  * shape).
     114              :  *
     115              :  * The Morton curve method is a quick way of getting acceptable spatial locality
     116              :  * -- usually, for approximately even distributions, it will ensure that
     117              :  * elements are assigned in large volume chunks, and the structure of the Morton
     118              :  * curve ensures that for a given processor and block, the elements will be
     119              :  * assigned in no more than two orthogonally connected clusters. In principle, a
     120              :  * Hilbert curve could potentially improve upon the gains obtained by this class
     121              :  * by guaranteeing that all elements within each block form a single
     122              :  * orthogonally connected cluster.
     123              :  *
     124              :  * The assignment of portions of blocks to processors may use partial blocks,
     125              :  * and/or multiple blocks to ensure an even distribution of elements to
     126              :  * processors.
     127              :  * We currently make no distinction between dividing elements between processors
     128              :  * within a node and dividing elements between processors across nodes. The
     129              :  * current technique aims to have a simple method of reducing communication
     130              :  * globally, though it would likely be more efficient to prioritize minimization
     131              :  * of inter-node communication, because communication across interconnects is
     132              :  * the primary cost of communication in charm++ runs.
     133              :  *
     134              :  * \warning The use of the Morton curve to generate a well-clustered element
     135              :  * distribution currently assumes that the refinement is uniform over each
     136              :  * block, with no internal structure that would be generated by, for instance
     137              :  * AMR.
     138              :  * This distribution method will need alteration to perform well for blocks with
     139              :  * internal structure from h-refinement. Morton curves can be defined
     140              :  * recursively, so a generalization of the present method is possible for blocks
     141              :  * with internal refinement
     142              :  *
     143              :  * \tparam Dim the number of spatial dimensions of the `Block`s
     144              :  */
     145              : template <size_t Dim>
     146            1 : struct BlockZCurveProcDistribution {
     147            0 :   BlockZCurveProcDistribution() = default;
     148              : 
     149              :   /// The `number_of_procs_with_elements` argument represents how many procs
     150              :   /// will have elements. This is not necessarily equal to the total number of
     151              :   /// procs because some global procs may be ignored by the fifth argument
     152              :   /// `global_procs_to_ignore`.
     153            1 :   BlockZCurveProcDistribution(
     154              :       const std::unordered_map<ElementId<Dim>, double>& element_costs,
     155              :       size_t number_of_procs_with_elements,
     156              :       const std::vector<Block<Dim>>& blocks,
     157              :       const std::vector<std::array<size_t, Dim>>& initial_refinement_levels,
     158              :       const std::unordered_set<size_t>& global_procs_to_ignore = {});
     159              : 
     160              :   /// Gets the suggested processor number for a particular `ElementId`,
     161              :   /// determined by the Morton curve weighted element assignment described in
     162              :   /// detail in the parent class documentation.
     163            1 :   size_t get_proc_for_element(const ElementId<Dim>& element_id) const;
     164              : 
     165              :   const std::vector<std::vector<std::pair<size_t, size_t>>>&
     166            0 :   block_element_distribution() const {
     167              :     return block_element_distribution_;
     168              :   }
     169              : 
     170              :  private:
     171              :   // in this nested data structure:
     172              :   // - The block id is the first index
     173              :   // - There is an arbitrary number of CPUs per block, each with an element
     174              :   //   allowance
     175              :   // - Each element allowance is represented by a pair of proc number, number of
     176              :   //   elements in the allowance
     177              :   std::vector<std::vector<std::pair<size_t, size_t>>>
     178            0 :       block_element_distribution_;
     179              : };
     180              : }  // namespace domain
     181              : 
     182              : template <>
     183            0 : struct Options::create_from_yaml<domain::ElementWeight> {
     184              :   template <typename Metavariables>
     185            0 :   static domain::ElementWeight create(const Options::Option& options) {
     186              :     return create<void>(options);
     187              :   }
     188              : };
     189              : 
     190              : template <>
     191            0 : domain::ElementWeight
     192              : Options::create_from_yaml<domain::ElementWeight>::create<void>(
     193              :     const Options::Option& options);
        

Generated by: LCOV version 2.0-1