Portfolio credit VaR/ES with a factor copula, sector contagion LGD and tail attribution
/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.
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 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).
How your port is verified
- 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. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- 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.
/app/inputs/timing_example.inhas 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.