#!/usr/bin/env bash # Builds wasm/matmul.c two ways: # - single-threaded ES module -> wasm/dist/matmul.{js,wasm} # (naive + blocked kernels; loaded through Vite's module worker) # - threaded (pthreads) module -> public/matmul/matmul_mt.{js,wasm} # (blocked+SIMD parallelized over rows; loaded by public/matmul/worker.js, # a classic worker, since it spawns nested pthread workers) # Requires: emsdk (expected at ~/emsdk, or already on PATH). set -euo pipefail cd "$(dirname "$0")" if ! command -v emcc >/dev/null 2>&1; then source ~/emsdk/emsdk_env.sh fi mkdir -p dist ../public/matmul # Single-threaded ES module. emcc -O3 -msimd128 matmul.c \ -sMODULARIZE=1 \ -sEXPORT_ES6=1 \ -sEXPORT_NAME=createMatmulModule \ -sENVIRONMENT=web,worker \ -sALLOW_MEMORY_GROWTH=1 \ -sEXPORTED_FUNCTIONS=_matmul_naive,_matmul_blocked,_malloc,_free \ -sEXPORTED_RUNTIME_METHODS=HEAPF64 \ -o dist/matmul.js cp matmul.d.ts dist/matmul.d.ts # Threaded (pthreads) module. Classic (non-ES6) module so a classic worker can # importScripts it and spawn pthread workers. Fixed shared memory (growth is # costly with shared memory); pool must cover max threads + margin. emcc -O3 -msimd128 -pthread -DMATMUL_MT matmul.c \ -sMODULARIZE=1 \ -sEXPORT_NAME=createMatmulMT \ -sENVIRONMENT=web,worker,node \ -sPTHREAD_POOL_SIZE=20 \ -sINITIAL_MEMORY=512MB \ -sEXPORTED_FUNCTIONS=_matmul_blocked_mt,_malloc,_free \ -sEXPORTED_RUNTIME_METHODS=HEAPF64 \ -o ../public/matmul/matmul_mt.js echo "Built wasm/dist/matmul.{js,wasm,d.ts} + public/matmul/matmul_mt.{js,wasm}"