Johnson all-pairs shortest paths with negative edges
/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
For each generated sparse directed graph with negative edge weights the program solves all-pairs shortest paths with Johnson's algorithm.
A graph with n vertices is built from a 64-bit seed. Every vertex v gets an integer
potential p[v] drawn uniformly in [-P, P]. Then m directed edges are drawn with a
counter-based hash (SplitMix64 mix64): draw i gives endpoints u, v and a
non-negative base r in [0, R], and the edge u -> v gets weight
w(u, v) = r + p[v] - p[u].
Self-loops are dropped; parallel edges are kept. Because every directed cycle telescopes to
sum of r >= 0, the graph has no negative cycle even though individual edges can be
negative.
Johnson's algorithm:
- Potentials
h[v]: Bellman-Ford from a virtual source with 0-weight edges to all vertices (equivalently, allhstart at 0 and are relaxed over the real edges to convergence).h[v] = min(0, min over paths into v). - Reweight every edge
w'(u,v) = w(u,v) + h[u] - h[v] >= 0. - Per-source Dijkstra on the non-negative graph gives
d'(s, .), and the true distance is recovered asd(s, t) = d'(s, t) + h[t] - h[s].
Over all ordered pairs (s, t) with s != t and t reachable from s, the program
reports:
reachpairs= number of such pairs,sumd= sum ofd(s, t)taken mod2^64(two's-complement wrap; printed unsigned),mind,maxd= minimum and maximum finite distance (signed;0/0if no reachable pair),apspdigest=sum over reachable pairs of mix64((s<<20) ^ t ^ (d(s,t) * golden))(order independent),samples=K = 6sampled entriesd(s_j, t_j)for pairs chosen from the seed; each is the exact signed distance, orxift_jis unreachable froms_j.
Input format
G
n_1 m_1 P_1 R_1 seed_1
...
n_G m_G P_G R_G seed_G
G graphs; graph g has n_g vertices, m_g directed edge draws, potentials in
[-P_g, P_g], bases in [0, R_g].
Output format
One line per graph:
graph <g> n <n> edges <kept_edges> reachpairs <rp> sumd <su> mind <mn> maxd <mx> apspdigest <dg> samples <s0> <s1> ... <s5>
Hidden inputs
Correctness inputs (8 files) include n = 1, n = 2, tiny graphs cross-checked against
Floyd-Warshall, P = 0 (non-negative weights), R = 0 (zero-sum cycles), a very sparse
graph (mostly unreachable), and graphs up to 1500 vertices. The timing input has 110 graphs
with n = 2560, m = 20480, P = 1000, R = 100. Ranges used by the grader:
1 <= n <= 2560, 0 <= m <= ~8n, 0 <= P <= 1000, 0 <= R <= 100, seeds arbitrary 63-bit.
Distances fit in 64-bit signed integers.
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: Four directed graphs with 512 vertices and 4096 edge draws each; compute shortest paths from every source.
/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.