Integer RAW-video ISP with homogeneity-directed Bayer demosaicing
/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
An integer camera ISP that develops a synthetic RAW video. For each frame f = 0..F-1 of a
W x H Bayer sensor (CFA pattern PAT: 0 RGGB, 1 BGGR, 2 GRBG, 3 GBRG; colour index 0 = R,
1 = G, 2 = B):
- Sensor model. The scene is a procedural texture on 128x128 cells (discs, stripes,
sinusoidal rings, ramps) seen through a pan offset that moves per frame; it is multiplied by a
per-frame illuminant, vignetting
vig = 4096 - r2 * 1638 / r2maxand signal-dependent noise (sum of four 16-bit hash fields, scaled byisqrt(4 s + 16)), offset by the black level 256 and clamped to 12 bits. All hashing usesmix64(SplitMix64 finaliser) exactly as in the CPU source;isqrt64is the exact floor square root. - Black level + lens shading.
lin = min(((raw - 256)+ * (4096*4096 / vig)) >> 10, 16383). - Gray-world AWB. Over pixels with
raw < 4000andlin >= 256, per colour sumsS_cand countsN_c; withmg = S_G / N_Gthe R and B gains areclamp(mg * 1024 / max(S_c / N_c, 1), 512, 8192)(1024 if a count is zero). Thenwb = min((lin * gain) >> 10, 16383)in place. - Demosaic (homogeneity-directed). Borders are mirrored with the CFA parity preserved:
x < 0 -> x & 1,x >= W -> W - 2 + (x & 1)(same for y). Hamilton-Adams horizontal and vertical green candidates at R/B sites (green sites keep their value); for each direction a full RGB candidate from colour differences (diagonal average at R/B sites, horizontal / vertical neighbours at G sites) and its integerL = (R + 2G + B) >> 2,a = R - G,b = B - G; homogeneity counts over the 4-neighbourhood with the adaptive thresholdsepsL,epsC(min over directions of the max luminance / squared-chroma differences along that direction); a 3x3 vote of the homogeneity maps picks the horizontal candidate, the vertical one, or their rounded average on a tie. - Colour matrix + tone curve.
CCM(Q10, rows{1690,-480,-186},{-260,1500,-216},{-40,-590,1654}), rounding+512 >> 10, clamp to 0..16383, then an 8-bit gamma LUTmin(255, (isqrt(i * 16384) * 255 + 8192) >> 14). - Statistics. Per frame: R/G/B gains, the sums of the 8-bit R, G and B outputs, the number
of pixels taking the horizontal / vertical / averaged candidate, and a checksum
sum mix64((R<<16 | G<<8 | B) ^ (i * 0x9E3779B97F4A7C15) ^ (f << 50))mod 2^64 over linear pixel indexi. Over the sequence: 256-bin histograms per channel, and 16 autofocus tiles (4x4 grid, tile(y*4/H)*4 + x*4/W) accumulating|4Y(x,y) - Y(x-1,y) - Y(x+1,y) - Y(x,y-1) - Y(x,y+1)|of the lumaY = (R + 2G + B + 2) >> 2over interior pixels.
The CPU source is the exact specification of every rounding and clamping step.
Input format
One line: SEED F W H PAT (SEED unsigned 64-bit; W, H even; PAT in 0..3).
Output format
One line per frame:
FRAME f GAINS gR gB MEAN sumR sumG sumB DIR nH nV nT CHK checksum
then for R, G, B: HIST c P1 p P10 p P50 p P90 p P99 p ZERO n0 FULL n255 (percentile =
first bin whose cumulative count reaches ceil(total * q / 100), capped at 255), then
DIRTOTAL nH nV nT and AF followed by the 16 tile sums.
Hidden inputs
Correctness inputs (16 files): 2x2, 4x2, 2x6, a sequence with F = 0, 64x48 with every
CFA pattern, 6x300, 300x6, 130x66, 256x160, and random even sizes up to 160 with 1 to 4
frames. All inputs satisfy 0 <= F <= 64, W and H even with 2 <= W, H <= 6144. The
timing input has F in 48..56, W in 5984..6048, H in 3968..4032 and a random pattern.
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 48-56 frame sequence from a ~24 MP sensor (5984..6048 x 3968..4032 pixels, random CFA pattern), about 1.2e9 pixels in total.
/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.