Multi-word Myers bit-vector matching of long IUPAC patterns
/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
Approximate matching of many long IUPAC patterns against a long DNA-like text, with Myers' bit-vector algorithm (multi-word version).
Text. N symbols over A C G T plus the masked symbol N, generated from the seed
(mix64 is the SplitMix64 finaliser; exact formulas in the CPU source):
- a base layer cut into 4096-symbol segments: the first segment and 60% of the others are fresh random bases; every other segment copies earlier text at a power-law distance (at least 4096 back), with 3% point mutations;
- an overlay on 2048-symbol slots: 1/16 of the slots get a run of 20-319 masked
Nsymbols, 3/16 get a planted occurrence of a random pattern with substitutions, insertions and deletions (cut off at the slot end).
Patterns. P patterns with lengths m in [LMIN, LMAX] over the 15 IUPAC codes
(A C G T R Y S W K M B D H V N, stored as 4-bit masks over A, C, G, T), each with a threshold
k = floor(m * rate / 1000), rate in [50, 150]. About half of the patterns are copies of the
base layer at a random position with ~3% substitutions and ~4% positions widened to an ambiguity
code; the others are random with ~6% ambiguity codes. A pattern symbol matches a text base when its
mask contains the base; the text symbol N matches nothing.
Matching. For every pattern and every text end position j (0-based) the program computes
the semi-global edit distance
D(j) = min over t <= j+1 of EditDistance(pattern, T[t..j]) (unit costs, empty substring allowed)
with Myers' algorithm on ceil(m/64) 64-bit words per pattern (multi-word addition and shift
with carries). A hit is an end position with D(j) <= k; a run is a maximal block of
consecutive hit positions. It also reports the exact minimum of D(j) over all j (not capped at
k) and the leftmost position where it occurs.
The CPU program processes (pattern, 65536-position chunk) work items and restarts each chunk 2m
columns early, which gives exactly the same columns as one pass over the whole text (an optimal
alignment never spans more than 2m text symbols).
Input format
One line:
N P LMIN LMAX SEED
Output format
text N masked X patterns P
hits H runs R
hithash HH
pathash PH
pat p m M k K hits H runs R min D at J (one line per pattern, p = 0..P-1)
X: number of masked text symbols.H,R: hits and runs (per pattern on thepatlines, summed on the second line).HH: sum over all hits ofmix64((p << 48) | (j << 10) | D(j))modulo 2^64.PH: sum over patterns ofmix64((p << 40) ^ (hits << 20) ^ (min + 1) ^ ((at + 1) << 24))modulo 2^64.min D at J: the minimum distance and its leftmost end position; for an empty text it ismin m+1 at -1.
Hidden inputs
0 <= N <= 2^27,1 <= P <= 128,1 <= LMIN <= LMAX <= 1024.- Correctness inputs include an empty text, a one-symbol text, a text shorter than every pattern,
patterns of exactly 64 and 65 symbols, 1024-symbol patterns, 1-8 symbol patterns (
k = 0), texts spanning several chunks, 128 patterns, and random mixes. - The timing input has
Nabout 2^26 (64M +- 5%),Pin [88, 104],LMIN = 128,LMAX = 1024: about 6*10^10 64-bit word steps.
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 text of about 64M symbols and 88-104 patterns of 128-1024 symbols with k = 5-15% of the length: about 6e10 64-bit word steps.
/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.