Fixed-point embedded-atom-method metal MD with two-pass neighbour gather
/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 embedded-atom-method (EAM) metal molecular dynamics. N
atoms live in a periodic cube of side 2^32 (positions are uint32) with signed integer
velocities. Initial positions are uniform, or (for a fraction of atoms) inside one of K
compact blobs; velocities are small and uniform. Six integer lookup tables are built
deterministically from seed:
- distance-bucket tables
wdens,gw,pw,peof length 1024, indexed byk = d2 >> QSHwhered2is the squared integer separation andQSH = 2*CS - 10(CS = 32 - LOGG); - density-bucket tables
EF(embedding energy) andDF(embedding derivative) of length 1024, indexed bymin(rho >> 12, 1023).
A cutoff cell list (cell size 2^CS, cutoff d2 < 2^(2*CS)) is rebuilt each step. Then each
of T steps does:
- Pass 1 (density): for each atom
i,rho_i = sum over neighbours j of wdens[k]. Map it to the embedding derivativemu_i = DF[min(rho_i>>12, 1023)]. - Pass 2 (force): for each atom
i, walk the neighbours again; for each pair the force scalar isfs = (mu_i + mu_j)*gw[k] + pw[k](so it gathers the neighbour's embedding derivativemu_j). The force issum of fs * (x_i - x_j)per component. Pair energy accumulatespe[k], the virial accumulatesfs * k. - Integrate:
v += force / 2^DS, then dampingv -= v/64, clamp to[-VMAX, VMAX](VMAX = 2^(CS - VSH)), thenx += v(wrapping).
Because (mu_i + mu_j) and the weights are symmetric while (x_i - x_j) is antisymmetric,
the pair forces are exactly antisymmetric, so total momentum is conserved. Per printed step
the program reports total pair energy (sum/2), total embedding energy (sum of EF) and
the virial (sum/2). Energies and virial are signed 64-bit (wrapping). After T steps it
reports a position hash, a velocity hash, total momentum, the summed density, and six
components each of four sampled atoms.
Input format
One simulation on a single line:
N LOGG T EVERY DS VSH K seed
N >= 1; LOGG in [2, 27] (G = 2^LOGG cells per axis); T >= 0 steps printing every
EVERY; DS >= 0 force shift; VSH with 1 <= VSH < CS; K >= 0 blobs; seed unsigned
64-bit.
Output format
eam N <N> grid <G>^3 steps <T>
step <t> epair <Ep> eemb <Ee> virial <W> (for each printed step)
position_hash <hex16> velocity_hash <hex16>
momentum <px> <py> <pz> sumrho <S>
atom <i> x <x> <y> <z> v <vx> <vy> <vz> (4 lines)
Hidden inputs
Correctness inputs cover N = 1 (no neighbours), N = 2, T = 0, K = 0 (no clustering),
clustered systems, finer grids, and random configurations with N in [1500, 12000],
LOGG in [3, 6], T in [1, 8], DS in [24, 30], VSH = 3, K in [0, 16]. The
timing input uses N = 400,000, LOGG = 4, T about 12, DS = 28, VSH = 3, K = 24.
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 = 400,000 atoms in a 16^3 cell grid (so cells hold tens to a few hundred atoms) run for about 12 steps, each doing two full neighbour sweeps; clustering into 24 blobs makes some cells much denser than others.
/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.