Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_ghost_bench_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
52#include <array>
53#include <cassert>
54#include <chrono>
55#include <cerrno>
56#include <cstdint>
57#include <cstdlib>
58#include <iomanip>
59#include <iostream>
60#include <limits>
61#include <utility>
62
63#include <ca-traits.H>
64#include <tpl_ca_storage.H>
65#include <tpl_ca_lattice.H>
67#include <tpl_ca_neighborhood.H>
68#include <tpl_ca_rule.H>
69#include <tpl_ca_engine.H>
70
71using namespace Aleph;
72using namespace Aleph::CA;
73
74namespace
75{
76 constexpr std::uint64_t SEED = 0xBEEFCAFE123456ULL;
77
79 inline std::uint64_t next(std::uint64_t &s)
80 {
81 s ^= s >> 12;
82 s ^= s << 25;
83 s ^= s >> 27;
84 return s * 0x2545F4914F6CDD1DULL;
85 }
86
87 template <typename L>
89 {
90 std::uint64_t s = SEED;
93 lat.set({ i, j }, (next(s) & 0xF) == 0 ? 1 : 0); // ~6% alive
94 }
95
96 template <typename A, typename B>
97 bool frames_match(const A &a, const B &b)
98 {
99 const ca_size_t rows = a.size(0);
100 const ca_size_t cols = a.size(1);
101 if (rows != b.size(0) or cols != b.size(1))
102 return false;
105 if (a.at({ i, j }) != b.at({ i, j }))
106 return false;
107 return true;
108 }
109
111 template <typename L>
112 std::size_t population(const L &lat)
113 {
114 std::size_t pop = 0;
115 for (ca_index_t i = 0; i < static_cast<ca_index_t>(lat.size(0)); ++i)
116 for (ca_index_t j = 0; j < static_cast<ca_index_t>(lat.size(1)); ++j)
117 pop += static_cast<std::size_t>(lat.at({ i, j }) != 0);
118 return pop;
119 }
120
121 struct Bench_Result
122 {
123 double wall_ms;
124 double steps_per_sec;
125 std::size_t final_population;
126 };
127
128 template <typename Engine>
129 Bench_Result run_bench(Engine &engine, std::size_t steps)
130 {
131 assert(steps > 0);
132 const auto t0 = std::chrono::steady_clock::now();
133 engine.run(steps);
134 const auto t1 = std::chrono::steady_clock::now();
135 const double ms
136 = std::chrono::duration<double, std::milli>(t1 - t0).count();
137 return { ms, 1000.0 * steps / ms, population(engine.frame()) };
138 }
139
140 bool parse_positive_size(const char *text,
141 const char *name,
142 unsigned long long &out)
143 {
144 if (text == nullptr or *text == '\0' or *text == '-')
145 {
146 std::cerr << "ERROR: " << name << " must be a positive integer.\n";
147 return false;
148 }
149
150 errno = 0;
151 char *end = nullptr;
152 const unsigned long long value = std::strtoull(text, &end, 10);
153 if (errno != 0 or end == text or *end != '\0' or value == 0)
154 {
155 std::cerr << "ERROR: " << name << " must be a positive integer; got '"
156 << text << "'.\n";
157 return false;
158 }
159 out = value;
160 return true;
161 }
162}
163
164int main(int argc, char *argv[])
165{
166 ca_size_t size = 1024;
167 std::size_t steps = 50;
168 if (argc > 1)
169 {
170 unsigned long long parsed = 0;
171 if (not parse_positive_size(argv[1], "size", parsed)
172 or parsed > static_cast<unsigned long long>(std::numeric_limits<ca_size_t>::max()))
173 {
174 if (parsed > static_cast<unsigned long long>(std::numeric_limits<ca_size_t>::max()))
175 std::cerr << "ERROR: size is too large for ca_size_t.\n";
176 return 1;
177 }
178 size = static_cast<ca_size_t>(parsed);
179 }
180 if (argc > 2)
181 {
182 unsigned long long parsed = 0;
183 if (not parse_positive_size(argv[2], "steps", parsed)
184 or parsed > static_cast<unsigned long long>(std::numeric_limits<std::size_t>::max()))
185 {
186 if (parsed > static_cast<unsigned long long>(std::numeric_limits<std::size_t>::max()))
187 std::cerr << "ERROR: steps is too large for std::size_t.\n";
188 return 1;
189 }
190 steps = static_cast<std::size_t>(parsed);
191 }
192
194 using Boundary = ToroidalBoundary;
197 using ClassicEngine
199 using GhostEngine
201
202 std::cout << "GoL " << size << "x" << size << " for " << steps
203 << " steps (toroidal).\n";
204
205 // Seed both lattices identically so the final frames must match.
207 GhostL ghost_init({ size, size }, 0);
210
213 Moore<2, 1>{});
216 Moore<2, 1>{});
217
218 std::cout << std::fixed << std::setprecision(2);
220 std::cout << "Phase 3 Lattice : "
221 << std::setw(9) << classic_res.wall_ms << " ms ("
222 << classic_res.steps_per_sec << " steps/s) "
223 << "population=" << classic_res.final_population << '\n';
224
225 const auto ghost_res = run_bench(ghost_engine, steps);
226 std::cout << "Phase 4 Ghost_Lattice : "
227 << std::setw(9) << ghost_res.wall_ms << " ms ("
228 << ghost_res.steps_per_sec << " steps/s) "
229 << "population=" << ghost_res.final_population << '\n';
230
231 const double speedup
232 = classic_res.wall_ms == 0.0 ? 0.0 : classic_res.wall_ms / ghost_res.wall_ms;
233 std::cout << "Speedup : " << speedup << "x\n";
234
235 if (not frames_match(classic_engine.frame(), ghost_engine.frame()))
236 {
237 std::cerr << "ERROR: final frames diverge — halo refresh is broken.\n";
238 return 1;
239 }
240 std::cout << "Final frames bit-identical: OK\n";
241 return 0;
242}
int main()
size_t steps
Definition ca-c-api.h:126
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t * rows
Definition ca-c-api.h:112
size_t cols
Definition ca-c-api.h:105
Common typedefs and tag types for the Cellular Automata module.
Row-major dense storage for N-dimensional cellular automata.
Lattice with Halo ghost layers around the user-visible cells.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
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
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
size_t size(Node *root) noexcept
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:171
The lattice wraps around on every axis.
Definition ca-traits.H:124
static mt19937 engine
Synchronous double-buffered engine for cellular automata.
Ghost-layer lattice for high-performance boundary handling.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).