58#include <gtest/gtest.h>
81TEST(CATiling, SplitRangeBalancedExactDivision)
84 for (std::size_t i = 0; i < 5; ++i)
87 EXPECT_EQ(
r.size(), 10u);
88 EXPECT_EQ(
r.begin, i * 10u);
89 EXPECT_EQ(
r.end, (i + 1) * 10u);
93TEST(CATiling, SplitRangeBalancedRemainder)
99 EXPECT_EQ(a.size(), 4u);
100 EXPECT_EQ(b.size(), 3u);
101 EXPECT_EQ(c.size(), 3u);
102 EXPECT_EQ(a.begin, 0u);
103 EXPECT_EQ(b.begin, a.end);
104 EXPECT_EQ(c.begin, b.end);
105 EXPECT_EQ(c.end, 10u);
108TEST(CATiling, SplitRangeBalancedEdgeCases)
114 EXPECT_TRUE(
out.empty());
115 EXPECT_EQ(
out.begin, 7u);
118TEST(CATiling, RowPartitionCoversEveryIndexExactlyOnce)
120 std::array<ca_size_t, 2> ext{17, 9};
121 std::vector<int> hits(ext[0], 0);
122 for (std::size_t p = 0; p < 5; ++p)
132TEST(CATiling, BlockPartition2DCoversEveryCellOnce)
134 std::array<ca_size_t, 2> ext{12, 18};
136 EXPECT_EQ(factor[0] * factor[1], 6u);
138 std::vector<std::vector<int>> hits(ext[0], std::vector<int>(ext[1], 0));
139 for (std::size_t t = 0; t < 6; ++t)
142 for (
ca_size_t i = tile.rows.begin; i < tile.rows.end; ++i)
143 for (
ca_size_t j = tile.cols.begin; j < tile.cols.end; ++j)
146 for (
const auto &
row : hits)
151TEST(CATiling, MortonEncodeRoundTripBits)
162TEST(CATiling, ShouldRunSequentialBelowThreshold)
178 std::bernoulli_distribution flip(
density);
179 using coord_t =
typename L::coord_type;
180 if constexpr (L::rank == 1)
184 lat.set(coord_t{
static_cast<ca_index_t>(i)}, flip(
rng) ? 1 : 0);
186 else if constexpr (L::rank == 2)
195 else if constexpr (L::rank == 3)
213 if (a.extents() != b.extents())
215 using coord_t =
typename L::coord_type;
216 if constexpr (L::rank == 1)
220 if (a.at(coord_t{static_cast<ca_index_t>(i)})
224 else if constexpr (L::rank == 2)
232 if (a.at(c) != b.at(c))
236 else if constexpr (L::rank == 3)
248 if (a.at(c) != b.at(c))
257template <
typename Lattice,
typename Rule,
typename Neighborhood>
263 std::size_t partitions,
264 std::size_t min_cells = 0)
279 <<
"Parallel engine diverged from sequential at " <<
steps <<
" steps with "
280 << partitions <<
" partitions";
287TEST(CAParallelEngine, GameOfLifeToroidalEquivalence)
293 for (std::size_t parts : {1u, 2u, 4u, 8u})
297TEST(CAParallelEngine, GameOfLifeOpenBoundaryEquivalence)
303 for (std::size_t parts : {1u, 2u, 4u})
307TEST(CAParallelEngine, GameOfLifeReflectiveEquivalence)
313 for (std::size_t parts : {1u, 3u, 8u})
317TEST(CAParallelEngine, GameOfLifeConstantBoundaryEquivalence)
323 for (std::size_t parts : {1u, 2u, 4u})
327TEST(CAParallelEngine, GameOfLifeNeumannEquivalence)
333 for (std::size_t parts : {1u, 4u, 8u})
337TEST(CAParallelEngine, GhostLatticeToroidalEquivalence)
343 for (std::size_t parts : {1u, 2u, 4u, 8u})
347TEST(CAParallelEngine, GhostLatticeReflectiveEquivalence)
353 for (std::size_t parts : {1u, 4u})
357TEST(CAParallelEngine, TotalisticVonNeumannRadius2Equivalence)
367 for (std::size_t parts : {1u, 2u, 8u})
371TEST(CAParallelEngine, TileOrderEquivalence)
384 cfg.num_partitions = 4;
385 cfg.min_parallel_cells = 0;
394TEST(CAParallelEngine, Wolfram1DEquivalence)
400 for (std::uint8_t rule_no : {std::uint8_t{30}, std::uint8_t{90}, std::uint8_t{110}})
403 seed.set(
typename L::coord_type{128}, 1);
406 for (std::size_t parts : {1u, 2u, 4u, 8u})
411TEST(CAParallelEngine, ThreeDOuterTotalisticEquivalence)
414 L
seed({12, 12, 12}, 0);
419 +[](
int current, std::size_t alive) ->
int {
420 const bool alive_now = current != 0;
421 const bool next_alive = (alive_now
and (alive == 4 or alive == 5))
422 or (not alive_now
and alive == 5);
423 return next_alive ? 1 : 0;
426 for (std::size_t parts : {1u, 2u, 4u})
434TEST(CAParallelEngine, SequentialFallbackBelowThreshold)
460TEST(CAParallelEngine, HookFiringSemantics)
472 std::size_t pre_count = 0;
473 std::size_t post_count = 0;
474 std::size_t last_pre_idx = 0;
475 std::size_t last_post_idx = 0;
476 par.
on_pre_step([&](std::size_t step_idx,
const L &) {
478 last_pre_idx = step_idx;
480 par.on_post_step([&](std::size_t step_idx,
const L &) {
482 last_post_idx = step_idx;
486 EXPECT_EQ(pre_count, 7u);
487 EXPECT_EQ(post_count, 7u);
488 EXPECT_EQ(last_pre_idx, 6u);
489 EXPECT_EQ(last_post_idx, 7u);
492TEST(CAParallelEngine, CustomThreadPoolInjection)
501 cfg.
pool = &custom_pool;
515TEST(CAParallelEngine, DeterminismAcrossRepeatedRuns)
537TEST(CAParallelEngine, GliderTrajectoryIsExact)
567TEST(CAParallelEngine, BlinkerOscillatesUnderEveryPartitionCount)
576 for (std::size_t parts : {1u, 2u, 5u, 8u})
585 <<
"Blinker did not return to seed with " << parts <<
" partitions";
589TEST(CAParallelEngine, RankOneEquivalenceSmallLattice)
604TEST(CAParallelEngine, BitCellStorageRunsConcurrentlyWithoutDataRaces)
607 L
seed({33, 32},
false);
614 cfg.num_partitions = 8;
615 cfg.min_parallel_cells = 0;
size_t size_t int32_t * out
Domain-decomposition utilities for the parallel CA engine.
Common typedefs and tag types for the Cellular Automata module.
User-supplied list of offsets for arbitrary connectivity.
Lattice with Halo ghost layers around the user-visible cells.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Rule whose next state depends on (current, alive_count).
Parallel synchronous double-buffered engine.
std::size_t steps_run() const noexcept
Return the number of completed steps.
void run(const std::size_t steps)
Run several synchronous steps.
const Lattice & frame() const noexcept
Return the current frame.
void on_pre_step(F &&f)
Register a hook fired before every step().
Synchronous double-buffered engine.
void run(const std::size_t steps)
Run several synchronous steps.
std::size_t steps_run() const noexcept
Return the number of completed steps.
const Lattice & frame() const noexcept
Return the current frame.
Rule whose next state depends on the sum of every cell in the neighbourhood (including the centre).
Von Neumann (L1) neighborhood of radius R in N dimensions.
A reusable thread pool for efficient parallel task execution.
Minimal std::expected-style result type for C++20.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
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.
double density(const Lattice &lat, const typename Lattice::state_type &s)
bool frames_equal(const Lattice &a, const Lattice &b)
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.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Coord_Vec< N > Offset_Vec
Default offset vector (aliases Coord_Vec).
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).
constexpr Lookup_Rule< 2, 2 > make_wolfram_elementary_rule(std::uint8_t rule_no) noexcept
Build the elementary 1D Wolfram rule rule_no (0..255) as a Lookup_Rule<2, 2> over neighbourhood {-1,...
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
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.
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.
Zero-gradient (Neumann) boundary.
Out-of-range neighbours behave as if the lattice ended.
Configuration for Parallel_Synchronous_Engine.
std::size_t num_partitions
Number of partitions per step.
ThreadPool * pool
Thread pool to schedule on. nullptr means Aleph::default_pool().
std::size_t min_parallel_cells
Workloads with fewer cells than this are executed sequentially without touching the pool.
Out-of-range coordinates mirror back into the lattice.
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.
Iterate a 2D lattice in W x H tiles.
The lattice wraps around on every axis.
A modern, efficient thread pool for parallel task execution.
Bit-packed dense storage for boolean cellular automata.
C++20 concepts for the Cellular Automata module.
Synchronous double-buffered engine for cellular automata.
Ghost-layer lattice for high-performance boundary handling.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Parallel synchronous engine for cellular automata (Phase 5).
static void seed_random(L &lat, std::uint32_t seed, double density=0.4)
static void expect_engine_equivalence(const Lattice &seed, Rule rule, Neighborhood nh, std::size_t steps, std::size_t partitions, std::size_t min_cells=0)
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).