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

182
tests/cuda_check.cpp Normal file
View file

@ -0,0 +1,182 @@
#include "gpu.hpp"
#include "wire.hpp"
#include <array>
#include <iostream>
using Cells = std::vector<unsigned char>;
static u32 mix(u32 x) {
x ^= x >> 16;
x *= 0x7feb352d;
x ^= x >> 15;
x *= 0x846ca68b;
return x ^ (x >> 16);
}
static Cells initial(const Config &c) {
Cells cells(size_t(c.w) * c.h);
u32 l = c.seed, h = c.seed >> 32;
for (u32 i = 0; i < cells.size() / 32; ++i) {
u32 a = mix(l ^ i ^ (h * 0x9e3779b9u) ^ 0xa341316cu),
b = mix(h ^ i ^ (l * 0x85ebca6bu) ^ 0xc8013ea4u);
for (int j = 0; j < 32; ++j)
cells[size_t(i) * 32 + j] = ((a >> j) & 1) + 2 * ((b >> j) & 1);
}
return cells;
}
// One byte per cell, direct species comparisons. Split off the two wraparound
// lanes so the compiler can vectorize the interior without modulus operations.
template <int N> static Cells step(const Cells &src, const Config &c, int p) {
static constexpr unsigned order[][3] = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3},
{2, 3, 1}, {3, 1, 2}, {3, 2, 1}};
Cells dst(src.size());
for (u32 y = 0; y < c.h; ++y) {
auto row = src.data() + size_t(y) * c.w;
auto up = src.data() + size_t(y == 0 ? c.h - 1 : y - 1) * c.w;
auto down = src.data() + size_t(y + 1 == c.h ? 0 : y + 1) * c.w;
auto out = dst.data() + size_t(y) * c.w;
auto update = [&](u32 x, u32 left, u32 right) {
auto present = [&](unsigned species) {
bool found = (row[left] == species) | (row[right] == species) |
(up[x] == species) | (down[x] == species);
if constexpr (N == 8)
found |= (up[left] == species) | (up[right] == species) |
(down[left] == species) | (down[right] == species);
return found;
};
bool rock = present(1), paper = present(2), scissors = present(3);
auto claim = [&](unsigned s) {
return s == 1 ? rock : s == 2 ? paper : scissors;
};
unsigned empty = claim(order[p][0]) ? order[p][0]
: claim(order[p][1]) ? order[p][1]
: claim(order[p][2]) ? order[p][2]
: 0;
out[x] = row[x] == 0 ? empty
: row[x] == 1 ? 1 + paper
: row[x] == 2 ? 2 + scissors
: rock ? 1
: 3;
};
update(0, c.w - 1, 1);
for (u32 x = 1; x < c.w - 1; ++x)
update(x, x - 1, x + 1);
update(c.w - 1, c.w - 2, 0);
}
return dst;
}
static Cells reference(const Cells &cells, const Config &c, int p) {
return c.n == 4 ? step<4>(cells, c, p) : step<8>(cells, c, p);
}
static void checkImage(const unsigned char *pixels, const Cells &cells,
const Config &c) {
static const int bgra[][3] = {
{0, 0, 0}, {70, 70, 230}, {230, 150, 70}, {120, 210, 80}};
for (u32 y = 0; y < c.ah(); ++y)
for (u32 x = 0; x < c.aw(); ++x) {
int sum[3]{};
for (u32 j = 0; j < c.k; ++j)
for (u32 i = 0; i < c.k; ++i) {
auto s = cells[size_t(c.y + y * c.k + j) * c.w + c.x + x * c.k + i];
for (int b = 0; b < 3; ++b)
sum[b] += bgra[s][b];
}
auto p = pixels + 4 * (size_t(y) * c.aw() + x);
auto area = c.k * c.k;
for (int b = 0; b < 3; ++b)
require(p[b] == (sum[b] + area / 2) / area, "pixel channel mismatch");
require(p[3] == 255, "alpha mismatch");
}
}
int main(int argc, char **argv) try {
u32 width = argc > 1 ? std::stoul(argv[1]) : 1024,
height = argc > 2 ? std::stoul(argv[2]) : 48;
for (u32 n : {4, 8})
for (u64 seed : {0, 1, 2, 4, 5, 8}) {
Config c{width, height, n, 128, 240, 16, 16, 32, 32, 1, seed};
for (u32 k : {1, 2, 4, 8, 16}) {
c.k = k;
GPU gpu(c, priority(seed));
auto cells = initial(c);
require(gpu.snapshot(true), "initial snapshot");
checkImage(gpu.pixels(), cells, c);
gpu.ack();
require(!gpu.snapshot(false), "unchanged snapshot");
if (k != 1)
continue;
for (int t = 0; t <= 128; ++t) {
if (t <= 3 || t == 10 || t == 128)
require(gpu.unpack() == cells, "CPU/GPU cell mismatch");
if (t == 128)
break;
cells = reference(cells, c, priority(seed));
gpu.step();
}
require(gpu.snapshot(true), "final snapshot");
checkImage(gpu.pixels(), cells, c);
}
}
// Place 32 disjoint neighbourhoods in one row for every kernel variant.
for (u32 n : {4, 8})
for (int p = 0; p < 6; ++p) {
Config c{1024, 48, n, 1, 240, 0, 0, 32, 32, 1, 42};
GPU gpu(c, p);
Cells grid(c.w * c.h);
for (int state = 0; state < 4; ++state)
for (int mask = 0; mask < 8; ++mask) {
size_t center = c.w * 2 + (state * 8 + mask) * 4 + 1;
grid[center] = state;
grid[center - 1] = (mask & 1) ? 1 : 0;
grid[center + 1] = (mask & 2) ? 2 : 0;
grid[center + c.w] = (mask & 4) ? 3 : 0;
}
gpu.loadTestCells(grid);
gpu.step();
require(gpu.unpack() == reference(grid, c, p),
"192 exhaustive transitions");
}
// Uniform states, single objects, corners, diagonal-only contacts and seams.
for (u32 n : {4, 8}) {
Config c{2048, 48, n, 1, 240, 2016, 16, 32, 32, 2, 42};
GPU gpu(c, priority(c.seed));
for (int pattern = 0; pattern < 7; ++pattern) {
Cells cells(c.w * c.h, pattern < 4 ? pattern : 0);
if (pattern == 4)
cells[0] = 1;
if (pattern == 5) {
for (u32 x : {0, 31, 32, 1023, 1024, 2047}) {
cells[x] = 1;
cells[c.w + ((x + 1) % c.w)] = 2;
cells[(c.h - 1) * c.w + x] = 3;
}
}
if (pattern == 6)
for (size_t i = 0; i < cells.size(); ++i)
cells[i] = 1 + ((i % 31 + i / c.w) % 3);
gpu.loadTestCells(cells);
gpu.snapshot(true);
checkImage(gpu.pixels(), cells, c);
gpu.ack();
auto expected = reference(cells, c, priority(c.seed));
gpu.step();
require(gpu.unpack() == expected, "manual torus fixture");
if (pattern < 4)
require(!gpu.snapshot(false), "fixed point changed");
}
Cells cells(c.w * c.h);
auto i = size_t(c.y) * c.w + c.x;
cells[i] = 1;
cells[i + 1] = 2;
gpu.loadTestCells(cells);
gpu.snapshot(true);
gpu.ack();
std::swap(cells[i], cells[i + 1]);
gpu.loadTestCells(cells);
require(!gpu.snapshot(false), "aggregation permutation");
cells[i] = 0;
gpu.loadTestCells(cells);
require(gpu.snapshot(false), "single changed pixel");
gpu.ack();
}
std::cout << "CPU/CUDA fields and BGRA snapshots match\n";
} catch (const std::exception &e) {
std::cerr << e.what() << "\n";
return 1;
}