Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement