Port an OpenMP simulation of thousands of independent single-lane ring roads (cellular traffic model with brake lights, anticipation, slow-to-start, slow zones, counter-based RNG) to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-sim-ring-brakelight-trafficharbor download elfsong/cpu2gpu-sim-ring-brakelight-traffic/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.
R independent single-lane ring roads of L cells each are simulated for STEPS
synchronous time steps. A cell is empty or holds one car with a speed 0..15 and a
brake-light flag. mix64 is the SplitMix64 finaliser and
crng(seed, a, b, c) = mix64(seed ^ mix64(a*0x9E3779B97F4A7C15 + b*0xC2B2AE3D27D4EB4F + c))
(all arithmetic modulo 2^64); see the CPU source for the exact code.
Per ring r: a density dens = DMIN + mix64(SEED ^ (r*0xA0761D6478BD642F + 1)) mod (DMAX-DMIN+1)
(per mille) and a slow zone of ZLEN cells starting at z0 = mix64(SEED ^ (r*0xE7037ED1A0B428DB + 2)) mod L
(wrapping). Initially cell i gets h = crng(SEED, r, 0xFFFFFFFF, i); it holds a car iff
h mod 1000 < dens, with speed (h >> 32) mod (VMAX+1) and brake light off.
In step t (1-based), every car at cell i with speed v, looking only at the state at the
start of the step:
i+1, i+2, ... (wrapping) up to VMAX+1 cells ahead for the next car.
If one is found at distance d, the gap is g = d-1 and pb is that car's brake light;
otherwise g = VMAX+1, pb = false (on short rings the scan can reach the car itself);lim = VZ inside the slow zone, else VMAX; v1 = min(v+1, lim);pb and g <= v: v1 = min(v1, max(v-1, 0));v2 = min(v1, g);p = P0 if v == 0, else PB if pb, else PD: if crng(SEED, r, t, i) mod 1000 < p
and v2 > 0, then v2 = v2 - 1;(i + v2) mod L with speed v2 and brake light v2 < v.
Passing the end of the ring (i + v2 >= L) counts one unit of flux.One line: R L VMAX VZ ZLEN DMIN DMAX P0 PB PD STEPS EVERY SEED.
rings R length L steps STEPS
step S: flux F moving M stopped Z braking B (for each S that is a multiple of EVERY, and S = STEPS)
cars C
ring flux min A max B hash H
state hash G
F: flux over all rings during the steps since the previous step line; M, Z, B: cars with
speed > 0, cars with speed 0, cars with brake light on, after step S.C: total cars at the end. A, B: min and max over rings of the ring's total flux f_r;
H = sum_r mix64((r << 32) ^ f_r). G = sum over rings and cells of mix64((r*L + i)*256 + code),
where code is 0 for an empty cell and 0x80 | speed | (brake ? 0x10 : 0) otherwise. Sums modulo 2^64.1 <= R <= 5000, 1 <= L <= 100000, R*L <= 2*10^7, 3 <= VMAX <= 15, 1 <= VZ <= VMAX,
0 <= ZLEN <= L, 0 <= DMIN <= DMAX <= 1000, 0 <= P0, PB, PD <= 1000, 1 <= STEPS <= 4000,
1 <= EVERY, at most about 50 step lines.VMAX, empty and completely
jammed roads, probabilities 0 and 1000, random small batches, and one ring of 100000 cells.R in 3500..4500, L in 1800..2300, VMAX 5..9, density between 8% and 50%,
4000 steps, EVERY = 100.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.
/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.nvcc works 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.reward = min(2, t_reference / t_yours) using the medians./app/inputs/ holds a few public inputs. /app/bin/run_public.sh compiles your port with the
flags above and compares it against the CPU baseline on each of them.