RDO block coding: integer DCT, dead-zone QP search, run-length rate
/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
Rate-distortion optimised transform coding of a synthetic video, block by block. Every constant and table is in the CPU source, which is the specification; this is a summary.
- Video.
Fframes ofW x H8-bit pixels generated from the seed: two 256x256 value-noise texture tiles (four octaves of bilinearly interpolated hashed lattice values), a slowly panning background, six textured discs moving with constant velocities (later discs drawn on top), a gradient and per-pixel hash noise (seepix()). - Residual. For frame 0 the residual is
pixel - 128; for framet > 0it ispixel(t) - pixel(t-1)(the previous original frame, so frames are independent). - Transform. Each 8x8 residual block gets the 8-point integer DCT
TMalong rows ((sum + 2) >> 2) and then columns ((sum + 256) >> 9), arithmetic shifts. - Candidates. The frame QP is
qb = max(0, QP0 - 3)whent % GOP == 0, elseQP0. For each of theNQoffsetsdthe block is quantised atq = clamp(qb + d, 0, 51): per zig-zag positionlevel = (|c| * (MF[q%6]*16/QM[pos]) + dz) >> (18 + q/6)with dead zonedz = 171 << (9 + q/6)at DC and85 << (9 + q/6)elsewhere, reconstructionrec = ((level * LS[q%6] * QM[pos]) << (q/6) + 32) >> 6, distortionD = sum (|c| - rec)^2(64-bit). RateR= 2 flag bits + signed Exp-Golomb length ofq - qb+ onetup_bits(run, |level|, last)per non-zero level in zig-zag order (run of zeros before it,lastfor the final one). CostJ = D + lambda * Rwithlambda = LAM0[qb%3] << (qb/3). The smallestJwins (ties: earlier offset in input order). A skip mode costsJ = sum c^2 + lambda(1 bit) and wins only if strictly smaller. - Reconstruction. The chosen levels are dequantised (
recclipped to 32767, sign restored), inverse transformed (columns(sum + 64) >> 7clipped to 16 bits, then rows(sum + 2048) >> 12), added to the prediction and clipped to 0..255; skip blocks reconstruct the prediction. - Per block it records the bits (1 for skip), the transform-domain distortion
D(sum ofc^2for skip), the pixel SSD of the reconstruction,sum_r mix64(row_r ^ ((bidx*8 + r) * 0xD1B54A32D192ED03))over the 8 reconstructed rows packed little-endian (bidx = (t*BH + by)*BW + bx), and the mode (offset index or skip).
Input format
W H F SEED
QP0 GOP
NQ d_1 ... d_NQ
Output format
video WxH frames F blocks N
frame t qp qb bits B dist D ssd S recon H (frames t < 8 and the last two frames)
framestats X
total bits B dist D ssd S
qp_hist d_1:n_1 ... d_NQ:n_NQ skip:n
qpmap Q recon H
Per-frame values are sums over its blocks (hashes modulo 2^64). framestats is
sum_t mix64(bits_t * 0x9E3779B97F4A7C15 ^ t) + mix64(dist_t + t) + mix64(ssd_t ^ (t << 40)) + recon_t * (2t + 1)
mod 2^64, qpmap = sum mix64(bidx * 64 + mode) over all blocks, recon the sum of the frame
recon hashes.
Hidden inputs
8 <= W, H <= 4096(multiples of 8),0 <= F <= 400,0 <= QP0 <= 51,1 <= GOP,1 <= NQ <= 16, offsets in [-12, 12] (duplicates and clamped QPs are allowed).- Correctness inputs include
F = 0, a single 8x8 block, QP clamping at 0 and 51, every frame intra-period (GOP = 1), 8-pixel-wide and 8-pixel-tall strips, one full 3840x2160 frame and random small videos. - The timing input is 3840x2160 with about 360 frames,
QP0in [26, 32],GOPin [8, 16] and the 16 offsets-8 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 8 10(about 47 million blocks, 750 million candidate quantisations).
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 is a 3840x2160 video with about 360 frames (about 47 million 8x8 blocks), each block evaluated at 16 candidate QPs plus skip.
/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.