Port an OpenMP DP that counts weighted walks on an obstacle grid with a generated step set and step-succession weights (state = last step x cell, modulo a prime)
harbor run -t elfsong/cpu2gpu-count-walks-step-followharbor download elfsong/cpu2gpu-count-walks-step-follow/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.
A walker moves on an X x Y grid of cells. Some cells are blocked (cell c = y*X + x is
blocked when mix64(seed ^ (c*0xC2B2AE3D27D4EB4F + 0x165667B19E3779F9)) mod 1000 < RHO;
the start cell (SX, SY) is never blocked). The walker uses a generated set of S
distinct non-zero step vectors with coordinates in [-R, R], and the weight of taking
step s right after step s' is A[s'][s] in {0, 1, 2, 3} (0 forbids that
succession). Both are generated from the seed exactly as in the CPU source.
A walk starts at the start cell together with a "previous step" chosen freely among the
S steps (so f_0[s][start] = 1 for every s). Every step must land inside the grid on
an unblocked cell. The weight of a walk is the product of the succession weights. The
program computes f_t[s][y][x], the total weight (modulo the prime P) of walks of
length t that end at (x, y) with last step s, for t = 1..T:
f_t[s][y][x] = 0 if (x,y) blocked, else sum_{s'} A[s'][s] * f_{t-1}[s'][y - dy_s][x - dx_s]
(source cells outside the grid contribute 0).
One line: X Y S R T P RHO SX SY SEED.
X=<X> Y=<Y> S=<S> T=<T> P=<P> blocked=<number of blocked cells> steps: (dx,dy) ...
t <t> sum <SUM> ret <RET> hash <HASH> (one line per checkpoint)
Checkpoints are t = floor((j+1)*T/8) for j = 0..7 (only t >= 1, duplicates once)
plus t = T, in increasing order.
SUM: sum of all f_t values modulo P.RET: sum_s f_t[s][SY][SX] modulo P (walks that are back at the start).HASH: sum over all states i = (s*Y + y)*X + x of mix64((i << 32) | f_t[i])
modulo 2^64, with every f_t value reduced to [0, P).1 <= X, Y <= 1024, 1 <= R <= 3, 1 <= S <= min(24, (2R+1)^2 - 1),
1 <= T <= 6000, 0 <= RHO <= 1000, 0 <= SX < X, 0 <= SY < Y.P is a prime with 2 <= P < 2^30.RHO = 1000), small primes, and a 1024x1024 grid with few steps.X, Y in [640, 1024], S in [8, 12], R in {2, 3}, RHO in
[50, 200], the start in the middle half of the grid, and T chosen so that
T * S^2 * X * Y is about 1.2e11 (roughly 800 to 4700 steps).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).
/app/gpu/main.cu is collected. It is compiled with
nvcc -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.system(, popen(, fork(, exec*, dlopen(, getenv(,
#include <filesystem>, /tests/, /proc/. The program may read only the input file it is given./app/inputs/timing_example.in has 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.