Prime census at 1e14 with a four-weight Lucy_Hedgehog DP
/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.
What the program computes
A prime census of [1, N] for several large N, built with the Lucy_Hedgehog dynamic
programme and then analysed.
Key set. Let r = isqrt(N) and rL = r - 1 if N / r == r, else rL = r. The keys are the
small values v = 1..r and the large values v = N / i for i = 1..rL (all larger than r);
K = r + rL distinct keys in total. Sorted ascending, key position pos < r is v = pos + 1,
and position pos >= r is v = N / (rL - (pos - r)).
Lucy DP. For every key four sums over 2 <= n <= v are kept (record (c, s, a, b)):
weight 1 (c, a count), weight n (s, modulo 2^64), chi4(n) (a) and chi3(n) (b), where
chi4(n) = +1, -1, 0 for n mod 4 = 1, 3, even and chi3(n) = +1, -1, 0 for n mod 3 = 1, 2, 0.
They start as the sums over all integers (v - 1, v(v+1)/2 - 1, ...). Then for every prime
p <= r in increasing order, every key v >= p^2 is updated with the values of the previous round:
F(v) -= f(p) * (F(floor(v/p)) - F(p-1)) for F in (c, s, a, b), f in (1, p, chi4, chi3)
Afterwards F(v) is the sum of f over the primes <= v: c = pi(v), s = sum of primes mod 2^64,
a = pi(v;4,1) - pi(v;4,3), b = pi(v;3,1) - pi(v;3,2). (The CPU program schedules the updated
keys of a round in "levels" so that a level never reads a key written in the same round, and
runs the many tiny rounds of large primes serially; any schedule giving the same values is fine.)
Analysis (all exact integers):
PI, SUMP, CHI4, CHI3: the record of the keyN.S2= number of pairs of primesp <= qwithp*q <= N(semiprimes<= N).S3= number of triples of primesp <= q <= twithp*q*t <= N.- Over all
Kkeys in ascending order:NEG4= number of keys witha < 0,NEG3= number withb < 0,FLIPS= number of consecutive key pairs whoseavalues have strictly opposite signs. Qinterval queries perN: forq = 0..Q-1,h1 = mix64(SEED ^ mix64(N + q*0xD1B54A32D192ED03)),h2 = mix64(h1 + 0x8CB92BA72F3D8DD7), positionsx = h1 mod K,y = h2 mod K(swapped so thatx <= y); the answer is the difference of the two key records (primes in(key_x, key_y]).QNEG4/QNEG3count queries whosea/bdifference is negative, andQHASHis the sum mod 2^64 ofmix64(dc ^ rotl(ds,17) ^ d4*0x9E3779B97F4A7C15 ^ d3*0xC2B2AE3D27D4EB4F ^ q)(products modulo 2^64,mix64= SplitMix64 finaliser as in the CPU source).
Input format
T SEED
N_1 Q_1
...
N_T Q_T
1 <= N <= 1.3*10^14, 0 <= Q <= 10^6, SEED is an unsigned 64-bit integer.
Output format
One line per N:
N PI SUMP CHI4 CHI3 S2 S3 NEG4 NEG3 FLIPS QNEG4 QNEG3 QHASH
CHI4 and CHI3 are signed, everything else unsigned decimal.
Hidden inputs
Correctness inputs (8 files, 1 to 8 values of N each): tiny N (1, 2, 3, 4, 8, 9), N = 26861
and 26862, N = r^2, r^2 - 1, r(r+1), r(r+1) - 1, the square of a prime, N = 10^12, and
random N in [10^4, 2*10^11]; Q from 0 to 20000, seeds include 0 and 2^64-1. The timing input
has three values of N uniform in [8*10^13, 1.2*10^14] with Q = 10^6 each.
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 CPU source is the specification: when this text and the code disagree, the code wins.
How you are scored
- 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. - This sandbox has no GPU.
nvccworks 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. - Correctness: every hidden input (small edge cases plus the timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout gives reward 0.
- Speed: The timing input has three values of N uniform in [8e13, 1.2e14] (about 1e7 small and 1e7 large keys and 6.6e5 prime rounds each) with 10^6 interval queries per N. Your port and our GPU reference are each run on it
once to warm up and then 5 times, interleaved; the time is the wall clock of the whole process
(including CUDA initialisation, input parsing and output). When correct,
reward = min(2, t_reference / t_yours)using the medians. /app/inputs/holds a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.