Customer segmentation with exact PAM k-medoids and silhouette model selection
/app/cpu/main.cpp is a multicore CPU program (C++17, OpenMP). It is built as
/app/bin/cpu_baseline with g++ -O3 -march=native -fopenmp -std=c++17 and run as
cpu_baseline INPUT_FILE
It reads one input file and writes its result to stdout.
What the program computes
A customer-segmentation run with k-medoids (PAM) on a mixed-type dissimilarity, with model selection over several k. All arithmetic is integer; the CPU source is the exact specification.
- Customer table.
Nrecords are generated from the seed (mix64= SplitMix64 finaliser,hh(seed,a,b) = mix64(seed + mix64(a*0x9E3779B97F4A7C15 + b))). Raw recordidbelongs to one ofGlatent segments with skewed sizes (2.5% are uniform outliers) and has 12 numeric bytes, 4 ordinal attributes (3/4/5/7 levels), 6 categorical attributes (3/5/8/12/4/6 categories) and 16 flag bits. Adupm/1000 fraction of the customersi > 0are exact copies of the raw record of an earlier id (so zero dissimilarities and ties are common whendupmis large). - Dissimilarity matrix. For every ordered pair,
d(i,j) = sum wn|x-y| (numeric) + sum wo*step|r-s| (ordinal) + sum wc*T_a[c][c'] (categorical, seeded symmetric tables) + 48*popcount(flags xor), weights derived from the seed;d < 2^15, stored as a fullN x Nuint16 matrix. - For every k in the list (independently):
- BUILD: the first medoid minimises
sum_j d(o,j); then repeatedly add the non-medoidomaximisingsum_j max(D_j - d(o,j), 0)(D_j= distance to the closest medoid so far). Ties always go to the smallest index. - Assignment: every point gets its nearest medoid slot (ties: smallest slot), nearest
distance
D_jand second-nearest distanceE_j(2^20if k = 1). - SWAP (best improvement): for every slot
iand non-medoido, the cost change of replacing medoidibyoisDelta(i,o) = shared(o) + bin_o[i], withshared(o) = sum_{j: d(o,j)<D_j} (d(o,j)-D_j)andbin_o[i] = sum_{j: near_j=i, d(o,j)>=D_j} (min(d(o,j),E_j) - D_j). The swap with the lexicographically smallest(Delta, o, i)is applied ifDelta < 0; repeat (at mostmaxswapswaps). Every swap is followed by a fresh assignment. - Exact silhouette in Q16: for point
iin clustercwith|c| > 1(and k > 1),ais its mean dissimilarity to the other members,bthe smallest mean dissimilarity to another non-empty cluster (compared as exact fractions),s = trunc(65536*(b-a)/max(a,b))computed from the cross-multiplied integers in the CPU source (0 if the max is 0); otherwises = 0.
- BUILD: the first medoid minimises
- Model selection: the k with the largest silhouette sum (first in list order on ties).
Input format
N G dupm seed maxswap
NK
k_1 k_2 ... k_NK
Output format
For each k (input order) three lines, then a final line:
k=<k> build=<cost after BUILD> final=<cost after SWAP> swaps=<swaps applied> sil=<sum of s> silq16=<sum/N>
medoids <medoid id of slot 0> ... <slot k-1>
sizes <cluster size of slot 0> ... <slot k-1>
best_k=<k> best_sil=<sum>
Hidden inputs
1 <= N <= 24000, 1 <= G <= 64, 0 <= dupm <= 950, 0 <= maxswap <= 100000,
1 <= NK <= 57, 1 <= k <= min(N, 128), any 64-bit seed. Correctness inputs (12 files) include
N = 1 and N = 2, k = 1, k = N, k = 128, 50-90% duplicate records (empty clusters),
maxswap = 0 and a small swap cap, and random cases with N <= 3000. The timing input has
N in [19000, 21000], G in [30, 60], dupm <= 20, an unbounded swap count and 7 distinct
values of k drawn from [8, 64].
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.
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.
- For public GPU experiments, use
/app/bin/gpu_run [--backend auto|local|remote] [--repeat N] [--quiet] SOURCE.cu INPUT_FILE.... The default usesGPU_RUNNER_URLwhen configured, otherwise a GPU attached to this sandbox. Sandbox GPU access is configured independently from verification; a CPU-only sandbox needs a runner URL. - 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 has about 20000 customers (0.8 GB matrix) and runs PAM for 7 distinct values of k between 8 and 64, typically a few hundred full matrix passes in total. 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 a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.