在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:gpujs/gpu.js开源软件地址:https://github.com/gpujs/gpu.js开源编程语言:JavaScript 98.5%开源软件介绍:GPU.jsGPU.js is a JavaScript Acceleration library for GPGPU (General purpose computing on GPUs) in JavaScript for Web and Node. GPU.js automatically transpiles simple JavaScript functions into shader language and compiles them so they run on your GPU. In case a GPU is not available, the functions will still run in regular JavaScript. For some more quick concepts, see Quick Concepts on the wiki. What is this sorcery?Creates a GPU accelerated kernel transpiled from a javascript function that computes a single element in the 512 x 512 matrix (2D array). The kernel functions are ran in tandem on the GPU often resulting in very fast computations! You can run a benchmark of this here. Typically, it will run 1-15x faster depending on your hardware. Matrix multiplication (perform matrix multiplication on 2 matrices of size 512 x 512) written in GPU.js: Browser<script src="dist/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> CDN
Nodeconst { 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); Typescriptimport { 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[][]; Click here for more typescript examples. Table of ContentsNotice documentation is off? We do try our hardest, but if you find something, please bring it to our attention, or become a contributor!
DemosGPU.js in the wild, all around the net. Add yours here!
InstallationOn Linux, ensure you have the correct header files installed: npmnpm install gpu.js --save yarnyarn add gpu.js Nodeconst { GPU } = require('gpu.js');
const gpu = new GPU(); Node Typescript New in V2!import { GPU } from 'gpu.js';
const gpu = new GPU(); BrowserDownload the latest version of GPU.js and include the files in your HTML page using the following tags: <script src="dist/gpu-browser.min.js"></script>
<script>
const gpu = new GPU();
</script>
|
Output size | How to specify output size | How to reference in kernel |
---|---|---|
1D | [length] |
value[this.thread.x] |
2D | [width, height] |
value[this.thread.y][this.thread.x] |
3D | [width, height, depth] |
value[this.thread.z][this.thread.y][this.thread.x] |
const settings = {
output: [100]
};
or
// You can also use x, y, and z
const settings = {
output: { x: 100 }
};
Create the function you want to run on the GPU. The first input parameter to createKernel
is a kernel function which will compute a single number in the output. The thread identifiers, this.thread.x
, this.thread.y
or this.thread.z
will allow you to specify the appropriate behavior of the kernel function at specific positions of the output.
const kernel = gpu.createKernel(function() {
return this.thread.x;
}, settings);
The created function is a regular JavaScript function, and you can use it like one.
kernel();
// Result: Float32Array[0, 1, 2, 3, ... 99]
Note: Instead of creating an object, you can use the chainable shortcut methods as a neater way of specifying settings.
const kernel = gpu.createKernel(function() {
return this.thread.x;
}).setOutput([100]);
kernel();
// Result: Float32Array[0, 1, 2, 3, ... 99]
GPU.js makes variable declaration inside kernel functions easy. Variable types supported are:
Number
(Integer or Number), example: let value = 1
or let value = 1.1
Boolean
, example: let value = true
Array(2)
, example: let value = [1, 1]
Array(3)
, example: let value = [1, 1, 1]
Array(4)
, example: let value = [1, 1, 1, 1]
private Function
, example: function myFunction(value) { return value + 1; }
Number
kernel example:
const kernel = gpu.createKernel(function() {
const i = 1;
const j = 0.89;
return i + j;
}).setOutput([100]);
Boolean
kernel example:
const kernel = gpu.createKernel(function() {
const i = true;
if (i) return 1;
return 0;
}).setOutput([100]);
Array(2)
kernel examples:
Using declaration
const kernel = gpu.createKernel(function() {
const array2 = [0.08, 2];
return array2;
}).setOutput([100]);
Directly returned
const kernel = gpu.createKernel(function() {
return [0.08, 2];
}).setOutput([100]);
Array(3)
kernel example:
Using declaration
const kernel = gpu.createKernel(function() {
const array2 = [0.08, 2, 0.1];
return array2;
}).setOutput([100]);
Directly returned
const kernel = gpu.createKernel(function() {
return [0.08, 2, 0.1];
}).setOutput([100]);
Array(4)
kernel example:
Using declaration
const kernel = gpu.createKernel(function() {
const array2 = [0.08, 2, 0.1, 3];
return array2;
}).setOutput([100]);
Directly returned
const kernel = gpu.createKernel(function() {
return [0.08, 2, 0.1, 3];
}).setOutput([100]);
private Function
kernel example:
const kernel = gpu.createKernel(function() {
function myPrivateFunction() {
return [0.08, 2, 0.1, 3];
}
return myPrivateFunction(); // <-- type inherited here
}).setOutput([100]);
Debugging can be done in a variety of ways, and there are different levels of debugging.
new GPU({ mode: 'dev' })
GPU.js
into development mode. Here you can insert breakpoints, and be somewhat liberal in how your kernel is developed.const gpu = new GPU({ mode: 'dev' });
const kernel = gpu.createKernel(function(arg1, time) {
// put a breakpoint on the next line, and watch it get hit
const v = arg1[this.thread.y][this.thread.x * time];
return v;
}, { output: [100, 100] });
debugger
:
const gpu = ne
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论