LINCS-constrained molecular dynamics of a batch of flexible molecules
/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 short molecular-dynamics run of NMOL independent molecules (there are no forces between
molecules). Bond lengths are rigid and are held by LINCS; angles and dihedrals feel
harmonic-cosine potentials; the integrator is velocity Verlet. Everything is FP64.
Molecules. Molecule m is generated from SEED and m alone (build in the source; it is
pure integer logic plus a few correctly rounded FP64 operations and must be reproduced
exactly). Its size class and atom count come from a hash: 55% have 10-30 atoms (small), 30%
have 31-80 (medium), 15% have 81-200 (large). Atoms sit on a lattice of spacing 1/4 and
are grown as a random tree: each new atom bonds to one of the three previous atoms (at most
three bonds per atom; when no vector fits there, to the most recent atom that can take one)
along one of 158 lattice vectors of squared length 9-14, chosen so that every bond angle at
the parent is between about 100 and 125 degrees. While growing, an atom
closes a 5- or 6-membered ring when a vector exists that also puts it at bond distance from its
3rd or 4th ancestor with bond angles in range at both ends. After growth, the first two leaf
children of an atom are joined by an extra angle-fixing constraint with probability 1/2 (a
rigid triangle). Masses are 1, 1.5, 2 or 2.5; initial velocities are random with temperature
TEMP. Constraint lengths are the initial distances, so the start is exactly constrained.
Potentials. Every pair of bonds (tree bonds and ring closures) meeting at an atom, except a
pair fixed by a constraint, has E = ka (cos(theta) - c0)^2 with ka in {300, 450, 600}; c0
is the cosine of the initial angle, computed from the integer lattice vectors u, v as
(u.v) / sqrt(|u|^2 |v|^2). Every atom at depth >= 3 has a dihedral term along its path to the
root, E = kd (cos(phi) - c1)^2 with kd in {1, 2, 3}; c1 is the cosine of the initial
dihedral, computed the same way from the integer cross products A = b1 x b2, B = b2 x b3.
The run therefore starts at the minimum of every potential. A dihedral term is skipped when
|A|^2 or |B|^2 is below 1e-12 (collinear bonds).
LINCS. For constraints c = (a, b) with length d_c, S_c = 1/sqrt(1/m_a + 1/m_b) and unit
directions r_c taken from reference positions, the coupling matrix is
A_ck = coef_ck (r_c . r_k) for constraints sharing an atom u, with
coef_ck = -/+ (1/m_u) S_c S_k (minus when u is the same end of both). With rhs set:
sol = rhs; repeat ORDER times: rhs = A rhs; sol += rhs
y_u += (1/m_u) * sum over constraints c at u of (+1 if u = b_c, -1 if u = a_c) S_c sol_c r_c
Positions: directions from the old positions, rhs_c = S_c (r_c . (xn_a - xn_b) - d_c), update,
then NITER corrections with p = sqrt(max(2 d_c^2 - |xn_a - xn_b|^2, 0)),
rhs_c = S_c (d_c - p), update. Velocities: directions from the current positions,
rhs_c = S_c r_c . (v_a - v_b), one expansion, update.
Step 0: project the initial velocities, compute forces. Each step:
v += dt/2 F/m; xn = x + dt v; LINCS(positions); v = (xn - x)/dt; x = xn; forces;
v += dt/2 F/m; LINCS(velocities). The exact operation order is in cpu.cpp.
Input format
One line:
NMOL STEPS DT ORDER NITER TEMP SEED
1 <= NMOL <= 600000, 0 <= STEPS <= 120, 0 < DT <= 0.01 (decimal), 1 <= ORDER <= 12,
0 <= NITER <= 4, 0 < TEMP <= 4 (decimal), SEED an unsigned 64-bit integer.
Output format
molecules NMOL atoms NA constraints NC couplings NCP
topology rings NR angle_constraints NAC angles NANG dihedrals NDIH
class C molecules K atoms A rg_start R0 rg_end R1 (C = small, medium, large)
step s ke KE pe_angle PA pe_dihedral PD maxdev MD rmsdev RD (s = 0..STEPS)
probe m atoms n constraints nc rg R ke KE pe PE end X Y Z (m = 0, NMOL/3, 2NMOL/3, NMOL-1, distinct)
NCP counts coupling entries (each constraint lists every other constraint at either end).
rg_start/rg_end are the class means of the mass-weighted radius of gyration before step 1
and after the last step. KE, PA, PD are totals over all molecules after step s;
MD and RD are the maximum and the root mean square of |l_c - d_c| / d_c over all
constraints. A probe line gives that molecule's final radius of gyration, kinetic and
potential energy and the position of its last atom. Numbers are printed with %.12e and
compared with rtol = 1e-8, atol = 1e-10. Rebuilding the CPU program without FMA, with
reversed force accumulation or with a scatter instead of a gather update moves every number by
less than 1/1000 of this tolerance (the runs are short, so rounding differences grow little);
an FP32 LINCS, a different expansion order or a missing correction does not pass.
Hidden inputs
- Correctness inputs: one molecule with
STEPS = 0; one molecule withORDER = 1,NITER = 0; 7 molecules withORDER = 12,NITER = 4; 50 molecules for 120 steps; and 300-5000 molecules withDTin[0.002, 0.005],ORDERin[4, 8],NITERin[1, 2],TEMPin[0.5, 1.5]and 20-80 steps. - The timing input has
NMOLin[430000, 470000],STEPS = 50,DTin[0.004, 0.005],ORDER = 6,NITER = 1,TEMPin[0.8, 1.2]. - Every molecule has at most 200 atoms, 300 constraints and 3 constraints per atom; global counts stay below 2^31.
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. - Grading runs on one H100 in a fresh container without network. 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 430000-470000 molecules of 10-200 atoms (about 2.2e7 atoms, 2.2e7 constraints and 5.6e7 coupling entries) and runs 50 steps with expansion order 6 and one correction iteration. 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.