28
River Trail: Adding Data Parallelism to JavaScript* Stephan Herhut, Richard L. Hudson , Tatiana Shpeisman, Jaswanth Sreeram QCon NYC- June. 19, 2012 14:00

River Trail: Adding Data Parallelism to JavaScript*

  • Upload
    norris

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

River Trail: Adding Data Parallelism to JavaScript*. Stephan Herhut, Richard L. Hudson , Tatiana Shpeisman, Jaswanth Sreeram QCon NYC- June. 19, 2012 14:00. JavaScript * – What You Need To Know. It is not Java * Blend of many programming paradigms Object oriented with prototypes - PowerPoint PPT Presentation

Citation preview

Page 1: River Trail:  Adding Data  Parallelism to JavaScript*

1

River Trail: Adding Data Parallelismto JavaScript*

Stephan Herhut, Richard L. Hudson , Tatiana Shpeisman, Jaswanth Sreeram

QCon NYC- June. 19, 2012 14:00

Page 2: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 3: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 4: River Trail:  Adding Data  Parallelism to JavaScript*

4

Yet Another Parallel Programming API?*

Page 5: River Trail:  Adding Data  Parallelism to JavaScript*

Design Considerations

5

Page 6: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 7: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 8: River Trail:  Adding Data  Parallelism to JavaScript*

8

River Trail API 3 Pillars:

• ParallelArray• Methods• Kernel

Page 9: River Trail:  Adding Data  Parallelism to JavaScript*

9

ParallelArray

• Basic data type for parallel computation• Created from • A JavaScript array• Canvas• Comprehension

• Immutable• Dense• Homogenous• Single or multiple dimensions

9

Page 10: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 11: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 12: River Trail:  Adding Data  Parallelism to JavaScript*

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;});

Page 13: River Trail:  Adding Data  Parallelism to JavaScript*

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 );

Page 14: River Trail:  Adding Data  Parallelism to JavaScript*

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}

Page 15: River Trail:  Adding Data  Parallelism to JavaScript*

PrototypeImplementation

Page 16: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 17: River Trail:  Adding Data  Parallelism to JavaScript*

17

Compiling River Trail (Prototype)

JavaScript Engine

Script

River TrailCompiler

OpenCL Runtime

OpenCL Kernel

Hardware

multi-coreCPUs

SIMDinstructio

ns

GPU

Page 18: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 19: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 20: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 21: River Trail:  Adding Data  Parallelism to JavaScript*

21

The other routes…

Web Workers

*

Page 22: River Trail:  Adding Data  Parallelism to JavaScript*

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?

Page 23: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 24: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 25: River Trail:  Adding Data  Parallelism to JavaScript*

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

Page 26: River Trail:  Adding Data  Parallelism to JavaScript*

26

Q&A

Page 27: River Trail:  Adding Data  Parallelism to JavaScript*
Page 28: River Trail:  Adding Data  Parallelism to JavaScript*

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