gpu.js GPU Accelerated JavaScript

Perform massively parallel GPGPU computations using WebGL.

Graceful pure JavaScript fallback when WebGL is not available.

Example


Matrix Multiplication

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)


Create a "kernel" (a fancy word for a function that runs on your GPU)

const gpu = new GPU();
const multiplyMatrix = 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;
}).setOutput([512, 512]);
const gpu = new GPU();
const multiplyMatrix = 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;
})
  .setOutput([512, 512])
  .setOutputToTexture(true);

Call kernel

Performs matrix multiplication on 2 matrices of size 512 x 512

const c = multiplyMatrix(a, b);

Benchmark it!

Texture Mode (GPU only)


Run Benchmark

Syntax Support


gpu.js relies on the assumption that the kernel function is using only a subset of legal JavaScript syntax:

  • 1D, 2D, 3D array of numbers or just numbers as kernel input
  • 1D, 2D, 3D array of numbers as kernel output
  • Number variables
  • Custom and custom native functions
  • Arithmetic operations (+, +=, -, *, /, %)
  • Javascript Math functions (Math.floor() and etc.)
  • Loops
  • if and else statements
  • const and let
  • No variables captured by a closure

Development Benchmarks


Here is a chart representing the performance of a 512x512 maltrix multiplication throughout our development (lower is better).

  • Hardware: Macbook Pro Retina 2012
  • Operating System: Mac OS X 10.12.5
  • Browser: Firefox 54.0.1 (64-Bit)
  • Hardware: i7-7700K + GTX1080
  • Operating System: Windows 10 (Build 15063.483)
  • Browser:
    Firefox 54.0.1 (32-Bit)
    Chrome 59.0.3071.115 (64-Bit)
    Edge 40.15063.0.0 (64-Bit)

Last Updated: 2017-07-23


Older Releases


This is the legacy version for backward compatiblity with the demos made using the "hackathon" API.

Download

2nd Place, Hack & Roll 2016

Made under 24 hours for a NUS Hackers hackathon, out of 68 competing projects.