SLIC superpixels on synthetic 4K frames with ground-truth segmentation scoring
/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 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; /app/cpu/main.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 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 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).
/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.