Port an OpenMP segmented sieve that evaluates the multiplicative function g(p^e)=p^e+e*p^(e-1)+1 on every n in large intervals near 1e12 and reports abundance counts, checksums and the best g(n)/n witness
harbor run -t elfsong/cpu2gpu-numfn-gfn-abundanceharbor download elfsong/cpu2gpu-numfn-gfn-abundance/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.
Define the multiplicative function g on positive integers by g(1) = 1 and, for a prime
power,
g(p^e) = p^e + e * p^(e-1) + 1 (so g(p) = p + 2, g(p^2) = (p+1)^2, g(8) = 21)
and g(m*n) = g(m) * g(n) when gcd(m, n) = 1. For each query interval [L, L+W) the
program evaluates g(n) for every n in the interval (with a segmented sieve) and
reports:
C3 C4 C6 C8 C12: how many n satisfy g(n) >= k*n for k = 3, 4, 6, 8, 12.SUM: the sum of g(n) modulo 2^64.HASH: the sum of mix64(g(n) XOR (n << 1)) modulo 2^64 (mix64 is the SplitMix64
finaliser in the CPU source; the shift is a 64-bit shift).BN BG: the n with the largest ratio g(n)/n (exact rational comparison; ties go to
the smaller n) and its value g(BN).All values of g(n) in the allowed range fit in an unsigned 64-bit integer.
Q
L_1 W_1
...
L_Q W_Q
1 <= L, 0 <= W, and L + W - 1 <= 2*10^12 (when W > 0).
One line per query, in input order:
L W C3 C4 C6 C8 C12 SUM HASH BN BG
When W = 0 the line is L W 0 0 0 0 0 0 0 - -.
Correctness inputs (about 10 files, each with 2 to 5 queries) include W = 0, W = 1,
L = 1, intervals ending exactly at 2*10^12, intervals around perfect squares and powers
of 2 and 3, and random intervals with W up to 300000 and L anywhere in range.
The timing input has 4 queries with W drawn uniformly from [9*10^8, 1.1*10^9] and L
uniform in [10^11, 2*10^12 - W + 1].
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.