/ concept-collection / matmul-bench
Sign in
concept-collection / matmul-bench
matmul-bench / wasm / build-wasm.sh
45 lines · 1.6 KBBlameHistoryRaw
1#!/usr/bin/env bash
2# Builds wasm/matmul.c two ways:
3# - single-threaded ES module -> wasm/dist/matmul.{js,wasm}
4# (naive + blocked kernels; loaded through Vite's module worker)
5# - threaded (pthreads) module -> public/matmul/matmul_mt.{js,wasm}
6# (blocked+SIMD parallelized over rows; loaded by public/matmul/worker.js,
7# a classic worker, since it spawns nested pthread workers)
8# Requires: emsdk (expected at ~/emsdk, or already on PATH).
9set -euo pipefail
11cd "$(dirname "$0")"
13if ! command -v emcc >/dev/null 2>&1; then
14 source ~/emsdk/emsdk_env.sh
15fi
17mkdir -p dist ../public/matmul
19# Single-threaded ES module.
20emcc -O3 -msimd128 matmul.c \
21 -sMODULARIZE=1 \
22 -sEXPORT_ES6=1 \
23 -sEXPORT_NAME=createMatmulModule \
24 -sENVIRONMENT=web,worker \
25 -sALLOW_MEMORY_GROWTH=1 \
26 -sEXPORTED_FUNCTIONS=_matmul_naive,_matmul_blocked,_malloc,_free \
27 -sEXPORTED_RUNTIME_METHODS=HEAPF64 \
28 -o dist/matmul.js
30cp matmul.d.ts dist/matmul.d.ts
32# Threaded (pthreads) module. Classic (non-ES6) module so a classic worker can
33# importScripts it and spawn pthread workers. Fixed shared memory (growth is
34# costly with shared memory); pool must cover max threads + margin.
35emcc -O3 -msimd128 -pthread -DMATMUL_MT matmul.c \
36 -sMODULARIZE=1 \
37 -sEXPORT_NAME=createMatmulMT \
38 -sENVIRONMENT=web,worker,node \
39 -sPTHREAD_POOL_SIZE=20 \
40 -sINITIAL_MEMORY=512MB \
41 -sEXPORTED_FUNCTIONS=_matmul_blocked_mt,_malloc,_free \
42 -sEXPORTED_RUNTIME_METHODS=HEAPF64 \
43 -o ../public/matmul/matmul_mt.js
45echo "Built wasm/dist/matmul.{js,wasm,d.ts} + public/matmul/matmul_mt.{js,wasm}"
moveopenescclose