Speculative self-synchronising decoding of prefix-code bitstreams
/app/cpu/main.cpp is the source of a multicore CPU program (C++17, OpenMP).
The program takes one input-file path (PROGRAM INPUT_FILE) and writes its result to stdout.
What the program computes
Decoding of a few very long bitstreams, each under its own complete canonical prefix code whose symbols carry raw extra bits (DEFLATE-like tokens), followed by a delta post-process and per-stream statistics. The CPU source is the specification; this is a summary.
- Code of stream
i.NSYMsymbols, code lengths at mostLMAX. Random numbers arernd_k = mix64(SEED ^ (0xC0DEC0DE00000000 + (i << 32) + k))fork = 1, 2, .... The leaf depths start as{1, 1}; while there are fewer thanNSYMleaves, take leafrnd % count(moving cyclically to the next leaf whose depth is belowLMAX), deepen it by one and append a copy (a split). A Fisher-Yates shuffle (j = rnd % (i + 1)fori = NSYM-1 .. 1) then assigns depthd[j]to symbolperm[j]. Symbolscarriesxb(s) = floor(s * (XMAX + 1) / NSYM)extra bits and has value basebase(s) = sum_{s' < s} 2^xb(s'). Codes are canonical: symbols sorted by (length, symbol id) take consecutive code values, most significant bit first. - Bits. Stream
iisNBITSbits long. Wordwismix64(key_i + w * 0xD6E8FEB86659FD93)withkey_i = mix64(SEED ^ (0x5EED000000000000 + i)); bitbis bit63 - b % 64of wordb / 64(MSB first). Bits pastNBITSread as 0. - Parse. From bit 0 the stream is a sequence of tokens: a code word, then
xb(sym)extra bits read as an unsigned MSB-first integere. Token valuet = base(sym) + e, deltadl = (t >> 1) ^ -(t & 1)(zig-zag), running valuev += dl(starting at 0). Parsing stops at the first token that would end past bitNBITS; the unread bits are the trailing bits. - Parallel formulation (CPU and GPU). Streams are cut into fixed-size chunks; a token belongs to the chunk where it starts. Every chunk is decoded speculatively from its first bit to find its exit; every chunk then checks, by walking the parse from the previous chunk's speculative exit and its own speculative parse in lock-step, whether they merge (prefix codes usually resynchronise after a few tokens); a serial pass confirms the true entries, re-walking only chunks whose guess failed; every chunk is decoded from its true entry; per-chunk statistics are combined with prefix offsets. The output depends only on the true sequential parse, never on the chunk size or thread count.
- Statistics per stream. Tokens, extra bits, trailing bits;
poshash = sum mix64(((p << 12) | sym) ^ ((i+1) * 0xA24BAED4963EE407))over tokens starting at bitp; symbol histogram,histhash = sum_s mix64((s << 40) ^ count_s ^ ((i+1) << 60));codehash= foldh = h * 0x100000001B3 + len(s) * 16 + xb(s)over symbols; the 8 most frequent symbols (ties to the smaller id); finalv; sums ofvandv^2after every token; maximum and minimum ofvwith the 1-based index of the first token that reaches them. All sums are modulo 2^64.
Input format
NS SEED
NSYM_0 LMAX_0 XMAX_0 NBITS_0
...
NSYM_{NS-1} LMAX_{NS-1} XMAX_{NS-1} NBITS_{NS-1}
Output format
streams NS
stream i nsym N lmax L xmax X bits B codehash H
stream i tokens T extrabits E trailing R
stream i poshash P histhash Q
stream i top s:c s:c ... (min(8, NSYM) entries, most frequent first)
stream i final V sum S sumsq S2 max MX at IMX min MN at IMN
The last line reads stream i final 0 sum 0 sumsq 0 max - at - min - at - when the stream has no
complete token. Sums are printed as unsigned 64-bit numbers.
Hidden inputs
1 <= NS <= 16,2 <= NSYM <= 4096,1 <= LMAX <= 24,2^LMAX >= NSYM,0 <= XMAX <= 8,0 <= NBITS <= 2^40(at most about 1e11 bits in total).- Correctness inputs include empty streams, streams of 1-65 bits, the two-symbol code, stream ends at and around multiples of 8192 and 65536 bits, fixed-length codes of length 3, 5 and 12 (which never resynchronise, so most chunk guesses fail and need a full re-walk), small alphabets with up to 8 extra bits, 24-bit codes over 4096 symbols, up to 16 streams per input and random mixes; correctness streams have at most 6e6 bits.
- The timing input has four streams of about 2.25e10 bits each with (NSYM, LMAX, XMAX) = (256, 16, 0), (1024, 20, 4), (4096, 24, 8) and (2048, 18, 2): about 1.0e10 tokens in total. Two parses of these codes started at different bits merge after 35-175 bits on average (rarely more than 2000 bits).
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 main computation must execute on the GPU. Renaming CPU code to .cu, running a
CPU fallback, or launching a dummy kernel while computing the answer on the CPU does not satisfy this task.
The CPU source is the specification: when this text and the code disagree, the code wins.
This sandbox is for writing the source only: it has g++ and the CPU program, but no CUDA
compiler and no GPU. Compilation, execution and verification happen later on an H100 in a
separate verifier, and a port that does not compile is rejected, so check your logic against
the CPU program before you finish (for example by emulating your kernels on the host).
How your port is verified
- 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. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: The timing input has four streams of about 2.25e10 bits each (about 1.0e10 tokens in total) with (NSYM, LMAX, XMAX) = (256, 16, 0), (1024, 20, 4), (4096, 24, 8) and (2048, 18, 2); parses resynchronise after 35-175 bits on average.
/app/inputs/timing_example.inhas the shape and size of the hidden timing input. Your port is timed against a reference GPU port on it; the faster your port, the higher its score. /app/inputs/holds public input examples for understanding the input format.