Port an OpenMP multi-scale detector (wrap-around 32-bit integral image, nine-box cross-ring response per scale, strict 3x3x3 scale-space non-maximum suppression, top-10 and hashed statistics) to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-image-boxfeat-nmsharbor download elfsong/cpu2gpu-image-boxfeat-nms/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 an 8-bit synthetic image I is built from the seed (see
pixel()) and its integral image is formed in wrap-around 32-bit arithmetic (box sums are
exact because every box sum is far below 2^32).
For scales s = 0..S-1 let h = s + 1, box side b = 2h + 1, and let box(cy, cx) be the sum
of I over the b x b box centred at (cy, cx). At centre (y, x):
C = box(y, x)
N = box(y-b, x) S = box(y+b, x) E = box(y, x+b) W = box(y, x-b)
NE, NW, SE, SW = the four diagonal boxes at (y -/+ b, x +/- b)
A = 4C - (N + S + E + W)
B = 4C - (NE + NW + SE + SW)
Dd = (N + S) - (E + W)
R = (A*B - ((Dd*Dd) >> 1)) / b^4 (64-bit, division truncates toward zero)
R is defined only where all nine boxes lie inside the image, i.e. 3h+1 <= y < H-3h-1 and
the same for x. A detection is a defined R > THR that is strictly greater than every
defined R among its 26 neighbours in (scale, y, x) (neighbours outside the image, outside
[0, S) or undefined are ignored).
Q
H_1 W_1 S_1 THR_1 SEED_1
...
1 <= H, W <= 8192, 1 <= S <= 12, 0 <= THR <= 10^7, SEED unsigned 64-bit; H*W*S stays
below 2.2e8 in every hidden input.
Three lines per image, in input order:
image i H W scales S valid NVALID sumR SUMR detections NDET hash HASH
perscale D_0 ... D_{S-1}
top R:s,y,x R:s,y,x ...
NVALID counts defined responses, SUMR is their signed sum, D_s counts detections at scale
s, HASH = sum mod 2^64 over detections of
mix64(((s << 52) | (y << 26) | x) + (uint32)R * 0x9E3779B97F4A7C15), and top lists up to 10
detections ordered by R descending, then s, y, x ascending.
Correctness inputs include 1x1 images and images too small for any defined response, single
rows and columns, all scale counts from 1 to 12, THR = 0 and very large THR (no detections),
strips 8192 long, and random images up to about 1300x1300. The timing input holds 18 images with
sides in [3600, 4400], S in [6, 10] and THR in [20000, 120000].
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.