Port a meet-in-the-middle OpenMP search (sorted x^4+z^4 table, binary search per (y,w)) for x^4+3y^4+z^4 = k*w^4 to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-dioph-quartic-3y4-kw4harbor download elfsong/cpu2gpu-dioph-quartic-3y4-kw4/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.
For a bound N and several values of k, it finds every integer quadruple (x, y, z, w) with
x^4 + 3*y^4 + z^4 = k * w^4, 0 <= x <= z <= N, 0 <= y <= N, 1 <= w <= N
(x and z play symmetric roles, so only x <= z is counted). The CPU program builds a
sorted table of all x^4 + z^4 with 0 <= x <= z <= N once, then for each k and each
(w, y) with 0 <= k*w^4 - 3*y^4 <= 2*N^4 it looks the difference up in the table
(w only runs while k*w^4 <= 5*N^4, which loses no solutions).
For each k it reports the number of solutions, how many are primitive
(gcd(x, y, z, w) = 1), an order-independent hash, and one extreme solution.
N Q
k_1
...
k_Q
0 <= N <= 6000, 1 <= Q, each 1 <= k <= 20000. All values fit comfortably in unsigned
64-bit arithmetic (5*N^4 < 2^53).
One line per k, in input order:
k COUNT PRIM HASH x,y,z,w
COUNT: number of solutions; PRIM: number with gcd(x, y, z, w) = 1.HASH: sum over all solutions of mix64(x | y<<16 | z<<32 | w<<48) modulo 2^64, with
mix64 the SplitMix64 finaliser in the CPU source.x,y,z,w: the solution with the largest w; ties broken by smallest x, then smallest y
(then smallest z).COUNT is 0 the line is k 0 0 0 -.Correctness inputs include N = 0, N = 1, small N with every k from 1 to 40,
N up to 1500 with a dozen k, and one input with N = 6000 and two k. About half of all
k are planted as x^4 + 3y^4 + z^4 with 0 <= x, y, z <= 7 (so they have solutions with
w = 1 and all their multiples); the others are uniform in [1, 20000]. The timing input has
N in [5500, 6000] and 400 values of k.
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.