gpt4 book ai didi

javascript - Nodejs GPU.js 使用 GPU 比使用 CPU 慢

转载 作者:行者123 更新时间:2023-12-03 12:18:18 28 4
gpt4 key购买 nike

我已经运行了一个基准测试来比较 nodejs 和 GPU.js 中 CPU 和 GPU 的使用情况。 NVidia 图标在第一个控制台计时器中显示 GPU 使用情况,但它比 CPU(第二个计时器)慢。

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
我做错了什么吗?

最佳答案

这不是对 CPU 和 GPU 进行基准测试的方法

  • GPU 有预热时间,所以如果你真的想在 1000 次执行而不是单次执行上对它们进行基准比较
  • GPU 并不总是更快,这取决于任务和 GPU RAM 大小
  • 最后,正如 Keith 在评论中提到的那样,gpu 在并行小任务和大批量中的效果比 cpu 更好
  • 关于javascript - Nodejs GPU.js 使用 GPU 比使用 CPU 慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65370419/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com