Initial RPS CUDA implementation

This commit is contained in:
Skeleton bootstrap 2026-11-26 09:07:21 +03:00
commit 8d583036d0
33 changed files with 2968 additions and 0 deletions

101
common/config.hpp Normal file
View file

@ -0,0 +1,101 @@
#pragma once
#include <charconv>
#include <fstream>
#include <limits>
#include <nlohmann/json.hpp>
#include <set>
#include <stdexcept>
using u32 = uint32_t;
using u64 = uint64_t;
using Json = nlohmann::json;
inline void require(bool ok, const char *why) {
if (!ok)
throw std::runtime_error(why);
}
struct Config {
u32 w, h, n, g, rate, x, y, rw, rh, k;
u64 seed;
u32 aw() const { return rw / k; }
u32 ah() const { return rh / k; }
size_t bytes() const { return size_t(aw()) * ah() * 4; }
};
inline std::string readConfig(const std::string &path) {
std::ifstream in(path, std::ios::binary | std::ios::ate);
auto n = in.tellg();
require(n > 0 && n <= 65536, "config file missing, empty or oversized");
in.seekg(0);
std::string bytes(size_t(n), '\0');
require(bool(in.read(bytes.data(), n)), "config read failed");
return bytes;
}
inline Config parse(const std::string &bytes) {
require(!bytes.empty() && bytes.size() <= 65536, "JSON length");
require(bytes.find(char(0)) == bytes.npos, "embedded NUL in config");
std::vector<std::set<std::string>> scopes;
auto root =
Json::parse(bytes, [&](int depth, Json::parse_event_t e, Json &v) {
require(depth <= 4, "unexpected JSON nesting");
using E = Json::parse_event_t;
if (e == E::object_start)
scopes.emplace_back();
if (e == E::key)
require(scopes.back().insert(v.get<std::string>()).second,
"duplicate key");
if (e == E::object_end)
scopes.pop_back();
return true;
});
auto keys = [](const Json &j, std::initializer_list<const char *> names) {
require(j.is_object() && j.size() == names.size(),
"unexpected object fields");
for (auto name : names)
require(j.contains(name), "missing field");
};
auto num = [](const Json &v) -> u32 {
require(v.is_number_unsigned() && v.get<u64>() <= UINT32_MAX,
"expected uint32");
return v.get<u32>();
};
keys(root, {"protocol_version", "simulation", "view", "snapshots"});
auto &s = root.at("simulation");
auto &v = root.at("view");
keys(s, {"width", "height", "neighbors", "seed", "generations",
"max_generations_per_second", "topology"});
keys(v, {"roi", "aggregation_side", "palette"});
auto &r = v.at("roi");
keys(r, {"x", "y", "width", "height"});
keys(root.at("snapshots"), {"mode"});
require(num(root.at("protocol_version")) == 1 &&
s.at("topology") == "torus" && v.at("palette") == "rps-v1" &&
root.at("snapshots").at("mode") == "auto",
"unsupported configuration");
auto seed = s.at("seed").get<std::string>();
Config c{num(s.at("width")),
num(s.at("height")),
num(s.at("neighbors")),
num(s.at("generations")),
num(s.at("max_generations_per_second")),
num(r.at("x")),
num(r.at("y")),
num(r.at("width")),
num(r.at("height")),
num(v.at("aggregation_side")),
0};
auto end = seed.data() + seed.size();
auto result = std::from_chars(seed.data(), end, c.seed);
require(result.ec == std::errc() && result.ptr == end &&
seed == std::to_string(c.seed),
"invalid seed");
require(c.w && c.h && c.w % 1024 == 0 && c.h % 48 == 0 &&
u64(c.w) * c.h / 32 <= UINT32_MAX,
"grid shape");
require((c.n == 4 || c.n == 8) && c.g >= 1 && c.g <= 1000000000 &&
c.rate >= 1 && c.rate <= 240,
"simulation bounds");
require(c.k && c.k <= 16 && !(c.k & (c.k - 1)), "aggregation side");
require(c.rw && c.rh && u64(c.x) + c.rw <= c.w && u64(c.y) + c.rh <= c.h &&
!(c.x % c.k || c.y % c.k || c.rw % c.k || c.rh % c.k),
"ROI");
require(u64(c.aw()) * c.ah() <= 8388608, "frame size");
return c;
}