/ concept-collection / matmul-bench
Sign in
concept-collection / matmul-bench
matmul-bench / blis / matmul_blis.c
35 lines · 1.3 KBBlameHistoryRaw
1/*
2 * Thin matmul wrapper over libflame's LAPACK-compat dgemm_ (backed by BLIS),
3 * built to WASM by build-blis-wasm.sh against the prebuilt libraries in
4 * ../../libflame2wasm. Two builds: single-threaded (st) and pthreads (mt).
5 *
6 * The rest of matmul-bench works in row-major; dgemm_ is column-major
7 * (Fortran). Row-major C = A*B is obtained from a column-major GEMM by
8 * swapping the operands: passing (B, A) computes C^T = B^T * A^T in
9 * column-major, which is exactly row-major C = A*B in the same flat buffer.
10 * So c[0] matches the other (row-major) methods bit-for-bit-ish.
11 */
12#include <emscripten.h>
14extern int dgemm_( char* transa, char* transb, int* m, int* n, int* k,
15 double* alpha, double* a, int* lda, double* b, int* ldb,
16 double* beta, double* c, int* ldc );
18#ifdef BLIS_MT
19/* dim_t is int32 in this BLIS build (--int-size=32). */
20extern void bli_thread_set_num_threads( int n_threads );
21#endif
23EMSCRIPTEN_KEEPALIVE
24void matmul_blis( double* a, double* b, double* c, int n, int nthreads )
26#ifdef BLIS_MT
27 if ( nthreads > 0 ) bli_thread_set_num_threads( nthreads );
28#else
29 (void) nthreads;
30#endif
31 char tr = 'N';
32 double alpha = 1.0, beta = 0.0;
33 /* Operand swap: (B, A) column-major => row-major C = A*B. */
34 dgemm_( &tr, &tr, &n, &n, &n, &alpha, b, &n, a, &n, &beta, c, &n );
moveopenescclose