Multi-reference full-search motion estimation with temporal predictors and 8x8 splits
/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
Full-search block-matching motion estimation on a synthetic video, as in a video encoder.
- Video.
Fframes ofW x Huint8 pixels: a value-noise background texture under global pan plus per-frame camera shake,NOBJtextured rectangles moving linearly (later objects on top), a small per-frame gain change and per-pixel noise. Textures are sampled with Q8 bilinear interpolation (seemake_texture,bilinearandgen_framein the source). - Search. Frame
t >= 1is split into 16x16 macroblocks (MBs). Each MB is matched against themin(R, t)previous frames (referenceris framet-1-r) at every integer vector(mvx, mvy)in[-S, S]^2; reference pixels outside the frame are edge-replicated. For each candidate the SADs of the four 8x8 quadrants are computed; the 16x16 SAD is their sum. - Predictor. The previous frame's motion field is kept per 8x8 sub-block as normalised
vectors
n = trunc(16*mv / (r+1)). For an MB the predictor is the component-wise median of the top-left sub-block vectors of the 3x3 MB neighbourhood (clamped at the borders) in that field (all zero fort = 1), scaled to referencerastrunc((med*(r+1) +- 8) / 16)(sign ofmed) and clamped to[-S, S]. - Cost.
cost = SAD + LAMBDA * (se(mvx - px) + se(mvy - py) + r),se= signed Exp-Golomb length. Each of the five partitions (16x16 and the four 8x8 quadrants) keeps the candidate with the smallest key(cost, r, |mvx|+|mvy|, mvy, mvx)over all references. - Mode decision. The MB is split iff
sum of the four 8x8 best costs + SPLITis smaller than the best 16x16 cost. The chosen vectors form the frame's motion field (a non-split MB copies its vector into its four sub-blocks).
Input format
One line: W H F R S LAMBDA SPLIT NOBJ SEED (integers, SEED unsigned 64-bit).
Output format
frame t cost C sad D split K refs N0 N1 N2 N3 hash HF (one line per frame t = 1 .. F-1)
total frames F mbs NMB cost C sad D split K hash HT
C and D sum the chosen cost and SAD over all MBs, K counts split MBs, Nr counts 8x8
sub-blocks predicted from reference r. HF = sum mod 2^64 over sub-blocks sb (row-major on
the 8x8 grid) of mix64((t << 48) ^ (sb << 20) ^ ((mvx+32) << 10) ^ ((mvy+32) << 2) ^ r);
HT = sum of mix64(HF ^ t) over frames.
Hidden inputs
W, Hmultiples of 16 with16 <= W, H <= 2048,1 <= F <= 200,1 <= R <= 4,0 <= S <= 32,0 <= LAMBDA <= 64,0 <= SPLIT <= 4096,0 <= NOBJ <= 32.- Correctness inputs include a single MB with
S = 0,S = 32,R = 4, no split penalty and a prohibitive one,LAMBDA = 64, 2048-wide and 2048-tall strips, 60 frames, 32 objects, a full 1920x1088 frame, and random small videos. - The timing input is 1920x1088 with about 45 frames,
R = 2,S = 32,LAMBDAin[3, 8],SPLITin[40, 160]and 6-16 objects (about 8e11 pixel differences).
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 1920x1088 video of about 45 frames searched against 2 reference frames with a +-32 full search (about 8e11 pixel differences).
/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.