logoalt Hacker News

articulatepang • today at 8:48 PM • 0 replies • view on HN

Whenever a GPU kernel is launched, each thread has an id. In cuda that's threadIdx.x, threadIdx.y, blockIdx.x, etc. So in any GPU kernel there's a very natural way to have a counter within each kernel.

To get a unique stream of random numbers across an entire program execution, you also need different kernel launches to each add an amount so it's different from the previous kernel's counters.

The problem with small-state PRNG's is that each GPU thread has to do memory reads and writes to VRAM to get and persist the state. That takes memory bandwidth, which is what most GPU workloads (but not all!) are typically bottlenecked on.

Classic PCG is a stateful generator, and takes O(log n) steps to seek ahead. The log n may or may not be okay in GPU code; one risk is that the branching causes warp divergence.

I agree that encryption is expensive! But encryption based PRNGs usually end up being the fastest for most GPU workloads, because the methods used in production are derived from ciphers, basically by reducing the number of rounds. That's how Philox and Threefry work.

For benchmarks, see https://klauria.tech/wgsl-rng-benchmark/