4D-Var data assimilation with a checkpointed discrete adjoint
/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
An observing-system simulation experiment (OSSE) with strong-constraint 4D-Var data assimilation for a two-species reactive tracer model, solved one problem after another.
Model. Species u, v on a doubly periodic NX x NY grid (cell c = j*NX + i). Face
winds U, V come from a generated stream function on cell corners (discretely divergence
free), modulated in time by s(n); centred flux-form advection, 5-point diffusion (ku,
kv), invented photochemistry (+a1 v - a2 u v - a3 u for u, -a1 v - a4 v^2 for v) and
emissions Eu, Ev scaled by a diurnal factor d(n). One time step is Heun's method
(explicit RK2): xs = x + dt F(x, n), x <- x + dt/2 (F(x, n) + F(xs, n+1)). All fields,
rate constants and the initial truth xt / background xb are generated from the seed with
SplitMix64-style hashing and smooth analytic functions (see the CPU source).
Observations. NST fixed stations observe u by bilinear interpolation; a moving satellite
swath observes 4x4-cell averages of v (footprints on a shifted 8-cell lattice, inside a
band that moves with time). Both are taken every OBSINT steps (state after the step) from a
truth run started at xt, plus Gaussian noise (SIG_ST = 0.02, SIG_SAT = 0.01) drawn from
hashes.
Cost. J(x0) = 1/2 |A (x0 - xb)|^2 / SIG_B^2 + 1/2 sum_obs (H(x_n) - y)^2 / sigma^2,
A = I - ELL2 * Laplacian (periodic 5-point), SIG_B = 0.1, ELL2 = 1, where x_n is the
model trajectory from x0.
Gradient. Exact discrete adjoint of the whole forward computation: a forward run storing
K = ceil(NSTEP / C) checkpoints (C = ceil(sqrt(NSTEP))), then a backward sweep over the
segments in reverse order; each segment is recomputed from its checkpoint, and every Heun step
is reversed with the transposed tendency Jacobian (written in the CPU source as the scatter an
AD tool would generate; rows processed in two colours of 4-row blocks so the order of
accumulation is fixed). Observation misfits add their forcing to the adjoint state at their
times; the background term adds A^T A (x0 - xb) / SIG_B^2.
Mission. Starting from x = xb, NOUT outer iterations of steepest descent: probe
J(x - tau g/|g|) (first tau = 0.25 |xb - xt|, afterwards the previous step), fit a parabola
through J(x), the slope -|g| and the probe, step to its minimum (alpha = |g| / (2 c2), or
tau if the curvature c2 <= 0), re-evaluate cost and gradient there. Reported are the cost
split, the gradient norms, the probe/step lengths, the cost reduction and the RMS error
against the truth of the background and of the analysis at the initial time and after
NSTEP steps (forecast). The iteration stops early if the gradient is exactly zero.
FP64 throughout. All reductions use fixed chunks of 4096 values summed in order, so the CPU output is independent of the thread count.
Input format
NP
NX NY NSTEP OBSINT NST NOUT DT SEED (NP lines)
NX, NY are multiples of 8 (8 <= NX, NY), 0 <= NSTEP, 1 <= OBSINT, 0 <= NST,
0 <= NOUT, DT a decimal with three digits, SEED an unsigned 64-bit integer.
Output format
Per problem:
problem p grid NX NY steps NSTEP obs_times NOBS stations NST footprints NFP
iter 0 J <J> Jb <Jb> Jst <Jst> Jsat <Jsat> gnorm <|g_u|> <|g_v|>
grad0 cell <c> <g_u[c]> <g_v[c]> (4 lines)
iter k probe <tau> <J(probe)> slope <(J-Jprobe)/(tau|g|)> step <alpha> J .. Jb .. Jst .. Jsat .. gnorm .. ..
reduction J_final/J_initial <ratio>
rmse_t0 background <u> <v> analysis <u> <v>
rmse_tN background <u> <v> analysis <u> <v>
Reals use %.10e; they are compared numerically with the task's tolerance (FP64 is
required), integers must match exactly.
Hidden inputs
Correctness inputs (10 files, 1-3 problems each): grids from 8 x 8 to 64 x 64,
0 <= NSTEP <= 160, 1 <= OBSINT <= 50, 0 <= NST <= 60, 0 <= NOUT <= 4,
DT in {0.1, 0.2, 0.25, 0.3, 0.35}; they include NSTEP = 0, no observation time
(OBSINT > NSTEP), no stations, NOUT = 0, observations every step, and NSTEP not a
multiple of the checkpoint interval. The timing input is one problem with NY = 512,
NX in {504, 512, 520}, NSTEP = 1200, OBSINT = 12, 300 <= NST <= 500, NOUT = 4,
DT in {0.25, 0.3}.
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-07 and atol = 1e-10; 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: The timing input is a single 512 x ~512 problem (about 260k cells, two species) with 1200 Heun steps, observations every 12 steps and 4 outer iterations (5 gradient evaluations with checkpointed adjoint sweeps plus 4 line-search forward runs).
/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.