Batch max-flow and minimal sink-side cuts
/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
For a batch of generated flow networks the program computes the maximum s-t flow value and the minimal sink side of the minimum cut: the set of vertices that can still reach the sink in the residual graph of a maximum flow. That set is the same for every maximum flow (and equals the one obtained from any maximum preflow), so it is well defined.
All capacities are integers produced by the counter-based hash mix64 (SplitMix64 finaliser in the
CPU source); the exact formulas and hash keys are in the CPU source and must be reproduced bit for
bit. Every network has n vertices with the source s = n-2 and the sink t = n-1. An edge slot
(u, v, c_uv, c_vu) gives an arc u -> v with capacity c_uv and an arc v -> u with capacity
c_vu (often 0); parallel edges are allowed.
Grid networks (type 0, parameters W H LAMBDA, segmentation style): W*H pixels plus s and
t. Pixel intensities are 170 inside 3-6 random discs and 80 outside, plus noise in [-32, 31].
Every pixel has one terminal edge: the disc centres are hard foreground (s -> p, capacity 100000),
the four corners hard background (p -> t, 100000), any other pixel is joined to s (bright) or to
t (dark) with a capacity that grows with its distance from 128. Horizontal and vertical neighbours
are joined by contrast-dependent capacities LAMBDA * 64 / (1 + |I_p - I_q|) plus LAMBDA in the
bright-to-dark direction (vertical neighbours use LAMBDA + LAMBDA/2).
Layered networks (type 1, parameters K WD D): K layers of WD vertices plus s and t.
s feeds every vertex of layer 0 and every vertex of layer K-1 drains into t (capacities
500..4999). Every vertex of layers 0..K-2 has D arcs to random vertices of the next layer
(capacity 1..1000), with probability 1/8 a skip arc two layers ahead (1..500) and, outside layer 0,
with probability 1/16 a back arc one layer behind (1..300).
Input format
B
TYPE A B C SEED (one line per network: TYPE 0 -> A B C = W H LAMBDA, TYPE 1 -> A B C = K WD D)
Output format
graph i type T n N arcs A flow F sinkside S cutarcs C hash H (one line per network)
total flow X
Ais the number of arcs,2 *the number of edge slots that are present (zero-capacity arcs included).Fis the maximum flow value,Xthe sum over all networks.Sis the number of vertices other thantthat can reachtin the residual graph.Ccounts the arcsu -> vwith positive capacity,uoutside andvinside that sink side.His the sum over thoseSverticesvofmix64(SEED ^ (v * 0xA24BAED4963EE407))modulo 2^64.
Hidden inputs
- Correctness inputs: 1 to 8 networks per input; grids with
1 <= W, H <= 128and0 <= LAMBDA <= 1000, layered networks with1 <= K <= 120,1 <= WD <= 128,1 <= D <= 8. They include 3-vertex networks (W = H = 1,K = WD = 1), single rows and columns,LAMBDA = 0(no smoothing, flow 0),LAMBDA = 1000, a single layer, width-1 layers and deep narrow networks. - Timing input: 800 networks; each is a grid with probability 0.45 (
W, Hin[256, 1024],LAMBDAin[5, 60]), otherwise a layered network (Kin[20, 200],WDin[64, 4096]withK * WD <= 250000,Din[2, 5]). About 215 million vertices and 1.4 billion arcs in total. SEED < 2^63. AlwaysW, H <= 1024,K * WD <= 2^20.
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: 800 networks: about 45% grids of 256..1024 x 256..1024 pixels (4-neighbour, directed contrast capacities, hard seeds) and 55% layered random networks of 20-200 layers with up to 250000 vertices; about 215 million vertices and 1.4 billion arcs in total.
/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.