gpu.js GPU Accelerated JavaScript

Perform massively parallel GPGPU computations using WebGL.

Graceful pure JavaScript fallback when WebGL is not available.


Matrix Multiplication

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);
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
  • Arithematic operations (+, -, *, /, %)
  • Javascript Math operations (Math.floor() and etc.)
  • for loops (of fixed sizes only!)
  • if and else statements
  • No variables captured by a closure

2nd Place, Hack & Roll 2016

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