Matrix multiplication with asm.js

JavaScript performance comparison

Revision 2 of this test case created

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)
    //  matrix mult code from Brandon Jones's Gl-matrix
   
    var COUNT = 100;
   
    var matrixpool = new Float32Array(16 * COUNT);
   
    function mat_mod(stdlib, foreign, m) {
      "use asm";
   
      function multiply(idx_a, idx_b, idx_dest) {
        idx_a = idx_a | 0;
        idx_b = idx_b | 0;
        idx_dest = idx_dest | 0;
   
        idx_a *= 16;
        idx_b *= 16;
        idx_dest *= 16;
   
        var a00 = +m[idx_a++],
          a01 = +m[idx_a++],
          a02 = +m[idx_a++],
          a03 = +m[idx_a++];
        var a10 = +m[idx_a++],
          a11 = +m[idx_a++],
          a12 = +m[idx_a++],
          a13 = +m[idx_a++];
        var a20 = +m[idx_a++],
          a21 = +m[idx_a++],
          a22 = +m[idx_a++],
          a23 = +m[idx_a++];
        var a30 = +m[idx_a++],
          a31 = +m[idx_a++],
          a32 = +m[idx_a++],
          a33 = +m[idx_a++];
   
        var b00 = +m[idx_b++],
          b01 = +m[idx_b++],
          b02 = +m[idx_b++],
          b03 = +m[idx_b++];
        var b10 = +m[idx_b++],
          b11 = +m[idx_b++],
          b12 = +m[idx_b++],
          b13 = +m[idx_b++];
        var b20 = +m[idx_b++],
          b21 = +m[idx_b++],
          b22 = +m[idx_b++],
          b23 = +m[idx_b++];
        var b30 = +m[idx_b++],
          b31 = +m[idx_b++],
          b32 = +m[idx_b++],
          b33 = +m[idx_b++];
   
        m[idx_dest++] = +(b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30);
        m[idx_dest++] = +(b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31);
        m[idx_dest++] = +(b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32);
        m[idx_dest++] = +(b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33);
        m[idx_dest++] = +(b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30);
        m[idx_dest++] = +(b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31);
        m[idx_dest++] = +(b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32);
        m[idx_dest++] = +(b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33);
        m[idx_dest++] = +(b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30);
        m[idx_dest++] = +(b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31);
        m[idx_dest++] = +(b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32);
        m[idx_dest++] = +(b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33);
        m[idx_dest++] = +(b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30);
        m[idx_dest++] = +(b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31);
        m[idx_dest++] = +(b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32);
        m[idx_dest++] = +(b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33);
      }
   
      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