/* Quick timing sanity: does the sandbox produce sane benchmark numbers? */ import { installWebGpu } from './nodeWebGpu.ts'; import { runScript } from '../src/mgpu/session.ts'; const script = ` n = 4000000; x = rand(n, 1); y = zeros(n, 1); tic; for k = 1:100 y = y + 0.1*sin(x + k) .* exp(-x) + x.^2; end toc fprintf('checksum %.4f\\n', mean(y)); m = 1024; A = rand(m, m); B = rand(m, m); C = A * B; tic; for k = 1:10 C = A * B; end toc fprintf('gemm checksum %.2f\\n', sum(C(:)) / m); tic; s = 0; for k = 1:100 s = s + sum(x); end toc fprintf('reduce checksum %.1f\\n', s / 100); `; async function main(): Promise { console.log(await installWebGpu()); const adapter = await navigator.gpu.requestAdapter(); const device = await adapter!.requestDevice({ requiredLimits: { maxStorageBufferBindingSize: adapter!.limits.maxStorageBufferBindingSize, maxBufferSize: adapter!.limits.maxBufferSize, }, }); const run = await runScript(device, script); console.log('--- output ---'); console.log(run.result.output); console.log('--- plan ---'); console.log(run.planDescription.join('\n')); console.log(`compile: ${(run.compileSeconds * 1000).toFixed(1)} ms`); console.log(`total: ${(run.result.totalSeconds * 1000).toFixed(1)} ms`); if (run.result.error) console.log('ERROR:', run.result.error); const flops = (2 * 1024 ** 3 * 10) / 1e9; const gemmSeg = run.result.segments[1]; console.log(`gemm: ${(flops / gemmSeg.seconds).toFixed(1)} GFLOP/s over 10 reps`); process.exit(0); } main().catch((e) => { console.error(e); process.exit(1); });