Skip to content

Instantly share code, notes, and snippets.

@robertleeplummerjr
Created April 13, 2019 02:43
Show Gist options
  • Save robertleeplummerjr/990e8063f6e012898974ca12b246bcdc to your computer and use it in GitHub Desktop.
Save robertleeplummerjr/990e8063f6e012898974ca12b246bcdc to your computer and use it in GitHub Desktop.
const { GPU } = require('gpu.js');
const gpu = new GPU({ mode: 'gpu' });
for (let i = 0; i < 1000; i++) { //1000 seems... so small...
const setup = setupBenchmark(i * 512);
benchmark(setup);
}
function setupBenchmark(size) {
const width = size;
const height = size;
return {
kernel: makeKernel(width, height),
inputA: randomMatrix(width, height),
inputB: randomMatrix(width, height),
};
}
function benchmark(setup) {
setup.kernel(setup.inputA, setup.inputB);
}
function makeKernel(width, height) {
const multiplyMatrix = gpu.createKernel(function(a, b) {
let sum = 0;
for (let i = 0; i < 512; i++) {
sum += a[this.thread.y][i] * b[i][this.thread.x];
}
return sum;
}, { output: [width, height] });
return multiplyMatrix;
}
function randomMatrix(width, height) {
const rows = [];
for (let y = 0; y < height; y++) {
const columns = new Float32Array(width);
rows.push(columns);
for (let x = 0; x < width; x++) {
columns[x] = Math.random();
}
}
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment