Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_ca_multi_field_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
58#ifndef TPL_CA_MULTI_FIELD_ENGINE_H
59#define TPL_CA_MULTI_FIELD_ENGINE_H
60
61#include <array>
62#include <cstddef>
63#include <functional>
64#include <span>
65#include <tuple>
66#include <type_traits>
67#include <utility>
68
69#include <ca-traits.H>
70#include <tpl_ca_concepts.H>
73#include <tpl_ca_neighborhood.H>
74
75namespace Aleph {
76namespace CA {
77
78namespace ca_mf_engine_detail {
79
81template <typename Lattice, typename Neighborhood, typename Buffers, std::size_t... I>
82inline auto build_views(const Lattice &lat,
83 const Neighborhood &nh,
84 const typename Lattice::coord_type &center,
85 Buffers &bufs,
86 std::index_sequence<I...>)
87{
88 using Coord = typename Lattice::coord_type;
89 std::size_t neigh_sz = nh.size();
90 if (neigh_sz > std::tuple_size_v<std::tuple_element_t<0, Buffers>>)
91 neigh_sz = std::tuple_size_v<std::tuple_element_t<0, Buffers>>;
92
93 std::size_t cursor = 0;
94 nh.for_each_offset(center, [&](const auto &off)
95 {
96 Coord c = center;
97 for (std::size_t d = 0; d < Lattice::rank; ++d)
98 c[d] = static_cast<ca_index_t>(c[d] + off[d]);
99 if (cursor < neigh_sz)
100 ((std::get<I>(bufs)[cursor] = lat.template at_safe<I>(c)), ...);
101 ++cursor;
102 });
103
104 return std::tuple{
105 Neighbor_View<typename Lattice::template field_type<I>>(std::get<I>(bufs).data(), neigh_sz)...};
106}
107
108} // namespace ca_mf_engine_detail
109
121template <typename Lattice, typename Rule, typename Neighborhood>
123{
124 static_assert(MultiFieldLatticeLike<Lattice>,
125 "Multi_Field_Engine requires a MultiFieldLatticeLike lattice");
126 static_assert(NeighborhoodLike<Neighborhood>,
127 "Multi_Field_Engine requires a NeighborhoodLike neighbourhood");
128 static_assert(Neighborhood::rank_v == Lattice::rank, "Neighbourhood rank must match lattice rank");
129
130public:
132 using rule_type = Rule;
133 using neighborhood_type = Neighborhood;
136 using fields_tuple = typename Lattice::field_types;
137
138 static constexpr std::size_t rank = Lattice::rank;
139 static constexpr std::size_t field_count = Lattice::field_count;
140 static constexpr std::size_t neighbour_count = Neighborhood::size_v;
141
143 using hook_type = std::function<void(std::size_t, const Lattice &)>;
144
145private:
148 Rule rule_;
149 Neighborhood nh_;
152 std::size_t step_count_ = 0;
153
154 template <std::size_t... I>
155 static auto make_buffers(std::index_sequence<I...>)
156 {
157 return std::tuple{std::array<typename Lattice::template field_type<I>,
158 (neighbour_count == 0 ? std::size_t{1} : neighbour_count)>{}...};
159 }
160
161 template <std::size_t... I>
162 void write_cell(const coord_type &c, const fields_tuple &v, std::index_sequence<I...>)
163 {
164 ((nxt_.template set<I>(c, std::get<I>(v))), ...);
165 }
166
167public:
178 Multi_Field_Engine(Lattice initial, Rule r, Neighborhood n)
179 : cur_(std::move(initial)), nxt_(cur_.extents()), rule_(std::move(r)), nh_(std::move(n))
180 {}
181
184 requires std::default_initializable<Neighborhood>
185 : Multi_Field_Engine(std::move(initial), std::move(r), Neighborhood{})
186 {}
187
192 Multi_Field_Engine(const Multi_Field_Engine &other) = default;
193
204 std::is_nothrow_move_constructible_v<Lattice> and std::is_nothrow_move_constructible_v<Rule>
205 and std::is_nothrow_move_constructible_v<Neighborhood>)
206 : cur_(std::move(other.cur_)), nxt_(std::move(other.nxt_)), rule_(std::move(other.rule_)),
207 nh_(std::move(other.nh_)), pre_hook_(std::exchange(other.pre_hook_, nullptr)),
208 post_hook_(std::exchange(other.post_hook_, nullptr)),
209 step_count_(std::exchange(other.step_count_, 0))
210 {}
211
218
224 Multi_Field_Engine &operator=(Multi_Field_Engine &&other) noexcept(noexcept(swap(other)))
225 requires (std::swappable<Rule> and std::swappable<Neighborhood>)
226 {
227 swap(other);
228 return *this;
229 }
230
243 void swap(Multi_Field_Engine &other) noexcept(
244 noexcept(cur_.swap(other.cur_)) and std::is_nothrow_swappable_v<Rule>
245 and std::is_nothrow_swappable_v<Neighborhood>)
246 requires (std::swappable<Rule> and std::swappable<Neighborhood>)
247 {
248 // When a swap may throw, that of a rule, a neighborhood or a lattice,
249 // the rules and the neighborhoods go first, and all go in a sequence
250 // that is all or none: one that throws leaves no lattice with the rule
251 // of the other. When none may, the order cannot be seen, and the rules
252 // and the neighborhoods go after the lattices, as they always did: a
253 // rule with members, even Game_Of_Life_Rule, would reorder the code if
254 // it went first
255 constexpr bool may_throw =
256 not std::is_nothrow_swappable_v<Rule> or not std::is_nothrow_swappable_v<Neighborhood>
257 or not noexcept(cur_.swap(other.cur_));
258 using std::swap;
259 if constexpr (may_throw)
260 swap_all_or_none(other);
261 else
262 {
263 cur_.swap(other.cur_);
264 nxt_.swap(other.nxt_);
265 swap(rule_, other.rule_);
266 swap(nh_, other.nh_);
267 }
268 pre_hook_.swap(other.pre_hook_);
269 post_hook_.swap(other.post_hook_);
270 swap(step_count_, other.step_count_);
271 }
272
273private:
274 // The rules, the neighborhoods and the lattices, with the swaps that the
275 // specification of swap() evaluates (found by their arguments, else
276 // std::swap()), all or none: if one throws, those before it are
277 // exchanged back
279 {
280 using std::swap;
281 int done = 0;
282 try
283 {
284 swap(rule_, other.rule_);
285 ++done;
286 swap(nh_, other.nh_);
287 ++done;
288 cur_.swap(other.cur_);
289 ++done;
290 nxt_.swap(other.nxt_);
291 }
292 catch (...)
293 {
294 if (done > 2)
295 cur_.swap(other.cur_);
296 if (done > 1)
297 swap(nh_, other.nh_);
298 if (done > 0)
299 swap(rule_, other.rule_);
300 throw;
301 }
302 }
303
304public:
305
307 [[nodiscard]] const Lattice &frame() const noexcept
308 {
309 return cur_;
310 }
311
313 [[nodiscard]] std::size_t steps_run() const noexcept
314 {
315 return step_count_;
316 }
317
319 [[nodiscard]] const extents_type &extents() const noexcept
320 {
321 return cur_.extents();
322 }
323
325 template <typename F>
326 void on_pre_step(F &&f)
327 {
328 pre_hook_ = std::forward<F>(f);
329 }
330
332 template <typename F>
333 void on_post_step(F &&f)
334 {
335 post_hook_ = std::forward<F>(f);
336 }
337
350 void step()
351 {
352 if (pre_hook_)
354
355 auto bufs = make_buffers(std::make_index_sequence<field_count>{});
356
357 auto compute_one = [&](const coord_type &c)
358 {
359 auto views = ca_mf_engine_detail::build_views(cur_, nh_, c, bufs,
360 std::make_index_sequence<field_count>{});
361 const fields_tuple cur_vals = cur_.at_cell(c);
362 const Cell_Context<rank> ctx{step_count_, c};
363 const fields_tuple new_vals
364 = apply_multi_field_rule<Rule, fields_tuple, rank>(rule_, cur_vals, views, ctx);
365 write_cell(c, new_vals, std::make_index_sequence<field_count>{});
366 };
367
368 iterate_lattice(compute_one);
369
370 cur_.swap(nxt_);
371 ++step_count_;
372
373 if (post_hook_)
375 }
376
381 void run(const std::size_t steps)
382 {
383 for (std::size_t i = 0; i < steps; ++i)
384 step();
385 }
386
387private:
388 template <typename F>
390 {
391 coord_type c{};
392 if constexpr (rank == 1)
393 {
394 const auto n0 = static_cast<ca_index_t>(cur_.size(0));
395 for (ca_index_t i = 0; i < n0; ++i)
396 {
397 c[0] = i;
398 f(c);
399 }
400 }
401 else if constexpr (rank == 2)
402 {
403 const auto n0 = static_cast<ca_index_t>(cur_.size(0));
404 const auto n1 = static_cast<ca_index_t>(cur_.size(1));
405 for (ca_index_t i = 0; i < n0; ++i)
406 for (ca_index_t j = 0; j < n1; ++j)
407 {
408 c[0] = i;
409 c[1] = j;
410 f(c);
411 }
412 }
413 else if constexpr (rank == 3)
414 {
415 const auto n0 = static_cast<ca_index_t>(cur_.size(0));
416 const auto n1 = static_cast<ca_index_t>(cur_.size(1));
417 const auto n2 = static_cast<ca_index_t>(cur_.size(2));
418 for (ca_index_t i = 0; i < n0; ++i)
419 for (ca_index_t j = 0; j < n1; ++j)
420 for (ca_index_t k = 0; k < n2; ++k)
421 {
422 c[0] = i;
423 c[1] = j;
424 c[2] = k;
425 f(c);
426 }
427 }
428 else
429 static_assert(rank <= 3, "Multi_Field_Engine currently supports rank <= 3");
430 }
431};
432
433} // namespace CA
434} // namespace Aleph
435
436#endif // TPL_CA_MULTI_FIELD_ENGINE_H
size_t steps
Definition ca-c-api.h:126
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
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
Synchronous double-buffered engine for multi-field lattices.
void run(const std::size_t steps)
Run several steps.
Multi_Field_Engine(const Multi_Field_Engine &other)=default
Copy another engine: its lattices, its rule, its neighborhood, its hooks and its count of steps.
typename Lattice::coord_type coord_type
Multi_Field_Engine(Lattice initial, Rule r)
Build with a default-constructed neighbourhood.
Multi_Field_Engine(Lattice initial, Rule r, Neighborhood n)
Build a multi-field engine.
void step()
Apply the multi-field rule to every cell once and swap buffers.
void on_pre_step(F &&f)
Register a hook fired before every step().
static constexpr std::size_t rank
void write_cell(const coord_type &c, const fields_tuple &v, std::index_sequence< I... >)
Multi_Field_Engine(Multi_Field_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 and no steps run.
static constexpr std::size_t neighbour_count
Multi_Field_Engine & operator=(Multi_Field_Engine &&other) noexcept(noexcept(swap(other)))
Exchange this engine with another, which is left with the previous simulation of this one.
const extents_type & extents() const noexcept
static constexpr std::size_t field_count
void on_post_step(F &&f)
Register a hook fired after every step().
typename Lattice::extents_type extents_type
Multi_Field_Engine & operator=(const Multi_Field_Engine &other)=default
Copy another engine into this one: its lattices, its rule, its neighborhood, its hooks and its count ...
const Lattice & frame() const noexcept
static auto make_buffers(std::index_sequence< I... >)
std::size_t steps_run() const noexcept
void swap_all_or_none(Multi_Field_Engine &other)
std::function< void(std::size_t, const Lattice &)> hook_type
Hook signature: (step_index_just_completed, frame).
typename Lattice::field_types fields_tuple
void swap(Multi_Field_Engine &other) noexcept(noexcept(cur_.swap(other.cur_)) and std::is_nothrow_swappable_v< Rule > and std::is_nothrow_swappable_v< Neighborhood >)
Exchange in O(1) the simulation of this engine and another.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
Definition ah-mdspan.H:303
A coordinate type with N integral components.
Concept: a lattice that exposes the multi-field interface.
Connectivity pattern around a coordinate.
auto build_views(const Lattice &lat, const Neighborhood &nh, const typename Lattice::coord_type &center, Buffers &bufs, std::index_sequence< I... >)
Recurse over the Field_Indices and gather one Neighbor_View per field.
@ I
Infected (and infectious).
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Definition ca-traits.H:90
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Definition ca-traits.H:106
static int * k
Gsl_Compatible_Rng r
C++20 concepts for the Cellular Automata module.
Phase 14 multi-field cellular automata lattice (AoS / SoA).
Phase 14 multi-field local rules for Aleph::CA.
Neighborhoods catalogue for Aleph::CA.