├── README.md ├── gpu-core.js ├── gpu-core.min.js ├── gpu.js ├── gpu.min.js ├── index.html ├── index.js └── style.css /README.md: -------------------------------------------------------------------------------- 1 | # gpu.js Demo 2 | 3 | Demo code for [Introducing gpu.js: GPU Accelerated JavaScript](https://hackernoon.com/introducing-gpu-js-gpu-accelerated-javascript-ba11a6069327) :tada: :rocket: -------------------------------------------------------------------------------- /gpu-core.js: -------------------------------------------------------------------------------- 1 | /** 2 | * gpu.js 3 | * http://gpu.rocks/ 4 | * 5 | * GPU Accelerated JavaScript 6 | * 7 | * @version 0.0.0 8 | * @date Thu Jul 20 2017 22:09:33 GMT+0530 (IST) 9 | * 10 | * @license MIT 11 | * The MIT License 12 | * 13 | * Copyright (c) 2017 gpu.js Team 14 | */ 15 | "use strict";(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | gpu.js demo 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const c = document.getElementById('c'); 2 | 3 | const gpu = new GPU({ 4 | mode: 'gpu' 5 | }); 6 | 7 | const gpuMatMult = gpu.createKernel(function (A, B) { 8 | var sum = 0; 9 | for (var i = 0; i < 512; i++) { 10 | sum += A[this.thread.y][i] * B[i][this.thread.x]; 11 | } 12 | return sum; 13 | }) 14 | .setDimensions([A.length, B.length]) 15 | .setOutputToTexture(true); 16 | 17 | function cpuMatMult(m, n) { 18 | var result = []; 19 | for (var i = 0; i < m.length; i++) { 20 | result[i] = []; 21 | for (var j = 0; j < n[0].length; j++) { 22 | var sum = 0; 23 | for (var k = 0; k < m[0].length; k++) { 24 | sum += m[i][k] * n[k][j]; 25 | } 26 | result[i][j] = sum; 27 | } 28 | } 29 | return result; 30 | } 31 | 32 | // Generate Matrices 33 | const matrices = generateMatrices(); 34 | const A = matrices.A; 35 | const B = matrices.B; 36 | 37 | //CPU 38 | const startCPU = window.performance.now(); 39 | const cpuResult = cpuMatMult(A, B); 40 | const endCPU = window.performance.now(); 41 | const cpuTime = endCPU - startCPU; 42 | console.log(`CPU: ${cpuTime}ms`); 43 | 44 | // //GPU 45 | const startGPU = window.performance.now(); 46 | const result = gpuMatMult(A, B); 47 | const endGPU = window.performance.now(); 48 | const gpuTime = endGPU - startGPU; 49 | console.log(`GPU: ${gpuTime}ms`); 50 | 51 | //Diff 52 | const diff = (cpuTime - gpuTime) / (gpuTime); 53 | console.log(`%c ${diff}`, 'color: red;', `times faster!`) 54 | 55 | 56 | function generateMatrices() { 57 | const matSize = 512; 58 | let A = []; 59 | let B = []; 60 | for (let n = 0; n < matSize * matSize; n++) { 61 | const randA = Math.random(); 62 | const randB = Math.random(); 63 | A.push(randA); 64 | B.push(randB); 65 | } 66 | 67 | A = splitArray(A, matSize); 68 | B = splitArray(B, matSize); 69 | 70 | function splitArray(array, part) { 71 | var tmp = []; 72 | for (var i = 0; i < array.length; i += part) { 73 | tmp.push(array.slice(i, i + part)); 74 | } 75 | return tmp; 76 | } 77 | 78 | return { 79 | A, 80 | B 81 | }; 82 | } -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhisheksoni27/gpu.js-demo/3909bf34b202276ddfa30415c33f15d92e6294d5/style.css --------------------------------------------------------------------------------