Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting over Moore’s Law Parallelization using JavaScript in the browser

Getting over Moore’s Law Parallelization using JavaScript in the browser

Modern computers and devices continue the trend of having a larger number of CPU cores instead of cores with more computing power. That means that the days of waiting for Moore's Law to take effect so our apps run faster are over, and we, developers, need to start adventuring into the land of parallelism.

In this talk we will go over some of the currently available approaches (and ongoing efforts) that make it possible to take advantage of parallelism in the browser using JavaScript. This includes not only the widely spread concept of Web Workers, but also Single Instruction Multiple Data (SIMD) and the downfall of ParallelJS and rise of SharedArrayBuffers.

Damian Schenkelman

October 17, 2015
Tweet

More Decks by Damian Schenkelman

Other Decks in Programming

Transcript

  1. UI Thread 1 2 Worker Thread 1 2 Split the

    bars User action Message passing UI reaction Parallelization
  2. Basic lifetime All major browsers const  w  =  new  Worker('worker.js');

      w.addEventListener('message',  function(e){   console.log('Received  from  w',  e.data);   worker.terminate();   }   w.postMessage('@jsconfco'); self.addEventListener('message',  function(e)  {      const  data  =  e.data;      self.postMessage(data  +  'rocks!!!');   }); worker.js index.html
  3. postMessage Transferable objects not in IE void  postMessage(any  message  ,

        optional  sequence  <  Transferable  >  transfer) const  worker  =  new  Worker('worker.js');   ...   const  items  =  new  Uint8Array([1,2,3,4]);   worker.postMessage(items,  [  items.buffer  ]); sample spec
  4. Cloning (2) No transferable UI Thread Worker Thread [1,2,3] [1,2,3]

    const  xs  =  new  Uint32Array([1,2,3]);   worker.postMessage(xs);
  5. Transferables(2) UI Thread Worker Thread [1,2,3] [1,2,3] const  xs  =

     new  Uint32Array([1,2,3]);   worker.postMessage(xs,  [  xs.buffer  ]);
  6. UI Thread p-j-s Worker 1 Worker 2 Worker 3 Worker

    4 A B C D A B C D A' B' C' D' A' B' C' D'
  7. Relative performance 0" 0.2" 0.4" 0.6" 0.8" 1" 1.2" 1.4"

    1.6" 2" 3" 4" 5" 6" Rela%ve'performance'of'parallel'compared'to'serial' #'of'elements'(log'scale)' Sapia'tone:'Parallel'vs'Serial' Firefox"41"
  8. 128 bits float32x4 and int32x4 0 1 2 3 4

    5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 Lanes Bytes
  9. const  float32x4  =  SIMD.Float32x4;   const  a  =  float32x4(1.0,  5.0,

     3.0,  7.0);   const  b  =  float32x4(2.0,  2.0,  4.0,  4.0);   const  sum  =  float32x4.add(a,  b);  //optimized 1.0 5.0 3.0 7.0 2.0 2.0 2.0 4.0 b a 2.0 5.0 3.0 7.0 sum
  10. sum var  f4  =  SIMD.Float32x4;   function  SIMDsum(xs){    

     var  i,  len  =  xs.length;      var  res  =  f32x4.splat(0);      for  (i  =  0;  i  <  len;  i+=4){          res  =  f4.add(res,  f4(xs,  ji));      }      return  res;   }; var  frnd  =  Math.fround;   function  sum(xs){      var  i,  len  =  xs.length;      var  res  =  frnd(0);      for  (i  =  0;  i  <  len;  i++){          res  =  frnd(frnd(res)  +  xs[i]);      }      return  res;   };
  11. ParallelJS a.k.a. Rivertrail var  items  =  new  ParallelArray(1,2,3,4);   var

     newItems  =  items.map(val  =>  val+1);   var  sum  =  items.reduce((a,  b)  =>  a  +  b);
  12. SharedArrayBuffers var  xs  =  new  SharedUint8Array(len);   ...   worker1.postMessage(xs,

     [  xs.buffer  ]);   worker2.postMessage(xs,  [  xs.buffer  ]);
  13. UI Thread p-j-s Worker 1 Worker 2 Worker 3 Worker

    4 A B C D A B C D A' B' C' D' A' B' C' D' with SharedArrayBuffers
  14. Credits • @kaptajnjensen: SIMD IDF demos • @bnjbvr: SIMD sum

    • @BrendanEich: shared memory links • @cristiandouce: ideas for demos • @mgonto & @woloski: reviewers
  15. SIMD Links • Proposal & polyfill: https://github.com/johnmccutchan/ecmascript_simd • Internals in

    FF: https://blog.mozilla.org/javascript/2015/03/10/state-of-simd-js- performance-in-firefox/ • Demos: http://peterjensen.github.io/idf2014-simd/ • IDF talk: http://intelstudios.edgesuite.net/idf/2014/sf/ti/SPCS003/index.html • Slides: https://docs.google.com/presentation/d/ 1yc2NDzFJ-0yD980URiTcV3oE_2cQDVzXuH4Rss1fG8s/ and http:// peterjensen.github.io/html5-simd/html5-simd.html#/36 • Paper: https://e9fe7ff0-a-62cb3a1a-s-sites.googlegroups.com/site/ wpmvp2014/paper_18.pdf
  16. Future Links • Shared memory model: https://lists.webkit.org/pipermail/ webkit-dev/2013-April/024682.html • SharedArrayBuffers

    spec draft: https://docs.google.com/ document/d/ 1NDGA_gZJ7M7w1Bh8S0AoDyEqwDdRh4uSoTPSNn77P Fk/ • TypedObjects spec draft: https://github.com/dslomov- chromium/typed-objects-es7 • TypedObjects explanations: https://github.com/ nikomatsakis/typed-objects-explainer/