LZ77 hash-chain parsing with a lookahead greedy parse
/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
LZ77 parsing of NB independent text blocks of B bytes each, with hash-chain match search and a
greedy parse with one-step lookahead. The program reports token statistics, an order-defined token
hash and the compressed size under a fixed static code.
Text. Block b is generated from the seed (mix64 is the SplitMix64 finaliser; exact formulas
in the CPU source) in 256-byte segments. The first segment and 55% of the others are fresh: words
from a 2048-word vocabulary (frequency falling off like 1/sqrt(rank)) separated by " ", ", "
or ". ", cut at the segment end. Every other segment copies earlier text of the same block at a
power-law distance of at least 256 bytes, with 1/40 of its bytes replaced by a random letter, space
or period.
Match search. Every position p <= B-4 is inserted into a hash chain keyed by a 16-bit
multiplicative hash of the 4 bytes at p (hash4). The candidates of p are the D most recent
earlier positions of the same block with the same hash value (hash collisions included) that lie
within distance W (p - q <= W). The match length at candidate q is the length of the common
prefix of the text at q and at p (overlap allowed), capped at 258 and at the block end. A
candidate is valid if its length is at least minlen(d) = 4 + [d > 2048] + [d > 65536]
(d = p - q). The best match at p is the longest valid candidate; ties go to the smaller distance.
Positions p > B-4 have no match.
Parse. Starting at position 0 of each block: if p has a valid best match of length L and
the best match at p+1 (if p+1 < B) is not at least L + 2 long, emit the match and continue at
p + L; otherwise emit the literal byte at p and continue at p + 1.
Static code. A literal costs 1 + 7 bits if it is a letter a-z or a space and 1 + 8 bits
otherwise; a match costs 1 + gamma(L - 3) + gamma(d) bits with gamma(x) = 2*floor(log2 x) + 1.
Input format
One line:
NB B D W SEED
Output format
blocks NB size B bytes NB*B
literals NL matches NM matched_bytes MB
bits TOTAL
tokenhash TH
blockhash BH
minblock b1 bits1 maxblock b2 bits2
lenhist h0 .. h7
disthist g0 .. g20
TH: sum over tokens ofmix64((b << 52) | (p << 29) | (L << 20) | d)for a match at positionpof blockb, andmix64((b << 52) | (p << 29) | byte)for a literal (modulo 2^64).BH: sum over blocks ofmix64(b * 0x9E3779B97F4A7C15 + bits_b);minblock/maxblockare the blocks with the fewest/most bits (ties: the smaller block index); with no blocks the line isminblock - 0 maxblock - 0.lenhist: match counts byfloor(log2(L - 3))(8 buckets);disthist: match counts byfloor(log2 d)(21 buckets).
Hidden inputs
0 <= NB <= 4095,1 <= B <= 2^23,1 <= D <= 64,1 <= W <= 2^20 - 1.- Correctness inputs include no blocks, 1-4 byte blocks, depth 1 with window 1, depth 64 with the widest window, 2^23-byte blocks, 64 blocks of 4096 bytes, block sizes that are not multiples of 256, and random mixes.
- The timing input has
NBabout 980-1080 blocks of 3.8-4.6 MB (about 4.3 GB of text),Din [32, 40] andW = 65535.
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 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. - Grading runs on one H100 in a fresh container without network. Inputs are generated with a new random seed at grading time, so they cannot be known in advance.
- 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.
- Speed: The timing input has about 1030 blocks of 3.8-4.6 MB (about 4.3 GB of text), chain depth 32-40 and a 64 KB window. 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.