aab0fc7libflame + BLIS compiled to WebAssemblyJeremy Magland 1// Runs the wasm benchmark modules off the main thread. The threaded (mt)
2// variant spawns its pthread workers from here (nested workers).
3;
5const instances = {}; // variant -> { module, run }
7async function getInstance(variant) {
8 if (!instances[variant]) {
9 importScripts(`dist/bench_${variant}.js`);
10 const factory = variant === 'mt' ? createBenchMT : createBenchST;
11 const module = await factory({
12 // Loaded via importScripts, the module would otherwise resolve the
13 // .wasm (and the script pthread workers re-load) relative to THIS
14 // worker's URL — point both back into dist/.
15 locateFile: (path) => 'dist/' + path,
16 mainScriptUrlOrBlob: `dist/bench_${variant}.js`,
17 print: (line) => postMessage({ type: 'log', line }),
18 printErr: (line) => postMessage({ type: 'log', line: '[stderr] ' + line }),
19 });
20 instances[variant] = {
21 module,
22 run: module.cwrap('run_bench', 'number', ['number', 'number', 'number']),
23 };
24 }
25 return instances[variant];
26}
28onmessage = async (e) => {
29 const { id, variant, routine, n, threads } = e.data;
30 try {
31 const inst = await getInstance(variant);
32 const t0 = performance.now();
33 const gflops = inst.run(routine, n, threads);
34 const seconds = (performance.now() - t0) / 1000;
35 postMessage({ type: 'result', id, gflops, seconds });
36 } catch (err) {
37 postMessage({ type: 'error', id, message: String(err && err.message || err) });
38 }
39};