76 constexpr std::uint64_t SEED = 0xBEEFCAFE123456ULL;
79 inline std::uint64_t
next(std::uint64_t &s)
84 return s * 0x2545F4914F6CDD1DULL;
90 std::uint64_t s = SEED;
93 lat.set({ i, j }, (
next(s) & 0xF) == 0 ? 1 : 0);
96 template <
typename A,
typename B>
97 bool frames_match(
const A &a,
const B &b)
101 if (
rows != b.size(0) or
cols != b.size(1))
105 if (a.at({ i, j }) != b.at({ i, j }))
111 template <
typename L>
112 std::size_t population(
const L &lat)
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);
124 double steps_per_sec;
125 std::size_t final_population;
128 template <
typename Engine>
129 Bench_Result run_bench(Engine &
engine, std::size_t
steps)
132 const auto t0 = std::chrono::steady_clock::now();
134 const auto t1 = std::chrono::steady_clock::now();
136 = std::chrono::duration<double, std::milli>(t1 - t0).count();
137 return { ms, 1000.0 *
steps / ms, population(
engine.frame()) };
140 bool parse_positive_size(
const char *text,
142 unsigned long long &
out)
144 if (text ==
nullptr or *text ==
'\0' or *text ==
'-')
146 std::cerr <<
"ERROR: " << name <<
" must be a positive integer.\n";
152 const unsigned long long value = std::strtoull(text, &end, 10);
153 if (errno != 0 or end == text or *end !=
'\0' or
value == 0)
155 std::cerr <<
"ERROR: " << name <<
" must be a positive integer; got '"
164int main(
int argc,
char *argv[])
167 std::size_t
steps = 50;
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()))
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";
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()))
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";
190 steps =
static_cast<std::size_t
>(parsed);
202 std::cout <<
"GoL " <<
size <<
"x" <<
size <<
" for " <<
steps
203 <<
" steps (toroidal).\n";
206 ClassicL classic_init({
size,
size }, 0);
207 GhostL ghost_init({
size,
size }, 0);
208 seed_lattice(classic_init,
size,
size);
209 seed_lattice(ghost_init,
size,
size);
211 ClassicEngine classic_engine(std::move(classic_init),
214 GhostEngine ghost_engine(std::move(ghost_init),
218 std::cout << std::fixed << std::setprecision(2);
219 const auto classic_res = run_bench(classic_engine,
steps);
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';
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';
232 = classic_res.wall_ms == 0.0 ? 0.0 : classic_res.wall_ms / ghost_res.wall_ms;
233 std::cout <<
"Speedup : " << speedup <<
"x\n";
235 if (not frames_match(classic_engine.frame(), ghost_engine.frame()))
237 std::cerr <<
"ERROR: final frames diverge — halo refresh is broken.\n";
240 std::cout <<
"Final frames bit-identical: OK\n";
size_t size_t int32_t value
size_t size_t int32_t * out
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.
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.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
void next()
Advance all underlying iterators (bounds-checked).
The lattice wraps around on every axis.
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).