LCA, path maximum, ordered path signature and path midpoint on a huge deep tree
/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
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.
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 about 8M nodes (deep: spines with windowed attachments, depth around a million or more) and 6 queries per node, 20-40% of them local.
/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.