Exact periodic kNN with expanding-ring search, mutual pairs and list persistence
/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.
What the program computes
N points with integer coordinates live in a periodic cube of side L = G*C (G odd,
G^3 cells of width C). Initial positions (one third uniform, two thirds in K clusters
whose offsets are sums of three uniforms in [-W, W]) and integer drift velocities in
[-V, V]^3 come from the seed via the SplitMix64 finaliser mix64; in frame f a point is at
(initial + f * velocity) mod L. Distances are squared minimum-image distances
(d = a - b wrapped into (-L/2, L/2]).
For every frame f = 0 .. F-1 and every point i, the program finds the exact list of its
k = min(KN, N-1) nearest other points, ordered by (d^2, index) (ties: smaller index first).
The CPU program searches cells ring by ring (Chebyshev rings around the point's cell) and stops
after ring s once the k-th distance is < (s*C + 1)^2 (or all cells are visited); the
number of rings s used per point is part of the output, so the same stopping rule must be
reproduced. Per frame it prints:
mutual: unordered pairs{i, j}withjin list(i) andiin list(j);hash:sum over i, rank r of mix64((i << 24) ^ (j << 4) ^ r ^ (f << 56))mod 2^64;- the sums of the nearest and of the k-th squared distances, the sum of rings used;
persist: forf > 0, the number of(i, j)withjin both the current and the previous frame's list ofi;- the point with the largest in-degree (number of lists containing it; ties: smaller index).
After the last frame it prints the histograms of rings used (bucket 7 = 7 or more) and of in-degrees (bucket 63 = 63 or more), and the 5 largest-in-degree points with their first 4 neighbours.
Input format
One line: N G C KN K W V F SEED (integers; SEED unsigned 64-bit).
Output format
frame f mutual M hash H nn_d2 A kth_d2 B rings R persist P hub I D (F lines)
points N box L k k frames F
ring_hist H_0 ... H_7
indegree_hist H_0 ... H_63
hub i indegree D j:d2 j:d2 ... (min(5, N) lines when F > 0)
Hidden inputs
0 <= N <= 2.3e6,Godd with3 <= G <= 85,4 <= C <= 60,1 <= KN <= 16,0 <= K <= 1000,0 <= W <= 120,0 <= V <= 60,0 <= F <= 6.- Correctness inputs include
N = 0,N = 1,KN > N - 1,F = 0,G = 3,W = 0(exactly coincident points, many distance ties),KN = 1, and sparse uniform boxes that need several rings. - The timing input has
Nin[1.8e6, 2.2e6], about 4 points per cell (Godd, 77-83),C = 30,KN = 16,K = N/3300,W = 40,Vin[2, 4]andF = 6.
Your task
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).
How your port is verified
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -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. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: The timing input has about 2 million points (two thirds in ~600 Gaussian-like clusters) in a periodic box of about 79^3 cells, k = 16, and 6 frames of drift.
/app/inputs/timing_example.inhas 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.