Panorama stitching: exact-arithmetic RANSAC homography with local optimisation
/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 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 the shape and size of
/app/inputs/timing_example.in.
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:
/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.