1% Monte Carlo estimate of pi from 20 BILLION samples, drawn in 2000 batches.
2% (Pasting into MATLAB? Cut batches to ~50 -- generating this many doubles
3% takes CPUs minutes, which is rather the point.)
4% The draws, the comparison and the mean all fuse into one reduction pass,
5% so each batch's 10 million points are generated, tested and counted
6% without ever touching GPU memory.
7n = 10000000;
8batches = 2000;
9s = zeros(1, 1);
10tic;
11for k = 1:batches
12 s = s + mean(((2*rand(n,1) - 1).^2 + (2*rand(n,1) - 1).^2) <= 1);
13end
14t = toc;
15fprintf('pi ~ %.6f (%.1f billion samples, %.0f million samples/sec)\n', ...
16 4*s/batches, batches*n/1e9, batches*n/1e6/t);