All-pairs shortest paths with negative arcs by blocked Floyd-Warshall
/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
All-pairs shortest path distances in one dense, clustered directed graph whose arc weights can
be negative, followed by summaries of the full distance matrix. The CPU program uses a blocked
Floyd-Warshall on int32 distances (64 x 64 tiles: diagonal tile, then its row and column of
tiles, then all other tiles as a min-plus product). Any method that produces the same distances
is fine.
Graph. Vertices are 0 .. N-1. With mix64 the SplitMix64 finaliser of the CPU source and
sA = mix64(SEED+1), sB = mix64(SEED+2), sC = mix64(SEED+3), sD = mix64(SEED+4)
(all arithmetic mod 2^64):
- cluster of
u:cl(u) = mix64(sB ^ u) mod C; - potential of
u:pot(u) = mix64(sD ^ u) mod (PRANGE + 1); - cluster
amay send arcs to clusterbwhena == b, or whenmix64(sC ^ (a << 32 | b)) mod 1000 < ARC_PM; - for
u != vleth = mix64(sA ^ (u << 32 | v))andr = h mod 1024. Ifcl(u) == cl(v)the arcu -> vexists whenr < P_INand has costc = 1000 + (h >> 10) mod 1000. Otherwise it exists when the clusters may send arcs andr < P_OUT, with costc = 3000 + (h >> 10) mod 2000. The arc weight isc + pot(u) - pot(v).
All costs are positive, so every cycle has positive weight (the potentials cancel around a
cycle) even though about half of all arc weights can be negative when PRANGE is large.
d(u, u) = 0; d(u, v) is the minimum weight of a directed path from u to v, or
"unreachable" when there is none. Reachable distances satisfy
|d| <= (N-1)*5000 + 2^20 < 2^28, so int32 with a large "no arc" value (the CPU uses 2^29
and treats any final value >= 2^28 as unreachable) is exact.
Input format
One line: N SEED C P_IN P_OUT ARC_PM PRANGE with 1 <= N <= 32768, SEED unsigned 64-bit,
1 <= C <= 1024, 0 <= P_IN, P_OUT <= 1024, 0 <= ARC_PM <= 1000, 0 <= PRANGE <= 2^20.
Output format
n N clusters C
unreachable U negative NEG
diameter D at I J (or: diameter none)
most_negative V at I J (or: most_negative none)
distance_sum S
rowsum_hash H
matrix_hash M
row r sum s (4 lines, r = 0, N/3, 2N/3, N-1 with integer division)
U: ordered pairs(i, j)with no path.NEG: reachable pairs withd < 0.diameter: the largest reachabled(i, j)withi != j; ties go to the smallesti, then the smallestj;nonewhen no pairi != jis reachable.most_negative: the smallestd(i, j) < 0with the same tie rule;nonewhen no distance is negative.rowsum_i= sum of the reachabled(i, j)overj(signed 64-bit);S= sum of all row sums.H= sum overiofmix64(rowsum_i ^ (i << 40)) * (2i + 1)mod 2^64 (row sum taken as its two's-complement 64-bit pattern).M= sum over all(i, j)ofmix64((i * 0x100000001B3 + j) ^ (code << 32))mod 2^64 wherecodeisd(i, j)as a 32-bit two's-complement pattern, or0x7FFFFFFFwhen unreachable.
Hex values are 16 lowercase digits.
Hidden inputs
Correctness inputs (16): N = 1, 2, 33, 65, 96, 127, 200, 257, 500, 1056, 2048 with: a graph
without arcs, a complete digraph without potentials, a single cluster, 128 tiny clusters with a
sparse cluster graph, a very sparse graph with long chains (P_IN = 25, P_OUT = 4),
clusters that cannot reach each other (ARC_PM = 0), C = 1024, and PRANGE from 0 to
2^20; plus five random graphs with 2 <= N <= 1300 and any parameter values in range.
The timing input has N from 12,500 to 13,800, C 6-12, P_IN 700-1000, P_OUT 80-200,
ARC_PM 400-800 and PRANGE from 2^18 to 2^20 (roughly 9-29% of all arcs present,
about half of all distances negative).
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. - This sandbox has no GPU.
nvccworks here for compiling. To run on the GPU, use/app/bin/gpu_run [--repeat N] [--quiet] SOURCE.cu INPUT_FILE...: it compiles the file with the flags above on a remote H100 worker, runs it on each input, and prints stdout, exit code and wall-clock time. Grading also runs your code on an H100 worker, one job per GPU at a time. 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: One graph with 12,500 to 13,800 vertices (a dense int32 distance matrix of about 0.7 GB), 6-12 clusters, roughly 9-29% of all arcs present and potentials up to 2^20, so about half of all distances are negative. 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.