Multi-asset limit-order-book agent-based market with FIFO maker settlement
/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
An agent-based market simulation: N trading agents and A assets, each asset with a
continuous limit order book of 1024 integer price levels (0..1023) per side, run for T ticks.
All arithmetic is exact integer arithmetic; all randomness comes from the counter-based hash
hh(seed, a, b, c) (SplitMix64 finaliser chain) in the CPU source.
Setup. Asset popularity is Zipf-like: asset a has weight floor(2^24/(a+4)). Agent i gets a
type (noise 50%, fundamentalist 25%, momentum 15%, market maker 10%), a primary asset drawn
from the Zipf weights, a uniform secondary asset, starting cash and 20 units of each asset.
Asset a gets an initial price and a fundamental value. Books start empty.
Each tick t runs these dependent stages:
- Fundamentals: every asset's fundamental takes a bounded random step (occasional 8x jumps).
- Decide: every agent draws an activity test (rate depends on its type). An active agent
first cancels its resting order (if any), then picks one of its two assets and, from the
asset's state at the end of the previous tick (mid price or last price, last return,
fundamental) and its own cash/position, may send one order (limit or immediate-or-cancel)
with a price clamped to
[1, 1022]and a quantity limited by its cash (buys) or by position + 40 (sells; short limit 40). Exact rules per type are in the CPU source. - Route: orders get a 24-bit arrival time from the hash; they are grouped by asset and
processed in (arrival, agent) order. The order's rank
jin this global sorted list gives its queue sequence number(t << 20) | j. - Match: per asset, sequentially, price-time priority: a buy walks the ask levels from the
best (lowest) upward while the level price is
<=its limit (a sell walks bids downward); every level touched produces one trade print at that level's price. The unfilled remainder of a limit order rests at its limit price (the agent's single live order); an IOC remainder is dropped. Books keep aggregated quantity per level plus an occupancy bitmap. - Taker settlement: the taker pays/receives the traded value plus/minus a fee
value >> 9. - Maker settlement (FIFO): at every (asset, side, level) the quantity consumed during the
tick is allocated to the resting orders in increasing sequence number (older first, then
this tick's orders in rank order). A maker receives a rebate
(fill*price) >> 10. Fully filled orders die. - Statistics: close = last trade price of the tick (or previous close), tick return
r = close - previous close, sums ofr^2,r^4, count of|r| >= 6, max|r|, volume, prints, end-of-tick spread when both sides are non-empty; per-window (10 windows of the run) market volume and prints.
Input format
One line: N A T SEED (all integers, SEED an unsigned 64-bit value).
Output format
TRADES <prints> VOLUME <units> NOTIONAL <sum of taker values> FEES <sum> REBATES <sum>
RESTING <live orders at end> BOOKQTY <total resting quantity>
KURT_HIST <8 counts> # buckets of floor(T*sum r^4*1000/(sum r^2)^2): <3000,<4000,<6000,<10000,<20000,<50000,>=50000,undefined
TOPVOL <asset> r2 .. big .. max .. vol .. trades .. spread_x100 .. kurt_x1000 .. close .. # 10 highest sum r^2 (ties: lower id)
WINDOW <w> volume <v> trades <n> # w = 0..9, tick t in window t*10/T
TYPE <name> n <count> wealth <sum> pnl <sum> abspos <sum> # wealth = cash + positions at final close
RICH <agent> type <k> wealth <w> # 5 richest (ties: lower id)
HASH agents <hex> assets <hex>
All values must match exactly.
Hidden inputs
Correctness inputs (12 files): N = 0, N = 1 with A = 1, a single book with 5000 agents,
T = 0, A = 4096 with 12000 agents, hot books (A = 16, 40000 agents), and random cases with
N <= 40000, A <= 4096, T <= 60. Always 0 <= N <= 1000000, 1 <= A <= 4096,
0 <= T <= 4000. The timing input has N in [950000, 1000000], A in [3600, 4096] and
T = 3000 ticks. Order flow per asset is very skewed: the most popular asset receives roughly
5% of all orders while thousands of assets receive only a few per tick.
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 about 1e6 agents, 3600-4096 assets and 3000 ticks (roughly 1e8 orders in total, heavily skewed toward the most popular assets). 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.