Best-improvement 2-opt to a local optimum on a batch of zoned tour instances
/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
P independent closed-tour instances are each improved by best-improvement 2-opt until
no improving move remains (a 2-opt local optimum) or a step cap is reached.
Instance p is derived from h = mix64(SEED ^ ((p+1) * 0xD1B54A32D192ED03)) (see
make_inst in the CPU source): n points (NMIN <= n <= NMAX) with integer coordinates in
[0, 65535], either uniform or clustered, each with a zone z in [0, K) (1 <= K <= 6),
a symmetric zone surcharge table ZT (zero diagonal, entries in [0, ZMAX]) and a
long-edge threshold L. The cost of the edge between points a and b is
e = floor(sqrt(dx^2 + dy^2)) (exact integer square root)
c(a,b) = e + ZT[z_a][z_b] + (e > L ? (e - L) >> 1 : 0)
The initial tour t[0..n-1] is a hashed Fisher-Yates shuffle of 0..n-1. With
t[n] = t[0], a move (i, j) with 0 <= i, i + 2 <= j <= n - 1 and (i, j) != (0, n-1)
replaces edges (t[i], t[i+1]) and (t[j], t[j+1]) by (t[i], t[j]) and (t[i+1], t[j+1]),
i.e. it reverses t[i+1..j] in place (position 0 never moves). Its change in tour length is
delta(i, j) = c(t[i], t[j]) + c(t[i+1], t[j+1]) - c(t[i], t[i+1]) - c(t[j], t[j+1])
Every step evaluates all moves of the current tour and applies the one with the smallest
delta, ties broken by the smallest i, then the smallest j. The search stops when the
smallest delta is >= 0, or after CAP * n steps (the instance is then counted as capped).
Every step changes the tour, so the exact step sequence matters for the output.
Input format
One line: P NMIN NMAX ZMAX CAP SEED (SEED is an unsigned 64-bit integer).
Output format
instances P steps TS capped TC len0 A len1 B hash H
inst p n N len0 a len1 b steps s tour T traj R (for p < min(P, 12))
len0/len1 are the initial and final tour lengths (sums of c over the tour's n edges),
steps the number of applied moves, TS, A, B their sums over instances and TC the
number of capped instances. T = sum over positions q of mix64((q << 32) ^ t[q] ^ 0x5851F42D4C957F2D)
for the final tour, and R = sum over steps k = 1, 2, ... of mix64((k << 40) ^ (i << 20) ^ j ^ ((-delta) << 44))
for the move (i, j, delta) applied at step k (32-bit -delta, all sums modulo 2^64).
H combines every instance's result as in the CPU source.
Hidden inputs
0 <= P <= 400, 3 <= NMIN <= NMAX <= 4000, 0 <= ZMAX <= 100000, 1 <= CAP <= 1000.
Correctness inputs include P = 0, tours with n = 3 (no legal move), n = 4, 5, many
small instances, ZMAX = 0, ZMAX = 100000 (surcharges dominate), CAP = 1 (the step cap
is reached) and instances with n up to 900. The timing input has 110-130 instances with
n uniform in [600, 1800] and CAP = 50 (the cap is never reached there; a random tour
needs about 1.35 n steps).
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 110-130 instances with n uniform in [600, 1800] (about 1.35 n steps each, every step scanning all moves).
/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.