forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocks.html
More file actions
434 lines (378 loc) · 15.9 KB
/
rocks.html
File metadata and controls
434 lines (378 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="gpu.js is a single-file JavaScript library for GPGPU in the browser. gpu.js will automatically compile specially written JavaScript functions into shader language and run them on the GPU using the WebGL API. In the case where WebGL is not available, the functions will still run in regular JavaScript." />
<meta property="og:title" content="gpu.js - GPU Accelerated JavaScript" />
<meta property="og:type" content="website" />
<meta property="og:url" content="http://gpu.rocks/" />
<meta property="og:image" content="http://gpu.rocks/img/ogimage.png" />
<title>gpu.js - GPU Accelerated JavaScript</title>
<script type="text/javascript">
//<![CDATA[
try{if (!window.CloudFlare) {var CloudFlare=[{verbose:0,p:0,byc:0,owlid:"cf",bag2:1,mirage2:0,oracle:0,paths:{cloudflare:"/cdn-cgi/nexp/dok3v=1613a3a185/"},atok:"5a08ba077d7ba0151ece4e74a05868ce",petok:"7bee0af6d9ea0c402209e75c203040a404d237f1-1494435921-1800",zone:"gpu.rocks",rocket:"0",apps:{"abetterbrowser":{"ie":"8","opera":null,"chrome":null,"safari":null,"firefox":null},"ga_key":{"ua":"UA-43926610-2","ga_bs":"2"}}}];!function(a,b){a=document.createElement("script"),b=document.getElementsByTagName("script")[0],a.async=!0,a.src="//ajax.cloudflare.com/cdn-cgi/nexp/dok3v=85b614c0f6/cloudflare.min.js",b.parentNode.insertBefore(a,b)}()}}catch(e){};
//]]>
</script>
<link rel="icon" href="img/jelly.png">
<!-- Bootstrap Core CSS - Uses Bootswatch Flatly Theme: http://bootswatch.com/flatly/ -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/freelancer.css?3" rel="stylesheet">
<!-- Custom Fonts -->
<link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
<link href="//fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic" rel="stylesheet" type="text/css">
<!-- highlight.js -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/color-brewer.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script type="text/javascript">
/* <![CDATA[ */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-43926610-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
(function(b){(function(a){"__CF"in b&&"DJS"in b.__CF?b.__CF.DJS.push(a):"addEventListener"in b?b.addEventListener("load",a,!1):b.attachEvent("onload",a)})(function(){"FB"in b&&"Event"in FB&&"subscribe"in FB.Event&&(FB.Event.subscribe("edge.create",function(a){_gaq.push(["_trackSocial","facebook","like",a])}),FB.Event.subscribe("edge.remove",function(a){_gaq.push(["_trackSocial","facebook","unlike",a])}),FB.Event.subscribe("message.send",function(a){_gaq.push(["_trackSocial","facebook","send",a])}));"twttr"in b&&"events"in twttr&&"bind"in twttr.events&&twttr.events.bind("tweet",function(a){if(a){var b;if(a.target&&a.target.nodeName=="IFRAME")a:{if(a=a.target.src){a=a.split("#")[0].match(/[^?=&]+=([^&]*)?/g);b=0;for(var c;c=a[b];++b)if(c.indexOf("url")===0){b=unescape(c.split("=")[1]);break a}}b=void 0}_gaq.push(["_trackSocial","twitter","tweet",b])}})})})(window);
/* ]]> */
</script>
</head>
<body id="page-top" class="index">
<!-- Right side, white on black -->
<a href="https://github.com/gpujs/gpu.js" class="github-corner"><svg width="80" height="80" viewBox="0 0 250 250" style="fill: rgba(21, 21, 19, 0.53); color:#fff; position: absolute; top: 0; border: 0; right: 0;"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
<!-- Header -->
<header>
<div class="container">
<div class="row">
<div class="col-lg-12">
<img class="img-responsive main-logo" src="img/jelly.png" alt="">
<div class="intro-text">
<span class="name">gpu.js</span>
<span class="skills">GPU Accelerated JavaScript</span>
</div>
</div>
<div class="col-lg-8 col-lg-offset-2 text-center">
<a href="js/gpu.min.js?nocache" download="gpu.js" class="btn btn-lg btn-outline">
<i class="fa fa-download"></i> Download (v0.0-Alpha)
</a>
<a href="getting-started/" class="btn btn-lg btn-outline">
Documentation
</a>
</div>
</div>
</div>
</header>
<!-- Features Section -->
<section style="background-color: #f5f5f5">
<div class="container">
<div class="row">
<div class="col-lg-4 col-lg-offset-2">
<div class="text-center">
<img src="img/threads.png" style="padding: 20px; width: 100px; height: 100px;" />
<p>
Perform massively parallel GPGPU computations using WebGL.
</p>
</div>
</div>
<div class="col-lg-4">
<div class="text-center">
<img src="img/thread.png" style="padding: 20px; width: 100px; height: 100px;" />
<p>
Graceful pure JavaScript fallback when WebGL is not available.
</p>
</div>
</div>
</div>
</div>
</section>
<!-- Main Content Section -->
<section>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Demo</h2>
<hr class="star-primary" />
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h3>Matrix Multiplication</h3>
<pre><code class="js" style="background: transparent;">var gpu = new GPU();
// Create the GPU accelerated function from a kernel
// function that computes a single element in the
// 512 x 512 matrix (2D array). The kernel function
// is run in a parallel manner in the GPU resulting
// in very fast computations! (...sometimes)
var mat_mult = gpu.createKernel(function(A, B) {
var sum = 0;
for (var i=0; i<512; i++) {
sum += A[this.thread.y][i] * B[i][this.thread.x];
}
return sum;
}).dimensions([512, 512]);
// Perform matrix multiplication on 2 matrices of size 512 x 512
var C = mat_mult(A, B);</code></pre>
<div class="alert alert-info demo-mult hide" role="alert"></div>
<a href="javascript:demoMult();" class="btn btn-primary">
<i class="fa fa-play"></i> Run Benchmark
</a>
<div id="canvas-container"></div>
</div>
</div>
</section>
<hr />
<section>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Syntax Support</h2>
<hr class="star-primary" />
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<p>
gpu.js relies on the assumption that the kernel function is using only a subset of legal JavaScript syntax:
</p>
<ul>
<li>1D, 2D, 3D array of numbers or just numbers as kernel input</li>
<li>1D, 2D, 3D array of numbers as kernel output</li>
<li>Number variables</li>
<li>Arithematic operations (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>%</code>)</li>
<li>Javascript Math operations (<code>Math.floor()</code> and etc.)</li>
<li><code>for</code></code> loops <em>(of fixed sizes only!)</em></li>
<li><code>if</code> and <code>else</code> statements</li>
<li>No variables captured by a closure</li>
</ul>
</div>
</div>
</div>
</section>
<!-- Awards Section -->
<section class="hacknroll" id="accolades">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<i class="fa fa-trophy" style="font-size: 100px;"></i>
<h3>2nd Place, Hack & Roll 2016</h3>
<p>
Made under 24 hours for a NUS Hackers hackathon, out of 68 competing projects.
</p>
</div>
</div>
</div>
</section>
<!-- Footer -->
<footer class="text-center">
<div class="footer-above">
<div class="container">
<div class="row">
<div class="col-lg-12">
<h2>Made By</h2>
<hr class="star-light-footer" />
<div class="container">
<div class="row">
<div class="footer-col col-md-4">
<img class="profile-photo img-circle" src="img/team/matthew.jpg" alt="">
<p>Matthew Saw</p>
</div>
<div class="footer-col col-md-4">
<img class="profile-photo img-circle" src="img/team/fazli.jpg" alt="">
<p>Fazli Sapuan</p>
</div>
<div class="footer-col col-md-4">
<img class="profile-photo img-circle" src="img/team/eugene.jpg" alt="">
<p>Cheah Eugene</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="footer-below">
<div class="container">
<div class="row">
<div class="col-lg-12">
Copyright © 2016 gpu.js Team. Licensed under MIT.
</div>
</div>
</div>
</div>
</footer>
<!-- Scroll to Top Button (Only visible on small and extra-small screen sizes) -->
<div class="scroll-top page-scroll visible-xs visible-sm">
<a class="btn btn-primary" href="#page-top">
<i class="fa fa-chevron-up"></i>
</a>
</div>
<!-- jQuery -->
<script src="http://gpu.rocks/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="http://gpu.rocks/js/bootstrap.min.js"></script>
<!-- Plugin JavaScript -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
<script src="http://gpu.rocks/js/classie.js"></script>
<script src="http://gpu.rocks/js/lodash.min.js"></script>
<script src="http://gpu.rocks/js/platform.js"></script>
<script src="http://gpu.rocks/js/benchmark.js"></script>
<script src="../../bin/gpu.js?nocache"></script>
<script>
var gpu = new GPU({ mode: 'gpu' });
var cpu = new GPU({ mode: 'cpu' });
function splitArray(array, part) {
var tmp = [];
for(var i = 0; i < array.length; i += part) {
tmp.push(array.slice(i, i + part));
}
return tmp;
}
//
// Startup code
//
var mat1_size = 512;
var mat2_size = 256;
var mat3_size = 128;
var A = [];
var B = [];
var C = [];
for(var n = 0; n < mat1_size*mat2_size; n++) {
var randA = Math.random();
var randB = Math.random();
A.push(randA);
B.push(randB);
}
for(var n = 0; n < mat2_size*mat3_size; n++) {
C.push(Math.random());
}
A = splitArray(A, mat1_size);
B = splitArray(B, mat1_size);
C = splitArray(C, mat2_size);
function createMult1(mode) {
var opt = {
dimensions: [mat1_size, mat2_size],
debug: true,
outputToTexture: true
};
if (mode === 'gpu') {
var kernel = gpu.createKernel(function(A, B) {
var sum = 0;
for (var i=0; i<512; i++) {
sum += A[this.thread.y][i] * B[i][this.thread.x];
}
return sum;
}, opt);
kernel.build(A, B);
return kernel;
}
return cpu.createKernel(function(A, B) {
var sum = 0;
for (var i=0; i<512; i++) {
sum += A[this.thread.y][i] * B[i][this.thread.x];
}
return sum;
}, opt);
}
function createMult2(mode) {
var opt = {
dimensions: [mat2_size, mat3_size],
debug: true,
outputToTexture: true
};
if (mode === 'gpu') {
var kernel = gpu.createKernel(function(B, C) {
var sum = 0;
for (var i=0; i<512; i++) {
sum += B[this.thread.y][i] * C[i][this.thread.x];
}
return sum;
}, opt);
kernel.build(A, B);
return kernel;
}
return cpu.createKernel(function(B, C) {
var sum = 0;
for (var i=0; i<512; i++) {
sum += B[this.thread.y][i] * C[i][this.thread.x];
}
return sum;
}, opt);
}
var mult1 = {
cpu: createMult1('cpu'),
gpu: createMult1('gpu')
};
var mult2 = {
cpu: createMult2('cpu'),
gpu: createMult2('gpu')
};
//$('#canvas-container').append(mult1.gpu.getCanvas());
var benchmarkOpt = {};
var suite = new Benchmark.Suite;
suite.add('mat_mult_cpu', function() {
var mode = 'cpu';
var mat_mult = mult1[mode];
return mat_mult(A, B);
}, benchmarkOpt);
suite.add('mat_mult_gpu', function() {
var mode = 'gpu';
var mat_mult = mult1[mode];
return mat_mult(A, B);
}, benchmarkOpt);
suite.add('mat_mult_cpu diff', function() {
var mode = 'cpu';
var mat_mult = mult2[mode];
return mat_mult(B, C);
}, benchmarkOpt);
suite.add('mat_mult_gpu diff', function() {
var mode = 'gpu';
var mat_mult = mult2[mode];
return mat_mult(B, C);
}, benchmarkOpt);
suite.on('complete', function(event) {
var stats = {};
stats.cpu = this.filter(function(benchmark) {
return benchmark.name == 'mat_mult_cpu';
}).map('stats')[0];
stats.gpu = this.filter(function(benchmark) {
return benchmark.name == 'mat_mult_gpu';
}).map('stats')[0];
console.dir(stats);
var faster = '';
if (stats.cpu.mean > stats.gpu.mean) {
var times = stats.cpu.mean / stats.gpu.mean;
faster = ' <em>(' + times.toFixed(2) + ' times faster!)</em>';
}
var html = '';
html += '<p>CPU: ' + stats.cpu.mean.toFixed(3) +'s \xb1' + stats.cpu.rme.toFixed(1) + '%</p>'
html += '<p>GPU: ' + stats.gpu.mean.toFixed(3) + 's \xb1' + stats.gpu.rme.toFixed(1) + '%' + faster + '</p>';
html += '<small><em>Benchmarks provided by <a href="https://github.com/bestiejs/benchmark.js">benchmark.js</a></em></small>';
$('.demo-mult').removeClass('text-center');
$('.demo-mult').html(html);
});
suite.on('error', function(event) {
$('.demo-mult').removeClass('text-center');
$('.demo-mult').removeClass('alert-info');
$('.demo-mult').addClass('alert-danger');
$('.demo-mult').html('There was an error running on the GPU.');
});
function demoMult() {
$('.demo-mult').removeClass('hide');
$('.demo-mult').addClass('text-center');
$('.demo-mult').html('<i class="fa fa-cog fa-spin" style="font-size: 60px;"></i>');
suite.run({ 'async': true });
}
</script>
</body>
</html>