1
River Trail: Adding Data Parallelismto JavaScript*
Stephan Herhut, Richard L. Hudson , Tatiana Shpeisman, Jaswanth Sreeram
QCon NYC- June. 19, 2012 14:00
2
JavaScript* – What You Need To Know
• It is not Java*• Blend of many programming paradigms
• Object oriented with prototypes• Higher-order functions and first class function objects• Dynamically typed and interpreted
• Safety and security built in• Requirement for web programming• Managed runtime• No pointers, no overflows, …
• Designed for portability• Fully abstracts hardware capabilities• No byte-codes, no dusty decks
3
Concurrency in JavaScript*
• Cooperative multi-tasking• Scripts compete with the browser for computing resources• Event driven execution model
• Concurrent programming mindset• Asynchronous call-backs for latency hiding
• Fully deterministic• Run-to-completion semantics• No concurrent side effects, no race conditions
• No support for concurrent execution• Single threaded evaluation of JavaScript
4
Yet Another Parallel Programming API?*
Design Considerations
5
6
Language Design with the Web in Mind
1. Ease of use– Build on developer’s existing knowledge– Allow for mash-up of sequential and parallel code
2. Platform independent– Support all kinds of platforms, parallel or not– Perform well on different parallel architectures (multi-core,
GPUs, …)3. Suitable for the Open Web
– Meet existing safety and security promises– Needs to be reasonably easy to implement in JavaScript JIT
engines
Challenge: meet these criteria and get good performance
7
Design Choices
• Performance portability⇒ Use High-Level Parallel Patterns
• Deterministic execution model⇒ No side effects: shared state is immutable⇒ Require commutative and associative operators ⇒ No magic: floating point anomalies may still occur
• Support mash-up coding⇒ All code still written purely in JavaScript⇒ Looks like JavaScript*, behaves like JavaScript*
• Maintain JavaScript*’s Safety and Security⇒ Use fully managed runtime
8
River Trail API 3 Pillars:
• ParallelArray• Methods• Kernel
9
ParallelArray
• Basic data type for parallel computation• Created from • A JavaScript array• Canvas• Comprehension
• Immutable• Dense• Homogenous• Single or multiple dimensions
9
10
ParallelArray Methods
• Provide the basic skeletons for parallel computing• Typically creates a freshly minted ParallelArray• Combine, Reduce, Scan, Scatter, Filter, Map• Plus a constructor and accessor• Others can be built on top of the above
• Sum, Max, Add, Gather, Histogram, etc.
10
Do Few Things Well
11
Kernel Function
• Methods take kernel function as an argument• Written purely in JavaScript, side effect free• combine and filter arguments
• index and array • get can use the index regardless of depth (dimensionality)
• reduce, scan• 2 values passed in 1 returned
• scatter conflict arguments• Array of target indices, conflict function for collisions
• map• Value passed as argument
11
12
Add 1 to Every Element in A
12
Sequential
var i; var a = new Array (...);var b = new Array(a.length);for(i=0;i<a.length;i++){
b[i] = a[i] + 1;}
Data parallel
var a = new ParallelArray(...);var b = a.map( function(val){return val+1;});
13
Sum Reduce-Style
13
Sequential
var i;var a = new Array (...);var sum = 0;for (i=0; i<a.length; i++) {
sum += a[i];}
Data parallel
var pa = new ParallelArray(...);var sum = pa.reduce( function (a, b) { return a + b; });
Data Parallelism is Beautiful More complex example in backup slides if we have time….
var sum = pa.reduce( (a, b) => a+b );
14
An Example: Grayscale Conversion
pixelData.map(toGrayScale) .map(function toRGBA(color) {
return [color,color,color,255]; }
)
toGrayScale – Given a pixel return the gray value}
PrototypeImplementation
16
• Type inference• Infers array types and shapes• Checks for side effects
• Representation analysis• Computes bounds on local
variables• Updates type information of
known Integer numbers• Static memory allocation• Bounds check elimination• Code generation
• Emits OpenCL code
Compiling River Trail (Prototype)
JavaScript Engine
Script
River TrailCompiler
17
Compiling River Trail (Prototype)
JavaScript Engine
Script
River TrailCompiler
OpenCL Runtime
OpenCL Kernel
Hardware
multi-coreCPUs
SIMDinstructio
ns
GPU
18
Performance Results: Particle Physics
Particle model (O(n2)) computed using River Trail on a 2nd Generation Core i7 with 4 cores
http://github.com/RiverTrail/RiverTrail/wiki
1 2 3 4 5 6 7 80
10
20
30
40
50
60
# of runtime threads
Fram
es/S
econ
d
19
Performance Results: Matrix Matrix Multiply
O(n3) dense matrix matrix multiplication on 1000 x 1000 element matrices;dual-core 2nd Generation Core i5 with HyperThreading enabled and 4GB RAM;
JavaScript* benchmarks use Firefox 8
JavaScript seq. C seq. ParallelArray0
2
4
6
8
10
12
1
5.421
10.99
20
Status Quo
• Open source Firefox prototype available on GitHub• Pre-built binary extension for Firefox 12• Sequential library fall back for other browsers
• ECMAScript proposal of the full API published• Removes many limitations of the prototype
• First sequential implementation for SpiderMonkey• Lives in Mozilla’s IonMonkey branch• Intended as API testing vehicle
http://github.com/RiverTrail/RiverTrail/wiki
http://wiki.ecmascript.org/doku.php?id=strawman:data_parallelism
21
The other routes…
Web Workers
*
22
Good for task parallelismImplement actors model
– No shared state– Communication using
messagesHeavy weight
– Typically implemented using OS threads
– Marshaling / Unmarshaling uses JSON (think strings)
What About Web Workers?
23
What about WebCL
JavaScript binding for OpenCL– Provides HPC parallelism on CPU & GPGPU– Portable and efficient access to heterogeneous devices
WebCL stays close to the OpenCL standard– Preserves OpenCL familiarity to facilitates adoption– Allows developers to translate OpenCL knowledge to web– Easier to keep OpenCL and WebCL in sync, as two evolve
An interface just above OpenCL– Higher level abstractions built on top of WebCL
Intended for performance programmers– Useful HW abstraction– Allows ultimate control, performance, and access to HW
24
Challenges
WebCL / OpenCL challenges• OpenCL standard leaves things undefined
– For example out of bounds– OpenCL makes these the programmer’s responsibility– Not a reasonable approach for web
Shared challenge –context management– GPUs do a poor job– Creates Denial of Service and Performance hazards– River Trail can fall back to JavaScript library or OpenCL CPU
execution– Currently River Trail is focused on CPU
25
Gently extended JavaScriptPreserves Web SecurityStandardization by ECMA TC39Unified high level JavaScript programming model and tool chain
Execution determinism maintainedApps: visual computing, physics simulation, games, augmented reality
Gently extends C99Retrofits Web SecurityStandardization by KhronosBifurcated JavaScript / OpenCL-C99 programming model, multiple tool chainsOpenCL (non) deterministic modelApps: visual computing, physics simulation, games, augmented reality
River Trail WebCL
26
Q&A
29
Dealing with boundary conditions
1. var pa = new ParallelArray(2. new Int32Array([2,4,8,16,32]));3. function blur(ind){return (this[i]+this[i-1])/2;};4. pa.combine(blur); -> throws error on this[-1]5. function halo(boundary, work) { 6. return function (indx){7. if (indx < boundary) { return this[i];}8. else { return work.apply(this, indx);}; 9. };10.};11.pa.combine(halo(3, blur)))->[2 4 8 12 24]12.pa.combine(halo(1, blur)))->[2 3 6 12 24]13.pa.combine(halo(pa.length-1, blur)))
->[2 4 8 16 24]
29
Top Related