/ concept-collection / math-webgpu-sandbox
Sign in
concept-collection / math-webgpu-sandbox
math-webgpu-sandbox / examples / monte_carlo_pi.m
9 lines · 287 BBlameHistoryRaw
1% Monte Carlo estimate of pi: comparisons and logicals fuse too, so the
2% mask never touches memory -- one fused pass plus the reduction.
3n = 10000000;
4tic;
5x = 2*rand(n, 1) - 1;
6y = 2*rand(n, 1) - 1;
7pi_est = 4*mean((x.^2 + y.^2) <= 1);
8toc
9fprintf('pi ~ %.6f (n = %d)\n', pi_est, n);
moveopenescclose