42#include <system_error>
64bool parse_u64(
const char *text, std::uint64_t &
out)
noexcept
68 std::string_view sv{text};
69 if (sv.empty() or sv.front() ==
'+' or sv.front() ==
'-')
72 const auto [ptr, ec] = std::from_chars(sv.data(), sv.data() + sv.size(), v, 10);
73 if (ec != std::errc{} or ptr != sv.data() + sv.size())
81 Grid g({side, side}, 0);
82 std::mt19937
rng(0xC0FFEE);
83 std::uniform_real_distribution<double> u(0.0, 1.0);
86 g.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
87 u(
rng) < 0.35 ? 1 : 0);
91std::size_t alive_count(
const Grid &g)
96 if (g.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}) != 0)
101bool grids_equal(
const Grid &a,
const Grid &b)
103 for (
ca_size_t i = 0; i < a.size(0); ++i)
104 for (
ca_size_t j = 0; j < a.size(1); ++j)
105 if (a.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
106 != b.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
115 std::size_t
steps = 200;
117 std::size_t every = 50;
123 auto assign_arg = [](
const char *arg,
const char *name,
auto &dst) ->
bool
125 using Target = std::remove_reference_t<
decltype(dst)>;
127 if (not parse_u64(arg, v))
129 std::cerr <<
"Invalid [" << name <<
"] (got '" << arg <<
"')\n";
132 if (v >
static_cast<std::uint64_t
>(std::numeric_limits<Target>::max()))
134 std::cerr <<
"Invalid [" << name <<
"] out of range\n";
137 dst =
static_cast<Target
>(v);
141 if (argc >= 2
and not assign_arg(argv[1],
"steps",
steps))
143 if (argc >= 3
and not assign_arg(argv[2],
"side", side))
145 if (argc >= 4
and not assign_arg(argv[3],
"every", every))
147 if (side < 4 or every == 0)
149 std::cerr <<
"Invalid arguments: side >= 4, every >= 1\n";
154 const auto tick = std::chrono::steady_clock::now().time_since_epoch().count();
155 const auto dir = std::filesystem::temp_directory_path()
156 / (
"aleph_ca_longrun_" + std::to_string(
static_cast<long long>(tick)));
157 std::filesystem::create_directories(dir);
159 std::cout <<
"Long-run example with periodic checkpoints\n";
160 std::cout <<
" steps=" <<
steps <<
" side=" << side
161 <<
" every=" << every <<
" steps\n";
162 std::cout <<
" checkpoint dir: " << dir.string() <<
"\n\n";
164 Grid initial = seed_grid(side);
165 std::cout <<
"Initial alive cells: " << alive_count(initial) <<
"\n\n";
169 reference.run(
steps);
170 std::cout <<
"Reference run: " << reference.steps_run()
171 <<
" steps completed, alive=" << alive_count(reference.frame()) <<
"\n";
176 (dir /
"snap_{step}.bin").
string(),
178 working.on_post_step([&](std::size_t s,
const Grid &f) { obs.on_step_end(s, f); });
184 working.run(partial);
185 std::cout <<
"Working run: " << working.steps_run() <<
" steps then 'crash'\n";
189 if (obs.last_path().empty())
195 std::cout <<
"No checkpoint produced (partial < every); restarting fresh.\n";
200 std::cout <<
"Most recent checkpoint: " << obs.last_path().string() <<
"\n\n";
203 <<
" (file=" << token.
source_path.string() <<
")\n";
207 const std::size_t remaining =
steps - resumed.steps_run();
208 resumed.run(remaining);
209 std::cout <<
"Resumed run: " << resumed.steps_run()
210 <<
" steps completed, alive=" << alive_count(resumed.frame()) <<
"\n";
213 if (not grids_equal(reference.frame(), resumed.frame()))
215 std::cerr <<
"\nFAIL: resumed frame differs from reference\n";
218 std::cout <<
"\nPASS — resumed frame is bit-by-bit identical to the "
219 "uninterrupted reference run.\n";
222 std::filesystem::remove_all(dir);
size_t size_t int32_t * out
Binary checkpoint format for Aleph::CA engines (Phase 15 + Phase 17 crash-safe / compress / async wri...
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Observer that auto-saves the engine state every every steps to a templated path.
Synchronous double-buffered engine.
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.
Resume_Token load_checkpoint_into(Engine &engine, const std::filesystem::path &path)
Restore an engine's state in-place from a checkpoint file.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Resume handle returned by load_checkpoint_into.
std::filesystem::path source_path
Path of the checkpoint file consumed by load.
Checkpoint_Header header
Validated header read from source_path.
The lattice wraps around on every axis.
Synchronous double-buffered engine for cellular automata.
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).