Exact fixed-point dense pyramidal Lucas-Kanade optical flow on 2048x2048 frame pairs
/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
Dense pyramidal Lucas-Kanade optical flow between the two frames of NPAIRS synthetic W x H
uint8 frame pairs, in exact fixed point (flow in Q16 pixels, Q8 bilinear weights, 64-bit sums).
- Frames. Frame 0 samples a three-octave value-noise texture at integer pixels; frame 1
samples it at
x - f(x), where the true flowf(Q16, at mostMAXFpixels per component) is a translation plus a linear field plus 3-6 smooth bumps. Both frames get noise in[-2, 2](see the source for the exact formulas). - Pyramids.
NLEVlevels per frame. Levell+1(sizeceil(w/2) x ceil(h/2)) is the level-limage filtered with the 5x5 binomial kernel[1 4 6 4 1]^T [1 4 6 4 1]around(2x, 2y)with clamped coordinates, then(sum + 128) >> 8. - Per level (coarsest first): central-difference gradients
gx, gyof frame 0 (clamped), and tent-weighted window sums over the(2R+1)^2window with weight(R+1-|i|)*(R+1-|j|)and clamped coordinatesq:Gxx = sum w gx^2,Gxy = sum w gx gy,Gyy = sum w gy^2,Cx = sum w gx I0,Cy = sum w gy I0. - Initial flow: zero at the coarsest level; otherwise the coarser flow upsampled by 2: even
xuses coarse columnsx/2 - 1(weight 1) andx/2(weight 3), oddxuses(x-1)/2(weight 3) and(x+1)/2(weight 1), indices clamped, likewise fory; withsthe weighted sum of the four coarse vectors (weights total 16),v = (s + 4) >> 3, clamped to+-256pixels. - Iterations.
NITGauss-Newton steps per pixel, each pixel independent. With the pixel's flowv,fxi = vx >> 16andax = (vx >> 8) & 255(same for y;>>is an arithmetic shift), every window positionqsamples frame 1 atq + v: the taps(qx+fxi, qy+fyi),(qx+fxi+1, ...),(..., qy+fyi+1)and(qx+fxi+1, qy+fyi+1)are clamped to the image andJ = ((t00*(256-ax) + t10*ax)*(256-ay) + (t01*(256-ax) + t11*ax)*ay + 128) >> 8(Q8). ThenJx = sum w gx(q) J,Jy = sum w gy(q) J(exact),bx = (Jx - 256*Cx) / 64,by = (Jy - 256*Cy) / 64,a = (Gxx >> 6) + LAMBDA,c = (Gyy >> 6) + LAMBDA,g = Gxy / 64,det = a*c - g*g(always positive), andvx += -((c*bx - g*by) * 512) / det,vy += -((a*by - g*bx) * 512) / det, each clamped to+-256pixels. All divisions are C++ 64-bit integer divisions (truncating toward zero).
Input format
One line: W H NPAIRS NLEV NIT R LAMBDA MAXF SEED (integers, SEED unsigned 64-bit).
Output format
pair i epe E good G hash HP (one line per pair)
level l size wxh sumvx SX sumvy SY hash HL (one line per level, coarsest first)
E sums |vx - fx| + |vy - fy| (Q16) over the full-resolution pixels of pair i, G counts the
pixels where that error is below 32768, HP sums mix64(k ^ (p * 0xD1B54A32D192ED03)) with
k = (uint32(vx) << 32) | uint32(vy) over pixels p = y*W + x. For each level, SX and SY sum
the final flow components over all pixels and pairs, and HL sums
mix64(k + 0x9E3779B97F4A7C15 * (p + 1) + (pair << 48)) (all sums mod 2^64 where unsigned).
Hidden inputs
1 <= W, H <= 4096,W*H <= 2^22,1 <= NPAIRS <= 32,1 <= NLEV <= 6,1 <= NIT <= 16,1 <= R <= 6,1 <= LAMBDA <= 65536,0 <= MAXF <= 48.- Correctness inputs include an 8x8 frame, a single row and a single column, odd sizes,
R = 6windows on small frames, no motion, the smallest and largestLAMBDA, six levels with 48-pixel motion, 16 iterations, a 2048x1536 frame, and random small inputs. - The timing input has 12-15 pairs of 2048x2048 frames with
NLEV = 5,NIT = 8,R = 5,LAMBDAin[100, 1000]andMAXFin[12, 24].
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 has 12-15 pairs of 2048x2048 frames, 5 pyramid levels, 8 iterations per level and 11x11 windows (about 5.4e9 warped window taps per pair). 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.