Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_parallel_wolfram_example.cc
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
42#include <array>
43#include <cstdint>
44#include <cstdio>
45
46#include <ca-bench.H>
47#include <ca-traits.H>
48#include <tpl_ca_engine.H>
49#include <tpl_ca_lattice.H>
50#include <tpl_ca_neighborhood.H>
52#include <tpl_ca_rule.H>
53#include <tpl_ca_storage.H>
54
55using namespace Aleph;
56using namespace Aleph::CA;
57
59
60namespace {
61
62void print_row(const Lat_t &lat)
63{
64 for (std::size_t i = 0; i < lat.size(0); ++i)
65 std::putchar(lat.at({static_cast<ca_index_t>(i)}) != 0 ? '#' : '.');
66 std::putchar('\n');
67}
68
69bool rows_equal(const Lat_t &a, const Lat_t &b)
70{
71 if (a.extents() != b.extents())
72 return false;
73 for (std::size_t i = 0; i < a.size(0); ++i)
74 if (a.at({static_cast<ca_index_t>(i)}) != b.at({static_cast<ca_index_t>(i)}))
75 return false;
76 return true;
77}
78
79void render(std::uint8_t rule_no, std::size_t width, std::size_t generations)
80{
82 std::array<Offset_Vec<1>, 2>{Offset_Vec<1>{-1}, Offset_Vec<1>{1}});
83 Lat_t seed({width}, 0);
84 seed.set({static_cast<ca_index_t>(width / 2)}, 1);
85
88 cfg.min_parallel_cells = 0;
91
92 std::printf("\n--- Wolfram rule %u, %zu cells, %zu generations ---\n",
93 static_cast<unsigned>(rule_no), width, generations);
94 print_row(par.frame());
95 for (std::size_t gen = 1; gen < generations; ++gen)
96 {
97 par.step();
98 print_row(par.frame());
99 }
100}
101
102void bench(std::uint8_t rule_no, std::size_t width, std::size_t steps)
103{
105 std::array<Offset_Vec<1>, 2>{Offset_Vec<1>{-1}, Offset_Vec<1>{1}});
106 Lat_t seed({width}, 0);
107 seed.set({static_cast<ca_index_t>(width / 2)}, 1);
108
111
114 cfg.min_parallel_cells = 0;
117
118 const double t_seq = bench_seconds([&] { seq.run(steps); });
119 const double t_par = bench_seconds([&] { par.run(steps); });
120
121 std::printf("\nrule=%-3u N=%-7zu steps=%-6zu sequential=%.4fs parallel=%.4fs "
122 "speedup=%.2fx (parallel=%s)\n",
123 static_cast<unsigned>(rule_no), width, steps, t_seq, t_par,
124 t_seq / t_par,
125 format_throughput(static_cast<double>(width) * steps / t_par));
126 if (not rows_equal(seq.frame(), par.frame()))
127 {
128 std::printf("*** divergence between sequential and parallel rule %u ***\n",
129 static_cast<unsigned>(rule_no));
130 std::exit(1);
131 }
132}
133
134} // namespace
135
136int main()
137{
138 std::printf("Aleph::CA Phase-5 example: parallel 1D Wolfram CAs\n");
139
140 // Visual ASCII renders so the reader can verify the canonical
141 // patterns (rule 30 noise, rule 110 sliding triangles).
142 render(30, 79, 32);
143 render(110, 79, 32);
144
145 // Microbench on a wide lattice. Numbers vary by hardware; the
146 // example mostly exercises the scheduler and confirms equivalence.
147 bench(30, 1u << 17, 200);
148 bench(110, 1u << 17, 200);
149
150 std::printf("\nParallel Wolfram rules 30 and 110 matched the sequential reference.\n");
151 return 0;
152}
Tiny chrono-based timer used by the CA module Examples.
size_t steps
Definition ca-c-api.h:126
Common typedefs and tag types for the Cellular Automata module.
User-supplied list of offsets for arbitrary connectivity.
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.
const extents_type & extents() const noexcept
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.
void run(const std::size_t steps)
Run several synchronous steps.
const Lattice & frame() const noexcept
Return the current frame.
void step()
Apply the rule to every cell once and swap buffers.
Synchronous double-buffered engine.
void run(const std::size_t steps)
Run several synchronous steps.
const Lattice & frame() const noexcept
Return the current frame.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
const char * format_throughput(double cells_per_second)
Format a "cells per second" rate as "X.XX M cells/s".
Definition ca-bench.H:126
Coord_Vec< N > Offset_Vec
Default offset vector (aliases Coord_Vec).
Definition ca-traits.H:79
double bench_seconds(F &&f)
Run f() once and return the wall-clock time it took, in seconds.
Definition ca-bench.H:111
constexpr Lookup_Rule< 2, 2 > make_wolfram_elementary_rule(std::uint8_t rule_no) noexcept
Build the elementary 1D Wolfram rule rule_no (0..255) as a Lookup_Rule<2, 2> over neighbourhood {-1,...
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
Configuration for Parallel_Synchronous_Engine.
std::size_t num_partitions
Number of partitions per step.
ValueArg< size_t > seed
Definition testHash.C:53
Synchronous double-buffered engine for cellular automata.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Parallel synchronous engine for cellular automata (Phase 5).
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).