LZW strip decoding of a compressed raster stack with predictor reversal
/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
Decoding of a stack of LZW-compressed raster images stored as independent strips (TIFF-like),
followed by predictor reversal and raster statistics. The compressed container is produced inside
the program by a seeded generator (mix64 = SplitMix64 finaliser). The CPU source is the
specification; this is a summary.
- Geometry.
NIMGimages ofW x Hpixels withCbyte channels; rows are grouped into strips ofRrows (the last strip of an image may be shorter); stripg(global index, image-major) holdsE = rows * W * Cbytes. Imageihas texture classmix64(SEED ^ (0x1A6E000000000000+i)) % 4. - LZW format. Codes are packed MSB-first.
CLEAR = 256,EOI = 257, the first dictionary code is 258. The width of the next code ismin(MAXW, bitlen(next + EARLY))wherenextis the decoder's next free code; afterCLEARthe next code is a literal and no entry is added; every later code adds entrynext = str(prev) + first byte of str(code)whilenext < 2^MAXW(then the dictionary is frozen).code == nextis the KwKwK case (str(prev) + first byte of str(prev)). - Container generator (per strip,
key = mix64(SEED ^ (0x5781900000000000 + g * 0x100000001B3))): a budget ofN = E * 1000 / CODES1000[class][MAXW-9] + 32codes is split into segments, each preceded byCLEAR; segmentjhasmcodes chosen frommix64(key ^ (j * 0xD6E8FEB86659FD93))(clear when the table is full, an early clear, or a frozen tail of up to 1024 extra codes; the last segment is cut to the budget), thenEOI. Codeiof segmentjis chosen frommix64(key + ((j << 20) | i) * 0x9E3779B97F4A7C15)among a literal (mostly small deltas), the newest entry, KwKwK, a recent entry or a random entry, with class-dependent probabilities; codes are always valid. - Decode. Each strip is parsed and decoded until it holds
Ebytes: the last string is cut at the strip end and the remaining codes are not read (unread_bits); ifEOIcomes first the rest of the strip is zero-filled (a short strip). - Predictor. In every row, byte
i >= Cbecomes(byte[i] + byte[i - C]) mod 256(TIFF horizontal differencing, applied left to right). - Statistics. Per channel: sum, sum of squares, 256-bin histogram (hash and mode, smallest value
on ties). Per row (global row id
img * H + y):sum_j mix64(w_j ^ (j * 0xD1B54A32D192ED03) ^ (rowid * 0xA24BAED4963EE407))over the little-endian 8-byte words of the row (last one zero-padded); the raster hash is the sum over all rows; the brightest row is the first row with the largest byte sum. Stream statistics: compressed bytes (ceil(bits/8)per strip), unread bits, short strips, zero-filled bytes, decoded codes, CLEAR codes, CLEARs seen with a full dictionary, codes decoded while frozen, KwKwK codes and the longest string.
Input format
One line: NIMG W H C R MAXW EARLY SEED (SEED is an unsigned 64-bit integer).
Output format
images NIMG width W height H channels C rows_per_strip R maxw MAXW early EARLY strips S
stream bytes B raw RAW unread_bits U short_strips SS zero_filled Z
codes K clears CL full_clears FC frozen_codes FZ kwkwk KW maxlen ML
channel c sum S sumsq Q mode v:n histhash H (one line per channel)
raster hash H brightest_row ROW sum S
image i class k bytes B codes K zero_filled Z hash H (one line per image)
All sums and hashes are modulo 2^64.
Hidden inputs
1 <= NIMG <= 64,1 <= W <= 16384,1 <= H <= 2^20,1 <= C <= 4,1 <= R <= 300,W * C * R <= 2^21,9 <= MAXW <= 13,EARLYin {0, 1}; the raw raster is at most about 2.5e10 bytes.- Correctness inputs include one-pixel images, one-pixel rows, strips taller than the image, a last
strip shorter than
R,MAXW = 9(many clears) and 13 (frozen tails), both width rules, all four texture classes, 2 MB strips, seeds 0 and 2^64-1 and random geometries (at most 6e6 raw bytes). - The timing input has 16 images of 6144 or 8192 x about 2.0e10 / (48 W) pixels with 3 channels (about 2.0e10 raw bytes, 8- or 16-row strips, MAXW in 11..13, EARLY = 1); about 2-4% of the strips are short.
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 16 images of 6144 or 8192 pixels by about 2.0e10/(48 W) rows with 3 channels (about 2.0e10 raw bytes, 8- or 16-row strips, 11- to 13-bit maximum code width, early change).
/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.