Port an OpenMP image pipeline (synthetic image, min-suppressed 3x3 filter, threshold, intensity-graded 8-connected components, per-component statistics) to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-image-ccl-gradedharbor download elfsong/cpu2gpu-image-ccl-graded/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.
For each image description the program builds an 8-bit synthetic image from the seed and runs a four-stage pipeline:
I (values 0..254): two octaves of bilinear integer value noise plus
white noise, all from 32-bit integer hashes of the pixel coordinates (see pixel() in the source).S = (8*c + 2*(n+s+e+w) + (nw+ne+sw+se) - 4*min9 + 8) >> 4 over the 3x3
neighbourhood, where min9 is the 3x3 minimum; coordinates outside the image are clamped to
the border (replicate).S >= G.|S(p) - S(q)| <= D. Connected components of this graph are labelled; each component is
identified by its smallest linear pixel index y*W + x.S over its pixels), a histogram of floor(log2(area)), and the five largest
components (ties broken by smaller minimum index).Q
H_1 W_1 D_1 G_1 SEED_1
...
H_Q W_Q D_Q G_Q SEED_Q
1 <= H, W <= 8192, 0 <= D <= 255, 0 <= G <= 255, SEED is an unsigned 64-bit integer.
Three lines per image, in input order:
image i H W comps NCOMP fg FGPIX hash HASH
hist C_0 C_1 ... C_k
top AREA@MINIDX AREA@MINIDX ...
HASH is the sum modulo 2^64, over all components, of
mix64(minidx ^ (area << 34)) + mix64(sumS * 0xD6E8FEB86659FD93 ^ minidx).hist lists bucket counts from bucket 0 up to the highest non-empty bucket (just hist 0
when there are no components).top lists up to five components ordered by area descending, then smallest index ascending
(empty after top when there are no components).Correctness inputs include 1x1 images, single rows and single columns (up to 8192 long),
G = 0 (every pixel foreground), G = 255 (no foreground), D = 0 and D = 255, random
small images, and images up to about 2500x2048. Most images use D in [0, 24] and G in
[100, 170]. The timing input holds 32 images with sides in [7000, 8192], D in [2, 10]
and G in [115, 155]; very large components spanning a sizeable part of the image are common.
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).
/app/gpu/main.cu is collected. It is compiled with
nvcc -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.system(, popen(, fork(, exec*, dlopen(, getenv(,
#include <filesystem>, /tests/, /proc/. The program may read only the input file it is given./app/inputs/timing_example.in has 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.