Customer segmentation with exact PAM k-medoids and silhouette model selection
/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
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.
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 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.
/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.