Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca-tiling.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
68#ifndef CA_TILING_H
69#define CA_TILING_H
70
71#include <array>
72#include <cstddef>
73#include <cstdint>
74
75#include <ah-errors.H>
76
77#include <ca-traits.H>
78
79namespace Aleph {
80namespace CA {
81
89struct Range1D
90{
95
98 {
99 return end > begin ? end - begin : 0;
100 }
101
103 [[nodiscard]] constexpr bool empty() const noexcept
104 {
105 return end <= begin;
106 }
107};
108
127[[nodiscard]] inline constexpr Range1D split_range_balanced(const ca_size_t n,
128 const ca_size_t parts,
129 const ca_size_t idx) noexcept
130{
131 if (parts <= 1 or n == 0)
132 {
133 if (idx == 0)
134 return Range1D{0, n};
135 return Range1D{n, n};
136 }
137 if (idx >= parts)
138 return Range1D{n, n};
139
140 const ca_size_t base = n / parts;
141 const ca_size_t rem = n % parts;
142 const ca_size_t begin = idx * base + (idx < rem ? idx : rem);
143 const ca_size_t extra = idx < rem ? 1 : 0;
144 const ca_size_t end = begin + base + extra;
145 return Range1D{begin, end};
146}
147
163[[nodiscard]] inline constexpr bool should_run_sequential(const ca_size_t cells,
164 const ca_size_t num_partitions,
165 const ca_size_t min_cells) noexcept
166{
167 return num_partitions <= 1 or cells == 0 or cells < min_cells;
168}
169
178template <std::size_t Rank>
180{
181 static_assert(Rank >= 1, "Row_Partition requires Rank >= 1");
182
190 [[nodiscard]] static constexpr Range1D slab(const std::array<ca_size_t, Rank> &extents,
191 const ca_size_t parts,
192 const ca_size_t idx) noexcept
193 {
194 return split_range_balanced(extents[0], parts, idx);
195 }
196};
197
208template <std::size_t Rank>
210{
211 static_assert(Rank >= 1, "Column_Partition requires Rank >= 1");
212
214 [[nodiscard]] static constexpr Range1D slab(const std::array<ca_size_t, Rank> &extents,
215 const ca_size_t parts,
216 const ca_size_t idx) noexcept
217 {
218 return split_range_balanced(extents[Rank - 1], parts, idx);
219 }
220};
221
225{
230
233 {
234 return rows.size() * cols.size();
235 }
236
238 [[nodiscard]] constexpr bool empty() const noexcept
239 {
240 return rows.empty() or cols.empty();
241 }
242};
243
258{
266 [[nodiscard]] static constexpr Tile2D_Range tile(const std::array<ca_size_t, 2> &extents,
267 const ca_size_t parts_y,
268 const ca_size_t parts_x,
269 const ca_size_t tile_index) noexcept
270 {
271 const ca_size_t py = parts_y == 0 ? 1 : parts_y;
272 const ca_size_t px = parts_x == 0 ? 1 : parts_x;
273 const ca_size_t total = py * px;
274 if (tile_index >= total)
275 return Tile2D_Range{Range1D{extents[0], extents[0]}, Range1D{extents[1], extents[1]}};
276 const ca_size_t ty = tile_index / px;
277 const ca_size_t tx = tile_index % px;
280 }
281
292 [[nodiscard]] static constexpr std::array<ca_size_t, 2> factor_partitions(
293 const std::array<ca_size_t, 2> &extents, const ca_size_t parts) noexcept
294 {
295 if (parts <= 1)
296 return {1, 1};
297
298 // Find the divisor pair (a, b) of `parts` whose ratio b/a best
299 // matches cols/rows. Iterate over divisors only; `parts` is small
300 // (a few dozen at most) so the linear search is trivial.
301 ca_size_t best_y = 1;
303 long long best_score = -1;
304 const long long rows = static_cast<long long>(extents[0]);
305 const long long cols = static_cast<long long>(extents[1]);
306
307 for (ca_size_t y = 1; y <= parts; ++y)
308 {
309 if (parts % y != 0)
310 continue;
311 const ca_size_t x = parts / y;
312 // score = - |rows / y - cols / x| (closer to balanced => better)
313 const long long ry = rows / static_cast<long long>(y);
314 const long long rx = cols / static_cast<long long>(x);
315 long long diff = ry - rx;
316 if (diff < 0)
317 diff = -diff;
318 const long long score = -diff;
319 if (score > best_score)
320 {
321 best_score = score;
322 best_y = y;
323 best_x = x;
324 }
325 }
326 return {best_y, best_x};
327 }
328};
329
330// ---------------------------------------------------------------------------
331// Z-order (Morton) encoding helpers.
332//
333// They are not used by the default partitioner, but they are documented as
334// part of the Phase-5 deliverables and exposed here so that future engines
335// (parallel 3D, hashlife streaming) can opt into a cache-friendly visit
336// order without having to redefine the bit-twiddling each time.
337// ---------------------------------------------------------------------------
338
339namespace ca_tiling_detail {
340
343inline constexpr std::uint64_t spread_bits_2(const std::uint32_t v) noexcept
344{
345 std::uint64_t r = 0;
346 for (std::size_t i = 0; i < 32; ++i)
347 r |= (static_cast<std::uint64_t>(v >> i) & 1ULL) << (2 * i);
348 return r;
349}
350
353inline constexpr std::uint64_t spread_bits_3(const std::uint32_t v) noexcept
354{
355 std::uint64_t r = 0;
356 for (std::size_t i = 0; i < 21; ++i)
357 r |= (static_cast<std::uint64_t>(v >> i) & 1ULL) << (3 * i);
358 return r;
359}
360
361} // namespace ca_tiling_detail
362
375[[nodiscard]] inline constexpr std::uint64_t morton_encode_2d(const std::uint32_t x,
376 const std::uint32_t y) noexcept
377{
379}
380
392[[nodiscard]] inline constexpr std::uint64_t morton_encode_3d(const std::uint32_t x,
393 const std::uint32_t y,
394 const std::uint32_t z) noexcept
395{
398}
399
400} // namespace CA
401} // namespace Aleph
402
403#endif // CA_TILING_H
Exception handling system with formatted messages for Aleph-w.
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Common typedefs and tag types for the Cellular Automata module.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
Definition ah-mdspan.H:303
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
static mpfr_t y
Definition mpfr_mul_d.c:3
constexpr std::uint64_t spread_bits_2(const std::uint32_t v) noexcept
Spread the low bits bits of v so that they occupy even positions.
Definition ca-tiling.H:343
constexpr std::uint64_t spread_bits_3(const std::uint32_t v) noexcept
Spread the low bits bits of v so that they occupy every third position (used for 3D Morton codes).
Definition ca-tiling.H:353
constexpr std::uint64_t morton_encode_3d(const std::uint32_t x, const std::uint32_t y, const std::uint32_t z) noexcept
Morton (Z-order) encoding of (x, y, z).
Definition ca-tiling.H:392
constexpr Range1D split_range_balanced(const ca_size_t n, const ca_size_t parts, const ca_size_t idx) noexcept
Balanced split of [0, n) into parts contiguous ranges.
Definition ca-tiling.H:127
constexpr bool should_run_sequential(const ca_size_t cells, const ca_size_t num_partitions, const ca_size_t min_cells) noexcept
Decide whether a workload should run sequentially.
Definition ca-tiling.H:163
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
constexpr std::uint64_t morton_encode_2d(const std::uint32_t x, const std::uint32_t y) noexcept
Morton (Z-order) encoding of (x, y).
Definition ca-tiling.H:375
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
2D blocked partitioning into a grid of parts_y x parts_x.
Definition ca-tiling.H:258
static constexpr Tile2D_Range tile(const std::array< ca_size_t, 2 > &extents, const ca_size_t parts_y, const ca_size_t parts_x, const ca_size_t tile_index) noexcept
Tile owned by tile_index over the parts_y * parts_x grid.
Definition ca-tiling.H:266
static constexpr std::array< ca_size_t, 2 > factor_partitions(const std::array< ca_size_t, 2 > &extents, const ca_size_t parts) noexcept
Pick a balanced (parts_y, parts_x) factorisation of parts.
Definition ca-tiling.H:292
Column partitioning along the last axis.
Definition ca-tiling.H:210
static constexpr Range1D slab(const std::array< ca_size_t, Rank > &extents, const ca_size_t parts, const ca_size_t idx) noexcept
Half-open column range owned by partition idx.
Definition ca-tiling.H:214
Half-open integer interval [begin, end).
Definition ca-tiling.H:90
ca_size_t end
Exclusive upper bound.
Definition ca-tiling.H:94
constexpr ca_size_t size() const noexcept
Definition ca-tiling.H:97
constexpr bool empty() const noexcept
Definition ca-tiling.H:103
ca_size_t begin
Inclusive lower bound.
Definition ca-tiling.H:92
Row partitioning along axis 0.
Definition ca-tiling.H:180
static constexpr Range1D slab(const std::array< ca_size_t, Rank > &extents, const ca_size_t parts, const ca_size_t idx) noexcept
Number of partitions along axis 0 with a balanced split.
Definition ca-tiling.H:190
2D tile descriptor: a rectangle in axis-0 / axis-1 coordinates.
Definition ca-tiling.H:225
constexpr ca_size_t size() const noexcept
Definition ca-tiling.H:232
Range1D cols
Half-open axis-1 range.
Definition ca-tiling.H:229
constexpr bool empty() const noexcept
Definition ca-tiling.H:238
Range1D rows
Half-open axis-0 range.
Definition ca-tiling.H:227
gsl_rng * r