|
| 1 | +#include "split.h" |
| 2 | + |
| 3 | +#include <array> |
| 4 | +#include <tuple> |
| 5 | + |
| 6 | +#include "grid.h" |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +using namespace celerity; |
| 11 | +using namespace celerity::detail; |
| 12 | + |
| 13 | +[[maybe_unused]] void sanity_check_split(const chunk<3>& full_chunk, const std::vector<chunk<3>>& split) { |
| 14 | + region<3> reconstructed_chunk; |
| 15 | + for(auto& chnk : split) { |
| 16 | + assert(region_intersection(reconstructed_chunk, box<3>(chnk)).empty()); |
| 17 | + reconstructed_chunk = region_union(box<3>(chnk), reconstructed_chunk); |
| 18 | + } |
| 19 | + assert(region_difference(reconstructed_chunk, box<3>(full_chunk)).empty()); |
| 20 | +} |
| 21 | + |
| 22 | +template <int Dims> |
| 23 | +std::tuple<range<Dims>, range<Dims>, range<Dims>> compute_small_and_large_chunks( |
| 24 | + const chunk<3>& full_chunk, const range<3>& granularity, const std::array<size_t, Dims>& actual_num_chunks) { |
| 25 | + range<Dims> small_chunk_size{zeros}; |
| 26 | + range<Dims> large_chunk_size{zeros}; |
| 27 | + range<Dims> num_large_chunks{zeros}; |
| 28 | + for(int d = 0; d < Dims; ++d) { |
| 29 | + const size_t ideal_chunk_size = full_chunk.range[d] / actual_num_chunks[d]; |
| 30 | + small_chunk_size[d] = (ideal_chunk_size / granularity[d]) * granularity[d]; |
| 31 | + large_chunk_size[d] = small_chunk_size[d] + granularity[d]; |
| 32 | + num_large_chunks[d] = (full_chunk.range[d] - small_chunk_size[d] * actual_num_chunks[d]) / granularity[d]; |
| 33 | + } |
| 34 | + return {small_chunk_size, large_chunk_size, num_large_chunks}; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Given a factorization of `num_chunks` (i.e., `f0 * f1 = num_chunks`), try to find the assignment of factors to |
| 39 | + * dimensions that produces more chunks under the given constraints. If they are tied, try to find the assignment |
| 40 | + * that results in a "nicer" split according to some heuristics (see below). |
| 41 | + * |
| 42 | + * The single argument `factor` specifies both factors, as `f0 = factor` and `f1 = num_chunks / factor`. |
| 43 | + * |
| 44 | + * @returns The number of chunks that can be created in dimension 0 and dimension 1, respectively. These are at most |
| 45 | + * (f0, f1) or (f1, f0), however may be less if constrained by the split granularity. |
| 46 | + */ |
| 47 | +std::array<size_t, 2> assign_split_factors_2d(const chunk<3>& full_chunk, const range<3>& granularity, const size_t factor, const size_t num_chunks) { |
| 48 | + assert(num_chunks % factor == 0); |
| 49 | + const size_t max_chunks[2] = {full_chunk.range[0] / granularity[0], full_chunk.range[1] / granularity[1]}; |
| 50 | + const size_t f0 = factor; |
| 51 | + const size_t f1 = num_chunks / factor; |
| 52 | + |
| 53 | + // Decide in which direction to split by first checking which |
| 54 | + // factor assignment produces more chunks under the given constraints. |
| 55 | + const std::array<size_t, 2> split_0_1 = {std::min(f0, max_chunks[0]), std::min(f1, max_chunks[1])}; |
| 56 | + const std::array<size_t, 2> split_1_0 = {std::min(f1, max_chunks[0]), std::min(f0, max_chunks[1])}; |
| 57 | + const auto count0 = split_0_1[0] * split_0_1[1]; |
| 58 | + const auto count1 = split_1_0[0] * split_1_0[1]; |
| 59 | + |
| 60 | + if(count0 > count1) { return split_0_1; } |
| 61 | + if(count0 < count1) { return split_1_0; } |
| 62 | + |
| 63 | + // If we're tied for the number of chunks we can create, try some heuristics to decide. |
| 64 | + |
| 65 | + // If domain is square(-ish), prefer splitting along slower dimension. |
| 66 | + // (These bounds have been chosen arbitrarily!) |
| 67 | + const double squareishness = std::sqrt(full_chunk.range.size()) / static_cast<double>(full_chunk.range[0]); |
| 68 | + if(squareishness > 0.95 && squareishness < 1.05) { return (f0 >= f1) ? split_0_1 : split_1_0; } |
| 69 | + |
| 70 | + // For non-square domains, prefer split that produces shorter edges (compare sum of circumferences) |
| 71 | + const auto circ0 = full_chunk.range[0] / split_0_1[0] + full_chunk.range[1] / split_0_1[1]; |
| 72 | + const auto circ1 = full_chunk.range[0] / split_1_0[0] + full_chunk.range[1] / split_1_0[1]; |
| 73 | + return circ0 < circ1 ? split_0_1 : split_1_0; |
| 74 | + |
| 75 | + // TODO: Yet another heuristic we may want to consider is how even chunk sizes are, |
| 76 | + // i.e., how balanced the workload is. |
| 77 | +} |
| 78 | + |
| 79 | +} // namespace |
| 80 | + |
| 81 | +namespace celerity::detail { |
| 82 | + |
| 83 | +std::vector<chunk<3>> split_1d(const chunk<3>& full_chunk, const range<3>& granularity, const size_t num_chunks) { |
| 84 | +#ifndef NDEBUG |
| 85 | + assert(num_chunks > 0); |
| 86 | + for(int d = 0; d < 3; ++d) { |
| 87 | + assert(granularity[d] > 0); |
| 88 | + assert(full_chunk.range[d] % granularity[d] == 0); |
| 89 | + } |
| 90 | +#endif |
| 91 | + |
| 92 | + // Due to split granularity requirements or if num_workers > global_size[0], |
| 93 | + // we may not be able to create the requested number of chunks. |
| 94 | + const std::array<size_t, 1> actual_num_chunks = {std::min(num_chunks, full_chunk.range[0] / granularity[0])}; |
| 95 | + const auto [small_chunk_size, large_chunk_size, num_large_chunks] = compute_small_and_large_chunks<1>(full_chunk, granularity, actual_num_chunks); |
| 96 | + |
| 97 | + std::vector<chunk<3>> result(actual_num_chunks[0], {full_chunk.offset, full_chunk.range, full_chunk.global_size}); |
| 98 | + for(auto i = 0u; i < num_large_chunks[0]; ++i) { |
| 99 | + result[i].range[0] = large_chunk_size[0]; |
| 100 | + result[i].offset[0] += i * large_chunk_size[0]; |
| 101 | + } |
| 102 | + for(auto i = num_large_chunks[0]; i < actual_num_chunks[0]; ++i) { |
| 103 | + result[i].range[0] = small_chunk_size[0]; |
| 104 | + result[i].offset[0] += num_large_chunks[0] * large_chunk_size[0] + (i - num_large_chunks[0]) * small_chunk_size[0]; |
| 105 | + } |
| 106 | + |
| 107 | +#ifndef NDEBUG |
| 108 | + sanity_check_split(full_chunk, result); |
| 109 | +#endif |
| 110 | + |
| 111 | + return result; |
| 112 | +} |
| 113 | + |
| 114 | +// TODO: Make the split dimensions configurable for 3D chunks? |
| 115 | +std::vector<chunk<3>> split_2d(const chunk<3>& full_chunk, const range<3>& granularity, const size_t num_chunks) { |
| 116 | +#ifndef NDEBUG |
| 117 | + assert(num_chunks > 0); |
| 118 | + for(int d = 0; d < 3; ++d) { |
| 119 | + assert(granularity[d] > 0); |
| 120 | + assert(full_chunk.range[d] % granularity[d] == 0); |
| 121 | + } |
| 122 | +#endif |
| 123 | + |
| 124 | + // Factorize num_chunks |
| 125 | + // We start out with an initial guess of `factor = floor(sqrt(num_chunks))` (the other one is implicitly given by `num_chunks / factor`), |
| 126 | + // and work our way down, keeping track of the best factorization we've found so far, until we find a factorization that produces |
| 127 | + // the requested number of chunks, or until we reach (1, num_chunks), i.e., a 1D split. |
| 128 | + size_t factor = std::floor(std::sqrt(num_chunks)); |
| 129 | + std::array<size_t, 2> best_chunk_counts = {0, 0}; |
| 130 | + while(factor >= 1) { |
| 131 | + while(factor > 1 && num_chunks % factor != 0) { |
| 132 | + factor--; |
| 133 | + } |
| 134 | + // The returned counts are at most (factor, num_chunks / factor), however may be less if constrained by the split granularity. |
| 135 | + const auto chunk_counts = assign_split_factors_2d(full_chunk, granularity, factor, num_chunks); |
| 136 | + if(chunk_counts[0] * chunk_counts[1] > best_chunk_counts[0] * best_chunk_counts[1]) { best_chunk_counts = chunk_counts; } |
| 137 | + if(chunk_counts[0] * chunk_counts[1] == num_chunks) { break; } |
| 138 | + factor--; |
| 139 | + } |
| 140 | + const auto actual_num_chunks = best_chunk_counts; |
| 141 | + const auto [small_chunk_size, large_chunk_size, num_large_chunks] = compute_small_and_large_chunks<2>(full_chunk, granularity, actual_num_chunks); |
| 142 | + |
| 143 | + std::vector<chunk<3>> result(actual_num_chunks[0] * actual_num_chunks[1], {full_chunk.offset, full_chunk.range, full_chunk.global_size}); |
| 144 | + id<3> offset = full_chunk.offset; |
| 145 | + |
| 146 | + for(size_t j = 0; j < actual_num_chunks[0]; ++j) { |
| 147 | + range<2> chunk_size = {(j < num_large_chunks[0]) ? large_chunk_size[0] : small_chunk_size[0], 0}; |
| 148 | + for(size_t i = 0; i < actual_num_chunks[1]; ++i) { |
| 149 | + chunk_size[1] = (i < num_large_chunks[1]) ? large_chunk_size[1] : small_chunk_size[1]; |
| 150 | + auto& chnk = result[j * actual_num_chunks[1] + i]; |
| 151 | + chnk.offset = offset; |
| 152 | + chnk.range[0] = chunk_size[0]; |
| 153 | + chnk.range[1] = chunk_size[1]; |
| 154 | + offset[1] += chunk_size[1]; |
| 155 | + } |
| 156 | + offset[0] += chunk_size[0]; |
| 157 | + offset[1] = full_chunk.offset[1]; |
| 158 | + } |
| 159 | + |
| 160 | +#ifndef NDEBUG |
| 161 | + sanity_check_split(full_chunk, result); |
| 162 | +#endif |
| 163 | + |
| 164 | + return result; |
| 165 | +} |
| 166 | +} // namespace celerity::detail |
0 commit comments