Eduardo Poço
Eduardo Poço

Reputation: 3079

Nodejs GPU.js slower using GPU than using CPU

I have run a benchmark to compare the use of CPU and GPU in nodejs with GPU.js. The NVidia icon shows GPU use in the first console timer, but it is slower than the CPU (second timer).

const {GPU} = require('gpu.js');
const gpu = new GPU();

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;
}).setOutput([512, 512]);

var a = [];
var b = [];
for (var i = 0; i < 512; i++) {
    a.push([]);
    b.push([]);
    for (var j = 0; j < 512; j++) {
        a[i].push(1);
        b[i].push(-1);
    }
}

console.time("gpu");
const c = multiplyMatrix(a, b);
console.timeEnd("gpu"); //2148ms

console.time("cpu");
var d = [];
for (var i = 0; i < 512; i++) {
    d.push([]);
    for (var j = 0; j < 512; j++) {
        let sum = 0;
        for (let k = 0; k < 512; k++) {
            sum += a[i][k] * b[k][j];
        }
        
        d[i].push(sum);
    }
}
console.timeEnd("cpu"); //710ms

Am I doing something clearly wrong?

Upvotes: 5

Views: 4327

Answers (1)

Naor Tedgi
Naor Tedgi

Reputation: 5699

this isn't the way to benchmarking CPU vs GPU

  1. the GPU got warmup time so if you really want to benchmark compare both of them on a 1000 execution and not single execution

  2. GPU won't always be faster it depends on the task and the GPU RAM Size

  3. and finally as Keith Mention at the comment gpu works better then cpu in parallel small task and large batches

Upvotes: 5

Related Questions