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 is one series with
Nin[400000, 480000]andMuniform in[256, 2048]. - 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 source generation only. Write /app/gpu/main.cu; compilation,
execution and verification are performed later in a separate verifier environment.
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. - GPU verification uses the backend selected for this run: an H100 attached to the separate verifier, or a remote H100 worker. Hidden inputs use a fresh random seed.
- 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.
- GPU execution: a separate, untimed CUPTI check must observe completed GPU kernel activity on the hidden timing input. A CPU-only submission receives reward 0 even if its output is correct.
- Speed: The timing input is one series of 400000-480000 samples with a window length between 256 and 2048, i.e. about 1e11 window pairs. 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 public input examples for understanding the input format.