LZW strip decoding of a compressed raster stack with predictor reversal
/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
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 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. - This sandbox has no GPU.
nvccworks here for compiling. To run on the GPU, use/app/bin/gpu_run [--repeat N] [--quiet] SOURCE.cu INPUT_FILE...: it compiles the file with the flags above on a remote H100 worker, runs it on each input, and prints stdout, exit code and wall-clock time. Grading also runs your code on an H100 worker, one job per GPU at a time. 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 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). 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.