Marching-tetrahedra isosurfaces of an integer field with shared vertices and exact volume
/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 integer scalar field f is generated on the (n+1)^3 points of a cubic grid, and for each of
m isovalues c the program extracts the surface f = c with marching tetrahedra, as an
indexed triangle mesh with shared vertices, and reports counts, the exact enclosed volume and
hashes of the vertex and index buffers. All arithmetic is integer; the CPU source is the
reference for every detail below.
Field. Grid point (x, y, z), 0 <= x, y, z <= n, has index P = (z*(n+1) + y)*(n+1) + x.
Points on the boundary (x, y or z equal to 0 or n) get f = -2^24. Interior points get
base + sum of octaves + sum of blobs, clamped to [-(2^24-1), 2^24-1]:
base = A0 - floor(A0 * r2 / n^2)withr2 = (2x-n)^2 + (2y-n)^2 + (2z-n)^2.- Octave
o(spacingS, amplitudeA): lattice valuesg(lx,ly,lz) = (mix64(seed ^ mix64(idx + 0xA0761D6478BD642F)) mod (2A+1)) - Awithidx = o<<48 | lz<<32 | ly<<16 | lx, combined by integer trilinear interpolation:floor( sum over the 8 corners of g(x/S + a, y/S + b, z/S + c) * wx_a * wy_b * wz_c / S^3 )wherewx_0 = S - x%S,wx_1 = x%S(same for y, z) and/is integer division. - Blob
t(centre, radiusr, amplitudeaderived frommix64(seed ^ (0xB10B000000000000 + 3t + k)),k = 0, 1, 2, see the source) addsfloor(a * q^2 / r^4)withq = r^2 - |p - centre|^2when every coordinate difference is belowrin absolute value andq > 0.
floor is floor division (towards minus infinity). mix64 is the SplitMix64 finaliser.
Tetrahedra. Every cube (x, y, z), 0 <= x, y, z < n, is split into 6 tetrahedra along its
main diagonal (Kuhn/Freudenthal split): for each axis permutation (i, j, k) in the order
xyz, xzy, yxz, yzx, zxy, zyx the tetrahedron is 0, e_i, e_i+e_j, (1,1,1) (cube-relative
corners). The split is conforming, so every tetrahedron edge goes from a grid point P to
P + d with d one of the 7 directions
0:(1,0,0) 1:(0,1,0) 2:(0,0,1) 3:(1,1,0) 4:(1,0,1) 5:(0,1,1) 6:(1,1,1); its key is 7P + dir.
Vertices. A point is inside when f > c (strict). An edge is crossed when exactly one end
is inside. Each crossed edge is one mesh vertex; its id is the rank of its key among all crossed
edges (0-based, increasing key). Its position is fixed point with 1024 subdivisions per cell:
with u = f(P), w = f(P+d), num = c - u, den = w - u (both negated if den < 0),
t = floor((2*num*1024 + den) / (2*den)) in [0, 1024], and position 1024*P + t*d.
Triangles. Each tetrahedron with 1 or 3 inside corners emits one triangle on its 3 crossed
edges; with 2 inside corners a < b (outside c < d, local vertex order) it emits the quad
E(a,c), E(a,d), E(b,d), E(b,c) (or its reverse) as two triangles split along the diagonal
E(a,c)-E(b,d). Triangles are oriented so that their normal points from the inside to the
outside (the case tables are built in build_tables()); the mesh is closed and consistently
oriented. Triangles are numbered (0-based) in the order cube index (x fastest, then y, then z),
tetrahedron 0..5, triangle 0..1 within the tetrahedron. Each triangle's vertex ids are rotated
cyclically so that the smallest id comes first (orientation kept).
Per isovalue output.
verticesV (crossed edges),trianglesT,active(cubes with at least one triangle).vol6:sum over triangles of det(p0, p1, p2)(positions in fixed-point units, exact, 128-bit), i.e. 6 * 1024^3 * the enclosed volume.vhash:sum over vertices of mix64(mix64(id ^ 0x5851F42D4C957F2D) ^ (px<<42 | py<<21 | pz)).thash:sum over triangles of h,h = mix64(v2 + 0x632BE59BD9B4E019),h = mix64(h ^ (v0<<32 | v1)),h = mix64(h ^ triangle_id)with(v0, v1, v2)the rotated ids.- Sums are modulo 2^64.
Input format
n SEED
base A0
octaves K S_1 A_1 ... S_K A_K
blobs B AB
isovalues m c_1 ... c_m
The words base, octaves, blobs, isovalues are literal. All values are decimal integers.
Output format
grid n points (n+1)^3 fieldsum SUM positive POS
iso c vertices V triangles T active A vol6 VOL6 vhash HV thash HT (one line per isovalue, input order)
SUM is the sum of f over all grid points (signed 64-bit) and POS the number of points with
f > 0. VOL6 is a signed decimal integer that may exceed 64 bits.
Hidden inputs
1 <= n <= 700;0 <= A0 <= 2^21;0 <= K <= 4,1 <= S <= 255,0 <= A <= 2^21;0 <= B <= 32,0 <= AB <= 2^21;1 <= m <= 64,-2^24 <= c <= 2^24 - 1; any 64-bit seed.- With these bounds every intermediate fits in signed 64-bit arithmetic except the volume sum, vertex ids fit in 32 bits, and V, T stay below 2^32.
- Correctness inputs include
n = 1andn = 2, fields with many ties (f = cexactly), isovalues at and beyond the field range (-2^24,2^24 - 1, empty or full surfaces), saturated (clamped) fields, and random fields withnup to 150 and up to 8 isovalues. - 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 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:
/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.