Using Javascript and WebCL for Numerical Computations

Background and Motivation

Clumsy Origins

  • Many new uses
    • Servers
    • Robotics
    • Game Dev
    • Running Linux
    • Presentations... :)

Current Javascript Uses

Performance

  • asm.js
    • high performing subset of JS
  • WebCL
    • GPU access in browser
  • node-cuda
    • GPU access in NodeJS
  • Typed Arrays

New Performance Requirements

var first = 5;
//By using a bitwise 
//operator, we make sure 
//that the value is 32-bit integer
var second = first | 0;
// Unsigned int array
var arr = new Uint32Array([21,31]);
console.log(arr[1]); // 31

// Float 32 bit array
var arr = new Float32Array([21,31]);
console.log(arr[1]); // 31

Speed

... but is it fast enough?

Is it good enough for number crunching?

  • Represent patterns of numerical computations
    • Sparse Linear Algebra
    • N-body methods
    • Map Reduce
    • Dynamic Programming
    • Combinatorial logic
    • etc

Thirteen Dwarfs

Sequential Performance

Driving Questions

  • Is performance of Javascript competitive with C?
  • Do typed arrays improve the performance of Javascript?
  • Does asm.js offer performance improvements over handwritten Javascript?

Configuration

  • Native code 
    • C (compiled with gcc)
  • Javascript
    • Handwritten
    • Typed arrays
    • asm.js
      • Transpiled from C using Emscripten

Browser Comparisons

JS and asm.js vs C - Chrome

16 out of 24 have a slowdown below 2x

No tests are more than 3x slower than C

Lavamd is 4x faster than C

Slowdowns are 1.62 and 1.35 for JS and asm.js, respectively

JS and asm.js vs C - Firefox

Only one asm.js benchmark has a slowdown higher than 2x

In 4 tests, at least one JS benchmark outperforms C

Slowdowns are 1.65 and 1.17 for JS and asm.js, respectively

JS and asm.js vs C - IE

Worse performance than other browsers

crc is 35x slower than C

Average slowdown is 3.11 and 2.18 for JS and asm.js in IE

JS and asm.js vs C - Safari

Suggests a performance optimization missed by Safari's JIT compiler

Average slowdown is 2.69 and 2.15 for JS and asm.js, respectively

crc and fft have a severe penalty in JS at 17x and 10x

Typed Arrays

Typed Arrays vs untyped (js-nota)

Speed up about 2x on average

back-prop speedup is over 16x

Multiple speed ups over greater than 4x

fft is slower.

Overall, speedups are similar across browsers

Observations

  • Browser choice is significant in program speed
  • When running numerical computation in the browser, also consider how the app will affect the browser overall
    • Chrome - one process per tab will have a minimal affect on other tabs
    • Firefox - long running Javascript computation can freeze the entire browser until it has finished

Parallel Performance

Comparing Parallelization performance boosts using OpenCL and WebCL

Background

  • OpenCL
    • Parallel computing framework for C
  • WebCL 
    • Standard bringing OpenCL to web
    • Allows for direct CPU/GPU access with JS

Driving Questions

  • Does WebCL provide performance improvements in JS?
  • Are the improvements with JS congruent with improvements of OpenCL (vs C)?

WebCL Speed up (Firefox)

OpenCL Speed up

WebCL vs OpenCL

Conclusions

  • Asm.js and WebCL make Javascript competitive for numerical computation
  • Performance can still vary a lot between browsers and JS interpreter implementations
  • Performance optimizations
    • Using typed arrays provided a performance boost of about 2x across browsers
    • Asm.js further improved this by 15-30%
    • WebCL improvements are incongruent with OpenCL
    • WebCL still provides significant improvements over native JS

References

Khan, F., Foley-Bourgon, V., Kathrotia, S., Lavoie, E., & Hendren, L. (2014, October). Using JavaScript and WebCL for numerical computations: a comparative study of native and web technologies. In Proceedings of the 10th ACM Symposium on Dynamic languages (pp. 91-102). ACM.

Made with Slides.com