/ concept-collection / matmul-bench
Sign in
concept-collection / matmul-bench
matmul-bench / src / methods / jsMatmul.ts
34 lines · 907 BCodeBlameHistory
666db68matmul-bench: browser GEMM benchmark (JS, WebGPU, custom C WASM, libFLAME/BLIS WASM)Jeremy Magland 1import type { MatmulMethod } from './types'
3// Plain triple loop, ikj order (row of A and row of C stay hot across the
4// inner loop) — the "no special effort" baseline the other methods are
5// measured against.
6function multiply(a: Float64Array, b: Float64Array, n: number): Float64Array {
7 const c = new Float64Array(n * n)
8 for (let i = 0; i < n; i++) {
9 const ai = i * n
10 for (let k = 0; k < n; k++) {
11 const aik = a[ai + k]
12 if (aik === 0) continue
13 const bk = k * n
14 for (let j = 0; j < n; j++) {
15 c[ai + j] += aik * b[bk + j]
16 }
17 }
18 }
19 return c
22export const jsMatmul: MatmulMethod = {
23 id: 'js',
24 label: 'JavaScript/TS',
25 precision: 'f64',
26 worker: true,
27 available: () => true,
28 async run(n, a, b) {
29 const t0 = performance.now()
30 const c = multiply(a, b, n)
31 const ms = performance.now() - t0
32 return { ms, sample: c[0] }
33 },
moveopenescclose