SIMD.js: Bringing the Full Power of Modern Hardware to...

30
SIMD.js: Bringing the Full Power of Modern Hardware to JavaScript Ivan Jibaja June 2015 Workshop on Parallelism in Mobile Platforms – PRISM

Transcript of SIMD.js: Bringing the Full Power of Modern Hardware to...

SIMD.js: Bringing the Full Power of Modern Hardware to JavaScript

Ivan JibajaJune 2015

Workshop on Parallelism in Mobile Platforms – PRISM

JS Performance

JavaScript

1995

Video courtesy Mozilla (https://blog.mozilla.org/blog/2014/03/12/mozilla-and-epic-preview-unreal-engine-4-running-in-firefox/)

2015

3

4

JavaScript Performance Improvements2012 - 2014

Dec-2012 Jul-2013 Mar-2014 Sep-2014

Octane Score0

5000

10000

15000

20000

25000

Image courtesy Mozilla (http://arewefastyet.com/)

Be

tte

r

Achieving ~ 1.5x native running time via targeting asm.js†, a highly optimizable subset

of JavaScript

Approaching Native Speeds

† Courtesy of Alon Zakai & Luke Wagner: http://people.mozilla.org/~lwagner/gdc-pres/gdc-2014.html#/

5

Emscripten

JavaScript

LLVM Bitcode

Native Source

What’s next and how we can get there?

6

Over 1M lines of C/C++ code compiled to JavaScript* by Mozilla* and Epic

Unreal Engine 3 Demo VideoUnreal Engine 4 Demo Video

Epic* Games Unreal Engine* 4

Image courtesy Mozilla (https://blog.mozilla.org/blog/2014/03/12/mozilla-and-epic-preview-unreal-engine-4-running-in-firefox/)

Architectural Scene

Transistor CountsLog Scale†

Clock RatesLog Scale†

Microprocessor Trends – “Free Lunch” is Over!

8

• Growth in processor clock rate halted around 2005

• Transistors per processor continues to grow exponentially

† (c) 2013, James Reinders and Jim Jeffers: Intel® Xeon Phi™ High-Performance Programming, used with permission.

Image courtesy ARM (http://www.arm.com/images/A9-osprey-hres.jpg)

Core

Core

Vector

Vector

ARM’s Cortex A9

Transistors Division

9

Web Runtimes

Optimizing Web Runtimes for Parallelism

11

11

JavaScript* Engine

Parser

Layout Engine

RenderingEngine

Optimizing Web Runtimes for Parallelism

12

12

JavaScript* Engine

• JS lacks language level parallelism

• Web runtimes of today are not scalable with number of cores

• Need parallelism for responsiveness and energy efficiency

SIMD.js

Cw

Cx

Cy

Cz

Bw

Bx

By

Bz

Aw

Ax

Ay

Az

+ =

SIMD – Single Instruction, Multiple Data

† Intel® Architecture currently has SIMD operations of vector length 4, 8, 16

14

Scalar Operations

Cx=Ax Bx+

Cy=Ay By+

Cz=Az Bz+

Cw=Aw Bw+

SIMD Operation of Vector Length 4 †

SIMD operations deliver great performance & power efficiency

SIMD in JavaScriptDesign Goals

15

1. Portability

JS runs everywhere

2. Vector performance when vector hardware available

Guaranteed performance for developers

Always use vector hardware when available

3. Compiler implementation without automatic vectorization technology to attain vector performance

Ease of adoption – multiple JS engines

Guaranteed performance for developers

16

SIMD.js: Bringing SIMD to JavaScript

Polyfill API: https://github.com/johnmccutchan/ecmascript_simd

float32x4, int32x4, ...

Constructors: float32x4(x,y,z,w) float32x4.splat(s)

Operations: abs, neg, add, sub, mul, div, clamp, min, max, reciprocal, reciprocalSqrt, scale, sqrt,

shuffle, shuffleMix, equal, notEqual, lessThan, greaterThan , withX, withY …

Status: Stage 2 approval for inclusion in ES2016 (ES7) by TC39†

In Firefox* Nightly, in Microsoft Edge*, prototyped in Chromium*

Industrial Collaborators: Intel, Mozilla*, Google*, Microsoft*, ARM*

† A copy of the TC39 Presentation: http://esdiscuss.org/notes/2014-07/simd-128-tc39.pdf

3DB5BCBE 222 ff3424 push [esp]3DB5BCC1 225 89442404 mov [esp+0x4],eax3DB5BCC5 229 ff75e8 push [ebp-0x18]3DB5BCC8 232 ba02000000 mov edx,000000023DB5BCCD 237 8b7c2408 mov edi,[esp+0x8];; call .getAt() 3DB5BCD1 241 e84a24fcff call 3DB1E120 3DB5BCD6 246 8b75fc mov esi,[ebp-0x4]3DB5BCD9 249 890424 mov [esp],eax3DB5BCDC 252 ba04000000 mov edx,000000043DB5BCE1 257 8b7c240c mov edi,[esp+0xc];; call .add() 3DB5BCE5 261 e876fdffff call 3DB5BA60 3DB5BCEA 266 8b75fc mov esi,[ebp-0x4]3DB5BCED 269 83c404 add esp,0x43DB5BCF0 272 8945ec mov [ebp-0x14],eax

JIT Compiler Support

17

sum = SIMD.float32x4.add(sum, f32x4list.getAt(i));

3DB5E306 358 0f101cc6 movups xmm3,[esi+eax*8]3DB5E30A 362 0f58d3 addps xmm2,xmm3

SIMD.js code

V8 Full CodegenCompiler

JIT compiler support:

Type SpeculationUnboxingInlining

Optimized Code

JIT Compiler Support

18

UnboxingInliningType Speculation

sum = SIMD.float32x4.add(sum, f32x4list.getAt(i));

3DB5E306 358 0f101cc6 movups xmm3,[esi+eax*8]3DB5E30A 362 0f58d3 addps xmm2,xmm3

SIMD.js - Example

19

function average(buffer) { var n = buffer.length; var sum = 0.0; for (var i = 0; i < n; i++) {

sum += buffer[i]; } return sum / n;

}

function average(buffer) { var n = buffer.length/4; var sum = SIMD.float32x4.zero(); for (var i = 0; i < n; i++) {

sum = SIMD.float32x4.add(sum,SIMD.float32x4.load(buffer,i));

} var total = SIMD.float32x4.extractLane(sum4, 0) +

SIMD.float32x4.extractLane(sum4, 1) +SIMD.float32x4.extractLane(sum4, 2) +SIMD.float32x4.extractLane(sum4, 3);

return total / (n * 4) ; }

SIMD.js – The API

20

function mandelx4(c_re4, c_im4, max_iterations) {var z_re4 = c_re4;var z_im4 = c_im4;var four4 = SIMD.float32x4.splat (4.0);var two4 = SIMD.float32x4.splat (2.0);var count4 = SIMD.int32x4.splat (0);var one4 = SIMD.int32x4.splat (1);

for (var i = 0; i < max_iterations; ++i) {var z_re24 = SIMD.float32x4.mul (z_re4, z_re4);var z_im24 = SIMD.float32x4.mul (z_im4, z_im4);

var mi4 = SIMD.float32x4.lessThanOrEqual (SIMD.float32x4.add (z_re24, z_im24), four4);

// if all 4 values are greater than 4.0, there's no reason to continueif (mi4.signMask === 0x00) {break;

}

var new_re4 = SIMD.float32x4.sub (z_re24, z_im24);var new_im4 = SIMD.float32x4.mul (SIMD.float32x4.mul (two4, z_re4), z_im4);z_re4 = SIMD.float32x4.add (c_re4, new_re4);z_im4 = SIMD.float32x4.add (c_im4, new_im4);count4 = SIMD.int32x4.add (count4, SIMD.int32x4.and (mi4, one4));

}return count4;

}

3.23.6 3.8 3.9

4.65.0

6.0

9.5

3.2

3.83.4

6.16.5

5.05.6

11.8

6.8

3.12.7

4.54.2

3.8

5.4

9.3

0

2

4

6

8

10

12

14

Transpose4x4 AOBench Mandelbrot MatrixMultiplication VertexTransform Average ShiftRows Matrix4x4Inverse

SIMD x-times faster than non-SIMD

Excellent early results while still focused on functionality

3rd Generation Intel® Core™ i7 processor (3667U)@ 2.00 GHz, 32-bit, Ubuntu* 133rd Generation Intel® Core™ i7 processor (3667U)@ 2.00 GHz, 64-bit, Ubuntu* 13 Intel® Atom™ processor Z3770 @ 1.46GHz, Android* 4.4

SIMD.js benchmarks: https://github.com/johnmccutchan/ecmascript_simd/tree/master/src/benchmarks

SIMD Speedups on Chromium*

21

SIMD speedup is nicely multiplied by WebWorkers†

† Source: Intel® Peter Jensen : https://github.com/PeterJensen/

SIMD.js demos: http://peterjensen.github.io/idf2014-simd

WW: Number of WebWorkers

Our Chromium* Prototype

Combining SIMD and Higher-Level Parallelism

22

$16 = HEAP32[$src >> 2] | 0;$i$02$i = 0;$sumx4$01$i = SIMD.float32x4.splat(0);$17 = $16 >> 4;do {$sumx4$01$i = SIMD.float32x4.add($sumx4$01$i,

HEAPF32x4[$17 + ($i$02$i >> 4)]);$i$02$i = $i$02$i + 4 | 0;

} while (($i$02$i | 0) < 1e4);HEAPF64[tempDoublePtr >> 3] =

($sumx4$01$i.w + ($sumx4$01$i.z +($sumx4$01$i.x + $sumx4$01$i.y))) / 1.0e4;

JavaScript*C/C++

float simdAverage(float *src, int len) {_m128 sumx4 = _mm_setzero_ps();

for (int i = 0; i < len; i+=4) {_m128 v = _mm_load_ps(src + i);sumx4 = _mm_add_ps(sumx4, v);

}float sumx4_mem[4];_mm_store_ps(sumx4_mem, sumx4);return (sumx4_mem[0] + sumx4_mem[1]

+ sumx4_mem[2] + sumx4_mem[3])/len;}

23

Emscripten now targets SIMD.js

Emscripten brings native SIMD apps to the open web platform

1.00

2.03

7.188.13

0123456789

Speedup over Scalar JS

Speedup

Scalar JS Scalar C++

SIMD JS SIMD C++

Near-native SIMD.js speedup

Devices sense and perceive user actions in a natural way

Learning & Education 3D Scanning and Sharing

Scan it

Customize and Print itShare it

24

Toward Perceptual Computing†

† Source: Intel® Perceptual Computing SDK: www.intel.com/software/perceptual

Gaming

Immersive Collaboration

Out-of-reach Device

Input

Speech

† W3C Media Capture Depth Stream Extensions: http://w3c.github.io/mediacapture-depth/

Web Application

Browser or HTML5 runtime

RGBStream

DepthStream

getUserMedia (WebRTC) API

25

25

3D Cameras Make Perceptual Computing Accessible

Media Capture Depth Stream Extensions are in W3C WG†

Embedded 3D Camera

navigator.getUserMedia({ video: true, depth: true }, success, failure);

function success(s) {

var video = document.querySelector(‘#video’);video.src = URL.createObjectURL(s);video.play();

// construct MediaStream from existing depth trackvar depthStream = new MediaStream(s.getDepthTracks());

// send the created depth stream over a RTCPeerConnectionvar peerConnection = new RTCPeerConnection(config);peerConnection.addStream(depthStream);

var depthVideo = document.querySelector(‘#depthVideo’);depthVideo.src = URL.createObjectURL(depthStream);depthVideo.play();}

Web: The Most Viable Cross-Platform Technology Today

and the Ubiquitous Application Platform of the Future

26

Big Data

Rich Capabilities and Content

SocialContextual

CrowdsourcedSensors“Things”

Visual, Perceptual, Full HW Access

Thank you

Legal DisclaimerINFORMATION IN THIS DOCUMENT IS PROVIDED IN CONNECTION WITH INTEL PRODUCTS. NO LICENSE, EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, TO ANY INTELLECTUAL PROPERTY RIGHTS IS GRANTED BY THIS DOCUMENT. EXCEPT AS PROVIDED IN INTEL'S TERMS AND CONDITIONS OF SALE FOR SUCH PRODUCTS, INTEL ASSUMES NO LIABILITY WHATSOEVER AND INTEL DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY, RELATING TO SALE AND/OR USE OF INTEL PRODUCTS INCLUDING LIABILITY OR WARRANTIES RELATING TO FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.A "Mission Critical Application" is any application in which failure of the Intel Product could result, directly or indirectly, inpersonal injury or death. SHOULD YOU PURCHASE OR USE INTEL'S PRODUCTS FOR ANY SUCH MISSION CRITICAL APPLICATION, YOU SHALL INDEMNIFY AND HOLD INTEL AND ITS SUBSIDIARIES, SUBCONTRACTORS AND AFFILIATES, AND THE DIRECTORS, OFFICERS, AND EMPLOYEES OF EACH, HARMLESS AGAINST ALL CLAIMS COSTS, DAMAGES, AND EXPENSES AND REASONABLE ATTORNEYS' FEES ARISING OUT OF, DIRECTLY OR INDIRECTLY, ANY CLAIM OF PRODUCT LIABILITY, PERSONAL INJURY, OR DEATH ARISING IN ANY WAY OUT OF SUCH MISSION CRITICAL APPLICATION, WHETHER OR NOT INTEL OR ITS SUBCONTRACTOR WAS NEGLIGENT IN THE DESIGN, MANUFACTURE, OR WARNING OF THE INTEL PRODUCT OR ANY OF ITS PARTS.Intel may make changes to specifications and product descriptions at any time, without notice. Designers must not rely on the absence or characteristics of any features or instructions marked "reserved" or "undefined". Intel reserves these for future definition and shall have no responsibility whatsoever for conflicts or incompatibilities arising from future changes to them. The information here is subject to change without notice. Do not finalize a design with this information.The products described in this document may contain design defects or errors known as errata which may cause the product to deviate from published specifications. Current characterized errata are available on request.Contact your local Intel sales office or your distributor to obtain the latest specifications and before placing your product order.Copies of documents which have an order number and are referenced in this document, or other Intel literature, may be obtained by calling 1-800-548-4725, or go to: http://www.intel.com/design/literature.htm

River Trail, Nehalem, and other code names featured are used internally within Intel to identify products that are in development and not yet publicly announced for release. Customers, licensees and other third parties are not authorized by Intel to use code names in advertising, promotion or marketing of any product or services and any such use of Intel's internal code names is at the sole risk of the user.

Intel, Xeon, VTune, Atom, Core, Xeon Phi, Look Inside and the Intel logo are trademarks of Intel Corporation in the United States and other countries.

*Other names and brands may be claimed as the property of others.Copyright ©2014 Intel Corporation.

Software and workloads used in performance tests may have been optimized for performance only on Intel microprocessors. Performance tests, such as SYSmark* and MobileMark*, are measured using specific computer systems, components, software, operations and functions. Any change to any of those factors may cause the results to vary. You should consult other information and performance tests to assist you in fully evaluating your contemplated purchases, including the performance of that product when combined with other products. For more information go to http://www.intel.com/performance.

Software Source Code Disclaimer: Any software source code reprinted in this document is furnished under a software license and may only be used or copied in accordance with the terms of that license.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Intel technologies’ features and benefits depend on system configuration and may require enabled hardware, software or service activation. Performance varies depending on system configuration. No computer system can be absolutely secure. Check with your system manufacturer or retailer or learn more at intel.com.

Legal Disclaimer

Risk Factors

The above statements and any others in this document that refer to plans and expectations for the third quarter, the year and the future are forward-looking statements that involve a number of risks and uncertainties. Words such as “anticipates,” “expects,” “intends,” “plans,” “believes,” “seeks,” “estimates,” “may,” “will,” “should” and their variations identify forward-looking statements. Statements that refer to or are based on projections, uncertain events or assumptions also identify forward-looking statements. Many factors could affect Intel’s actual results, and variances from Intel’s current expectations regarding such factors could cause actual results to differ materially from those expressed in these forward-looking statements. Intel presently considers the following to be the important factors that could cause actual results to differ materially from the company’s expectations. Demand could be different from Intel's expectations due to factors including changes in business and economic conditions; customer acceptance of Intel’s and competitors’ products; supply constraints and other disruptions affecting customers; changes in customer order patterns including order cancellations; and changes in the level of inventory at customers. Uncertainty in global economic and financial conditions poses a risk that consumers and businesses may defer purchases in response to negative financial events, which could negatively affect product demand and other related matters. Intel operates in intensely competitive industries that are characterized by a high percentage of costs that are fixed or difficult to reduce in the short term and product demand that is highly variable and difficult to forecast. Revenue and the gross margin percentage are affected by the timing of Intel product introductions and the demand for and market acceptance of Intel's products; actions taken by Intel's competitors, including product offerings and introductions, marketing programs and pricing pressures and Intel’s response to such actions; and Intel’s ability to respond quickly to technological developments and to incorporate new features into its products. The gross margin percentage could vary significantly from expectations based on capacity utilization; variations in inventory valuation, including variations related to the timing of qualifying products for sale; changes in revenue levels; segment product mix; the timing and execution of the manufacturing ramp and associated costs; start-up costs; excess or obsolete inventory; changes in unit costs; defects or disruptions in the supply of materials or resources; product manufacturing quality/yields; and impairments of long-lived assets, including manufacturing, assembly/test and intangible assets. Intel's results could be affected by adverse economic, social, political and physical/infrastructure conditions in countries where Intel, its customers or its suppliers operate, including military conflict and other security risks, natural disasters, infrastructure disruptions, health concerns and fluctuations in currency exchange rates. Expenses, particularly certain marketing and compensation expenses, as well as restructuring and asset impairment charges, vary depending on the level of demand for Intel's products and the level of revenue and profits. Intel’s results could be affected by the timing of closing of acquisitions and divestitures. Intel's results could be affected by adverse effects associated with product defects and errata (deviations from published specifications), and by litigation or regulatory matters involving intellectualproperty, stockholder, consumer, antitrust, disclosure and other issues, such as the litigation and regulatory matters described in Intel's SEC reports. An unfavorable ruling could include monetary damages or an injunction prohibiting Intel from manufacturing or selling one or more products, precluding particular business practices, impacting Intel’s ability to design its products, or requiring other remedies such as compulsory licensing of intellectual property. A detailed discussion of these and other factors that could affect Intel’s results is included in Intel’s SEC filings, including the company’s most recent reports on Form 10-Q, Form 10-K and earnings release.