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

Download Report

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

SIMD.js: Bringing the Full Power of
Modern Hardware to JavaScript
Ivan Jibaja
June 2015
Workshop on Parallelism in Mobile Platforms – PRISM
JS Performance
JavaScript
1995
2015
Video courtesy Mozilla (https://blog.mozilla.org/blog/2014/03/12/mozilla-and-epic-preview-unreal-engine-4-running-in-firefox/)
3
JavaScript Performance Improvements
2012 - 2014
Octane Score
0
Better
5000
10000
15000
20000
25000
Dec-2012
Jul-2013
Image courtesy Mozilla (http://arewefastyet.com/)
Mar-2014
Sep-2014
4
Approaching Native Speeds
Native Source
LLVM Bitcode
Emscripten
Achieving ~ 1.5x native running time via targeting
asm.js†, a highly optimizable subset of JavaScript
JavaScript
† Courtesy of Alon Zakai & Luke Wagner: http://people.mozilla.org/~lwagner/gdc-pres/gdc-2014.html#/
5
What’s next and how we can get there?
Epic* Games Unreal Engine* 4
Unreal Engine 3 Demo Video
Unreal Engine 4 Demo Video
Over 1M lines of C/C++ code compiled to JavaScript* by Mozilla* and Epic
Image courtesy Mozilla (https://blog.mozilla.org/blog/2014/03/12/mozilla-and-epic-preview-unreal-engine-4-running-in-firefox/)
6
Architectural Scene
Microprocessor Trends – “Free Lunch” is Over!
Clock Rates
Log Scale†
Log Scale†
Transistor Counts
• 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.
8
Transistors Division
Core
Vector
Vector
Core
ARM’s Cortex A9
Image courtesy ARM (http://www.arm.com/images/A9-osprey-hres.jpg)
9
Web Runtimes
Optimizing Web Runtimes for Parallelism
Parser
JavaScript*
Engine
Layout Engine
Rendering
Engine
11
11
Optimizing Web Runtimes for Parallelism
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
12
12
SIMD.js
SIMD – Single Instruction, Multiple Data
Scalar Operations
Ax
+
Bx
=
Cx
Ay
+
By
=
Cy
Az
+
Bz
SIMD Operation of
Vector Length 4 †
Ax
Ay
=
Cz
Az
Aw
Aw
+
Bw
=
Bx
+
By
Cx
=
Cy
Bz
Cz
Bw
Cw
Cw
SIMD operations deliver great performance & power efficiency
† Intel® Architecture currently has SIMD operations of vector length 4, 8, 16
14
SIMD in JavaScript
Design Goals
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
15
SIMD.js: Bringing SIMD to JavaScript
Polyfill API:
https://github.com/johnmccutchan/ecmascript_simd
float32x4, int32x4, ...
Constructors:
Operations:
Status:
float32x4(x,y,z,w) float32x4.splat(s)
abs, neg, add, sub, mul, div, clamp, min, max, reciprocal, reciprocalSqrt, scale, sqrt, shuffle,
shuffleMix, equal, notEqual, lessThan, greaterThan , withX, withY …
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
16
JIT Compiler Support
SIMD.js code
V8 Full Codegen
Compiler
JIT compiler support:
Type Speculation
Unboxing
Inlining
Optimized Code
sum = SIMD.float32x4.add(sum, f32x4list.getAt(i));
3DB5BCBE
222 ff3424
3DB5BCC1
225 89442404
3DB5BCC5
229 ff75e8
3DB5BCC8
232 ba02000000
3DB5BCCD
237 8b7c2408
;; call .getAt()
3DB5BCD1
241 e84a24fcff
3DB5BCD6
246 8b75fc
3DB5BCD9
249 890424
3DB5BCDC
252 ba04000000
3DB5BCE1
257 8b7c240c
;; call .add()
3DB5BCE5
261 e876fdffff
3DB5BCEA
266 8b75fc
3DB5BCED
269 83c404
3DB5BCF0
272 8945ec
push [esp]
mov [esp+0x4],eax
push [ebp-0x18]
mov edx,00000002
mov edi,[esp+0x8]
3DB5E306
3DB5E30A
movups xmm3,[esi+eax*8]
addps xmm2,xmm3
358
362
0f101cc6
0f58d3
call 3DB1E120
mov esi,[ebp-0x4]
mov [esp],eax
mov edx,00000004
mov edi,[esp+0xc]
call 3DB5BA60
mov esi,[ebp-0x4]
add esp,0x4
mov [ebp-0x14],eax
17
JIT Compiler Support
sum = SIMD.float32x4.add(sum, f32x4list.getAt(i));
Type Speculation
3DB5E306
3DB5E30A
Inlining
358
362
0f101cc6
0f58d3
Unboxing
movups xmm3,[esi+eax*8]
addps xmm2,xmm3
18
SIMD.js - Example
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) ;
}
19
SIMD.js – The API
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 continue
if (mi4.signMask === 0x00) {
break;
}
var new_re4
var new_im4
z_re4
z_im4
count4
=
=
=
=
=
SIMD.float32x4.sub (z_re24, z_im24);
SIMD.float32x4.mul (SIMD.float32x4.mul (two4, z_re4), z_im4);
SIMD.float32x4.add (c_re4, new_re4);
SIMD.float32x4.add (c_im4, new_im4);
SIMD.int32x4.add (count4, SIMD.int32x4.and (mi4, one4));
}
return count4;
}
20
SIMD Speedups on Chromium*
14
SIMD x-times faster than non-SIMD
11.8
12



