FM-index backward search with bounded wildcard branching
/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
The program reads N, M, seed. It generates a text T of N symbols over the
alphabet {0,1,2} (symbol i is mix64(seed ^ (i*0xD1B54A32D192ED03)) % 3, where mix64
is the SplitMix64 finaliser in the source), appends a unique sentinel that sorts before every
real symbol, and builds an FM-index:
- the suffix array of the sentinel-terminated string,
- the Burrows-Wheeler transform, stored packed 2 bits per symbol (32 symbols per 64-bit word; the sentinel is stored as the value 3 and is never counted by a query),
- checkpoint counts: for every block of
K = 256BWT positions, the cumulative number of each real symbol before that block.rank(c, i)= checkpoint value + a scan of the packed words from the block start toi. - the first-column array
C[c](C[0]=1,C[1]=1+#0,C[2]=1+#0+#1).
It then generates M patterns and runs backward search on each. Pattern idx has length
P = 6 + (h0 % 11) with h0 = mix64((seed ^ 0xA5A5A5A5A5A5A5A5) + idx*0x9E3779B97F4A7C15).
Character j (0-based) is derived from hj = mix64(h0 + (j+1)*0xC2B2AE3D27D4EB4F):
- if
hj % 6 == 0and fewer thanWMAX = 2wildcards have been used so far (scanning the pattern right to left, as backward search does), positionjis a wildcard: the current suffix-array interval[lo,hi)is split into up to three children, one per real symbol; - otherwise it is the literal symbol
hj % 3.
Backward search maintains the set of live intervals (at most 3^WMAX = 9); each step maps
[lo,hi) to [C[c]+rank(c,lo), C[c]+rank(c,hi)) and drops empty intervals. If all intervals
die, the pattern search stops early. The pattern's occurrence count is the sum of the
widths of the surviving intervals.
Aggregated over all patterns the program reports:
MATCHED= number of patterns with count > 0,B1,B2,B3= number of patterns with count in[1,9],[10,99],>=100,TOTALOCC= sum of all counts (mod 2^64),HASH= sum ofmix64((count * 0x9E3779B97F4A7C15) ^ h0)over patterns (mod 2^64),MAXCOUNT= largest count,MAXIDX= the smallest pattern index achieving it (-1ifM=0).
Input format
N M seed
Output format
One line:
N M MATCHED B1 B2 B3 TOTALOCC HASH MAXCOUNT MAXIDX
Hidden inputs
0 <= N <= 262144, 0 <= M <= 280,000,000, seed is any 64-bit value. Correctness inputs
(15 files) include the empty text (N=0), a single symbol (N=1), M=0 and M=1, texts of
exactly 255, 256, 257 symbols (around one checkpoint block), and random mixes with
N up to 60000 and M up to 120000. The timing input has the shape and size of /app/inputs/timing_example.in.
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:
/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.