
Parallel Programming for the Web Stephan Herhut Richard L. Hudson Tatiana Shpeisman Jaswanth Sreeram Intel Labs fstephan.a.herhut, rick.hudson, tatiana.shpeisman, [email protected] Abstract ming features and tools are emerging for most popu- lar programming languages. Yet, JavaScript applica- Parallel hardware is today's reality and language tions remain predominately sequential. extensions that ease exploiting its promised perfor- This lack of parallel web applications is not due mance flourish. For most mainstream languages, one to the lack of demand for more computing power for or more tailored solutions exist that address the spe- the client side of the web. Many applications, such as cific needs of the language to access parallel hard- photo and video editing, physics-based games, aug- ware. Yet, one widely used language is still stuck in mented reality, financial software and data visualiza- the sequential past: JavaScript, the lingua franca of tion could readily benefit from unused compute cy- the web. cles available in parallel client hardware, as they have Our position is that existing solutions do not trans- abundant latent data parallelism. The problem lies in fer well to the world of JavaScript due to differences the lack of appropriate parallel programming models. in programming models, the additional requirements The web developer community has refused to adopt of the web, like safety, and to developer expecta- traditional multi-threaded shared memory parallel tions. To address this we propose River Trail, a new programming models claiming that such models are parallel programming API designed specifically for too dangerous for their domain. While delivering JavaScript and we show how it satisfies the needs of performance, shared memory programming comes to- the web. To prove that our approach is viable, we gether with classical pitfalls of data races, dead locks have implemented a prototype JIT compiler in Fire- and live locks, all of which can easily lead to hard to fox that shows an order of magnitude performance reproduce concurrency bugs - something neither web improvement for a realistic web application. developers nor users want as part of the web brows- ing experience. Web workers [13], the only widely adopted support for parallel compute in JavaScript 1 Introduction that bring actor style threads to the web, were care- fully designed to steer around all these issues. While Despite Wired magazine proclaiming the Web dead they achieve their design goal of offloading long run- two years ago [1], it is alive and well. Browser-based ning computations to background threads, they are applications written in HTML and JavaScript con- not suitable for the development of parallel scalable tinue to flourish. Moreover, HTML and JavaScript compute intense workloads due to high cost of com- are also emerging as a popular development platform munication and low level of abstraction. for stand-alone applications, especially for smart We believe that parallelism should not be reserved phones and tablets [2, 3]. Part of HTML's popularity to expert programmers writing in C, C++, or Java. is due to its growing capabilities. 3D graphics, audio, Web developers should be able to exploit parallel video, web cam input, geo location, offline storage, hardware without fundamentally changing their pro- video conferencing - all are features traditionally re- gramming style. In particular, they should not need served for native applications that are proposed for to learn a new language or adapt to different seman- or will be part of HTML5 [4] or WebGL [9], the up- tics. coming standards for the next iteration of the web. River Trail puts this belief into action. Build- One capability, however, remains exclusive to native ing on well known data-parallel programming tech- application: the ability to take advantage of parallel niques [6, 5], we have designed an API that makes hardware. expressing parallelism easy, sacrificing performance Parallel hardware is an everyday reality, across all for productivity where need be. We have not ex- vendors and form factors. All major processors sup- tended JavaScript's semantics, nor introduced funda- port vector instructions and a majority of them come mentally new concepts. The API can be fully imple- with multiple cores. Parallel software is also slowly mented in JavaScript, albeit without performance im- moving to mainstream, as various parallel program- provements. However, River Trail is carefully crafted 1 so that it can easily be compiled to a broad range erative multi-tasking style has trained programmers of parallel hardware, from vector units to multi-core to design concurrent programs while relying on de- CPUs. We have proven this with an open source pro- terministic sequential execution. totype, written for the popular Firefox web browser, So in essence, JavaScript can be described as safe, that exploits vector instructions and multiple cores portable, rapid prototyping friendly and determinis- to achieve an order of magnitude speedup on an off- tic. A parallel programming extension for JavaScript the-shelf desktop system. thus should strive to maintain these properties. 2 JavaScript 3 River Trail Design JavaScript is the language behind today's web appli- River Trail builds on well known principles of data- cations. It was developed specifically for client side parallel programming drawing inspiration from [6, 5, scripting on the open web, a purpose that has heavily 8]. The design is based on three pillars, a type called influenced its design: Safety and portability are two ParallelArray that holds data values, several proto- of the key concerns. typical methods of ParallelArray that implement par- The safety requirement stems from the particulars allel constructs like map, and the concept of an ele- of the open web as an application platform. Whereas mental function which is passed to the constructs and traditionally, a user would have to acquire software, typically returns a single data element. In the simple either by download or a physical medium, install it example below, in and out are two parallel arrays - on his local machine and run it, web applications are in created using new and out computed by invoking instant on. A single click on a URL suffices to start the prototype method map with the elemental func- an application, and thus run JavaScript code. Even tion inc1 to create a freshly minted ParallelArray more, the origin of an application is often not appar- with each element in pa incremented by 1. ent, and thus trust cannot easily be established. Portability is a requirement for any open platform. function inc1(val) {return val + 1.0;}; Web applications in particular need to run across de- var in = vices, spanning different architectures and form fac- new ParallelArray([1.0, 2.0, 3,0]); tors. As a consequence, JavaScript is completely var out = in.map(inc1); hardware agnostic. The next example shows a pairwise add of two Par- Other than the name suggests, JavaScript is not re- allelArrays, pa1 and pa2. First, the example creates lated to Java. Both share the above design goals and a function that takes a ParallelArray as an argument they can be categorized as object oriented languages. and returns a function that adds the values located at Yet, JavaScript is dynamically typed and has no no- index i of the this array and the otherPA array. Then tion of classes. Instead, it employs a form of proto- it calls a combine method that produces a new paral- type based inheritance in the spirit of Self [11]. This lel array with each element being a result of invoking design is particularly well suited for rapid and iter- the generated elemental function on the correspond- ative application development, a style that is preva- ing elements of pa1 and pa2. Notice how this exam- lent in web applications. Software is released early ple leverages closures, free variables, and the object- and released often, resolving bugs and issues as they oriented JavaScript programming style. arise. Until today, JavaScript has remained mostly se- function pairwiseAdd(otherPA){ quential and program execution is fully deterministic. return function(i){ The latter forces implementations to essentially halt return this[i]+otherPA[i];} the world while scripts execute. To avoid unrespon- }; sive browsers, typical JavaScript programs there- out = pa1.combine(pairwiseAdd(pa2)); fore make heavy use of callbacks and asynchronicity. Larger tasks, in particular those involving long la- ParallelArray comes with multiple constructors, in- tency operations, are decomposed into independent cluding a comprehension, and the following 5 data chunks that are executed atomically and only ensure parallel methods: map, combine, scan, filter, and a consistent state when yielding control. This coop- scatter. When combined with elemental functions 2 each of these methods creates a freshly minted Par- formance on all forms and factors of available client allelArray. ParallelArray's sixth core data parallel devices. Consequently, we have chosen a high-level method is reduce which typically returns a scalar data-parallel programming approach [6, 5], featur- value. We have chosen a small set of constructs that ing well known primitives like map and reduce. This compose well and can be used to create other data forces the programmer to express the structure of a parallel constructs. For example, gather can be im- parallel algorithm and leaves platform dependent ex- plemented using a comprehension parallel array con- ecution strategies to the specific runtime system. structor, while sum could be implemented using re- duce. This approach enables a do few things well 3.3 Rapid Prototyping implementation strategy which reduces the complex- ity of the compiler and increases our confidence in its River Trail preserves JavaScript friendliness to rapid correctness while anticipating the creation of useful prototyping by staying within the boundaries of the libraries and infrastructures. same programming language. ParallelArray is noth- We now show how our design meets JavaScript's ing more than a new JavaScript type and it behaves properties of safety, portability, rapid prototyping as the JavaScript programmer expects.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-