Matrix multiplication with asm.js

JavaScript performance comparison

Revision 4 of this test case created by Alexander Gessler

Info

Trying super-fast matrix multiplication with asm.js by using a huge pool of matrices exclusively used as asm.js heap. User code indexes matrices by their position in that pool.

github.com/acgessler Matrix mult code from Brandon Jones's Gl-matrix

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
   
    // trying super-fast matrix multiplication with asm.js by using a huge pool
    // of matrices exclusively used as asm.js heap. User code indexes matrices
    // by their position in that pool.
   
    // Alexander Gessler, 2013 (github.com/acgessler)
   
    var COUNT = 100;
    var elements = 16 * COUNT * 4;
   
    var matrixpool = new ArrayBuffer((Math.ceil(elements/4096.0) * 4096) | 0); // multiple of 4096 bytes
    console.log('mpool: ' + matrixpool.length);
   
    function mat_mod(stdlib, foreign, heap) {
        "use asm";
   
        var m = new stdlib.Float32Array(heap);
   
        function multiply(idx_a, idx_b, idx_dest) {
                idx_a = idx_a | 0;
                idx_b = idx_b | 0;
                idx_dest = idx_dest | 0;
   
                var  i = 0, j = 0, c = 0.0, k = 0, t0 = 0.0, t1 = 0.0, i0 = 0, i1 = 1;
   
                idx_a = idx_a << 4;
                idx_b = idx_b << 4;
                idx_dest = idx_dest << 4;
   
                for(i = 0; (i|0) < 4; i = (i + 1) | 0 ) {
                        for(j = 0; (j|0) < 4; j = (j + 1) | 0 ) {
                                c = 0.0;
                                for(k = 0; (k|0) < 4; k = (k + 1) | 0 ) {
                                        i0 = (idx_a + (i<<2) + k) | 0;
                                        i1 = (idx_b + (k<<2) + j) | 0;
                                        t0 = +m[i0>>2];
                                        t1 = +m[i1>>2];
                                        c = +(c + t0 * t1);
                                }
                                i1 = (idx_dest + (i<<2) + j) | 0;
                                m[i1>>2] = c;
                        }
                }
        }
   
        return {
                multiply : multiply
        }
    }
   
    var mod = mat_mod(window, undefined, matrixpool);
   
   
    // fill all matrices with values
    for(var i = matrixpool-1; i >= 0; --i) {
        matrixpool[i] = i % 2 ? 0.5 : 2.0;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
matrix multiplication
// multiply lots of matrices
for (var k = 0; k < 5; ++k) {
  for (var i = 0; i < COUNT; ++i) {
    for (var j = COUNT - 1; j >= 0; --j) {
      mod.multiply(i, j, (i + j) % COUNT);
    }
  }
}
pending…
same as before
// multiply lots of matrices
for (var k = 0; k < 5; ++k) {
  for (var i = 0; i < COUNT; ++i) {
    for (var j = COUNT - 1; j >= 0; --j) {
      mod.multiply(i, j, (i + j) % COUNT);
    }
  }
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment