Exact best-first branch-and-bound for multidimensional 0-1 knapsack with surrogate bounds
/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
The program solves a batch of multidimensional 0-1 knapsack instances exactly: maximise
sum p_i x_i subject to sum_i a_ki x_i <= b_k for every constraint k < m, x_i in {0, 1}.
Instances are generated from (n, m, tight, noise, seed) with a counter-based hash
(mix64, see the CPU source for every constant): weights a_ki in [1, 1000], profit
p_i = (sum_k a_ki) / m + h_i % (noise + 1) (small noise = strongly correlated = hard),
capacity b_k = floor(sum_i a_ki * tight / 1000).
Surrogate multipliers. 32 hashed candidate vectors U_c[k] (perturbations of 2^20 / b_k) are
scored by their root bound; the 4 with the smallest root bound (ties to the lower candidate index)
are kept, best first. Items are then put into branching order: decreasing p_i / sw_0(i) with
sw_s(i) = sum_k U_s[k] a_ki, ties to the lower original index.
Upper bound of a partial assignment (first d items decided, profit P, residual capacities
r_k): for each kept surrogate s, the Dantzig bound of the free items (positions >= d) that
individually fit (a_kj <= r_k for all k), scanned in decreasing p / sw_s order (ties to the
lower position) against the capacity sum_k U_s[k] r_k, whole items while they fit and then
floor(p * remaining / sw) of the first item that does not; the bound is P + the minimum over
the 4 surrogates. All arithmetic is exact integer arithmetic.
Greedy solution: take the items in branching order whenever they fit in every dimension.
The CPU program runs a bulk-synchronous best-first branch-and-bound (rounds of the 4096 open nodes with the highest bounds, branching on the next item in branching order, greedy completion of every child as a candidate incumbent, pruning by the incumbent) and prints the optimum. Any exact search is acceptable: only the printed values must match.
Input format
T
n m tight noise seed (T lines, one per instance; seed is an unsigned 64-bit integer)
Output format
One line per instance, then the sum of the optima:
inst t n N m M greedy G root_bound UB optimum OPT
total X
G is the greedy value, UB the upper bound of the empty assignment (the root), OPT the
optimum.
Hidden inputs
0 <= T <= 64, 1 <= n <= 192, 1 <= m <= 12, 0 <= tight <= 1000, 0 <= noise <= 100000.
Correctness inputs have at most 12 instances each and include T = 0, n = 1, tight = 0
(nothing fits), tight = 1000 (everything fits), n at the 64-bit word boundaries (64, 65, 129,
130), long uncorrelated instances (n up to 192 with m <= 3), m up to 12, noise = 0;
every correctness instance is small enough for the CPU program to solve in well under a second.
The timing input has T = 32 strongly correlated instances with 46 <= n <= 50, m = 5,
tight = 500, noise = 100 (the hardness knob is n with noise fixed); each needs millions of
search nodes (about 2-11M for the CPU program), varying by about an order of magnitude between
instances.
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 32 strongly correlated instances (n = 46-50 items, m = 5 constraints, half-tight capacities); the CPU search opens about 1.6e8 nodes in total (2-11M per instance).
/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.