Portfolio credit VaR/ES with a factor copula, sector contagion LGD and tail attribution
/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 portfolio credit-risk run (economic-capital style) in exact integer / fixed-point arithmetic (Q15 loadings, Q16 normals and LGDs). The CPU source is the exact specification.
- Portfolio.
NOBLobligors, each with a sector (NSsectors, skewed sizes), a rating grade (7 grades with default thresholdsTH), a factor loadingbeta(0.3..0.65, Q15) andgam = isqrt(2^30 - beta^2), a heavy-tailed exposureeadand a base LGD. Each sector has a unit loading vector onNFglobal factors. Everything comes from SplitMix64 hashes of the seed. - Scenarios. For scenario
sc(keysk = mix64(seed ^ mix64(C + sc))):NFglobal factorsF(Irwin-Hall normals from two hashes each), sector factorsZ_s = (A_s . F) >> 15, and forNU > 0a Student-t scalem = isqrt((NU << 48) / max(chi, 1))wherechisumsNUsquared normals (m = 1.0for the Gaussian copulaNU = 0). Obligoridraws its idiosyncratic normal frommix64(sk ^ i*K), formsx = (((beta*Z_s + gam*eps) >> 15) * m) >> 16and defaults iffx <= TH[grade]. Sector contagion: a defaulted obligor's LGD islgd0 + 0.15*max(0, -Z_s) + 0.3*frac_s(capped at 1) wherefrac_s = (defaults in its sector in this scenario << 16) / sector size, so all default decisions of a scenario must be known before any loss can be computed. The scenario loss is the sum of(ead * lgd) >> 16over the defaulted obligors. - Loss distribution. Scenarios are ordered by
(loss, scenario index). Reported: expected loss, standard deviation (exact 128-bit moments), maximum loss and default counts; for levels 99%, 99.5%, 99.9%, 99.97%:j = ceil(NSC*level) - 1(at least 0), VaR = loss of rankj, ES = integer mean of the losses of ranksj..NSC-1; a 20-bin histogram of widthmax/20 + 1;SORTHASH= sum over ranksrofmix64(loss_r ^ r*0x9E3779B97F4A7C15). - Attribution of ES(99%). The tail scenarios (ranks
j99..NSC-1) are re-simulated; per sector the mean tail loss and the tail default rate in parts per million (SECTOR_TAILPD_ppm, 0 for an empty sector) are reported; for every obligor its summed loss and number of defaults over the tail scenarios (es_contrib = sum / ntail); the 12 largest contributors (ties by index).
Input format
One line: NOBL NS NF NSC NU SEED.
Output format
PORTFOLIO obligors=.. sectors=.. factors=.. scenarios=.. nu=.. total_ead=..
LOSS el=.. sd=.. max=.. mean_defaults_x1000=.. max_defaults=..
RISK level=<9900|9950|9990|9997> var=.. es=.. tail=<scenarios in the tail> (4 lines)
HIST width=<w> <20 counts>
SORTHASH <u64>
SECTOR_ES <NS values>
SECTOR_TAILPD_ppm <NS values>
ATTRIB tail_loss=<sum of tail losses> resim_loss=<same, from re-simulation> obligor_sum=.. obligors_hit=..
TOP <rank> obligor=.. sector=.. grade=.. ead=.. tail_defaults=.. es_contrib=.. (min(NOBL,12) lines)
Hidden inputs
Limits: 0 <= NOBL <= 200000, 1 <= NS <= 32, 1 <= NF <= 16, 0 <= NSC <= 4000000,
0 <= NU <= 30, NOBL * NSC <= 3*10^10.
Correctness inputs (13 files): an empty portfolio, NSC = 0, a single obligor and scenario,
NS = 32, NF = 16, NU = 1 with 2 tail scenarios, a case with exactly one tail scenario and
NU = 30, and random runs with NOBL in [100, 20000], NSC up to 20000
(NOBL * NSC <= 4*10^7), NU in {0, 3, 4, 5, 6, 10, 30}.
The timing input has NOBL uniform in [28000, 36000], NSC = round(2.4*10^10 / NOBL)
(about 750000 scenarios), NS in [20, 32], NF in [6, 12] and NU in {0, 4, 6, 8}.
Under the t copula, tail scenarios can default 40% or more of the portfolio.
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 simulates about 750000 scenarios of a 28000-36000 obligor portfolio (about 2.4e10 obligor-scenario default tests) with 20-32 sectors, 6-12 factors and a Gaussian or t copula. 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.