/* Time every example on desktop Dawn: npx vite-node scripts/time-examples.ts */ import { readFileSync, readdirSync } from 'node:fs'; import { installWebGpu } from './nodeWebGpu.ts'; import { runScript } from '../src/mgpu/session.ts'; import { formatFailure } from '../src/mgpu/errors.ts'; 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, }, }); for (const f of readdirSync('examples').sort()) { if (!f.endsWith('.m')) continue; const source = readFileSync(`examples/${f}`, 'utf8'); try { const run = await runScript(device, source); const segs = run.result.segments.map((s) => s.seconds.toFixed(3)).join(', '); console.log(`\n=== ${f} [tic..toc: ${segs} s ยท total ${run.result.totalSeconds.toFixed(3)} s]`); console.log(run.result.output.trimEnd()); if (run.result.error) console.log('ERROR:', run.result.error); } catch (e) { console.log(`\n=== ${f} COMPILE FAIL: ${formatFailure(e, source)}`); } } process.exit(0); } main().catch((e) => { console.error(e); process.exit(1); });