扫一扫分享
Gpu.js提供了一种方便的方式来运行GPU上的JavaScript函数。Brain.js是旧的神经网络库的延续,并使用Gpu.js硬件加速。
GPU.js是用于Web和节点的JavaScript的GPGPU(GPU上的通用计算)的JavaScript加速库。GPU.js自动将简单的JavaScript函数转换为着色器语言并编译它们,以便它们在GPU上运行。如果GPU不可用,这些功能仍将在常规JavaScript中运行。
<script src="bin/gpu-browser.min.js"></script>
<script>
// GPU is a constructor and namespace for browser
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]);
const c = multiplyMatrix(a, b);
</script>
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]);
const c = multiplyMatrix(a, b);
import { GPU } from 'gpu.js';
const gpu = new GPU();
const multiplyMatrix = gpu.createKernel(function(a: number[][], b: number[][]) {
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]);
const c = multiplyMatrix(a, b) as number[][];
手机预览