Clock-type spin model with diagonal couplings, 4-colour Metropolis
/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
Metropolis Monte Carlo of a q-state spin model (2 <= q <= 8, spins 0..q-1) on an
L x L periodic lattice (L even). The energy is
H = sum over horizontal and vertical neighbour pairs J1[b(s_i, s_j)]
+ sum over both diagonal neighbour pairs J2[b(s_i, s_j)]
+ h * (number of sites with spin 0)
with b(a, c) = min(d, q - d), d = (a - c) mod q. All couplings are integers.
Random numbers come from the SplitMix64 finaliser mix64 (see the CPU source) with
base(seed, stream) = mix64(seed XOR stream * 0xD1B54A32D192ED03) and the value for index
i in a stream being mix64(base + i * 0x9E3779B97F4A7C15) (64-bit wraparound).
- Initial state:
s_i = value(stream 0, i) mod q, site indexi = y*L + x. - Sweep
sw(0-based) visits the 4 coloursc = 0..3; colourcis the set of sites withx mod 2 = c & 1andy mod 2 = c >> 1. Sites of one colour do not interact, so each colour is updated as a whole using stream1 + 4*sw + c. For siteiwith randomr: proposals' = (s + 1 + (r mod 2^32) mod (q-1)) mod q;dEis the integer energy change; accept whendE <= 0or(r >> 32) < thr[dE], wherethr[1] = wandthr[d] = floor(thr[d-1] * w / 2^32)(64-bit unsigned). - After every sweep with
(sw + 1) mod measure == 0the total energyEand the number of zero spinsZare measured.
Input format
R
seed L q w h sweeps measure J1[0] .. J1[q/2] J2[0] .. J2[q/2] (R lines)
Output format
One line per run:
run E_SUM E2_SUM Z_SUM ACCEPTED HASH H_0 .. H_{q-1}
E_SUM is the signed sum of the measured energies, E2_SUM the sum of E*E modulo 2^64,
Z_SUM the sum of the measured zero counts, ACCEPTED the total number of accepted moves,
HASH = sum_i mix64(8*i + s_i) mod 2^64 over the final state and H_k the final number of
spins equal to k.
Hidden inputs
1 <= R <= 5,2 <= L <= 2048(even),2 <= q <= 8,1 <= w <= 2^32 - 1,-8 <= h <= 8,-8 <= J1[d], J2[d] <= 8,0 <= sweeps <= 200in correctness inputs,1 <= measure <= 50.- Edge cases:
L = 2(neighbours coincide),q = 2,sweeps = 0,w = 1andw = 2^32-1, frustrated diagonal couplings, one run withL = 2048. - The timing input has 4 runs with
768 <= L <= 1536, about 4.5e9 site updates each, and5 <= measure <= 20.
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 has 4 runs on L x L lattices with L between 768 and 1536 and about 4.5e9 site updates per run, measuring the energy every 5 to 20 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.