gpu.js - Tăng tốc Javascript với GPU

Mình lướt github và vô tình thấy thư viện gpu.js này. gpu.js tự động compile mã Javascript thành một ngôn ngữ ánh xạ khác, và chạy trên GPU nhờ vào WebGL API. Nếu máy trình duyệt không có GPU, mã JS vẫn được thực thi bình thường.

Ví dụ về phép nhân ma trận 512x512 trên gpu.js

const 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)
const matMult = 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;
  })
  .setDimensions([512, 512]);

// Perform matrix multiplication on 2 matrices of size 512 x 512
const c = matMult(a, b);

Khi chạy benchmark ở trang chủ, tùy vào cấu hình máy bạn, thường phép tính này nhanh hơn 1-15x lần.

Tham khảo

WebGPUintro-jsJavascript