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 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.
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 CPU source is the specification: when this text and the code disagree, the code wins.
/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.nvcc works 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.reward = min(2, t_reference / t_yours) using the medians./app/inputs/ holds a few public inputs. /app/bin/run_public.sh compiles your port with the
flags above and compares it against the CPU baseline on each of them.