SLIC superpixels on synthetic 4K frames with ground-truth segmentation scoring
/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
A superpixel segmentation job: NIMG synthetic colour frames of W x H pixels are segmented with
SLIC (simple linear iterative clustering) and the result is scored against the known ground-truth
region map. All arithmetic is integer; cpu.cpp is the exact specification.
- Frame synthesis. Per frame,
NGregions get a hashed seed point, weightwtin[8, 15], a base colour, a per-channel linear ramp and a noise amplitude. The ground-truth label of a pixel isargmin_g wt_g * ((x-sx_g)^2 + (y-sy_g)^2)(ties: lowestg). Its three channelsL, a, b(uint8) are the region's base colour plus ramp plus hashed per-pixel noise, clamped to[0, 255]. - Seeding. The image is cut into an
nx x nygrid (nx = max(1, W/S),ny = max(1, H/S), cells ofSpixels, the last cell of a row/column takes the remainder). Each of theK = nx*nycentres starts at its cell midpoint moved to the pixel of the 3x3 neighbourhood with the smallest|dL/dx| + |dL/dy|(central differences, clamped coordinates, first minimum in raster order). Centres are stored in Q4: colour16*value, position16*pixel + 8. Every pixel starts with the label of its own cell. - SLIC iterations (
NITtimes). For a pixel(x, y)in grid cell(i, t)with Q4 valuesl, a, b, qx = 16x+8, qy = 16y+8, the candidates are the centres of the up to 3x3 neighbouring cells, in increasing indexk = jj*nx + ii. A candidate is admissible when|qx - cx| <= 16*Sand|qy - cy| <= 16*S. Its distance isD = (dl^2 + da^2 + db^2) * S^2 + (dx^2 + dy^2) * M^2(64-bit). The pixel takes the admissible candidate with the smallestD(ties: smallestk); with no admissible candidate it keeps its previous label (an "orphan" pixel-iteration). Afterwards every centre withn > 0pixels becomes the rounded mean of its pixels:c.l = (16*sum L + n/2) / n(same fora,b),c.x = (16*sum x + 8n + n/2) / n(same fory); empty centres stay. The residual of the iteration is the sum over the updated centres of|new cx - old cx| + |new cy - old cy|. - Evaluation on the final labels:
nonempty: centres with at least one pixel;hist: those counted in binmin(15, 4n / S^2);asa: sum over all centres of the largest number of its pixels that share one ground-truth region (achievable segmentation accuracy numerator);homog: sum over centres and channels ofn * sum v^2 - (sum v)^2(mod 2^64);spb: pixels whose right or lower neighbour has a different label (superpixel boundary);gtb: the same on the ground-truth map;recall: ground-truth boundary pixels with a superpixel boundary pixel within Chebyshev distance 2;lhash = sum_p mix64(p * 0x9E3779B97F4A7C15 ^ label)and a per-centre hashchashof the final centre and its pixel count (see the source), both mod 2^64.
Input format
One line: W H NIMG S M NIT NG SEED (integers, SEED unsigned 64-bit).
Output format
One line per frame:
img i K K nonempty NE orphans O asa A gtb G recall R spb B homog HM hist h0 ... h15 lhash LH chash CH res r_0 ... r_{NIT-1}
and one final line
total images NIMG pixels P orphans O asa A gtb G recall R spb B homog HM lhash LH chash CH res RS
with the per-frame values summed (hashes and homog mod 2^64, RS the sum of all residuals).
Hidden inputs
1 <= W, H <= 4096,1 <= NIMG <= 72,8 <= S <= 64,1 <= M <= 40,1 <= NIT <= 20,2 <= NG <= 48(the program acceptsS >= 1,M >= 0,NIT >= 0andNG <= 255).- Correctness inputs include a 1x1 image, images smaller than one grid step (a single centre),
strips narrower or lower than the step, remainder cells wider than
S, a single iteration,M = 1,M = 2withS = 64(pixels without an admissible centre do occur),M = 40, two regions, a 4096-wide image, a full-HD frame and random small cases. - The timing input is 3840x2160 with 56-72 frames,
Sin[12, 20],Min[16, 28],NIT = 10,NGin[32, 48].
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 56-72 frames of 3840x2160 pixels, grid step 12-20 (about 20k-58k superpixels), 32-48 ground-truth regions and 10 SLIC iterations per frame (about 8.3e7 pixel-iterations per frame, each testing up to 9 centres). 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.