Exact fixed-point dense pyramidal Lucas-Kanade optical flow on 2048x2048 frame pairs
/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
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 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 12-15 pairs of 2048x2048 frames, 5 pyramid levels, 8 iterations per level and 11x11 windows (about 5.4e9 warped window taps per pair).
/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.