2D shallow-water MUSCL-Hancock / HLLC solver with hydrostatic reconstruction, Manning friction and a global CFL step
/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
A finite-volume simulation of the 2D shallow-water equations (depth h, discharges hu, hv, gravity
g = 9.81) over a static random bed z on an NX x NY grid of unit cells (cell (i, j) stored at
j*NX + i, centre (i + 0.5, j + 0.5)):
h_t + (hu)_x + (hv)_y = 0
(hu)_t + (hu^2 + g h^2/2)_x + (huv)_y = -g h z_x - friction
(hv)_t + (huv)_x + (hv^2 + g h^2/2)_y = -g h z_y - friction
- Initial state (all parameters from
SEEDexactly as in the CPU source; the cosines come from 1D tables overiandjevaluated with the host libm): a bed made of six separable cosine modes and four rational bumps (|z| <= 0.9), a still-water leveleta0in[2, 2.4]with a small swell,NDAMdam-break columns that raise the surface inside integer discs(i-cx)^2 + (j-cy)^2 <= r^2(heights add up), a weak smooth initial current and a smooth Manning coefficient field. Every cell stays wet (hstays well above zero), so there is no wet/dry logic. - Boundaries. Reflecting walls at the east, south and north sides (mirror ghost states). The west
side is an inflow: the ghost state has the inner depth, normal velocity
QP[j] * m(t) / hand zero tangential velocity, withQP[j] = QIN * (0.75 + 0.25 cos(...))andm(t) = 1 + 0.3 sin(w t). - One time step (
STEPSof them, no fixed end time):- Primitive velocities and the global wave-speed maximum
smax = max(max(|u|, |v|) + sqrt(g h))over all cells;dt = CFL / smax. This reduction gates the step. - MUSCL-Hancock predictor per cell: slopes of
h,eta = h + z,uandvin x and in y with the generalised minmod limiterminmod(THETA*a, (a+b)/2, THETA*b); face values (bed at a face iseta_face - h_face); a half-step increment from the physical fluxes at the four face points plus the bed source, added to all four face states (conservative variables, then back tou,v). - For every x face and every y face: hydrostatic reconstruction (Audusse:
z* = max(zL, zR),h*L = max(0, hL + zL - z*), ...), HLLC flux of the reconstructed states (Toro's two-rarefaction wave-speed estimates, contact speedS*, tangential momentum upwinded onS*) and the two pressure correctionsg/2 (h^2 - h*^2)for the cells on either side. The inflow ghost usesm(t + dt/2). - Conservative update with the centred bed source, then semi-implicit Manning friction:
(hu, hv) /= 1 + dt * g n^2 * |velocity| / h^(4/3)withh^(4/3) = h * cbrt(h).
- Primitive velocities and the global wave-speed maximum
All arithmetic is FP64. The limiter, HLLC branches and max operations are continuous, so the
solution depends smoothly on rounding: ports may use other loop orders, fused kernels, other
reduction orders and FMA contraction, as long as the printed numbers agree within the tolerance.
Input format
One line: NX NY STEPS CFL THETA NDAM QIN SEED (NX, NY >= 1, NX*NY <= 2^26, STEPS >= 0,
0 < CFL <= 0.5, 1 <= THETA <= 2, 0 <= NDAM <= 64, 0 <= QIN <= 10, SEED unsigned 64-bit).
Output format
check K step S t T dt D mass M momx X momy Y energy E smax C hmin H (8 lines, K = 1..8)
blocks BX BY
BY lines of BX values: block means of h
BY lines of BX values: block means of hu
BY lines of BX values: block means of hv
probe I J h hu hv (8 lines)
- Checkpoint
Kis taken after stepceil(K * STEPS / 8)(all at step 0 witht = dt = 0whenSTEPS = 0): time, the lastdt, the cell means ofh,hu,hvand of the energy(hu^2 + hv^2)/(2h) + g h (h/2 + z), the current wave-speed maximum and the minimum depth. BX = min(8, NX),BY = min(8, NY); block(bx, by)coversi in [bx*NX/BX, (bx+1)*NX/BX),j in [by*NY/BY, (by+1)*NY/BY).- Probe cells are derived from the seed.
Numbers are printed with %.12e.
Hidden inputs
1 <= NX, NY <= 2112,0 <= STEPS <= 400,0.30 <= CFL <= 0.45,1 <= THETA <= 2,0 <= NDAM <= 64,0 <= QIN <= 3.- Correctness inputs include
1 x 1,1 x NandN x 1grids, a2 x 2grid without dams or inflow,STEPS = 0,THETA = 1andTHETA = 2, 64 dam breaks, a512 x 512grid with 5 steps and a128 x 128grid with 400 steps. - The timing input has
NX, NYin[1984, 2112], 16-32 dam breaks,CFLin[0.35, 0.45],THETAin[1.2, 2]and 340-400 steps.
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 have the same tokens as the CPU program's output. Numeric tokens may differ by at most
atol + rtol*|expected|with rtol = 1e-08 and atol = 1e-10; every other token must be identical. Any mismatch, crash or timeout gives reward 0. - Speed: The timing input has an NX x NY grid with NX, NY in 1984-2112, 16-32 dam breaks and 340-400 time steps (one global CFL reduction per step). 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.