Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_async_engine.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
69#ifndef TPL_CA_ASYNC_ENGINE_H
70#define TPL_CA_ASYNC_ENGINE_H
71
72#include <cstddef>
73#include <functional>
74#include <type_traits>
75#include <utility>
76
77#include <ah-errors.H>
78
79#include <ca-traits.H>
80#include <tpl_ca_concepts.H>
81#include <tpl_ca_neighborhood.H>
83
84namespace Aleph {
85namespace CA {
86
87namespace ca_async_engine_detail {
88
93template <typename Scheme, typename Engine, typename Rule>
95 = requires(const Scheme &s, Engine &e, Rule &r, ca_index_t o) { s.apply_with_origin(e, r, o); };
96
97} // namespace ca_async_engine_detail
98
109template <std::size_t Rank>
111{
112public:
114 static constexpr std::size_t rank_v = Rank;
116 static constexpr std::size_t size_v = 0;
118 static constexpr std::size_t radius_v = 0;
121
125 [[nodiscard]] static constexpr std::size_t radius() noexcept
126 {
127 return 0;
128 }
129
133 [[nodiscard]] static constexpr std::size_t size() noexcept
134 {
135 return 0;
136 }
137
141 template <typename F>
142 static constexpr void for_each_offset(const coord_type &, F &&) noexcept
143 {}
144};
145
161template <typename Lattice, typename Rule, typename Neighborhood, typename Update_Scheme = Synchronous_Update<RowMajor>>
163{
164 static_assert(LatticeLike<Lattice>, "Async_Engine requires a LatticeLike lattice");
165 static_assert(NeighborhoodLike<Neighborhood>,
166 "Async_Engine requires a NeighborhoodLike neighborhood (use Null_Neighborhood for "
167 "block rules)");
168 static_assert(Neighborhood::rank_v == Lattice::rank, "Neighborhood rank must match lattice rank");
169
170public:
172 using rule_type = Rule;
173 using neighborhood_type = Neighborhood;
174 using scheme_type = Update_Scheme;
175
179
181 static constexpr std::size_t rank = Lattice::rank;
182
184 using hook_type = std::function<void(std::size_t, const Lattice &)>;
185
186private:
188 Lattice nxt_; // empty for in-place schemes
189 Rule rule_;
190 Neighborhood nh_;
191 Update_Scheme scheme_;
194 std::size_t step_count_ = 0;
195
196public:
209 Async_Engine(Lattice initial, Rule r, Neighborhood n, Update_Scheme s = {})
210 : cur_(std::move(initial)), rule_(std::move(r)), nh_(std::move(n)), scheme_(std::move(s))
211 {
212 if constexpr (Update_Scheme::requires_double_buffer)
214 }
215
222 Async_Engine(Lattice initial, Rule r, Update_Scheme s = {})
223 requires std::default_initializable<Neighborhood>
224 : Async_Engine(std::move(initial), std::move(r), Neighborhood{}, std::move(s))
225 {}
226
231 Async_Engine(const Async_Engine &other) = default;
232
242 Async_Engine(Async_Engine &&other) noexcept(
243 std::is_nothrow_move_constructible_v<Lattice> and std::is_nothrow_move_constructible_v<Rule>
244 and std::is_nothrow_move_constructible_v<Neighborhood>
245 and std::is_nothrow_move_constructible_v<Update_Scheme>)
246 : cur_(std::move(other.cur_)), nxt_(std::move(other.nxt_)), rule_(std::move(other.rule_)),
247 nh_(std::move(other.nh_)), scheme_(std::move(other.scheme_)),
248 pre_hook_(std::exchange(other.pre_hook_, nullptr)),
249 post_hook_(std::exchange(other.post_hook_, nullptr)),
250 step_count_(std::exchange(other.step_count_, 0))
251 {}
252
259 Async_Engine &operator=(const Async_Engine &other) = default;
260
266 Async_Engine &operator=(Async_Engine &&other) noexcept(noexcept(swap(other)))
267 requires (std::swappable<Rule> and std::swappable<Neighborhood>
268 and std::swappable<Update_Scheme>)
269 {
270 swap(other);
271 return *this;
272 }
273
286 void swap(Async_Engine &other) noexcept(
287 noexcept(cur_.swap(other.cur_)) and std::is_nothrow_swappable_v<Rule>
288 and std::is_nothrow_swappable_v<Neighborhood> and std::is_nothrow_swappable_v<Update_Scheme>)
289 requires (std::swappable<Rule> and std::swappable<Neighborhood>
290 and std::swappable<Update_Scheme>)
291 {
292 // When a swap may throw, that of a rule, a neighborhood, a scheme or a
293 // lattice, the rules, the neighborhoods and the schemes go first, and
294 // all go in a sequence that is all or none: one that throws leaves no
295 // lattice with the rule or the scheme of the other. When none may, the
296 // order cannot be seen, and the rules, the neighborhoods and the
297 // schemes go after the lattices, as they always did: a rule or a
298 // scheme with members, even Game_Of_Life_Rule, would reorder the code
299 // if it went first
300 constexpr bool may_throw =
301 not std::is_nothrow_swappable_v<Rule> or not std::is_nothrow_swappable_v<Neighborhood>
302 or not std::is_nothrow_swappable_v<Update_Scheme> or not noexcept(cur_.swap(other.cur_));
303 using std::swap;
304 if constexpr (may_throw)
305 swap_all_or_none(other);
306 else
307 {
308 cur_.swap(other.cur_);
309 nxt_.swap(other.nxt_);
310 swap(rule_, other.rule_);
311 swap(nh_, other.nh_);
312 swap(scheme_, other.scheme_);
313 }
314 pre_hook_.swap(other.pre_hook_);
315 post_hook_.swap(other.post_hook_);
316 swap(step_count_, other.step_count_);
317 }
318
319private:
320 // The rules, the neighborhoods, the schemes and the lattices, with the
321 // swaps that the specification of swap() evaluates (found by their
322 // arguments, else std::swap()), all or none: if one throws, those before
323 // it are exchanged back
325 {
326 using std::swap;
327 int done = 0;
328 try
329 {
330 swap(rule_, other.rule_);
331 ++done;
332 swap(nh_, other.nh_);
333 ++done;
334 swap(scheme_, other.scheme_);
335 ++done;
336 cur_.swap(other.cur_);
337 ++done;
338 nxt_.swap(other.nxt_);
339 }
340 catch (...)
341 {
342 if (done > 3)
343 cur_.swap(other.cur_);
344 if (done > 2)
345 swap(scheme_, other.scheme_);
346 if (done > 1)
347 swap(nh_, other.nh_);
348 if (done > 0)
349 swap(rule_, other.rule_);
350 throw;
351 }
352 }
353
354public:
355
357 [[nodiscard]] const Lattice &frame() const noexcept
358 {
359 return cur_;
360 }
361
363 [[nodiscard]] std::size_t steps_run() const noexcept
364 {
365 return step_count_;
366 }
367
369 [[nodiscard]] const extents_type &extents() const noexcept
370 {
371 return cur_.extents();
372 }
373
375 [[nodiscard]] const Neighborhood &neighborhood() const noexcept
376 {
377 return nh_;
378 }
379
381 [[nodiscard]] Lattice &current_buffer() noexcept
382 {
383 return cur_;
384 }
385
387 [[nodiscard]] const Lattice &current_buffer() const noexcept
388 {
389 return cur_;
390 }
391
393 [[nodiscard]] Lattice &write_lattice() noexcept
394 {
395 return cur_;
396 }
397
404 void set_step_count(const std::size_t s) noexcept
405 {
406 step_count_ = s;
407 }
408
411 [[nodiscard]] Lattice &next_buffer() noexcept
412 {
413 return nxt_;
414 }
415
417 [[nodiscard]] Rule &rule() noexcept
418 {
419 return rule_;
420 }
421
423 [[nodiscard]] const Rule &rule() const noexcept
424 {
425 return rule_;
426 }
427
429 [[nodiscard]] const Update_Scheme &scheme() const noexcept
430 {
431 return scheme_;
432 }
433
435 [[nodiscard]] std::size_t step_count() const noexcept
436 {
437 return step_count_;
438 }
439
441 void swap_buffers() noexcept(noexcept(cur_.swap(nxt_)))
442 {
443 cur_.swap(nxt_);
444 }
445
450 template <typename F>
451 void on_pre_step(F &&f)
452 {
453 pre_hook_ = std::forward<F>(f);
454 }
455
460 template <typename F>
461 void on_post_step(F &&f)
462 {
463 post_hook_ = std::forward<F>(f);
464 }
465
475 void step()
476 {
477 if constexpr (HasRefreshHalo<Lattice>)
478 cur_.refresh_halo();
479
480 if (pre_hook_)
482
483 scheme_.apply(*this, rule_);
484 ++step_count_;
485
486 if (post_hook_)
488 }
489
504 requires ca_async_engine_detail::HasApplyWithOrigin<Update_Scheme, Async_Engine, Rule>
505 {
506 ah_out_of_range_error_if(step_count_ == 0) << "Async_Engine::step_back: no step to reverse";
507
508 --step_count_;
509 if constexpr (HasRefreshHalo<Lattice>)
510 cur_.refresh_halo();
511
512 if (pre_hook_)
514
515 scheme_.apply_with_origin(*this, rule_, Update_Scheme::origin_for_step(step_count_));
516
517 if (post_hook_)
519 }
520
525 void run(const std::size_t steps)
526 {
527 for (std::size_t i = 0; i < steps; ++i)
528 step();
529 }
530
535 void run_back(const std::size_t steps)
537 {
538 for (std::size_t i = 0; i < steps; ++i)
539 step_back();
540 }
541};
542
543} // namespace CA
544} // namespace Aleph
545
546#endif // TPL_CA_ASYNC_ENGINE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
size_t steps
Definition ca-c-api.h:126
Common typedefs and tag types for the Cellular Automata module.
Update-scheme aware engine.
const Lattice & frame() const noexcept
void swap(Async_Engine &other) noexcept(noexcept(cur_.swap(other.cur_)) and std::is_nothrow_swappable_v< Rule > and std::is_nothrow_swappable_v< Neighborhood > and std::is_nothrow_swappable_v< Update_Scheme >)
Exchange in O(1) the simulation of this engine and another.
Lattice & current_buffer() noexcept
void step_back()
Reverse the most recent step (Margolus only).
std::size_t step_count() const noexcept
Async_Engine & operator=(Async_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation of this one.
void swap_buffers() noexcept(noexcept(cur_.swap(nxt_)))
O(1) swap of the two buffers (called by double-buffer schemes).
const Neighborhood & neighborhood() const noexcept
const Update_Scheme & scheme() const noexcept
typename Lattice::extents_type extents_type
Async_Engine & operator=(const Async_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its neighborhood, its update scheme,...
void on_post_step(F &&f)
Register a hook fired after every step().
Async_Engine(Async_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 > and std::is_nothrow_move_constructible_v< Update_Scheme >)
Take the simulation of another engine, which is left with empty lattices, no hooks and no steps run.
Lattice & next_buffer() noexcept
void step()
Run one engine step using the configured update scheme.
void set_step_count(const std::size_t s) noexcept
Override the completed-step counter.
void on_pre_step(F &&f)
Register a hook fired before every step().
Async_Engine(Lattice initial, Rule r, Update_Scheme s={})
Build an engine with a default-constructed neighborhood.
void run_back(const std::size_t steps)
Reverse several steps (Margolus only).
void swap_all_or_none(Async_Engine &other)
typename Lattice::coord_type coord_type
std::size_t steps_run() const noexcept
std::function< void(std::size_t, const Lattice &)> hook_type
Hook signature: (step_index, frame).
const Lattice & current_buffer() const noexcept
typename Lattice::state_type state_type
void run(const std::size_t steps)
Run several steps.
Async_Engine(Lattice initial, Rule r, Neighborhood n, Update_Scheme s={})
Build an engine.
Lattice & write_lattice() noexcept
Alias for current_buffer(). Used by checkpoint restore.
const extents_type & extents() const noexcept
static constexpr std::size_t rank
Lattice dimension.
Async_Engine(const Async_Engine &other)=default
Copy another engine: its lattices, its rule, its neighborhood, its update scheme, its hooks and its c...
const Rule & rule() const noexcept
Lattice that adds boundary-aware access on top of a storage.
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
Placeholder neighbourhood for engines that drive block rules.
static constexpr std::size_t size_v
Empty neighbourhood: zero offsets.
static constexpr std::size_t radius() noexcept
Always returns 0 (no offsets).
static constexpr void for_each_offset(const coord_type &, F &&) noexcept
No-op: the neighbourhood is empty.
static constexpr std::size_t rank_v
Lattice dimension.
static constexpr std::size_t radius_v
Compile-time radius (zero, since the neighbourhood is empty).
static constexpr std::size_t size() noexcept
Always returns 0 (no offsets).
Coord_Vec< Rank > coord_type
Coordinate type accepted by for_each_offset.
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.
Detect whether the scheme exposes apply_with_origin(engine, rule, origin) (Margolus).
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Gsl_Compatible_Rng r
C++20 concepts for the Cellular Automata module.
Neighborhoods catalogue for Aleph::CA.
Phase 13 update-scheme strategies for Aleph::CA.