Port an OpenMP exact big-integer product tree (self-written NTTs over three primes, Garner CRT, carry propagation) to CUDA and make it fast
harbor run -t elfsong/cpu2gpu-fft-ntt-product-treeharbor download elfsong/cpu2gpu-fft-ntt-product-tree/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.
The exact product of M pseudo-random big integers, built with a binary product tree.
2^20 limbs. Factor i (0 <= i < M) has
L_i = LMIN + mix64(SEED ^ (0x8CB92BA72F3D8DD7 * (i+1))) mod (LMAX - LMIN + 1) limbs; limb j
is mix64(base_i ^ (j * 0xD6E8FEB86659FD93)) & (2^20 - 1) with
base_i = mix64(SEED + 0x632BE59BD9B4E019 * (i+1)) (all arithmetic mod 2^64, mix64 is the
SplitMix64 finaliser), then the top limb gets bit 19 set and limb 0 gets bit 0 set.l+1 is obtained from level l by multiplying nodes
2v and 2v+1 into node v; when the level has an odd number of nodes the last one is
carried up unchanged. This repeats until one node (the full product) is left.The CPU program multiplies with its own number-theoretic transforms modulo the primes
2013265921, 469762049 and 167772161, Garner CRT and carry propagation. Any exact method
is acceptable (other primes, Montgomery/Barrett/Shoup reductions, other limb sizes internally,
batching, mixed strategies per size, ...): only the printed integers matter.
One line: M LMIN LMAX SEED (M >= 1, 1 <= LMIN <= LMAX, unsigned 64-bit SEED).
factors M limbs TOTAL
level l nodes K limbs T hash H (one line per level, l = 0 .. last)
product limbs n top a b c d bottom e f g h
TOTAL is the number of limbs over all factors; T the number of limbs over all nodes of the
level; H = sum over nodes v of the level and limbs j of
mix64(limb + (j << 20) + (v << 45) + (l << 56)) modulo 2^64.Output is compared exactly.
1 <= M <= 512, and M * LMAX <= 2^24 (so the product has at most 2^24 limbs).M = 1, single-limb factors, M = 2 with factors of up to
260000 limbs, very uneven lengths (LMIN = 1, LMAX = 40000), and M = 512 tiny factors.M in [192, 320] and limb lengths in about
[26000, 46000] * 256 / M, about 9 million limbs in total.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.