forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu.js
More file actions
346 lines (317 loc) · 8.8 KB
/
gpu.js
File metadata and controls
346 lines (317 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
'use strict';
const utils = require('./utils');
const WebGLRunner = require('../backend/web-gl/runner');
const CPURunner = require('../backend/cpu/runner');
const WebGLValidatorKernel = require('../backend/web-gl/validator-kernel');
const GPUCore = require("./gpu-core");
/**
* Initialises the GPU.js library class which manages the webGlContext for the created functions.
* @class
* @extends GPUCore
*/
class GPU extends GPUCore {
/**
* Creates an instance of GPU.
* @param {any} settings - Settings to set mode, andother properties. See #GPUCore
* @memberOf GPU#
*/
constructor(settings) {
super(settings);
settings = settings || {};
this._canvas = settings.canvas || null;
this._webGl = settings.webGl || null;
let mode = settings.mode || 'webgl';
if (!utils.isWebGlSupported()) {
console.warn('Warning: gpu not supported, falling back to cpu support');
mode = 'cpu';
}
this.kernels = [];
const runnerSettings = {
canvas: this._canvas,
webGl: this._webGl
};
if (mode) {
switch (mode.toLowerCase()) {
case 'cpu':
this._runner = new CPURunner(runnerSettings);
break;
case 'gpu':
case 'webgl':
this._runner = new WebGLRunner(runnerSettings);
break;
case 'webgl-validator':
this._runner = new WebGLRunner(runnerSettings);
this._runner.Kernel = WebGLValidatorKernel;
break;
default:
throw new Error(`"${mode}" mode is not defined`);
}
}
}
/**
*
* This creates a callable function object to call the kernel function with the argument parameter set
*
* @name createKernel
* @function
* @memberOf GPU##
*
* @param {Function} inputFunction - The calling to perform the conversion
* @param {Object} settings - The parameter configuration object
* @property {String} settings.dimensions - Thread dimension array (Defeaults to [1024])
* @property {String} settings.mode - CPU / GPU configuration mode (Defaults to null)
*
* The following modes are supported
* *null* / *'auto'* : Attempts to build GPU mode, else fallbacks
* *'gpu'* : Attempts to build GPU mode, else fallbacks
* *'cpu'* : Forces JS fallback mode only
*
*
* @returns {Function} callable function to run
*
*/
createKernel(fn, settings) {
//
// basic parameters safety checks
//
if (typeof fn === 'undefined') {
throw 'Missing fn parameter';
}
if (!utils.isFunction(fn) && typeof fn !== 'string') {
throw 'fn parameter not a function';
}
const kernel = this._runner.buildKernel(fn, settings || {});
//if canvas didn't come from this, propagate from kernel
if (!this._canvas) {
this._canvas = kernel.getCanvas();
}
if (!this._runner.canvas) {
this._runner.canvas = kernel.getCanvas();
}
this.kernels.push(kernel);
return kernel;
}
/**
*
* Create a super kernel which executes sub kernels
* and saves their output to be used with the next sub kernel.
* This can be useful if we want to save the output on one kernel,
* and then use it as an input to another kernel. *Machine Learning*
*
* @name createKernelMap
* @function
* @memberOf GPU#
*
* @param {Object|Array} subKernels - Sub kernels for this kernel
* @param {Function} rootKernel - Root kernel
*
* @returns {Function} callable kernel function
*
* @example
* const megaKernel = gpu.createKernelMap({
* addResult: function add(a, b) {
* return a[this.thread.x] + b[this.thread.x];
* },
* multiplyResult: function multiply(a, b) {
* return a[this.thread.x] * b[this.thread.x];
* },
* }, function(a, b, c) {
* return multiply(add(a, b), c);
* });
*
* megaKernel(a, b, c);
*
* Note: You can also define subKernels as an array of functions.
* > [add, multiply]
*
*/
createKernelMap() {
let fn;
let settings;
if (typeof arguments[arguments.length - 2] === 'function') {
fn = arguments[arguments.length - 2];
settings = arguments[arguments.length - 1];
} else {
fn = arguments[arguments.length - 1];
}
if (!utils.isWebGlDrawBuffersSupported()) {
this._runner = new CPURunner(settings);
}
const kernel = this.createKernel(fn, settings);
if (Array.isArray(arguments[0])) {
const functions = arguments[0];
for (let i = 0; i < functions.length; i++) {
kernel.addSubKernel(functions[i]);
}
} else {
const functions = arguments[0];
for (let p in functions) {
if (!functions.hasOwnProperty(p)) continue;
kernel.addSubKernelProperty(p, functions[p]);
}
}
return kernel;
}
/**
*
* Combine different kernels into one super Kernel,
* useful to perform multiple operations inside one
* kernel without the penalty of data transfer between
* cpu and gpu.
*
* The number of kernel functions sent to this method can be variable.
* You can send in one, two, etc.
*
* @name combineKernels
* @function
* @memberOf GPU#
*
* @param {Function} subKernels - Kernel function(s) to combine.
* @param {Function} rootKernel - Root kernel to combine kernels into
*
* @example
* combineKernels(add, multiply, function(a,b,c){
* return add(multiply(a,b), c)
* })
*
* @returns {Function} Callable kernel function
*
*/
combineKernels() {
const lastKernel = arguments[arguments.length - 2];
const combinedKernel = arguments[arguments.length - 1];
if (this.getMode() === 'cpu') return combinedKernel;
const canvas = arguments[0].getCanvas();
let webGl = arguments[0].getWebGl();
for (let i = 0; i < arguments.length - 1; i++) {
arguments[i]
.setCanvas(canvas)
.setWebGl(webGl)
.setOutputToTexture(true);
}
return function() {
combinedKernel.apply(null, arguments);
const texSize = lastKernel.texSize;
const gl = lastKernel.getWebGl();
const threadDim = lastKernel.threadDim;
let result;
if (lastKernel.floatOutput) {
result = new Float32Array(texSize[0] * texSize[1] * 4);
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.FLOAT, result);
} else {
const bytes = new Uint8Array(texSize[0] * texSize[1] * 4);
gl.readPixels(0, 0, texSize[0], texSize[1], gl.RGBA, gl.UNSIGNED_BYTE, bytes);
result = new Float32Array(bytes.buffer);
}
result = result.subarray(0, threadDim[0] * threadDim[1] * threadDim[2]);
if (lastKernel.output.length === 1) {
return result;
} else if (lastKernel.output.length === 2) {
return utils.splitArray(result, lastKernel.output[0]);
} else if (lastKernel.output.length === 3) {
const cube = utils.splitArray(result, lastKernel.output[0] * lastKernel.output[1]);
return cube.map(function(x) {
return utils.splitArray(x, lastKernel.output[0]);
});
}
};
}
/**
*
* Adds additional functions, that the kernel may call.
*
* @name addFunction
* @function
* @memberOf GPU#
*
* @param {Function|String} fn - JS Function to do conversion
* @param {String[]|Object} paramTypes - Parameter type array, assumes all parameters are 'float' if null
* @param {String} returnType - The return type, assumes 'float' if null
*
* @returns {GPU} returns itself
*
*/
addFunction(fn, paramTypes, returnType) {
this._runner.functionBuilder.addFunction(null, fn, paramTypes, returnType);
return this;
}
/**
*
* Adds additional native functions, that the kernel may call.
*
* @name addNativeFunction
* @function
* @memberOf GPU#
*
* @param {String} name - native function name, used for reverse lookup
* @param {String} nativeFunction - the native function implementation, as it would be defined in it's entirety
*
* @returns {GPU} returns itself
*
*/
addNativeFunction(name, nativeFunction) {
this._runner.functionBuilder.addNativeFunction(name, nativeFunction);
return this;
}
/**
*
* Return the current mode in which gpu.js is executing.
* @name getMode
* @function
* @memberOf GPU#
*
* @returns {String} The current mode, "cpu", "webgl", etc.
*
*/
getMode() {
return this._runner.getMode();
}
/**
*
* Return TRUE, if browser supports WebGl AND Canvas
*
* @name get isWebGlSupported
* @function
* @memberOf GPU#
*
* Note: This function can also be called directly `GPU.isWebGlSupported()`
*
* @returns {Boolean} TRUE if browser supports webGl
*
*/
isWebGlSupported() {
return utils.isWebGlSupported();
}
/**
*
* Return the canvas object bound to this gpu instance.
*
* @name getCanvas
* @function
* @memberOf GPU#
*
* @returns {Object} Canvas object if present
*
*/
getCanvas() {
return this._canvas;
}
/**
*
* Return the webGl object bound to this gpu instance.
*
* @name getWebGl
* @function
* @memberOf GPU#
*
* @returns {Object} WebGl object if present
*
*/
getWebGl() {
return this._webGl;
}
};
// This ensure static methods are "inherited"
// See: https://stackoverflow.com/questions/5441508/how-to-inherit-static-methods-from-base-class-in-javascript
Object.assign(GPU, GPUCore);
module.exports = GPU;