Deterministic-annealing EM for a weighted diagonal Gaussian mixture with background
/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
Deterministic-annealing EM for a mixture of K axis-aligned (diagonal) Gaussians in D
dimensions plus a fixed uniform background component, on N weighted points. Everything is
double precision.
Data. K planted components have means in K distinct cells of a 4^D lattice
(spacing 16, centred at the origin, the cells chosen by sorting cell hashes) plus a jitter
of up to +-3, and standard deviations in [0.5, 2). Point i (with
h = mix64(SEED + 0x5555*(i+1))) belongs to planted component h % K, has weight
w_i in [0.5, 1.5) and Box-Muller normal coordinates; see the CPU source for the exact
formulas (mix64 is the SplitMix64 finaliser).
Initial guess. Planted means shifted by up to +-1.5, all variances 4, equal weights
pi_k = (1 - PI0)/K, with PI0 = 0.02 the fixed background weight. The background
density is uniform on a box of side 64: log b = log(PI0) - D log 64.
Iteration t = 0..T-1. beta = BETA0 + (1 - BETA0) * t / (T/2) for t < T/2 (integer
T/2), else beta = 1. With l_ik = log pi_k - 1/2 sum_d [log(2 pi var_kd) + (x_id - mu_kd)^2 / var_kd]:
r_ik = exp(beta l_ik) / (exp(beta log b) + sum_j exp(beta l_ij)) (tempered responsibilities)
N_k = sum_i w_i r_ik, S1_kd = sum_i w_i r_ik x_id, S2_kd = sum_i w_i r_ik x_id^2
pi_k = (1 - PI0) (N_k + 1) / (Wsum (1 - PI0) + K) (Wsum = sum_i w_i)
mu_kd = S1_kd / (N_k + 1)
var_kd = (S2_kd - 2 mu_kd S1_kd + mu_kd^2 N_k + 1) / (N_k + 2)
When beta == 1 the iteration also reports the log-likelihood (under the parameters used
in that E-step) sum_i w_i log(b + sum_k exp(l_ik)).
Input format
One line: N D K T BETA0 SEED (BETA0 decimal, SEED unsigned 64-bit).
Output format
points N dims D comps K iters T
weight_sum Wsum
iter t loglik LL (for every t with beta == 1: t >= T/2, or all t if BETA0 = 1)
comp k pi P mu M_1 .. M_D var V_1 .. V_D (K lines, final parameters)
Values are printed with %.12e and compared numerically with rtol = 1e-6, atol = 1e-9.
The problem is well conditioned: a correct FP64 port with any summation order or FMA
contraction differs by ~1e-12, and computing the E-step (distances, exponentials) in FP32
while accumulating the statistics in FP64 differs by ~1e-8, which the tolerance allows.
FP32 accumulation of the statistics over millions of points is not accurate enough.
Hidden inputs
0 <= N <= 7.5e6,D in {3, 4},1 <= K <= 32,0 <= T <= 44,0.2 <= BETA0 <= 1.- Correctness inputs include
N = 0, 1, 5,Naround multiples of 2048,K = 1,K = 32,T = 0, 1, 2,BETA0 = 1, and problems with up to 80000 points. - The timing input has the shape and size of
/app/inputs/timing_example.in.
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 have the same tokens as the CPU program's output. Numeric tokens may differ by at most
atol + rtol*|expected|with rtol = 1e-06 and atol = 1e-09; every other token must be identical. 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:
/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.