2D shallow-water MUSCL-Hancock / HLLC solver with hydrostatic reconstruction, Manning friction and a global CFL step
/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
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 the shape and size of
/app/inputs/timing_example.in.
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 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 is rejected. - GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed:
/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.