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 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 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 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.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.