10
3rd Generation Intel® Core™ i7 processor (3667U)@ 2.00 GHz, 32-bit, Ubuntu* 13
3rd Generation Intel® Core™ i7 processor (3667U)@ 2.00 GHz, 64-bit, Ubuntu* 13
Intel® Atom™ processor Z3770 @ 1.46GHz, Android* 4.4
9.5
9.3
8
6.8
6
4
6.5
6.1
3.2 3.2
3.6
3.8
3.8
3.1
3.9
3.4
4.5
4.6
6.0
5.0 5.0
4.2
5.6 5.4
3.8
2.7
2
0
Transpose4x4
AOBench
Mandelbrot
MatrixMultiplication
VertexTransform
Average
ShiftRows
Matrix4x4Inverse
Excellent early results while still focused on functionality
SIMD.js benchmarks: https://github.com/johnmccutchan/ecmascript_simd/tree/master/src/benchmarks
21
Combining SIMD and Higher-Level Parallelism
WW: Number of WebWorkers
Our Chromium* Prototype
SIMD speedup is nicely multiplied by WebWorkers†
† Source: Intel® Peter Jensen : https://github.com/PeterJensen/
SIMD.js demos: http://peterjensen.github.io/idf2014-simd
22
Emscripten now targets SIMD.js
$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)]);
Scalar
C++ = $i$02$i + 4 | 0;
$i$02$i
} while (($i$02$i | 0) < 1e4);
SIMD
C++
HEAPF64[tempDoublePtr >> 3] =
($sumx4$01$i.w + ($sumx4$01$i.z +
($sumx4$01$i.x + $sumx4$01$i.y))) / 1.0e4;
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];
Scalar JS
_mm_store_ps(sumx4_mem, sumx4);
SIMD JS
return (sumx4_mem[0] + sumx4_mem[1]
+ sumx4_mem[2] + sumx4_mem[3])/len;
}
9
Speedup
C/C++
8
7
6
5
4
3
2
1
0
8.13
7.18
JavaScript*
2.03
1.00
Near-native
SIMD.js speedup
Speedup over Scalar JS
Emscripten brings native SIMD apps to the open web platform
23
Toward Perceptual Computing†
Learning &
Education
Immersive Collaboration
3D Scanning and Sharing
Scan it
Gaming
Speech
Out-of-reach
Device Input
Share it
Customize
and Print it
Devices sense and perceive user actions in a natural way
† Source: Intel® Perceptual Computing SDK: www.intel.com/software/perceptual
24
3D Cameras Make Perceptual Computing
Accessible
Embedded 3D Camera
Web Application
navigator.getUserMedia
({ video: true, depth: true }, success, failure);
function success(s) {
RGB
Stream
Depth
Stream
getUserMedia (WebRTC) API
Browser or HTML5 runtime
var video = document.querySelector(‘#video’);
video.src = URL.createObjectURL(s);
video.play();
// construct MediaStream from existing depth track
var depthStream = new MediaStream(s.getDepthTracks());
// send the created depth stream over a RTCPeerConnection
var peerConnection = new RTCPeerConnection(config);
peerConnection.addStream(depthStream);
var depthVideo = document.querySelector(‘#depthVideo’);
depthVideo.src = URL.createObjectURL(depthStream);
depthVideo.play();
}
Media Capture Depth Stream Extensions are in W3C WG†25
† W3C Media Capture Depth Stream Extensions: http://w3c.github.io/mediacapture-depth/
25
Web: The Most Viable Cross-Platform Technology Today
Rich Capabilities
and Content
Big Data
Social
Contextual
Crowdsourced
Sensors
“Things”
Visual, Perceptual, Full HW Access
and the Ubiquitous Application Platform of the
Future
26
Thank you
Legal Disclaimer
INFORMATION 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, in personal
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.
Legal Disclaimer
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.
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 intellectual property, 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.
Parallel Parsing and Compilation
Bootstrap:
Launch: 1 thread
4 threads
PESPMA 2009
Epic* Citadel* profile on Firefox*
Cycle Breakdown
js::compile
JS and GFX execution
Four threads for
JavaScript*
parsing and
compilation
6.4
6.2
4.6 2.2
0.9
os::others
43.6
6.7
12.8
gfx::compile
16.6
js::parse
js::others
browser::others
os::mem
js::jitted
gfx::exec
Background JIT compilers now in Chrome*, Firefox, Internet Explorer*, Safari*
31
Parallelism is now Required to Benefit from Moore’s Law
SS: Sequential Scalar
Vector
PS: Parallel Scalar
PV: Parallel
Higher is better
Higher is better
Higher is better
Open web client platform needs to be on Moore’s Law curve
†
Courtesy of Intel® Robert Geva: & Jim Jeffers
32