|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Parallel synchronous double-buffered engine. More...
#include <tpl_ca_parallel_engine.H>
Public Types | |
| using | lattice_type = Lattice |
| using | rule_type = Rule |
| using | neighborhood_type = Neighborhood |
| using | order_type = Order |
| using | state_type = typename Lattice::state_type |
| using | coord_type = typename Lattice::coord_type |
| using | extents_type = typename Lattice::extents_type |
| using | hook_type = std::function< void(std::size_t, const Lattice &)> |
Hook signature: (step_index, frame). | |
Public Member Functions | |
| Parallel_Synchronous_Engine (Lattice initial, Rule r, Neighborhood n, Parallel_Engine_Config cfg={}) | |
| Build an engine on top of an existing initial lattice. | |
| Parallel_Synchronous_Engine (Lattice initial, Rule r, Parallel_Engine_Config cfg={}) | |
| Build an engine with a default-constructed neighbourhood. | |
| const Lattice & | frame () const noexcept |
| Return the current frame. | |
| std::size_t | steps_run () const noexcept |
| Return the number of completed steps. | |
| const extents_type & | extents () const noexcept |
| const Parallel_Engine_Config & | config () const noexcept |
| void | set_config (Parallel_Engine_Config cfg) noexcept |
Replace the configuration. Takes effect on the next step(). | |
| template<typename F > | |
| void | on_pre_step (F &&f) |
Register a hook fired before every step(). | |
| template<typename F > | |
| void | on_post_step (F &&f) |
Register a hook fired after every step(). | |
| void | step () |
| Apply the rule to every cell once and swap buffers. | |
| void | run (const std::size_t steps) |
| Run several synchronous steps. | |
Static Public Attributes | |
| static constexpr std::size_t | rank = Lattice::rank |
| Lattice dimension. | |
| static constexpr std::size_t | neighbour_count = Neighborhood::size_v |
| Number of neighbours. | |
Private Member Functions | |
| void | compute_cell (const coord_type &c) const =delete |
| 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 | update_slab_1d (const ca_size_t r_begin, const ca_size_t r_end) |
| void | update_slab_2d_row_major (const ca_size_t r_begin, const ca_size_t r_end) |
| template<std::size_t TileW, std::size_t TileH> | |
| void | update_slab_2d_tile (const ca_size_t r_begin, const ca_size_t r_end) |
| void | update_slab_3d_row_major (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. | |
| std::size_t | effective_partitions (ThreadPool *pool) const noexcept |
Resolve the effective number of partitions for the next step(). | |
Private Attributes | |
| Lattice | cur_buf_ |
| Lattice | nxt_buf_ |
| Rule | rule_ |
| Neighborhood | nh_ |
| hook_type | pre_hook_ |
| hook_type | post_hook_ |
| std::size_t | step_count_ = 0 |
| Parallel_Engine_Config | cfg_ {} |
Parallel synchronous double-buffered engine.
Same template parameters as Synchronous_Engine. The only user-visible additions are the configuration knobs and the fact that step() schedules work on a thread pool when the lattice is large enough.
| Lattice | lattice type satisfying LatticeLike. |
| Rule | rule type satisfying RuleLike<Rule, Lattice>. |
| Neighborhood | neighbourhood type satisfying NeighborhoodLike. |
| Order | iteration order tag (RowMajor or Tile<W, H>). |
step() is O((N * S) / P) wall-clock with P workers, where N is the cell count and S is the neighbour count.step() / run() at a time. Worker threads inside the pool cooperate without synchronisation by reading the previous frame (read-only) and writing into disjoint slices of the next frame. Bit-packed Bit_Cell_Storage writes are safely parallelized because the underlying bits use atomic operations to avoid data races. Definition at line 186 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::coord_type = typename Lattice::coord_type |
Definition at line 201 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::extents_type = typename Lattice::extents_type |
Definition at line 202 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::hook_type = std::function<void(std::size_t, const Lattice &)> |
Hook signature: (step_index, frame).
Definition at line 210 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::lattice_type = Lattice |
Definition at line 195 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::neighborhood_type = Neighborhood |
Definition at line 197 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::order_type = Order |
Definition at line 198 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rule_type = Rule |
Definition at line 196 of file tpl_ca_parallel_engine.H.
| using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::state_type = typename Lattice::state_type |
Definition at line 200 of file tpl_ca_parallel_engine.H.
|
inline |
Build an engine on top of an existing initial lattice.
| [in] | initial | initial lattice (consumed by move). |
| [in] | r | rule instance. |
| [in] | n | neighbourhood instance. |
| [in] | cfg | parallel-engine configuration. |
| std::bad_alloc | on next-buffer allocation. |
Definition at line 377 of file tpl_ca_parallel_engine.H.
|
inline |
Build an engine with a default-constructed neighbourhood.
| [in] | initial | initial lattice (consumed by move). |
| [in] | r | rule instance. |
| [in] | cfg | parallel-engine configuration. |
| std::bad_alloc | on next-buffer allocation. |
Definition at line 390 of file tpl_ca_parallel_engine.H.
|
privatedelete |
|
inlinenoexcept |
Definition at line 418 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_.
|
inlineprivatenoexcept |
Resolve the effective number of partitions for the next step().
Falls back to 1 when the pool is unavailable or the lattice is too small to amortise the scheduling overhead.
Definition at line 344 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, Aleph::CA::Parallel_Engine_Config::min_parallel_cells, Aleph::CA::Parallel_Engine_Config::num_partitions, Aleph::CA::should_run_sequential(), and Aleph::CA::Lattice< Storage, Boundary >::size().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step().
|
inlinenoexcept |
Definition at line 412 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, and Aleph::CA::Lattice< Storage, Boundary >::extents().
|
inlinenoexcept |
Return the current frame.
Definition at line 398 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_.
Referenced by expect_engine_equivalence().
|
inline |
Register a hook fired after every step().
Definition at line 439 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::post_hook_.
|
inline |
Register a hook fired before every step().
Hooks run on the driving thread, never inside a worker.
Definition at line 432 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::pre_hook_.
Referenced by TEST().
|
inlineprivate |
Drive the iteration over a single row slab according to Order.
Picked at compile time so workers run a tight, branch-free loop.
Definition at line 318 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rank, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_1d(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_row_major(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_3d_row_major().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step().
|
inline |
Run several synchronous steps.
| [in] | steps | number of consecutive step() calls to perform. |
| Any | exception propagated by step(). |
Definition at line 521 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step(), and steps.
Referenced by expect_engine_equivalence(), main(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Replace the configuration. Takes effect on the next step().
Definition at line 424 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_.
|
inline |
Apply the rule to every cell once and swap buffers.
Refreshes the halo (when applicable), fires the pre-step hook, schedules the per-partition work onto the configured thread pool (or runs sequentially when the workload is below the threshold), swaps the buffers in O(1), and fires the post-step hook.
| Any | exception propagated by the rule, lattice or hooks. |
| std::runtime_error | if a worker encountered an exception during the parallel section. |
N is the cell count, K the neighbourhood size and P the effective worker count. Definition at line 459 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, Aleph::default_pool(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::effective_partitions(), Aleph::ThreadPool::enqueue(), Aleph::CA::Lattice< Storage, Boundary >::extents(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::nxt_buf_, Aleph::CA::Parallel_Engine_Config::pool, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::post_hook_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::pre_hook_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab(), rng, Aleph::CA::Lattice< Storage, Boundary >::size(), Aleph::CA::Row_Partition< Rank >::slab(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step_count_, and Aleph::CA::Lattice< Storage, Boundary >::swap().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::run().
|
inlinenoexcept |
Return the number of completed steps.
step() invocations performed so far. Definition at line 406 of file tpl_ca_parallel_engine.H.
References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step_count_.
Referenced by expect_engine_equivalence().
|
inlineprivate |
Update a single cell c: gather its neighbours, evaluate the rule and write the result into nxt_buf_.
The neighbour buffer is on the caller's stack to avoid any heap activity per cell. The Cell_Context is derived from the engine's step counter and the cell coordinate, so contextual rules (e.g. Phase 8 stochastic rules) see the same (step, coord) pair regardless of which worker visits the cell.
Definition at line 236 of file tpl_ca_parallel_engine.H.
References Aleph::CA::apply_rule(), Aleph::CA::Lattice< Storage, Boundary >::at(), Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, Aleph::CA::gather_neighbors(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::nh_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::nxt_buf_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rule_, Aleph::CA::Lattice< Storage, Boundary >::set(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step_count_.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_1d(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_row_major(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_tile(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_3d_row_major().
|
inlineprivate |
Definition at line 251 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab().
|
inlineprivate |
Definition at line 262 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), cols, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, Aleph::CA::Lattice< Storage, Boundary >::size(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab().
|
inlineprivate |
Definition at line 277 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), cols, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, Aleph::CA::Lattice< Storage, Boundary >::size(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
|
inlineprivate |
Definition at line 299 of file tpl_ca_parallel_engine.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, k, Aleph::CA::Lattice< Storage, Boundary >::size(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab().
|
private |
Definition at line 220 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::config(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::effective_partitions(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::set_config(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step().
|
private |
Definition at line 213 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::effective_partitions(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::extents(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::frame(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_row_major(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_tile(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_3d_row_major().
|
staticconstexpr |
Number of neighbours.
Definition at line 207 of file tpl_ca_parallel_engine.H.
|
private |
Definition at line 216 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
|
private |
Definition at line 214 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
|
private |
Definition at line 218 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::on_post_step(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step().
|
private |
Definition at line 217 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::on_pre_step(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step().
|
staticconstexpr |
Lattice dimension.
Definition at line 205 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab().
|
private |
Definition at line 215 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().
|
private |
Definition at line 219 of file tpl_ca_parallel_engine.H.
Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::steps_run(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell().