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");
253 std::array<state_type, neighbour_count>
nbuf{};
264 std::array<state_type, neighbour_count>
nbuf{};
276 template <std::
size_t TileW, std::
size_t TileH>
279 std::array<state_type, neighbour_count>
nbuf{};
301 std::array<state_type, neighbour_count>
nbuf{};
320 if constexpr (
rank == 1)
324 else if constexpr (
rank == 2)
326 if constexpr (ca_parallel_detail::is_tile_v<Order>)
331 else if constexpr (
rank == 3)
337 static_assert(
rank <= 3,
"Parallel_Synchronous_Engine currently supports rank <= 3");
391 requires std::default_initializable<Neighborhood>
431 template <
typename F>
438 template <
typename F>
483 std::vector<std::future<void>>
futures;
486 for (std::size_t p = 0; p <
parts; ++p)
493 this->process_slab(b, e);
523 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
std::size_t steps_run() const noexcept
Return the number of completed steps.
void set_config(Parallel_Engine_Config cfg) noexcept
Replace the configuration. Takes effect on the next step().
void run(const std::size_t steps)
Run several synchronous steps.
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(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)
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).
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
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.