67#ifndef TPL_CA_UPDATE_SCHEME_H
68#define TPL_CA_UPDATE_SCHEME_H
88namespace ca_scheme_detail {
96template <std::
size_t W, std::
size_t H>
105template <
typename Coord,
typename Cb>
109 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
117template <
typename Coord,
typename Cb>
121 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
122 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
131template <
typename Coord,
typename Cb>
135 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
136 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
145template <std::
size_t W, std::
size_t H,
typename Coord,
typename Cb>
152 const ca_size_t i_end = ti + H < n0 ? ti + H : n0;
153 const ca_size_t j_end = tj + W < n1 ? tj + W : n1;
165template <
typename Coord,
typename Cb>
169 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(n0); ++i)
170 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(n1); ++j)
171 for (
ca_index_t k = 0; k < static_cast<ca_index_t>(n2); ++
k)
181template <
typename Order,
typename Lattice,
typename Cb>
187 for_each_coord_1d<Coord>(lat.
size(0), std::forward<Cb>(cb));
191 if constexpr (is_tile_v<Order>)
192 for_each_coord_2d_tile<Order::width, Order::height, Coord>(lat.
size(0), lat.
size(1),
193 std::forward<Cb>(cb));
194 else if constexpr (std::is_same_v<Order, ColumnMajor>)
195 for_each_coord_2d_col<Coord>(lat.
size(0), lat.
size(1), std::forward<Cb>(cb));
197 for_each_coord_2d_row<Coord>(lat.
size(0), lat.
size(1), std::forward<Cb>(cb));
201 for_each_coord_3d_row<Coord>(lat.
size(0), lat.
size(1), lat.
size(2), std::forward<Cb>(cb));
205 static_assert(
Lattice::rank <= 3,
"Update schemes currently support rank <= 3");
210template <
typename Lattice>
220template <std::
size_t Rank>
224 for (std::size_t d = 0; d < Rank; ++d)
226 const ca_size_t n = ext[d] == 0 ? 1 : ext[d];
229 h =
splitmix64(
h ^ (
static_cast<std::uint64_t
>(d) + 1));
249template <
typename Order = RowMajor>
267 template <
typename Engine,
typename Rule>
270 using Lattice =
typename Engine::lattice_type;
273 using Neighborhood =
typename Engine::neighborhood_type;
274 constexpr std::size_t K = Neighborhood::size_v;
276 auto &cur = e.current_buffer();
277 auto &nxt = e.next_buffer();
278 const auto &nh = e.neighborhood();
279 const std::size_t step = e.step_count();
281 std::array<State, K == 0 ? 1 : K> nbuf{};
282 const std::size_t neigh_sz = (std::min) (nh.size(),
static_cast<std::size_t
>(nbuf.size()));
284 ca_scheme_detail::for_each_coord<Order>(cur, [&](
const Coord &c)
309template <
typename Order = RowMajor>
326 template <
typename Engine,
typename Rule>
329 using Lattice =
typename Engine::lattice_type;
332 using Neighborhood =
typename Engine::neighborhood_type;
333 constexpr std::size_t K = Neighborhood::size_v;
335 auto &cur = e.current_buffer();
336 const auto &nh = e.neighborhood();
337 const std::size_t step = e.step_count();
339 std::array<State, K == 0 ? 1 : K> nbuf{};
340 const std::size_t neigh_sz = (std::min) (nh.size(),
static_cast<std::size_t
>(nbuf.size()));
342 ca_scheme_detail::for_each_coord<Order>(cur, [&](
const Coord &c)
371template <
typename Engine_Type = std::mt19937_64>
392 const std::size_t sub_steps = 0) noexcept
407 template <
typename Engine,
typename Rule>
410 using Lattice =
typename Engine::lattice_type;
413 using Neighborhood =
typename Engine::neighborhood_type;
414 constexpr std::size_t K = Neighborhood::size_v;
416 auto &cur = e.current_buffer();
417 const auto &nh = e.neighborhood();
418 const std::size_t step = e.step_count();
423 const std::size_t subs
426 std::array<State, K == 0 ? 1 : K> nbuf{};
427 const std::size_t neigh_sz = (std::min) (nh.size(),
static_cast<std::size_t
>(nbuf.size()));
429 for (std::size_t sub = 0; sub < subs; ++sub)
433 const std::uint64_t pick_seed =
mix_seed(
435 Coord c = ca_scheme_detail::coord_from_hash<Lattice::rank>(pick_seed, cur.extents());
462template <std::
size_t BlockSize = 4,
typename Order = RowMajor>
465 static_assert(BlockSize >= 1,
"Block_Synchronous_Update requires BlockSize >= 1");
482 template <
typename Engine,
typename Rule>
485 using Lattice =
typename Engine::lattice_type;
488 using Neighborhood =
typename Engine::neighborhood_type;
489 constexpr std::size_t K = Neighborhood::size_v;
491 auto &cur = e.current_buffer();
492 auto &nxt = e.next_buffer();
493 const auto &nh = e.neighborhood();
494 const std::size_t step = e.step_count();
497 std::array<ca_size_t, Lattice::rank> blocks_per_axis{};
502 blocks_per_axis[d] = (n + BlockSize - 1) / BlockSize;
503 total_blocks *= (blocks_per_axis[d] == 0 ? 1 : blocks_per_axis[d]);
506 if (total_blocks == 0)
508 const std::size_t active = step % total_blocks;
511 std::array<ca_size_t, Lattice::rank> tile_coord{};
513 std::size_t rem = active;
516 const ca_size_t bn = blocks_per_axis[d] == 0 ? 1 : blocks_per_axis[d];
517 tile_coord[d] = rem % bn;
524 ca_scheme_detail::for_each_coord<Order>(cur, [&](
const Coord &c)
526 nxt.set(c, cur.at(c));
529 std::array<State, K == 0 ? 1 : K> nbuf{};
530 const std::size_t neigh_sz = (std::min) (nh.size(),
static_cast<std::size_t
>(nbuf.size()));
535 const ca_size_t i0 = tile_coord[0] * BlockSize;
536 const ca_size_t i_end = (std::min) (i0 + BlockSize, cur.size(0));
547 const ca_size_t i0 = tile_coord[0] * BlockSize;
549 const ca_size_t i_end = (std::min) (i0 + BlockSize, cur.size(0));
550 const ca_size_t j_end = (std::min) (
j0 + BlockSize, cur.size(1));
562 const ca_size_t i0 = tile_coord[0] * BlockSize;
564 const ca_size_t k0 = tile_coord[2] * BlockSize;
565 const ca_size_t i_end = (std::min) (i0 + BlockSize, cur.size(0));
566 const ca_size_t j_end = (std::min) (
j0 + BlockSize, cur.size(1));
567 const ca_size_t k_end = (std::min) (k0 + BlockSize, cur.size(2));
623 return (
k % 2 == 0) ? 0 : 1;
635 template <
typename Engine,
typename Block_Rule>
638 using Lattice =
typename Engine::lattice_type;
641 static_assert(
Lattice::rank == 2,
"Margolus_Update requires a 2D lattice");
643 auto &lat = e.current_buffer();
646 if (n0 < 2 or n1 < 2)
653 const Coord nw{i, j};
654 const Coord ne{i, j + 1};
655 const Coord sw{i + 1, j};
656 const Coord se{i + 1, j + 1};
658 std::array<State, 4> in{lat.at(nw), lat.at(ne), lat.at(sw), lat.at(se)};
659 const std::array<State, 4>
out =
r(in);
674 template <
typename Engine,
typename Block_Rule>
675 void apply(Engine &e, Block_Rule &
r)
const
Exception handling system with formatted messages for Aleph-w.
size_t size_t int32_t * out
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
typename Storage::state_type state_type
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
A coordinate type with N integral components.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_j0_function > > j0(const __gmp_expr< T, U > &expr)
Coord_Vec< Rank > coord_from_hash(std::uint64_t h, const std::array< ca_size_t, Rank > &ext) noexcept
Decode a 64-bit hash into a coordinate uniformly drawn over extents.
ca_size_t cell_count(const Lattice &lat) noexcept
Total cell count of a lattice (product of extents).
void for_each_coord_1d(const ca_size_t n0, Cb &&cb)
Iterate every coordinate of a rank-1 lattice in row order.
void for_each_coord_3d_row(const ca_size_t n0, const ca_size_t n1, const ca_size_t n2, Cb &&cb)
Iterate every coordinate of a rank-3 lattice in row-major order.
void for_each_coord(const Lattice &lat, Cb &&cb)
Visit every coordinate of lat honouring the chosen Order.
void for_each_coord_2d_row(const ca_size_t n0, const ca_size_t n1, Cb &&cb)
Iterate every coordinate of a rank-2 lattice in row-major order.
void for_each_coord_2d_tile(const ca_size_t n0, const ca_size_t n1, Cb &&cb)
Iterate a rank-2 lattice in H × W tiles (cache-friendly).
void for_each_coord_2d_col(const ca_size_t n0, const ca_size_t n1, Cb &&cb)
Iterate every coordinate of a rank-2 lattice in column-major order.
constexpr std::uint64_t mix_seed(const std::uint64_t a, const std::uint64_t b) noexcept
Combine two 64-bit values into a single deterministic hash.
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
void gather_neighbors(const Nbh &nh, const L &lat, const typename L::coord_type ¢er, std::span< T > out)
Populate out[0..nh.size()) with neighbour values of center.
constexpr std::uint64_t splitmix64(std::uint64_t x) noexcept
64-bit SplitMix hash.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
State apply_rule(const Rule &r, const State &s, Neighbor_View< State > v, const Cell_Context< Rank > &ctx)
Invoke a rule, optionally forwarding the per-cell context.
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Synchronous update over rotating sub-blocks.
static constexpr std::size_t block_size
void apply(Engine &e, Rule &r) const
Execute one block-synchronous step.
static constexpr bool requires_double_buffer
static constexpr bool requires_block_rule
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Margolus 2×2 partition update for reversible CAs.
static constexpr bool requires_block_rule
Operates on Block_Rule, not on cell rules.
void apply_with_origin(Engine &e, Block_Rule &r, const ca_index_t origin) const
Apply the block rule to every 2×2 block of the active partition.
static constexpr bool requires_double_buffer
Margolus uses a single buffer; reads and writes the same lattice block by block.
static constexpr ca_index_t origin_for_step(const std::size_t k) noexcept
Origin offset used at step k: even → (0,0), odd → (1,1).
void apply(Engine &e, Block_Rule &r) const
Apply the block rule using the parity of the engine step.
Pick one cell at random per sub-step, update it in place.
std::size_t sub_steps_per_step
Number of single-cell sub-steps per step().
static constexpr bool requires_double_buffer
In-place updates → no second buffer.
static constexpr bool requires_block_rule
Operates on cell rules.
std::uint64_t master_seed
constexpr Random_Asynchronous_Update(const std::uint64_t seed=0, const std::size_t sub_steps=0) noexcept
Build a random asynchronous update strategy.
void apply(Engine &e, Rule &r) const
Execute one randomised step.
In-place sequential update (no double buffer).
static constexpr bool requires_block_rule
Operates on cell rules.
void apply(Engine &e, Rule &r) const
Execute one sequential in-place step.
static constexpr bool requires_double_buffer
Operates in place, no second buffer needed.
Classical double-buffer synchronous update.
static constexpr bool requires_block_rule
Operates on cell rules — not on block rules.
static constexpr bool requires_double_buffer
Engines must allocate a second buffer for this scheme.
void apply(Engine &e, Rule &r) const
Execute one synchronous step.
Iterate a 2D lattice in W x H tiles.
Detect whether O is a Tile<W, H> instantiation.
C++20 concepts for the Cellular Automata module.
Neighborhoods catalogue for Aleph::CA.