Offset-invariant matrix profile of an integer time series
/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 builds an integer time series x[0..N-1] (values in [-1000, 1000]) from a seed
and computes its matrix profile for window length M: for every window (length-M
subsequence starting at i, 0 <= i < W = N - M + 1) the distance to, and the index of, its
nearest non-trivial neighbour.
The distance is offset-invariant and exact. For windows i and j let
d_t = x[i+t] - x[j+t] for t = 0..M-1, S1 = sum d_t, S2 = sum d_t^2, and
D(i, j) = M * S2 - S1 * S1 (a 64-bit integer, always >= 0)
which is M^2 times the squared Euclidean distance after removing the mean difference, so two
windows that differ only by a constant offset have distance 0. Pairs with |i - j| < E,
E = ceil(M / 4), are trivial matches and are ignored. The profile value of window i is the
minimum of D(i, j) over all other windows j with |i - j| >= E; ties go to the smallest j.
The CPU program walks each diagonal j = i + k with the O(1) update of S1 and S2 and keeps
the packed key (D << 20) | neighbour for both windows of every pair. Windows with no
admissible neighbour get profile -1 and index -1.
The series is produced by a sequential SplitMix64 stream (Rng in the source): random-walk
stretches, noisy plateaus, and copies of three random motif shapes shifted by random offsets
with small noise. The generator is cheap and must be reproduced exactly.
Input format
One line:
N M SEED
1 <= N <= 600000, 1 <= M <= 2048, SEED an unsigned 64-bit integer.
Output format
windows W exclusion E with_neighbour C
profile_hash H1
index_hash H2
min P i j (smallest profile value, its window, its neighbour; first such window)
max P i j (largest profile value, first such window)
hist b count (profile values with bit length b; only non-empty b, ascending)
motif r a b P (up to 8 lines)
discord r i P j (up to 4 lines)
H1 = sum_i mix64(P_i XOR i*0xD1B54A32D192ED03)andH2 = sum_i mix64(I_i + (i << 32))modulo 2^64, withP_i,I_itaken as 64-bit two's complement (so-1is all ones) andmix64the SplitMix64 finaliser of the source.- Motifs: windows sorted by
(P_i, i)ascending; the pair(min(i, I_i), max(i, I_i))is kept if both windows are at leastmax(1, M/2)(integer division) away from every window of the pairs kept before. Discords: windows by profile value descending (smallest index first among equal values), kept if at leastmax(1, M/2)away from every discord kept before. - When
C = 0the output ends with the linesmin -andmax -.
Hidden inputs
- Correctness inputs:
N < M(no windows),N = M, a series whose windows all lie inside the exclusion zone,M = 1andM = 2, small random series,M = 2048withNup to 12000, and series up toN = 30000withMup to 1024. - The timing input has the shape and size of
/app/inputs/timing_example.in. - All profile values are below
2^44and all window indices below2^20.
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.