Suffix array, LCP and longest-prefix-match queries on a repetitive text
/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
- 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; K, M, C, P are numbers)
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 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 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.
/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.