Lane and edge line detection on a synthetic dashcam clip with gradient-windowed Hough voting
/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
A line-detection job on a synthetic dashcam clip of NF grey frames of W x H pixels. Every frame
is processed independently; all arithmetic that reaches the output is integer. /app/cpu/main.cpp is the
exact specification (hash constants, scene formulas, tie rules).
- Frame synthesis. A per-frame scene (hashed from
SEEDand the frame index) has 6-12 painted segments (lane markings from near a vanishing point to below the frame, or free segments; some dashed; Q4 coordinates, half thickness 1-4 px, positive or negative contrast) and 3-6 textured clutter rectangles. A pixel value is a vertical ramp plus bilinear value noise on a 32-pixel lattice; the highest-index rectangle covering the pixel replaces it with its stripe texture; otherwise every segment containing the pixel centre (16x+8, 16y+8, exact integer distance-to-segment and dash tests) adds its contrast. Hashed per-pixel noise is added and the value clamped to[0, 255]. - Edges. 3x3 binomial blur
(sum + 8) >> 4and 3x3 Sobelgx, gy, both with replicated borders;m = gx^2 + gy^2. An interior pixel is an edge ifm >= TMAGand it beats its two neighbours along the gradient direction quantised to 4 directions (m > m(forward)andm >= m(backward)). Its theta bintein[0, NT)is the bin of the integer trig table (C[t] = llround(16384 cos(pi t / NT)),S[t]likewise) nearest to the gradient direction folded into[0, pi)(binary search, then the better of the two neighbouring bins; binNTwraps to 0). Its weight class ismc = min(7, m >> 14). - Voting. With centred doubled coordinates
xc = 2x - (W-1),yc = 2y - (H-1), every edge votes fort = te + d (mod NT),|d| <= WIN, atrho = ((xc*C[t] + yc*S[t] + 16384) >> 15)(floor) with weight(1 + mc) * (WIN + 1 - |d|)into anNT x NRaccumulator,R = (W+H)/2 + 2,NR = 2R + 1, cellt*NR + rho + R. - Peaks. A cell with
a >= TVOTEvotes is a peak if it beats every other cell of its(2PT+1) x (2PR+1)window by votes, ties going to the smaller cell index. Theta wraps: a neighbour row outside[0, NT)is taken moduloNTwith the rho index mirrored to2R - r; neighbours with rho index outside[0, NR)are skipped. TheKstrongest peaks (votes descending, then cell index ascending) are the frame's lines. - Line support. For every line
(t, r), the edges with|xc*C[t] + yc*S[t] - (r-R)*32768| <= 2048*BANDand circular theta distance|te - t| <= WINsupport it; the program reports their countnand the minimum, maximum and sum of their position along the lines = -xc*S[t] + yc*C[t](0 whenn = 0). - Hashes. Per frame,
hashEsums a hash of every edge(p, te, mc)andhashAsums a hash of every non-zero accumulator cell(index, votes)(both mod 2^64, so order independent).
Input format
One line: W H NF NT WIN TMAG TVOTE PT PR K BAND SEED (integers, TMAG and TVOTE 64-bit,
SEED unsigned 64-bit).
Output format
hough W W H H frames NF NT NT NR NR
totals edges E peaks P lines L
frame f edges E_f peaks P_f lines n_f hashE HE_f hashA HA_f (for the first 3 frames)
line k t T r R votes V n N smin A smax B ssum S (n_f lines of that frame)
theta_hist h0 ... h15 (frames by 16*t/NT of their strongest line)
nlines_hist c0 ... cK (frames by number of lines)
hash edges HE acc HA lines HL
HE and HA are sums of the per-frame hashes and HL a sum of per-frame hashes of all lines of
all frames (see the source), all mod 2^64.
Hidden inputs
3 <= W, H <= 4096,0 <= NF <= 1850,4 <= NT <= 4096,0 <= WIN,2*WIN + 1 <= NT,0 <= PT <= 32,2*PT + 1 <= NT,0 <= PR <= 32,1 <= K <= 16,0 <= BAND <= 64,0 <= TMAG <= 6000,0 <= TVOTE <= 10^12.- Correctness inputs include a 3x3 frame, no frames,
2*WIN+1and2*PT+1equal toNT,TMAG = TVOTE = 0(every non-zero cell may be a peak), a vote threshold above every cell,NT = 4096, strips of width or height 5-16, a 4096-wide frame, one 1920x1080 frame and random small clips. - The timing input is 1920x1080 with 1650-1850 frames,
NTin {720, 900, 1024},WINin[NT/5, NT/4],TMAGin[2000, 4000],TVOTE = (250..350) * (WIN+1),PT, PRin[2, 4],Kin[8, 16],BANDin[16, 40].
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 1650-1850 frame clip of 1920x1080 frames, NT in {720, 900, 1024} theta bins and a voting half-window WIN between NT/5 and NT/4, so each of the roughly 5e4 edge pixels per frame casts 289-513 votes.
/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.