Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > Class Template Reference

Parallel synchronous double-buffered engine. More...

#include <tpl_ca_parallel_engine.H>

Collaboration diagram for Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >:
[legend]

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.
 
 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 configuration, whose pool the copy uses too: neither engine owns it.
 
 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, no steps run and the same configuration.
 
Parallel_Synchronous_Engine & operator= (const Parallel_Synchronous_Engine &other)=default
 Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks, its count of steps and its configuration, whose pool this one uses too: neither engine owns it.
 
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 this one.
 
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.
 
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().
 
void swap_all_or_none (Parallel_Synchronous_Engine &other)
 

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_ {}
 

Detailed Description

template<typename Lattice, typename Rule, typename Neighborhood, typename Order = RowMajor>
class Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >

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.

Template Parameters
Latticelattice type satisfying LatticeLike.
Rulerule type satisfying RuleLike<Rule, Lattice>.
Neighborhoodneighbourhood type satisfying NeighborhoodLike.
Orderiteration order tag (RowMajor or Tile<W, H>).
Complexity
step() is O((N * S) / P) wall-clock with P workers, where N is the cell count and S is the neighbour count.
Thread-safety
The engine is not concurrent: only one driving thread should call 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.

Member Typedef Documentation

◆ coord_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
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.

◆ extents_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
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.

◆ hook_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
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.

◆ lattice_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::lattice_type = Lattice

Definition at line 195 of file tpl_ca_parallel_engine.H.

◆ neighborhood_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::neighborhood_type = Neighborhood

Definition at line 197 of file tpl_ca_parallel_engine.H.

◆ order_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::order_type = Order

Definition at line 198 of file tpl_ca_parallel_engine.H.

◆ rule_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
using Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rule_type = Rule

Definition at line 196 of file tpl_ca_parallel_engine.H.

◆ state_type

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
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.

Constructor & Destructor Documentation

◆ Parallel_Synchronous_Engine() [1/4]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::Parallel_Synchronous_Engine ( Lattice  initial,
Rule  r,
Neighborhood  n,
Parallel_Engine_Config  cfg = {} 
)
inline

Build an engine on top of an existing initial lattice.

Parameters
[in]initialinitial lattice (consumed by move).
[in]rrule instance.
[in]nneighbourhood instance.
[in]cfgparallel-engine configuration.
Exceptions
std::bad_allocon next-buffer allocation.
Complexity
O(N) for allocating the next buffer.

Definition at line 377 of file tpl_ca_parallel_engine.H.

◆ Parallel_Synchronous_Engine() [2/4]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::Parallel_Synchronous_Engine ( Lattice  initial,
Rule  r,
Parallel_Engine_Config  cfg = {} 
)
inline

Build an engine with a default-constructed neighbourhood.

Parameters
[in]initialinitial lattice (consumed by move).
[in]rrule instance.
[in]cfgparallel-engine configuration.
Exceptions
std::bad_allocon next-buffer allocation.

Definition at line 390 of file tpl_ca_parallel_engine.H.

◆ Parallel_Synchronous_Engine() [3/4]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::Parallel_Synchronous_Engine ( const Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > &  other)
default

Copy another engine: its lattices, its rule, its neighborhood, its hooks, its count of steps and its configuration, whose pool the copy uses too: neither engine owns it.

Parameters
[in]otherengine to copy.

◆ Parallel_Synchronous_Engine() [4/4]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::Parallel_Synchronous_Engine ( Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > &&  other)
inlinenoexcept

Take the simulation of another engine, which is left with empty lattices, no hooks, no steps run and the same configuration.

The implicit move took the lattices but copied the step counter: the moved-from engine reported the steps of a simulation that it no longer had.

Parameters
[in,out]otherengine to move from.

Definition at line 412 of file tpl_ca_parallel_engine.H.

Member Function Documentation

◆ compute_cell()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::compute_cell ( const coord_type &  c) const
privatedelete

◆ config()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
const Parallel_Engine_Config & Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::config ( ) const
inlinenoexcept
Returns
the active configuration.

Definition at line 544 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_.

◆ effective_partitions()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
std::size_t Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::effective_partitions ( ThreadPool *  pool) const
inlineprivatenoexcept

◆ extents()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
const extents_type & Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::extents ( ) const
inlinenoexcept

◆ frame()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
const Lattice & Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::frame ( ) const
inlinenoexcept

Return the current frame.

Returns
read-only view of the current lattice buffer.

Definition at line 524 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_.

