Interleaved rANS container transcoding with per-block model refresh
/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
A storage system re-packs a container of rANS-compressed blocks: every block is decoded, checked
against its header, given a refreshed frequency model and re-encoded with a different number of
interleaved lanes and a different probability precision. The container itself is generated from
a counter-based hash (mix64 = SplitMix64 finaliser). The CPU source is the specification; this
is a summary.
- Static tables.
Ttables over an alphabet ofAbyte symbols with resolutionMIN = 2^PBIN. Tabletpermutes the alphabet (Fisher-Yates driven byht = mix64(SEED ^ (0x7AB1E00000000000 + t))), gives rankrthe weight2^24 / dor2^24 / d^2(d = r + 1 + ht % 8, steep if(ht >> 8) % 2) plus1 + (12 hash bits), and normalises the weights. Normalisation (also used for the refreshed models): withPpresent symbols and totalT,f[s] = 1 + floor(h[s] * (M - P) / T)forh[s] > 0, else 0; the deficitM - sum fis added to the most frequent symbol (smallest id on ties). Cumulative frequenciesc[s]and a slot-to-symbol table follow.ebits_t = (sum_s f[s] * (256*PBIN - lg256(f[s]))) >> PBINwherelg256(f) = floor(256 log2 f)is computed with integer squaring. - Block
b(hb = mix64(SEED ^ (0xB10C000000000000 + b))):n = NMIN + hb % (NMAX-NMIN+1)symbols, tablet = (hb >> 40) % T, declared word countW = ((n * ebits_t) >> 12) * (1000 + jit) / 1000withjit = (hb >> 20) % 37 - 7,key = mix64(hb ^ 0x5EED5EED5EED5EED), initial lane statesx_k = 2^16 + mix64(key + 0x1000 + k) % (2^32 - 2^16)fork < KIN, and an endless 16-bit word stream: wordjis bits16*(j%4) .. 16*(j%4)+15ofmix64(key + (j/4) * 0x9E3779B97F4A7C15). - Decode (32-bit states, 16-bit renormalisation, L = 2^16). Symbol
i = 0..n-1belongs to lanei % KIN:slot = x & (MIN-1),s = sym[slot],x = f[s]*(x >> PBIN) + slot - c[s]; ifx < Lthe lane reads the next word of the shared stream:x = (x << 16) | word(rd++).rdis the number of words consumed. The block overruns (is damaged) ifrd > W. - Payload hash
datahash = sum_j mix64(w_j ^ (j * 0xD1B54A32D192ED03) ^ (b << 44))over the little-endian 8-symbol wordsw_jof the decoded block (last word zero-padded). Histograms of all blocks are summed into a global symbol histogram. - Re-encode (only blocks that did not overrun): refreshed table = normalisation of the block
histogram to
MOUT = 2^PBOUT;KOUTlane states start atL; symbols are encoded fromi = n-1down to 0 with lanei % KOUT: ifx >= ((L >> PBOUT) << 16) * f[s]the lane emitsx & 0xFFFF(theE-th emitted word, hashed asmix64((b << 40) ^ (E << 16) ^ word)) andx >>= 16; thenx = ((x / f[s]) << PBOUT) + x % f[s] + c[s]. Final states and the refreshed table are hashed (see the source).saved = rd - Eper block.
Input format
One line: NB A KIN KOUT PBIN PBOUT NMIN NMAX T SEED (SEED is an unsigned 64-bit integer).
Output format
blocks NB symbols N alphabet A kin KIN kout KOUT pbin PBIN pbout PBOUT
table t ebits E blocks B symbols S words W overruns O (one line per table, t = 0..T-1)
decode words W overrun_blocks O quarantined_symbols Q datahash H
histogram hash H nonzero Z top s:c s:c ... (8 most frequent, ties to smaller s)
reencode blocks R symbols S words E saved D
reencode streamhash H statehash H tablehash H
best block b saved d worst block b saved d (ties to the smaller block id)
The last line is best block - saved - worst block - saved - when every block overran. All hashes
are sums modulo 2^64.
Hidden inputs
1 <= NB <= 2^20,2 <= A <= 256,1 <= KIN, KOUT <= 32,10 <= PBIN, PBOUT <= 15,1 <= NMIN <= NMAX <= 262144,1 <= T <= 16.- Correctness inputs include single-symbol blocks, binary alphabets, blocks shorter than the lane
count, one-lane containers,
PBIN != PBOUTin both directions, the largest blocks, seeds 0 and 2^64-1 and random mixes (at most a few million symbols each). - The timing input has 100000 blocks of 65536..262144 symbols (about 1.6e10 symbols),
Ain {128, 192, 256},KINin {4, 8},KOUT = 32,PBINin 13..15,PBOUTin 11..13,Tin 8..16; roughly 13% of the blocks overrun and are quarantined.
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 100000 blocks of 65536-262144 symbols (about 1.6e10 symbols decoded and re-encoded), A in {128,192,256}, KIN in {4,8}, KOUT = 32, PBIN 13-15, PBOUT 11-13 and 8-16 static tables.
/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.