Query batch over a bit-packed column store: FOR, delta and dictionary decoding, LIKE and range predicates, hash 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 batch of analytical queries over a 7-column table that exists only in compressed form. The
program generates the block headers and the packed payload words from the seed, decodes the
columns, writes zone maps (min/max per block, used to skip blocks) and column checksums, then runs
every query as a full scan: a conjunction of predicates, a group key and four aggregates. All
arithmetic is integer; the CPU source is the reference for every detail (mix64 is the SplitMix64
finaliser, sums are modulo 2^64).
Blocks. Rows are cut into NB = ceil(R / B) blocks of B rows (the last one may be short).
For block b and column c, h = mix64(SEED ^ mix64(16*b + c + 0x51ED270B27A3C5)) gives the
block's encoding enc, bit width w and reference ref:
| c | column | encoding, width, reference |
|---|---|---|
| 0 | ts | DELTA if h mod 8 != 0 (w = 1 + (h>>3) mod 9), else FOR (w = 10 + (h>>3) mod 11); ref = running time stamp: starts at 10^6, grows by 4096*B + (h>>40) mod 65536 per block |
| 1 | price | FOR, w = 4 + (h>>3) mod 15; ref = random walk from 2^20, step (h>>24) mod 4097 - 2048 (applied before use) |
| 2 | qty | FOR, w = (h>>3) mod 8, ref = (h>>8) mod 11 |
| 3 | cat | DICT over NC ids, w = 1 + (h>>3) mod max(1, ceil(log2 NC)), ref = (h>>16) mod NC |
| 4 | name | DICT over NS ids, w = max(1, ceil(log2 NS) - (h>>3) mod 4), ref = (h>>16) mod NS |
| 5 | flags | FOR, w = (h>>3) mod 13, ref = 0 |
| 6 | amount | FOR if h mod 4 != 0 (w = (h>>3) mod 33), else DELTA (w = 4 + (h>>3) mod 13); ref = (h>>24) mod 2^34 - 2^33 |
Payload. Column c is one array of 32-bit words, W[j] = mix64(base_c + j*0x9FB21C651E98DF25) >> 32
with base_c = SEED ^ (0xA24BAED4963EE407 * (c+1)). Block b starts at word
off = sum_{b'<b} ceil(n_b' * w_b' / 32); its field i is the w-bit value at bit
32*off + i*w, least significant bit first, crossing word boundaries freely. Values: FOR
v = ref + f; DELTA v_i = ref + sum_{t<=i} unzigzag(f_t) with unzigzag(u) = (u>>1) ^ -(u&1);
DICT id = (ref + f) mod N.
Dictionary. Name id k is a string: h = mix64(SEED ^ mix64(k + 0x7C3A5D9B1E2F4061)), length
3 + h mod 18, character t: when t mod 7 == 0 first set h = mix64(h + t); then
x = (h >> 8*(t mod 7)) & 255 and the character is "etaoinshrdlu"[((x*x) >> 12) mod 12].
Queries. Predicates (all must hold): range c lo hi (lo <= v <= hi), in c qseed p
(mix64(qseed ^ id) mod 1000 < p, on cat or name), like pat (the name string matches the
SQL LIKE pattern: % any sequence, _ any one character, no escapes), mask c m x
((v & m) == x on the 64-bit value). Group key: none (0), col c s
((v_c >> s) & (2^63-1), arithmetic shift) or pair c1 s1 c2 s2
((((v1 >> s1) & (2^31-1)) << 32) | ((v2 >> s2) & (2^32-1))). Aggregates agg cs cn cx per group:
count, sum of v_cs, min of v_cn, max of v_cx (signed). Dictionary columns use their ids as
values everywhere.
Input format
table R B NS NC SEED
queries Q
query P <pred 1> ... <pred P> group <none | col c s | pair c1 s1 c2 s2> agg cs cn cx
Output format
table rows R blocks NB dict NS cats NC
column c delta_blocks D words W sum S hash H (c = 0..6)
query q rows N groups G sum T hash X (q = 0..Q-1)
S = sum of the column's values, H = sum_rows mix64(v ^ (row * 0xD6E8FEB86659FD93)); N = number
of qualifying rows, G = number of groups, T = sum of v_cs over qualifying rows,
X = sum_groups mix64(key ^ mix64(count ^ mix64(sum ^ mix64(min ^ mix64(max))))).
Hidden inputs
0 <= R <= 3.3e8,Ba power of two in[256, 8192],1 <= NS <= 2^18,1 <= NC <= 5000, any 64-bit seed,0 <= Q <= 48,0 <= P <= 4; every group key has at most 2^20 distinct values.- Correctness inputs include an empty table, one row with one-entry dictionaries, a single partial
block, one full 8192-row block and one more row, every predicate kind alone (patterns
%,___, prefixes,_inside), queries without predicates, and random tables of 1e5 to 1.5e6 rows. - The timing input has about 3e8 rows in blocks of 4096 (about 2.4 GB of packed payload),
NSin [2^17, 2^18],NCin [1000, 4000] and 48 queries with 1-4 predicates each.
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 3e8 rows in 4096-row blocks (about 2.4 GB of packed payload), a dictionary of 1.3e5-2.6e5 strings, 1000-4000 categories and 48 queries with 1-4 predicates and group keys of up to 2^18 distinct values.
/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.