/ concept-collection / math-webgpu-sandbox
Sign in
concept-collection / math-webgpu-sandbox
math-webgpu-sandbox / examples / fused_loop.m
14 lines · 513 BCodeBlameHistory
02f2376Scale examples to 1-4 s of GPU work so tic/toc comparisons are meaningfulJeremy Magland 1% A long chain of elementwise math on 8 million elements, fused into ONE
2% GPU kernel per source line and replayed 600 times (about 5 billion updates).
3% Paste this whole script into MATLAB to compare (lower niter if slow).
4n = 8000000;
5niter = 600;
7y = zeros(n, 1);
8tic;
e01ddf1MATLAB-syntax scripts on WebGPU: fused kernels, tic/toc timing, CPU comparisonJeremy Magland 10 y = y + 0.1*sin(x + k) .* exp(-x) + x.^2;
11end
12t = toc;
02f2376Scale examples to 1-4 s of GPU work so tic/toc comparisons are meaningfulJeremy Magland 13fprintf('%.0f million element-updates/sec\n', niter*n/1e6/t);
e01ddf1MATLAB-syntax scripts on WebGPU: fused kernels, tic/toc timing, CPU comparisonJeremy Magland 14fprintf('checksum %.4f (rand differs from MATLAB; expect ~equal, not equal)\n', mean(y));
moveopenescclose