Emscripten, asm.js, and billions of math ops

10
EMSCRIPTEN, ASM.JS AND BILLIONS OF MATH OPS Luka Zakraj š ek @bancek Webcamp Ljubljana 2016 March 12, 2016

Transcript of Emscripten, asm.js, and billions of math ops

Page 1: Emscripten, asm.js, and billions of math ops

EMSCRIPTEN, ASM.JSAND BILLIONS OF MATH OPS

Luka Zakrajšek

@bancek

Webcamp Ljubljana 2016

March 12, 2016

Page 2: Emscripten, asm.js, and billions of math ops

PROBLEMmusic transcription - from audio samples to music notes

Page 3: Emscripten, asm.js, and billions of math ops

ASM.JSan extraordinarily optimizable, low-level subset of JavaScript

"Assembler for Web"

Page 4: Emscripten, asm.js, and billions of math ops

EMSCRIPTENLLVM-based project that compiles C and C++ into highly-

optimizable JavaScript in asm.js format

It lets you run C and C++ on the web at near-native speed,without plugins.

Page 5: Emscripten, asm.js, and billions of math ops

C CODE#include<stdio.h>

int main() { printf("hello, world!\n"); return 0; }

Page 6: Emscripten, asm.js, and billions of math ops

COMPILE$ emcc hello_world.c

And run:$ node a.out.js hello, world!

Page 7: Emscripten, asm.js, and billions of math ops

C LIBRARY EXAMPLE

var samplesData = Module._malloc(samples.length * 8); var outJson = Module._malloc(4); var outJsonSize = Module._malloc(4);

Module.HEAPF64.set(samples, samplesData / 8); Module.ccall('samplesToTtm', 'number', ['number', 'number', 'number', 'number', [samplesData, samplesCount, sampleRate, layer, outJson, outJsonSize]); Module._free(samplesData);

var jsonSize = Module.getValue(outJsonSize, 'i32'); var jsonPtr = Module.getValue(outJson, 'i32'); var jsonBytes = Module.HEAPU8.subarray(jsonPtr, jsonPtr + jsonSize); var json = new TextDecoder('utf-8').decode(jsonBytes);

JSON.parse(json);

Page 8: Emscripten, asm.js, and billions of math ops

SOME NUMBERS180s × 41 000 samples per second = 7 380 000 samples

3 600 hops × 345 buckets × 4 410 samples × 2 =

10 954 440 000 multiplications

Page 9: Emscripten, asm.js, and billions of math ops

RESULTS

Page 10: Emscripten, asm.js, and billions of math ops

DEMO