Octal-game nim-sequences with certified periods and position solving
/app/cpu/main.cpp is a multicore CPU program (C++17, OpenMP). It is built as
/app/bin/cpu_baseline with g++ -O3 -march=native -fopenmp -std=c++17 and run as
cpu_baseline INPUT_FILE
It reads one input file and writes its result to stdout.
What the program computes
An analysis run over a batch of generated octal games (take-and-break heap games, as studied
in combinatorial game theory). For game t a code 0.d1 d2 ... dD is derived from the seed
(make_code). Digit d_k (0..7) says what a move that removes k tokens from one heap may
leave: bit 1 - nothing (the heap had exactly k tokens), bit 2 - one non-empty heap,
bit 4 - two non-empty heaps (only possible for k <= 3). Every code has at least one split
digit and d_D != 0.
For every game the program runs three dependent stages:
- Nim-sequence. For
n = 1 .. N-1in order,G(n) = mex( {0 if bit1 of d_k and n == k} U {G(n-k) if bit2 of d_k and n > k} U {G(a) xor G(n-k-a) : bit4 of d_k, 1 <= a <= (n-k)/2} ),G(0) = 0. Values are stored in 16 bits; a mex of 65535 or more is stored as 65535 (the hidden inputs never get there; values do grow for many codes, into the thousands). Any algorithm that produces the sameGis fine. - Sequence report.
MAXG, number of zeros, sum and hash ofG, the record values (nwithG(n) > max G(1..n-1)), and the smallest periodp <= min(N/3, 4096)certified by the octal-game periodicity theorem: withlast(p)= largestn <= N-1such thatn < porG(n) != G(n-p)andn0 = last(p) - p + 1, the firstpwith2*n0 + 2*p + D <= NgivesPERIOD p PRE n0(0 0if none). - Position solving.
Qmulti-heap positions (2..6 heaps, sizes generated from the seed; about 30 % of heaps are up toN-1tokens, the rest up to 1000). A position is a P-position iff the xorXof its heap values is 0. For every N-position the number of winning movesW(moves to a position of xor 0, counting unordered splits{a, m-a}once) is computed; the program reports sums, a hash, a histogram ofWand the position with the largestW(smallest index on ties).
Everything is integer; the exact formulas, generators and output hashes are in the CPU source, which is the specification.
Input format
T SEED
N_1 D_1 Q_1
...
N_T D_T Q_T
1 <= T <= 8, SEED an unsigned 64-bit integer, 1 <= N <= 1300000, 1 <= D <= 12,
0 <= Q <= 300000.
Output format
Four lines per game:
GAME t CODE 0.<digits> N <N> MAXG <maxG> ZEROS <c0> GSUM <sum> GHASH <hash> PERIOD <p> PRE <n0>
RECORDS t <number of records> LAST <last record n> RHASH <hash>
POS t P <#P> N <#N> SUMW <sum W> MAXW <max W> ARG <q or -1> WHASH <hash>
WHIST t h0 h1 ... h7
Hidden inputs
Correctness inputs (8 files, 1 to 5 games each) include N = 1, 2, 3, D = 1 and D = 12,
Q = 0, games with N up to 50000 (large enough for the multi-thread path of the CPU),
periodic and non-periodic codes, and random seeds. The timing input has 3 games with D in
[5, 11]; N is chosen per game so that it does about 5e10 split-pair evaluations
(N about 4.5e5 for one split digit, 3.2e5 for two, 2.6e5 for three); none of the games is
certified periodic within its first 1200 values; 60000 positions per game.
Your task
Port this program to CUDA in /app/gpu/main.cu: same command line (PROGRAM INPUT_FILE),
same output on stdout. Then make it as fast as you can on the GPU while keeping the output correct.
The CPU source is the specification: when this text and the code disagree, the code wins.
How you are scored
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -O3 -arch=sm_90 -std=c++17 -o port /app/gpu/main.cu; no extra libraries are linked. The CUDA runtime and the header-only libraries that ship with the CUDA toolkit (CUB, Thrust, libcu++) are allowed. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Grading runs on one H100 in a fresh container without network. Inputs are generated with a new random seed at grading time, so they cannot be known in advance.
- Correctness: every hidden input (small edge cases plus the timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout gives reward 0.
- Speed: Three splitting games totaling about 1.5 billion pair evaluations, with up to 60000 queried positions per game. Your port and our GPU reference are each run on it
once to warm up and then 5 times, interleaved; the time is the wall clock of the whole process
(including CUDA initialisation, input parsing and output). When correct,
reward = min(2, t_reference / t_yours)using the medians. /app/inputs/holds a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.