Referenced by expect_engine_equivalence().

◆ on_post_step()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
template<typename F >
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::on_post_step ( F &&  f)
inline

◆ on_pre_step()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
template<typename F >
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::on_pre_step ( F &&  f)
inline

Register a hook fired before every step().

Hooks run on the driving thread, never inside a worker.

Definition at line 558 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::pre_hook_.

Referenced by TEST().

◆ operator=() [1/2]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Parallel_Synchronous_Engine & Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::operator= ( const Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > &  other)
default

Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks, its count of steps and its configuration, whose pool this one uses too: neither engine owns it.

Parameters
[in]otherengine to copy.
Returns
this engine.

◆ operator=() [2/2]

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
Parallel_Synchronous_Engine & Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::operator= ( Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > &&  other)
inlinenoexcept

Exchange this engine with another, which is left with the previous simulation and configuration of this one.

Parameters
[in,out]otherengine to move from.
Returns
this engine.

Definition at line 436 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap().

◆ process_slab()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::process_slab ( const ca_size_t  r_begin,
const ca_size_t  r_end 
)
inlineprivate

◆ run()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::run ( const std::size_t  steps)
inline

Run several synchronous steps.

Parameters
[in]stepsnumber of consecutive step() calls to perform.
Exceptions
Anyexception propagated by step().
Complexity
O(steps * N * K / P).

Definition at line 647 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().

◆ set_config()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::set_config ( Parallel_Engine_Config  cfg)
inlinenoexcept

Replace the configuration. Takes effect on the next step().

Definition at line 550 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_.

◆ step()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step ( )
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.

Exceptions
Anyexception propagated by the rule, lattice or hooks.
std::runtime_errorif a worker encountered an exception during the parallel section.
Complexity
O((N * K) / P) wall-clock, where N is the cell count, K the neighbourhood size and P the effective worker count.

Definition at line 585 of file tpl_ca_parallel_engine.H.

References 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().

◆ steps_run()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
std::size_t Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::steps_run ( ) const
inlinenoexcept

Return the number of completed steps.

Returns
the number of step() invocations performed so far.

Definition at line 532 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step_count_.

Referenced by expect_engine_equivalence().

◆ swap()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap ( Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order > &  other)
inlinenoexcept

Exchange in O(1) the simulation and the configuration of this engine and another.

Parameters
[in,out]otherengine to exchange with.
Exceptions
whatswapping the rules, the neighborhoods or the lattices throws. When one of those swaps may throw, the rules and the neighborhoods are exchanged before the lattices, all or none: if a swap throws, those before it are exchanged back, and both engines are left as they were, if the swap that threw left its arguments as they were. If a swap that puts one back throws too, both engines can only be destroyed or assigned. std::swap(), for a rule without a swap of its own, may leave it moved from.

Definition at line 457 of file tpl_ca_parallel_engine.H.

References Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cfg_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::cur_buf_, 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 >::post_hook_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::pre_hook_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rule_, Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::step_count_, Aleph::CA::Lattice< Storage, Boundary >::swap(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap_all_or_none().

Referenced by Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::operator=(), Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap(), and Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::swap_all_or_none().

◆ swap_all_or_none()

◆ update_cell()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_cell ( const coord_type &  c,
std::span< state_type >  nbuf 
)
inlineprivate

◆ update_slab_1d()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_1d ( const ca_size_t  r_begin,
const ca_size_t  r_end 
)
inlineprivate

◆ update_slab_2d_row_major()

◆ update_slab_2d_tile()

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
template<std::size_t TileW, std::size_t TileH>
void Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::update_slab_2d_tile ( const ca_size_t  r_begin,
const ca_size_t  r_end 
)
inlineprivate

◆ update_slab_3d_row_major()

Member Data Documentation

◆ cfg_

◆ cur_buf_

◆ neighbour_count

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
constexpr std::size_t Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::neighbour_count = Neighborhood::size_v
staticconstexpr

Number of neighbours.

Definition at line 207 of file tpl_ca_parallel_engine.H.

◆ nh_

◆ nxt_buf_

◆ post_hook_

◆ pre_hook_

◆ rank

template<typename Lattice , typename Rule , typename Neighborhood , typename Order = RowMajor>
constexpr std::size_t Aleph::CA::Parallel_Synchronous_Engine< Lattice, Rule, Neighborhood, Order >::rank = Lattice::rank
staticconstexpr

◆ rule_

◆ step_count_


The documentation for this class was generated from the following file: