Suffix array, LCP and longest-prefix-match queries on a repetitive text
/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
- Text.
Ncharacters over the alphabet0 .. SIGMA-1, generated position by position (seegen_charin the CPU source): positioniis in segments = i / SEG; a segment withs > 0is a copy with probabilityCOPY% of an earlier stretch that starts in one of the previousmin(s, 1024)segments; inside copies each position is replaced by a fresh random character with probabilityMUT/1000. Copies of copies are followed back to a fresh character. - Suffix array of the text (shorter suffix first when one is a prefix of another),
built by prefix doubling; the number of doubling rounds is printed, where round
k(k = 1, 2, 4, ...) sorts by(rank[i], rank[i+k]+1 or 0)and the loop stops after the first round whose ranks are all distinct (0 rounds forN = 0). Starting ranks are the characters. A port may build the suffix array any way it likes but must print the same round count. - LCP array (
lcp[0] = 0,lcp[r]= longest common prefix of the suffixes at ranksr-1andr). - Queries. Query
q(0 <= q < Q) takes a window of the text of lengthm(10 to 48, at mostN) at a hashed position, and with probability 1/2 substitutes one character (see the CPU source for the exact hashing).mlenis the length of the longest prefix of the pattern that occurs in the text,cntthe number of occurrences of that prefix (the size of its suffix-array interval;Nwhenmlen = 0) andminposthe smallest starting position among them (Nwhen the interval is empty, i.e.N = 0).
Input format
One line: N SIGMA SEG COPY MUT Q SEED.
Output format
n N rounds R texthash TH sahash SH
lcpsum LS lcpmax LM at rank RK sa P distinct DS
queries Q full F sumcnt C summlen ML qhash QH
q k mlen m cnt c minpos p (for the first min(Q, 8) queries)
TH = sum of mix64(64*i + text[i]), SH = sum of mix64((r << 32) ^ sa[r]), LS = sum of lcp,
LM = max lcp, RK the smallest rank with lcp = LM and P = sa[RK] (both -1 when N = 0),
DS = N(N+1)/2 - LS (the number of distinct non-empty substrings), F the number of
queries with mlen = m, C = sum of cnt, ML = sum of mlen,
QH = sum of mix64(q ^ (cnt << 40) ^ (mlen << 32) ^ minpos); sums are modulo 2^64.
Hidden inputs
0 <= N <= 40,000,000, 2 <= SIGMA <= 16, 1 <= SEG <= 100000, 0 <= COPY <= 100,
0 <= MUT <= 1000, 0 <= Q <= 30,000,000. Correctness inputs include N = 0, N = 1, a text
shorter than every pattern, an almost constant text (one long run, many doubling rounds),
random text without copies, exact copies only (long repeats), a 16-letter alphabet with
heavy mutation, a binary alphabet, and random mid-sized cases.
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 a 30-34M-character text over 4 letters (segments of 1500-2500 characters, 60-75% of them mutated copies of earlier text) and 22-26M queries. 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.