Cellular Potts model with a global per-cell volume constraint
/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 cellular Potts model (CPM) on a periodic L x L lattice (L even).
Every site holds a cell ID in [0, M). Each cell c has a type ctype[c] in [0, 4) and a
target volume Vt[c], both built deterministically from seed. A symmetric 4x4 contact
energy table J (per type pair) is also built from seed. The lattice is initialised as
GB x GB compact square blocks (GB = round(sqrt(M))), and V[c] is the number of sites
with ID c (a global per-cell volume accumulator).
Dynamics are checkerboard Metropolis copy attempts. A sweep has two sub-sweeps, one per
colour (x+y)&1. At the start of each sub-sweep the volumes are frozen into a snapshot
Vf. Then every site t of that colour:
- draws one 64-bit random number
rfrom a counter-based hash keyed by(seed, sweep, colour, t); - picks a von-Neumann neighbour
srcbyr & 3(up/down/left/right, periodic) and proposes copyingid[src]intot. Ifid[src] == id[t]nothing happens; - otherwise computes
dE = (adhesion with new ID) - (adhesion with old ID) + (volume term). Adhesion of a labelwis the sum over the 4 neighboursnofJ[type[w]][type[n]]whenw != id[n](else 0). The volume term, using the frozen volumes, isLAMBDA*(2*(Vf[v]-Vt[v])+1) + LAMBDA*(-2*(Vf[u]-Vt[u])+1)whereu = id[t],v = id[src]; - accepts if
dE <= 0, rejects ifdE > 4096, else accepts when(r >> 32) < thr[dE](thr[0] = 2^32,thr[d] = (thr[d-1]*w) >> 32).
An accepted move writes id[t] = v and updates the global volumes V[u] -= 1, V[v] += 1
(integer, so the order of concurrent updates does not matter). Because decisions use the
frozen snapshot and no two same-colour sites are adjacent, all decisions in a sub-sweep are
independent.
Per printed sweep the program reports the total adhesion energy, the total volume energy
LAMBDA * sum_c (V[c]-Vt[c])^2, and the cumulative accepted-copy count. After all sweeps it
reports a 16-bucket histogram of cell volumes, the total volume per type, and a lattice hash.
Input format
One simulation on a single line:
L M LAMBDA w SWEEPS EVERY seed
L even in [2, 4096]; M >= 1 cells; LAMBDA >= 1; w with 0 < w < 2^32
(= round(2^32 * exp(-1/T))); SWEEPS >= 0 printing every EVERY; seed unsigned 64-bit.
Output format
cpm L <L> cells <M> sweeps <SWEEPS>
sweep <s> eadh <Ea> evol <Ev> acc <cumulative_accepts> (for each printed sweep)
hist <h0> ... <h15>
tvol <t0> <t1> <t2> <t3>
hash <H>
All reported values are non-negative 64-bit integers (sums modulo 2^64).
Hidden inputs
Correctness inputs cover M = 1 (single cell, no moves), tiny lattices, few-big-cells and
many-small-cells, SWEEPS = 0, and random configurations with even L in [8, 220],
M a perfect square up to 256, LAMBDA in [1, 4], w in [3.6e9, 4.2e9], SWEEPS in
[1, 16]. The timing input uses L = 4096, M = 1024, LAMBDA = 2, w = 4.0e9,
SWEEPS about 1360.
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 L = 4096 lattice with M = 1024 cells (so ~16k sites per cell contend on each volume counter) run for about 1360 checkerboard sweeps (two sub-sweeps each), printing every ~68 sweeps.
/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.