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