Fixed-point rarefied-gas DSMC with per-cell stochastic collisions
/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
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 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: 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. 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.