Mixed-size batched Montgomery modular exponentiation with CRT-style split items
/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 batch of M independent multi-precision modular exponentiations whose moduli have mixed
sizes (128 to 4096 bits, multiples of 128). Every item's data come from the counter hash in the
CPU source: key_i = mix64(SEED ^ mix64(i + 0x5851F42D4C957F2D)) and
word(key, s, j) = mix64(key + (s << 40) + j) (all arithmetic mod 2^64; mix64 is the
SplitMix64 finaliser). Numbers are little-endian arrays of 64-bit limbs built from word.
For item i:
- Class
k: the smallestkwithword(key,0,0) mod W < wcum_k(cumulative class weights,W= their total). The item hasb = BITS_kbits andL = b/64limbs. - Kind:
t = word(key,0,1) mod 1000; split ift < SPLIT, else near-power ift < SPLIT + NEARPOW, else plain. - Exponent length
E: ifword(key,0,2) mod 1000 < SMALLEXPthenE = word(key,0,3) mod 33(0..32, where 0 means the exponent is 0), otherwiseE = b(plain, near-power) orE = b/2(split). An exponent "of lengthEfrom streams" is the limbsword(key,s,j)truncated toEbits with bitE-1set. - Plain: modulus
N= limbsword(key,1,j),j < L, with the top bit and bit 0 forced to 1. Near-power:N = 2^b - cwithc = (word(key,1,0) mod 2^32) | 1. Basex= limbsword(key,2,j)(bbits, may exceedN), exponent of lengthEfrom stream 3. Resultr = x^e mod N. - Split (CRT-style,
H = L/2limbs, half sizeb/2):p= limbsword(key,1,j),j < H, top bit and bit 0 set. For attempta = 0, 1, 2, ...,q= limbsword(key,8+a,j)with top bit and bit 0 set; the firstqwithq mod p != 0andgcd(q mod p, p) = 1is kept. Basec= limbsword(key,2,j),j < L(bbits). Exponentsdp(stream 3) anddq(stream 4), both of lengthE. Thenm1 = c^dp mod p,m2 = c^dq mod q,h = (q mod p)^(-1) * (m1 - m2) mod p, andr = m2 + h*q(so0 <= r < p*q).pandqare not primes; this is simply the defined recombination.
x^0 = 1 for every modulus. The CPU uses CIOS Montgomery multiplication with 64-bit limbs and
fixed 4-bit exponent windows, but any method producing the same exact integers is fine
(other limb sizes, other windowing, several threads per number, ...).
Input format
SEED M K SPLIT NEARPOW SMALLEXP
BITS_1 WEIGHT_1
...
BITS_K WEIGHT_K
SEED unsigned 64-bit, 0 <= M, 1 <= K <= 32, 128 <= BITS_k <= 4096 with BITS_k mod 128 = 0,
WEIGHT_k >= 1, permille values SPLIT + NEARPOW <= 1000, 0 <= SMALLEXP <= 1000.
Output format
items M classes K
class k bits BITS_k count C split S nearpow P hash H xorlow X (one line per class)
first k item i ebits E r 0x... (first non-split item of class k, if any)
firstsplit k item i ebits E r 0x... (first split item of class k, if any)
total T
r is printed as L zero-padded 16-digit hex limbs, most significant first. With
h_i = fold_j mix64(h ^ r_j ^ (j << 56)) starting from h = mix64(i ^ 0xD1B54A32D192ED03)
(the result_hash function), H is the sum of h_i over the class mod 2^64, X the XOR of the
low limbs r_0, and T = sum_i h_i * (2i+1) mod 2^64. Hex fields are 16 digits. The first
and firstsplit lines come in class order, first before firstsplit.
Hidden inputs
Correctness inputs (about 12): an empty batch, a single 128-bit item, three full-size 4096-bit items, batches of 20 to 300 items over random subsets of sizes 128..4096, all-split batches, all-near-power batches, all-short-exponent batches, and mixed batches. The timing input has 28,000 to 34,000 items over the four classes 1024, 2048, 3072 and 4096 bits (weights 3-5, 3-5, 1-3 and 1-3, in random order), with 20-30% split items, 5-10% near-power moduli and 3-6% short exponents.
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 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.
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. - GPU verification uses the backend selected for this run: an H100 attached to the separate verifier, or a remote H100 worker. Hidden inputs use a fresh random seed.
- For public GPU experiments, use
/app/bin/gpu_run [--backend auto|local|remote] [--repeat N] [--quiet] SOURCE.cu INPUT_FILE.... The default usesGPU_RUNNER_URLwhen configured, otherwise a GPU attached to this sandbox. Sandbox GPU access is configured independently from verification; a CPU-only sandbox needs a runner URL. - 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.
- GPU execution: a separate, untimed CUPTI check must observe completed GPU kernel activity on the hidden timing input. A CPU-only submission receives reward 0 even if its output is correct.
- Speed: 28,000 to 34,000 exponentiations: moduli of 1024, 2048, 3072 and 4096 bits (the big classes dominate the work), 20-30% CRT-style split items, 5-10% near-power moduli and 3-6% short exponents. 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.