Skewed equi-join with an opaque residual predicate and group-by
/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 relational query over two tables that the program generates from the seed:
SELECT a.g, b.h, COUNT(*), SUM(a.v * b.w + (z >> 32)), XOR(z)
FROM build a JOIN probe b ON a.key = b.key
WHERE z mod 7 < 3 -- z = mix64(a.x XOR b.y)
GROUP BY a.g, b.h
- Build rows
i = 0..NB-1and probe rowsi = 0..NP-1are produced by counter-based hashing (mix64is the SplitMix64 finaliser; the exact formulas are in the CPU source). A build row has a 32-bitkey, a 64-bit attributex, a 32-bit valuevand a group codegin 0..3. A probe row haskey, a 64-bit attributey, a 32-bit valuewand a group codehin 0..15. - Keys are extremely skewed. A row is a heavy-hitter row with probability
HBPM/10^6(build) orHPPM/10^6(probe); it then takes one ofHheavy keys, chosen with a further skew (the first heavy key gets about1/sqrt(H)of them). Every other row takes a light key id whose frequency falls off like1/sqrt(id): build rows useNKlight ids, probe rows use2*NKids, so many probe rows find no partner. A probe row may therefore match anything from zero to hundreds of thousands of build rows. - For every pair of rows with equal keys, the residual predicate
z mod 7 < 3withz = mix64(a.x ^ b.y)is evaluated; it is opaque, so every pair has to be visited. Passing pairs are aggregated into the 64 groups(a.g, b.h): their count, the sum ofa.v * b.w + (z >> 32)modulo 2^64, and the XOR ofz.
The joined pairs are never materialised.
Input format
One line:
NB NP NK H HBPM HPPM SEED
NB build rows, NP probe rows, NK light key ids, H heavy keys, heavy shares HBPM/HPPM
in rows per million, and a 64-bit unsigned SEED.
Output format
rows NB NP keys K
joinpairs J
passing P
misses M
group G H COUNT SUM XOR (64 lines, G = 0..3, H = 0..15)
K: number of distinct keys in the build table.J: number of equal-key (build, probe) pairs before the residual predicate;P: pairs that pass it (the sum of all group counts);M: probe rows whose key does not occur in the build table.SUMis printed as a signed 64-bit integer (two's complement of the sum modulo 2^64),XORas 16 lowercase hex digits.
Hidden inputs
0 <= NB <= 2*10^7,0 <= NP <= 2*10^8,1 <= NK <= 2^24,0 <= H <= 64,0 <= HBPM, HPPM <= 10^6.- Correctness inputs include an empty build table, an empty probe table, a single build row, a single light key, every row heavy (64 heavy keys), one huge key holding half of both tables, no heavy keys, the largest key domain (almost all probes miss), the largest build table, and random mixes.
- The timing input has
NBin [9.5M, 10.5M],NKin [0.8M, 1.2M],Hin [6, 10],HBPMandHPPMin [10000, 13000], andNP(about 80M-170M) chosen so that the join has about 3.410^10 equal-key pairs, almost 90% of them on the heavy keys; about 1.510^10 pairs pass the predicate.
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 10M build rows, 6-10 heavy keys holding 1.0-1.3% of each table, about 1M light keys, and as many probe rows (80M-170M) as give about 3.4e10 equal-key pairs, almost 90% of them on the heavy keys.
/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.