80#ifndef TPL_CA_PARALLEL_ENGINE_H
81#define TPL_CA_PARALLEL_ENGINE_H
105namespace ca_parallel_detail {
113template <std::
size_t W, std::
size_t H>
121template <
typename Storage>
126template <std::
size_t N>
131template <
typename Lattice>
134}
and is_bit_cell_storage<typename Lattice::storage_type>::value;
185template <
typename Lattice,
typename Rule,
typename Neighborhood,
typename Order = RowMajor>
188 static_assert(
LatticeLike<Lattice>,
"Parallel_Synchronous_Engine requires a LatticeLike lattice");
190 "Parallel_Synchronous_Engine requires a NeighborhoodLike neighborhood");
192 static_assert(Neighborhood::rank_v ==
Lattice::rank,
"Neighborhood rank must match lattice rank");
238 const std::size_t neigh_sz = std::min(
nh_.size(), nbuf.size());
253 std::array<state_type, neighbour_count> nbuf{};
255 for (
ca_size_t i = r_begin; i < r_end; ++i)
264 std::array<state_type, neighbour_count> nbuf{};
267 for (
ca_size_t i = r_begin; i < r_end; ++i)
276 template <std::
size_t TileW, std::
size_t TileH>
279 std::array<state_type, neighbour_count> nbuf{};
284 for (
ca_size_t ti = r_begin; ti < r_end; ti += TileH)
287 const ca_size_t i_end = ti + TileH < r_end ? ti + TileH : r_end;
301 std::array<state_type, neighbour_count> nbuf{};
305 for (
ca_size_t i = r_begin; i < r_end; ++i)
320 if constexpr (
rank == 1)
324 else if constexpr (
rank == 2)
326 if constexpr (ca_parallel_detail::is_tile_v<Order>)
327 update_slab_2d_tile<Order::width, Order::height>(r_begin, r_end);
331 else if constexpr (
rank == 3)
337 static_assert(
rank <= 3,
"Parallel_Synchronous_Engine currently supports rank <= 3");
348 const std::size_t pool_workers = pool->num_threads();
357 parts = outer == 0 ? 1 : outer;
391 requires std::default_initializable<Neighborhood>
413 std::is_nothrow_move_constructible_v<Lattice>
and std::is_nothrow_move_constructible_v<Rule>
414 and std::is_nothrow_move_constructible_v<Neighborhood>)
437 noexcept(
swap(other)))
438 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
458 noexcept(
cur_buf_.
swap(other.cur_buf_))
and std::is_nothrow_swappable_v<Rule>
459 and std::is_nothrow_swappable_v<Neighborhood>)
460 requires (std::swappable<Rule>
and std::swappable<Neighborhood>)
469 constexpr bool may_throw =
470 not std::is_nothrow_swappable_v<Rule> or not std::is_nothrow_swappable_v<Neighborhood>
473 if constexpr (may_throw)
557 template <
typename F>
564 template <
typename F>
609 std::vector<std::future<void>> futures;
610 futures.reserve(parts);
612 for (std::size_t p = 0; p < parts; ++p)
617 futures.emplace_back(pool->
enqueue([
this, b =
rng.begin, e =
rng.end]
619 this->process_slab(b, e);
624 for (
auto &f : futures)
627 for (
auto &f : futures)
649 for (std::size_t i = 0; i <
steps; ++i)
Domain-decomposition utilities for the parallel CA engine.
Common typedefs and tag types for the Cellular Automata module.
Bit-packed row-major storage for N-dimensional binary CAs.
Lattice that adds boundary-aware access on top of a storage.
void set(const coord_type &c, const state_type &v)
Strict write: throws if c is out of range.
typename Storage::state_type state_type
typename Storage::extents_type extents_type
const extents_type & extents() const noexcept
void swap(Lattice &other) noexcept(noexcept(store_.swap(other.store_)))
O(1) swap.
typename Storage::coord_type coord_type
static constexpr std::size_t rank
ca_size_t size() const noexcept
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Parallel synchronous double-buffered engine.
std::function< void(std::size_t, const Lattice &)> hook_type
Hook signature: (step_index, frame).
const Parallel_Engine_Config & config() const noexcept
typename Lattice::extents_type extents_type
static constexpr std::size_t neighbour_count
Number of neighbours.
void update_cell(const coord_type &c, std::span< state_type > nbuf)
Update a single cell c: gather its neighbours, evaluate the rule and write the result into nxt_buf_.
void on_post_step(F &&f)
Register a hook fired after every step().
Parallel_Synchronous_Engine(Lattice initial, Rule r, Parallel_Engine_Config cfg={})
Build an engine with a default-constructed neighbourhood.
void update_slab_1d(const ca_size_t r_begin, const ca_size_t r_end)
const extents_type & extents() const noexcept
Parallel_Synchronous_Engine & operator=(const Parallel_Synchronous_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks,...
std::size_t steps_run() const noexcept
Return the number of completed steps.
void swap(Parallel_Synchronous_Engine &other) noexcept(noexcept(cur_buf_.swap(other.cur_buf_)) and std::is_nothrow_swappable_v< Rule > and std::is_nothrow_swappable_v< Neighborhood >)
Exchange in O(1) the simulation and the configuration of this engine and another.
void set_config(Parallel_Engine_Config cfg) noexcept
Replace the configuration. Takes effect on the next step().
Parallel_Synchronous_Engine(Parallel_Synchronous_Engine &&other) noexcept(std::is_nothrow_move_constructible_v< Lattice > and std::is_nothrow_move_constructible_v< Rule > and std::is_nothrow_move_constructible_v< Neighborhood >)
Take the simulation of another engine, which is left with empty lattices, no hooks,...
void run(const std::size_t steps)
Run several synchronous steps.
Parallel_Synchronous_Engine & operator=(Parallel_Synchronous_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation and configuration of th...
static constexpr std::size_t rank
Lattice dimension.
void update_slab_2d_tile(const ca_size_t r_begin, const ca_size_t r_end)
void process_slab(const ca_size_t r_begin, const ca_size_t r_end)
Drive the iteration over a single row slab according to Order.
void update_slab_2d_row_major(const ca_size_t r_begin, const ca_size_t r_end)
typename Lattice::state_type state_type
Neighborhood neighborhood_type
const Lattice & frame() const noexcept
Return the current frame.
Parallel_Engine_Config cfg_
void compute_cell(const coord_type &c) const =delete
std::size_t effective_partitions(ThreadPool *pool) const noexcept
Resolve the effective number of partitions for the next step().
void step()
Apply the rule to every cell once and swap buffers.
void on_pre_step(F &&f)
Register a hook fired before every step().
Parallel_Synchronous_Engine(const Parallel_Synchronous_Engine &other)=default
Copy another engine: its lattices, its rule, its neighborhood, its hooks, its count of steps and its ...
Parallel_Synchronous_Engine(Lattice initial, Rule r, Neighborhood n, Parallel_Engine_Config cfg={})
Build an engine on top of an existing initial lattice.
typename Lattice::coord_type coord_type
void update_slab_3d_row_major(const ca_size_t r_begin, const ca_size_t r_end)
void swap_all_or_none(Parallel_Synchronous_Engine &other)
Number Theoretic Transform over Z / MOD Z.
A reusable thread pool for efficient parallel task execution.
auto enqueue(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Submit a task for execution and get a future for the result.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
Detect whether a lattice exposes a refresh_halo() method (the hallmark of a Ghost_Lattice or any othe...
Storage + topology that carries the cell values.
Connectivity pattern around a coordinate.
Local transition function (any supported signature).
constexpr bool uses_bit_cell_storage_v
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.
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 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.
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.
ThreadPool & default_pool()
Return the default shared thread pool instance.
and
Check uniqueness with explicit hash + equality functors.
Per-cell context handed to rules that need to know "where" and "when" they are firing.
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.
Half-open integer interval [begin, end).
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.
Detect whether O is a Tile<W, H> instantiation.
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.
Neighborhoods catalogue for Aleph::CA.