RDO block coding: integer DCT, dead-zone QP search, run-length rate
/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
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 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. - This sandbox has no GPU.
nvccworks here for compiling. To run on the GPU, use/app/bin/gpu_run [--repeat N] [--quiet] SOURCE.cu INPUT_FILE...: it compiles the file with the flags above on a remote H100 worker, runs it on each input, and prints stdout, exit code and wall-clock time. Grading also runs your code on an H100 worker, one job per GPU at a time. 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: 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. 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.