Panorama stitching: exact-arithmetic RANSAC homography with local optimisation
/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 panorama-stitching front end: for each of NP synthetic image pairs it estimates the homography
between the two images from N putative feature matches with RANSAC plus a local-optimisation (LO)
stage, using only exact integer arithmetic, and reports the winning model, its inliers and the warped
image corners. All formulas are in the CPU source; the essentials:
- Scene and matches. Pair
pdraws (frommix64ofSEEDandp) an inlier percentage (15..70), a second-plane percentage (0..30), a noise amplitude (0..3 px), an integer ground-truth homographyG(Q16, mild rotation/scale/shear/translation/perspective) and a second "parallax" planeG2. Matchihas a source point(x, y)with-1024 <= x < 1024,-768 <= y < 768; depending on its category the destination isround(G p)orround(G2 p)(exact integer rounding, plus integer noise; destinations outside[-2047, 2047]become outliers) or a uniform random point. Only matches generated byGare "true inliers". - Hypotheses. Hypothesis
j(0 <= j < M) draws 4 distinct match indices from a counter-based stream (at most 64 draws, otherwise invalid). From source pointsp0..p3and destinationsq0..q3it builds the exact homographyH = sum_k D_k q_k r_k^T(projective-basis construction):r_kare the rows ofadj([p0 p1 p2])(cross products),lambda = adj(P) p3,mu = adj(Q) q3,D_0 = mu_0 lambda_1 lambda_2etc., all in signed 128-bit integers. It is invalid if any three of the four points are collinear in either image.His reduced toH' = H >> s(arithmetic shift) with the smalleststhat makes every entry fit in 31 bits plus sign, and negated if needed so thatw(p0) > 0;w(p0) == 0is invalid. - Scoring. Match
(x, y) -> (X, Y)is an inlier ofH'iff, with(u, v, w) = H' (x, y, 1),w > 0and16 * ((u - X w)^2 + (v - Y w)^2) <= T4^2 * w^2(exact, unsigned 128-bit). The best hypothesis has the most inliers; ties go to the smaller index. - LO stage. If
LO > 0and the best model has at least 4 inliers,LOmore hypotheses are drawn in the same way but from the ascending list of the best model's inliers (a different stream); one of them replaces the current best only with strictly more inliers (earliest wins ties). - Report. The final model's inlier set is recomputed; the program reports the number of valid
hypotheses, the winning stage and index, the inlier count, how many inliers are true inliers,
H', the four source-frame corners(-1024,-768), (1023,-768), (1023,767), (-1024,767)mapped byH'(floor(u/w), floor(v/w), orinf infwhenw <= 0) and a hash of the inlier index set.
Input format
One line: NP N M LO T4 SEED (integers; SEED unsigned 64-bit).
Output format
For each of the first min(NP, 40) pairs one line, either pair p valid 0 none (no valid hypothesis) or
pair p valid V best S:J inliers C true T H h0 h1 ... h8 corners X0 Y0 X1 Y1 X2 Y2 X3 Y3 mask MH
(S = 0 for the main stage, 1 for the LO stage, J the hypothesis index in that stage; corners may be
inf inf; MH is 16 hex digits). Then one line
total pairs NP solved S valid V lo_wins L inliers C true T hash GH
summed over all pairs; GH (16 hex digits) is a wrapping sum of per-pair hashes over all pairs (the
exact construction is in the source).
Hidden inputs
1 <= NP <= 400,1 <= N <= 16384,1 <= M <= 32768,0 <= LO <= 4096,1 <= T4 <= 64.- Correctness inputs include
N = 3(every hypothesis invalid),N = 4, tinyNwith tinyM,M = 1,LO = 0,T4 = 1andT4 = 64,N = 16384, 45 pairs (more than are printed) and random small cases. - The timing input has 190-210 pairs with
Nin[7000, 8192],Min[14000, 16384],LOin[768, 1024]andT4in[6, 16].
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 190-210 image pairs, each with 7000-8192 correspondences, 14000-16384 RANSAC hypotheses and 768-1024 LO hypotheses (about 2.6e10 exact reprojection tests 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.