Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
game_of_life_bench.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
18#include <chrono>
19#include <cstddef>
20#include <cstdint>
21#include <filesystem>
22#include <fstream>
23#include <iomanip>
24#include <iostream>
25
27#include <tpl_ca_hashlife.H>
28#include <tpl_ca_rule.H>
29
30using namespace Aleph::CA;
31using namespace Aleph::CA::Reproductions;
32
33#ifndef ALEPH_REPRODUCTIONS_SOURCE_DIR
34# define ALEPH_REPRODUCTIONS_SOURCE_DIR "reproductions"
35#endif
36
37namespace
38{
39
41
47template <typename F>
48double seconds_for(F &&operation)
49{
50 const auto start = std::chrono::steady_clock::now();
51 operation();
52 return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
53}
54
59std::size_t population(const Grid &frame)
60{
61 std::size_t count = 0;
62 for (ca_size_t row = 0; row < frame.size(0); ++row)
63 for (ca_size_t column = 0; column < frame.size(1); ++column)
64 if (frame.at({static_cast<ca_index_t>(row), static_cast<ca_index_t>(column)}) != 0)
65 ++count;
66 return count;
67}
68
69} // namespace
70
71int main()
72{
73 constexpr ca_size_t side = 384;
74 constexpr std::size_t generations = 1000;
75 constexpr std::int64_t offset = 32;
76
77 Grid seed({side, side}, 0);
78 for_each_gosper_gun_cell([&](const std::int64_t x, const std::int64_t y)
79 {
80 seed.set({offset + y, offset + x}, 1);
81 });
84 const double dense_seconds = seconds_for([&] { dense.run(generations); });
85
87 for_each_gosper_gun_cell([&](const std::int64_t x, const std::int64_t y)
88 {
89 hashlife.set_alive(x, y);
90 });
91 std::uint64_t actual_generations = 0;
92 const double hashlife_seconds
94
95 const std::filesystem::path root = ALEPH_REPRODUCTIONS_SOURCE_DIR;
96 std::filesystem::create_directories(root / "results");
97 std::ofstream summary(root / "results" / "game_of_life_bench.csv");
98 if (not summary)
99 {
100 std::cerr << "Cannot write Game of Life benchmark\n";
101 return 1;
102 }
103 summary << "engine,requested_generations,actual_generations,population,seconds\n"
104 << "dense_step," << generations << ',' << generations << ','
105 << population(dense.frame()) << ',' << std::setprecision(12) << dense_seconds << '\n'
106 << "hashlife," << generations << ',' << actual_generations << ','
107 << hashlife.population() << ',' << hashlife_seconds << '\n';
108
109 std::cout << "Game of Life Gosper gun benchmark (informational only)\n"
110 << " dense step: " << std::fixed << std::setprecision(6)
111 << dense_seconds << " s\n"
112 << " Hashlife: " << hashlife_seconds << " s"
113 << " (actual generations " << actual_generations << ")\n";
115 std::cout << " population match: "
116 << (population(dense.frame()) == hashlife.population() ? "yes" : "no")
117 << '\n';
118 return 0;
119}
size_t row
Definition ca-c-api.h:115
Internal helpers shared by the cellular-automata reproductions.
Hashlife engine for outer-totalistic binary cellular automata.
Lattice that adds boundary-aware access on top of a storage.
Synchronous double-buffered engine.
#define ALEPH_REPRODUCTIONS_SOURCE_DIR
int main()
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4071
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
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
static mpfr_t y
Definition mpfr_mul_d.c:3
void for_each_gosper_gun_cell(F &&visitor, const std::int64_t offset_x=0, const std::int64_t offset_y=0)
Visit every live coordinate in the canonical Gosper gun.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Out-of-range neighbours behave as if the lattice ended.
Definition ca-traits.H:119
ValueArg< size_t > seed
Definition testHash.C:53
Hashlife engine for outer-totalistic binary cellular automata.
Rule mechanisms for Aleph::CA.