Fixed-point rarefied-gas DSMC with per-cell stochastic collisions
/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
The program is a fixed-point Direct Simulation Monte Carlo (DSMC) rarefied gas. N
particles live in a periodic cube of side 2^32 (positions are uint32, so motion wraps
naturally) with signed integer velocity components. Initial state comes from a SplitMix64
hash of the particle index: about half the particles are placed uniformly, the rest inside
one of 16 compact blobs (so cell occupancy is very uneven); each velocity component is
uniform in [-VMAX, VMAX].
Each of T steps does:
- Move:
pos += velper component (wrappinguint32). - Bin: particles are counting-sorted into a
G x G x Gcell grid (G = 2^LOGG, cell index((x>>CS)*G + (y>>CS))*G + (z>>CS)withCS = 32-LOGG). Within a cell, particles are ordered by ascending particle index (a stable sort). - Collide: for a cell with
nparticles the program runsncand = min( n*(n-1)/(2*CDIV), n*CAPC )candidate pairs (at least 1 whenn >= 2). For candidatepa counter-based hash keyed by(seed, step, cell, p)picks two distinct in-cell slots(a, b), then with probabilitymin(g2, GMAX2)/GMAX2(whereg2is the squared relative speed) the pair collides: letS = v_a + v_b,D = v_a - v_b; three random sign bits giveD' = (±D_x, ±D_y, ±D_z); the new velocities arev_a = (S+D')/2,v_b = (S-D')/2. SinceD'keeps each component's parity, the halving is exact, and the reflection conserves the pair's momentum and kinetic energy exactly. Collisions inside a cell are applied in sequence (later pairs see earlier updates); different cells are independent.
Per printed step the program reports the total candidate count and the total accepted
collisions. Global kinetic energy sum v^2 and global momentum sum v are exact
invariants (they never change). After T steps it reports a 16-bucket histogram of
per-particle squared speed, and for a 4 x 4 x 4 super-cell grid the particle count and
summed squared speed per super-cell, plus a hash of all final positions and velocities.
Input format
One simulation on a single line:
N LOGG T EVERY CDIV CAPC GMAX2 VMAX seed
N >= 1 particles; LOGG in [1, 7] (G = 2^LOGG cells per axis); T >= 0 steps,
printing every EVERY steps (and on the last step); CDIV >= 1; CAPC >= 1; GMAX2 >= 1;
VMAX >= 1; seed is an unsigned 64-bit integer.
Output format
dsmc N <N> grid <G>^3 steps <T>
step <t> cand <C> acc <A> (for each printed step)
ke <KE> mom <MX> <MY> <MZ>
hist <h0> <h1> ... <h15>
sc <s> <count_s> <ke_s> (64 lines, s = 0..63)
hash <H>
All counts, energies and hashes are non-negative 64-bit integers (sums modulo 2^64); momentum components are signed.
Hidden inputs
Correctness inputs cover N = 1 (no collisions), N = 2, T = 0, a single-cell-heavy
configuration (LOGG = 1), GMAX2 = 1 (nearly all pairs accepted) and very large GMAX2
(few accepted), small VMAX, and random configurations with N in [2000, 30000],
LOGG in [2, 6], T in [1, 14], CDIV in [1, 12], CAPC in [8, 48]. The timing
input uses N = 2,000,000, LOGG = 6, T about 180, CDIV = 12, CAPC = 48,
GMAX2 = VMAX^2, VMAX = 2^18.
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 be identical to the CPU program's output (trailing whitespace ignored). 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: The timing input is one simulation of N = 2,000,000 particles in a 64^3 cell grid run for about 180 steps (printing every ~9 steps); clustering into 16 blobs makes a handful of cells very dense, so collision work is strongly imbalanced across cells.
/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.