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 a multicore CPU program (C++17, OpenMP). It is built as
/app/bin/cpu_baseline with g++ -O3 -march=native -fopenmp -std=c++17 and run as
cpu_baseline INPUT_FILE
It reads one 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 CPU source is the specification: when this text and the code disagree, the code wins.
/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.nvcc works here for compiling. To run on the GPU, use
/app/bin/gpu_run [--repeat N] [--quiet] SOURCE.cu INPUT_FILE...: it compiles the file with the
flags above on a remote H100 worker, runs it on each input, and prints stdout, exit code and
wall-clock time. Grading also runs your code on an H100 worker, one job per GPU at a time.
Inputs are generated with a new random seed at grading time, so they cannot be known in advance.reward = min(2, t_reference / t_yours) using the medians./app/inputs/ holds a few public inputs. /app/bin/run_public.sh compiles your port with the
flags above and compares it against the CPU baseline on each of them.