mirror of
https://github.com/gpujs/gpu.js.git
synced 2026-01-18 16:04:10 +00:00
435 lines
16 KiB
HTML
435 lines
16 KiB
HTML
|
|
<!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>
|