Johnson all-pairs shortest paths with negative edges
/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.
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 CPU source is the specification: when this text and the code disagree, the code wins.
How you are scored
- 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. - Grading runs on one H100 in a fresh container without network. Inputs are generated with a new random seed at grading time, so they cannot be known in advance.
- Correctness: every hidden input (small edge cases plus the timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout gives reward 0.
- Speed: Four directed graphs with 512 vertices and 4096 edge draws each; compute shortest paths from every source. Your port and our GPU reference are each run on it
once to warm up and then 5 times, interleaved; the time is the wall clock of the whole process
(including CUDA initialisation, input parsing and output). When correct,
reward = min(2, t_reference / t_yours)using the medians. /app/inputs/holds a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.