Point-in-polygon spatial join: 1e8 clustered points against 40000 star polygons with holes
/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 spatial join: N clustered points are tested against P star-shaped polygons (overlaps
allowed, some with a hole ring) through a uniform grid index over polygon bounding boxes. For every
polygon the program counts the points inside it and hashes their ids; for every point it counts how
many polygons contain it. Everything is integer and generated from the seed; the CPU source is the
reference for every detail (mix64 is the SplitMix64 finaliser). Coordinates lie in [0, 2^24);
clamp clips to that range.
Clusters c < C: h = mix64(seed ^ mix64(c + 0x7F4A7C159E3779B9)), centre
(2^20 + h mod (2^24 - 2^21), 2^20 + (h>>24) mod (2^24 - 2^21)), spread
s = 2^e + (h>>52) mod 2^e with e = 14 + (h>>48) mod 4.
Points i < N: h = mix64(seed ^ mix64(i + 0x3C6EF372FE94F82B)). If h mod 1024 < 307 (or C = 0)
the point is uniform: ((h>>10) mod 2^24, (h>>34) mod 2^24). Otherwise it belongs to cluster
(h>>10) mod C and is clamp(centre + gauss(h2, s)) with h2 = mix64(h ^ 0xA54FF53A5F1D36F1),
using h2 for x and h2>>16 ^ h2<<48 for y, where gauss(h, s) = u*s/4096 (C truncating division)
and u = sum of four 12-bit fields of h minus 8190.
Polygons p < P (poly_head, gen_ring): h = mix64(seed ^ mix64(p + 0x1F83D9ABFB41BD6B)),
h2 = mix64(h ^ 0x5BE0CD19137E2179), h3 = mix64(h ^ 0x9B05688C2B3E6C1F).
- Vertex count
k= truncated power law in[kmin, kmax]fromh3(same formula as the CPU'spowerlaw). - If
h mod 4 != 0andC > 0: a cluster polygon around cluster(h>>2) mod C, centreclamp(centre + gauss(h2, s)),clamp(centre_y + gauss(h2>>20, s)), radiusRin[s/32, s). Otherwise a background polygon with a uniform centre andRlog-uniform in[rmin, rmax]. - A hole ring with
kh = max(3, k/4)vertices and radiusR/4exists when(h3>>56) mod 100 < holepct. - Ring vertices: angle codes
a_i = pre_i * 4T / tot(T = 2^14,pre_i= exclusive prefix sum of gaps1 + (mix64(hr ^ mix64(i)) mod 1024),tot= sum of all gaps), quadrantq = a/T,t = a mod T, direction(T^2 - t^2, 2tT)rotated byqquarter turns, lengthT^2 + t^2; vertex= clamp(centre + dir * r / (T^2 + t^2))withr = R*(512 + (hv mod 512))/1024,hv = mix64(hr + 0x6A09E667F3BCC908*(i+1))(C truncating divisions).hrdiffers per ring. Rounding and clamping can make rings slightly self-intersecting; the even-odd rule below defines inside in all cases.
Containment. A point (px, py) is inside polygon p iff the number of crossing edges over both
rings is odd. Edge a -> b (consecutive vertices, the last one closing the ring) crosses iff
(a.y > py) != (b.y > py) and, with s = (b.x-a.x)*(py-a.y) - (px-a.x)*(b.y-a.y), s > 0 when
b.y > a.y and s < 0 otherwise (exact 64-bit integers; half-open rules decide points on edges
and vertices).
Index (only affects speed, not results): grid G x G of cells of size 2^24/G; a polygon is
listed in every cell its bounding box touches; a point only tests the polygons of its cell whose box
contains it. Any index or none at all gives the same output.
Outputs. count_p and idsum_p = sum over contained points of mix64(i ^ 0x2545F4914F6CDD1D);
depth_i = number of polygons containing point i;
polyhash = sum over p of mix64((p*0x9E3779B97F4A7C15) ^ mix64(idsum_p + count_p));
pointhash = sum over points with depth > 0 of mix64((i<<6) ^ depth); all sums modulo 2^64.
Input format
points N clusters C
polygons P kmin kmax rmin rmax holepct
grid G
seed SEED
The words points, clusters, polygons, grid, seed are literal.
Output format
points N polygons P edges E cells G*G refs F
pairs SUM_DEPTH nonempty NE polyhash PH pointhash QH
depth d_0 ... d_31 (number of points with depth 0..30, and >= 31)
top t poly p count c idhash x verts k hole kh (t = 0..min(8,P)-1)
E is the total number of ring vertices, F the number of (cell, polygon) entries of the grid,
NE the number of polygons containing at least one point. top lists the polygons with the most
points (ties: smaller id).
Hidden inputs
0 <= N <= 1.2e8,0 <= C <= 10000,0 <= P <= 200000,3 <= kmin <= kmax <= 4000,1 <= rmin <= rmax <= 2^22,0 <= holepct <= 100,Ga power of two in[1, 2048], any 64-bit seed.- Correctness inputs include
N = 0,P = 0, a single grid cell, triangles only, huge polygons clamped to the domain border with points on the border, the finest grid with 4000-vertex rings, and random mixes with up to 120000 points and 4000 polygons. - The timing input has
Nin [0.9e8, 1.1e8],C = 1000,P = 40000,kmin = 8,kmax = 3000,rmin = 4096,rmax = 524288,holepct = 25,G = 512. Points have containment depth about 5 on average and each point tests about 8 candidate polygons (about 500 edges).
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 has about 1e8 points (70% in 1000 Gaussian-like clusters) and 40000 polygons (8..3000 vertices, power law; 25% with hole rings; radii tied to the cluster spreads), indexed with a 512 x 512 grid. 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.