Port an OpenMP exact anisotropic squared Euclidean distance transform (column sweeps plus integer lower-envelope row pass) with ridge extraction and statistics to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-image-edt-ridgeharbor download elfsong/cpu2gpu-image-edt-ridge/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 a binary feature map from the seed and computes the exact anisotropic squared distance transform
D(x, y) = min over feature pixels (fx, fy) of A*(x-fx)^2 + B*(y-fy)^2
in 64-bit integers. A pixel is a feature when two octaves of integer value noise reach level
G, or when a per-pixel hash falls below SP/65536 (isolated points); see feature().
It then marks ridge pixels: D(p) > 0, D(p) >= D(q) for every 8-neighbour q inside the
image, and at least two of the 4-neighbours inside the image have D(q) < D(p) strictly.
Q
H_1 W_1 A_1 B_1 G_1 SP_1 SEED_1
...
1 <= H, W <= 8192, 1 <= A, B <= 9, 0 <= G <= 255, 0 <= SP <= 65536, SEED unsigned 64-bit.
Per image, in input order. If the image has no feature pixel at all, a single line
image i H W nofeature. Otherwise two lines:
image i H W feat NFEAT sum SUMD max MAXD at IDX ridges NR hash HD HR
hist C_0 C_1 ... C_k
SUMD is the sum of D over all pixels; MAXD the maximum and IDX the smallest linear
index y*W + x attaining it.HD = sum mod 2^64 of mix64(D ^ (i * 0xD6E8FEB86659FD93)) over all pixels i;
HR = sum mod 2^64 of mix64((i << 32) | D) over ridge pixels.hist: bucket 0 counts D == 0, bucket b >= 1 counts 2^(b-1) <= D < 2^b; printed up to
the highest non-empty bucket.Correctness inputs include 1x1 images, single rows and columns up to 8192 long, images with no
feature (output nofeature), images where every pixel is a feature, very sparse features
(distances spanning the whole image), extreme weights (1 and 9), and random images up to
2048x2048 with G in [150, 235] and SP in {0, 5, 20, 100, 400, 1500, 3000}. The timing
input holds 48 images with sides in [6500, 8192].
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.