Port an OpenMP adaptive equalisation pipeline (12-bit synthetic image, per-tile 256-bin histograms, clipping with strided remainder redistribution, integer bilinear blending of tile maps, unsharp mask, statistics) to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-image-tile-equalizeharbor download elfsong/cpu2gpu-image-tile-equalize/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 a 12-bit synthetic image I is built (smooth illumination times
texture plus noise, see pixel12()); bin(v) = v >> 4 gives 256 bins. Then:
TS x TS tiles (the last row/column of tiles may be
partial; n = pixels in the tile). For each tile t (row-major tile index): histogram of
bins; clip every bin at lim = max(1, CLIP*n/1024); the clipped-off excess is
redistributed as excess/256 to every bin plus one more to each of the excess%256 bins
(o + 37*j) % 256, j = 0, 1, ..., with o = (t*97) % 256; then
map_t[b] = cdf[b] * 65535 / n (inclusive cumulative sum, integer division).E = integer bilinear interpolation of the four nearest tile maps at the pixel's
bin, in doubled coordinates (pixel centre 2p+1, tile centre t*2TS + TS; outside the
first/last centres the nearest tile is used), rounded: (sum + den/2) / den, den = (2TS)^2.
See tile_axis() and stage 3 in the source.O = clamp(E + 3*(E - s9/9)/8, 0, 65535) where s9 is the 3x3 sum with
replicated borders and / truncates toward zero.Q
H_1 W_1 TS_1 CLIP_1 SEED_1
...
1 <= H, W <= 8192, TS in {8, 16, 32, 64, 128}, 1 <= CLIP <= 2048, SEED unsigned 64-bit.
Two lines per image, in input order:
image i H W tiles NT excess EXCESS sum SUMO hash HO HM
hist B_0 ... B_63
EXCESS is the total clipped excess over all tiles, SUMO the sum of O, B_j counts
pixels with O >> 10 == j, HO = sum mod 2^64 of mix64((i << 16) | O) over pixels
i = y*W + x, HM = sum mod 2^64 of mix64((j << 16) | map[j]) over all NT*256 map
entries j = t*256 + b.
Correctness inputs include 1x1 images, images smaller than one tile, partial edge tiles,
single rows and columns up to 8192 long, all tile sizes, CLIP from 1 up to 2048 (no
clipping), and random images up to about 2500x2500. The timing input holds 48 images with sides in
[6500, 8192], TS in {16, 32, 64, 128} and CLIP in [2, 48].
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 source generation only. Write /app/gpu/main.cu; compilation,
execution and verification are performed later in a separate verifier environment.
/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 public input examples for understanding the input format.