LCA, path maximum, ordered path signature and path midpoint on a huge deep tree
/app/cpu/main.cpp is a multicore CPU program (C++17, OpenMP). It is built as
/app/bin/cpu_baseline with g++ -O3 -march=native -fopenmp -std=c++17 and run as
cpu_baseline INPUT_FILE
It reads one input file and writes its result to stdout.
What the program computes
A rooted tree with N nodes is generated, then Q path queries are answered.
Tree. Nodes are created in generation order g = 0..N-1; node g >= 1 attaches to an
older node pg < g chosen from the hash h = mix64(SEED ^ g * 0xD1B54A32D192ED03):
with probability PFAR / 10^6 uniformly among 0..g-1 (a far attachment), otherwise with
probability PSPINE / 1000 to g - 1 (the path continues), otherwise to g - 1 - r with
r uniform in [0, min(g, WIN)). The edge to the parent has a weight in [0, 65535].
Node labels are scrambled: generation index g gets label perm(g), a 4-round Feistel
permutation of [0, 4^hb) with cycle walking (hb smallest with 4^hb >= N). The root is
perm(0). Trees can be very deep (a pure path has depth N - 1).
Queries. Query q draws two generation indices from a hash (LOCAL / 1000 of the
queries pick the second one within +-2048 of the first, the others uniformly) and maps them
to labels u, v. Its answer is
lca: the lowest common ancestorwofuandv;dist: the number of edges on the pathu .. v;max: the largest edge weight on the path (0 whenu = v);sig: the ordered path signaturesum_k wt(e_k) * B^k mod (2^61 - 1)over the path edgese_0, e_1, ...listed fromutov(B = 0x0123456789ABCDE);mid: the vertex at distancefloor(dist / 2)fromualong the path.
The CPU program uses binary lifting tables (ancestor and maximum weight 2^k steps up) and
root-path prefix sums (sum wt B^(-depth) and sum wt B^depth) for the signature.
Input format
One line: N Q PSPINE PFAR WIN LOCAL SEED (SEED is an unsigned 64-bit integer).
Output format
nodes N queries Q maxdepth D levels LOG root R
depth_hist c_0 ... c_31
sum_dist S1 sum_max S2 ancestor_queries C hash H
q i u U v V lca W dist X max M sig G mid Y (for i < min(Q, 8))
LOG is the smallest L >= 1 with 2^L > D. depth_hist counts nodes by
depth / ceil((D + 1) / 32). S1, S2 sum dist and max over all queries, C counts
queries whose lca is u or v, and H is the sum over queries of
mix64(ah ^ q) with ah the hash of the answer tuple defined in the CPU source (all sums
modulo 2^64).
Hidden inputs
1 <= N <= 12,000,000, 0 <= Q <= 60,000,000, 0 <= PSPINE <= 1000, 0 <= PFAR <= 10^6,
1 <= WIN <= 10^6, 0 <= LOCAL <= 1000. Correctness inputs include N = 1, N = 2,
Q = 0, a pure path (PSPINE = 1000), a ladder (WIN = 2), a random recursive tree
(PFAR = 10^6), only local and only global queries, and trees up to 10^6 nodes. The timing
input has 7.5M <= N <= 8.5M, Q = 6 N, 400 <= PSPINE <= 600, 0 <= PFAR <= 3,
8 <= WIN <= 64 and 200 <= LOCAL <= 400; its depth reaches about a million or more.
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.
How you are scored
- 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. - GPU verification uses the backend selected for this run: an H100 attached to the separate verifier, or a remote H100 worker. Hidden inputs use a fresh random seed.
- For public GPU experiments, use
/app/bin/gpu_run [--backend auto|local|remote] [--repeat N] [--quiet] SOURCE.cu INPUT_FILE.... The default usesGPU_RUNNER_URLwhen configured, otherwise a GPU attached to this sandbox. Sandbox GPU access is configured independently from verification; a CPU-only sandbox needs a runner URL. - Correctness: every hidden input (small edge cases plus the timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout gives reward 0.
- GPU execution: a separate, untimed CUPTI check must observe completed GPU kernel activity on the hidden timing input. A CPU-only submission receives reward 0 even if its output is correct.
- Speed: The timing input has about 8M nodes (deep: spines with windowed attachments, depth around a million or more) and 6 queries per node, 20-40% of them local. Your port and our GPU reference are each run on it
once to warm up and then 5 times, interleaved; the time is the wall clock of the whole process
(including CUDA initialisation, input parsing and output). When correct,
reward = min(2, t_reference / t_yours)using the medians. /app/inputs/holds a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.