forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-builder-base.js
More file actions
321 lines (299 loc) · 9.46 KB
/
function-builder-base.js
File metadata and controls
321 lines (299 loc) · 9.46 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
'use strict';
module.exports = class FunctionBuilderBase {
/**
* @constructor FunctionBuilderBase
*
* @desc This handles all the raw state, converted state, etc. of a single function.
* [INTERNAL] A collection of functionNodes.
*
* @prop {Object} nodeMap - Object map, where nodeMap[function] = new FunctionNode;
* @prop {Object} gpu - The current gpu instance bound to this builder
* @prop {Object} rootKernel - The root kernel object, contains the paramNames, dimensions etc.
*
*/
constructor(gpu) {
this.nodeMap = {};
this.nativeFunctions = {};
this.gpu = gpu;
this.rootKernel = null;
this.Node = null;
}
addNativeFunction(functionName, glslFunctionString) {
this.nativeFunctions[functionName] = glslFunctionString;
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name addFunction
*
* @desc Instantiates a FunctionNode, and add it to the nodeMap
*
* @param {String} functionName - Function name to assume, if its null, it attempts to extract from the function
* @param {Function} jsFunction - JS Function to do conversion
* @param {Object} [options]
* @param {String[]|Object} [paramTypes] - Parameter type array, assumes all parameters are 'float' if falsey
* @param {String} [returnType] - The return type, assumes 'float' if falsey
*
*/
addFunction(functionName, jsFunction, options, paramTypes, returnType) {
this.addFunctionNode(
new this.Node(functionName, jsFunction, options, paramTypes, returnType)
.setAddFunction(this.addFunction.bind(this))
);
}
addFunctions(functions, options) {
if (functions) {
if (Array.isArray(functions)) {
for (let i = 0; i < functions.length; i++) {
this.addFunction(null, functions[i], options);
}
} else {
for (let p in functions) {
this.addFunction(p, functions[p], options);
}
}
}
}
addNativeFunctions(nativeFunctions) {
for (let functionName in nativeFunctions) {
if (!nativeFunctions.hasOwnProperty(functionName)) continue;
this.addNativeFunction(functionName, nativeFunctions[functionName]);
}
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name addFunctionNode
*
* @desc Add the function node directly
*
* @param {functionNode} inNode - functionNode to add
*
*/
addFunctionNode(inNode) {
this.nodeMap[inNode.functionName] = inNode;
if (inNode.isRootKernel) {
this.rootKernel = inNode;
}
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name traceFunctionCalls
*
* @desc Trace all the depending functions being called, from a single function
*
* This allow for 'unneeded' functions to be automatically optimized out.
* Note that the 0-index, is the starting function trace.
*
* @param {String} functionName - Function name to trace from, default to 'kernel'
* @param {String[]} retList - Returning list of function names that is traced. Including itself.
* @param {Object} [parent] - Parent node
*
* @returns {String[]} Returning list of function names that is traced. Including itself.
*/
traceFunctionCalls(functionName, retList, parent) {
functionName = functionName || 'kernel';
retList = retList || [];
const fNode = this.nodeMap[functionName];
if (fNode) {
// Check if function already exists
const functionIndex = retList.indexOf(functionName);
if (functionIndex === -1) {
retList.push(functionName);
if (parent) {
fNode.parent = parent;
}
fNode.getFunctionString(); //ensure JS trace is done
for (let i = 0; i < fNode.calledFunctions.length; ++i) {
this.traceFunctionCalls(fNode.calledFunctions[i], retList, fNode);
}
} else {
/**
* https://github.com/gpujs/gpu.js/issues/207
* if dependent function is already in the list, because a function depends on it, and because it has
* already been traced, we know that we must move the dependent function to the end of the the retList.
* */
const dependantFunctionName = retList.splice(functionIndex, 1)[0];
retList.push(dependantFunctionName);
}
}
if (this.nativeFunctions[functionName]) {
if (retList.indexOf(functionName) >= 0) {
// Does nothing if already traced
} else {
retList.push(functionName);
}
}
return retList;
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name addKernel
*
* @desc Add a new kernel to this instance
*
* @param {String} fnString - Kernel function as a String
* @param {Object} options - Settings object to set constants, debug mode, etc.
* @param {Array} paramNames - Parameters of the kernel
* @param {Array} paramTypes - Types of the parameters
*
*
* @returns {Object} The inserted kernel as a Kernel Node
*
*/
addKernel(fnString, options, paramNames, paramTypes) {
const kernelNode = new this.Node('kernel', fnString, options, paramTypes);
kernelNode.setAddFunction(this.addFunction.bind(this));
kernelNode.paramNames = paramNames;
kernelNode.paramTypes = paramTypes;
kernelNode.isRootKernel = true;
this.addFunctionNode(kernelNode);
return kernelNode;
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name addSubKernel
*
* @desc Add a new sub-kernel to the current kernel instance
*
* @param {Function} jsFunction - Sub-kernel function (JavaScript)
* @param {Object} options - Settings object to set constants, debug mode, etc.
* @param {Array} paramNames - Parameters of the sub-kernel
* @param {Array} returnType - Return type of the subKernel
*
* @returns {Object} The inserted sub-kernel as a Kernel Node
*
*/
addSubKernel(jsFunction, options, paramTypes, returnType) {
const kernelNode = new this.Node(null, jsFunction, options, paramTypes, returnType);
kernelNode.setAddFunction(this.addFunction.bind(this));
kernelNode.isSubKernel = true;
this.addFunctionNode(kernelNode);
return kernelNode;
}
/**
* @memberOf CPUFunctionBuilder#
* @name getPrototypeString
* @function
*
* @desc Return the string for a function
*
* @param {String} functionName - Function name to trace from. If null, it returns the WHOLE builder stack
*
* @returns {String} The full string, of all the various functions. Trace optimized if functionName given
*
*/
getPrototypeString(functionName) {
return this.getPrototypes(functionName).join('\n');
}
/**
* @memberOf CPUFunctionBuilder#
* @name getPrototypeString
* @function
*
* @desc Return the string for a function
*
* @param {String} [functionName] - Function name to trace from. If null, it returns the WHOLE builder stack
*
* @returns {Array} The full string, of all the various functions. Trace optimized if functionName given
*
*/
getPrototypes(functionName) {
this.rootKernel.generate();
if (functionName) {
return this.getPrototypesFromFunctionNames(this.traceFunctionCalls(functionName, []).reverse());
}
return this.getPrototypesFromFunctionNames(Object.keys(this.nodeMap));
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name getStringFromFunctionNames
*
* @desc Get string from function names
*
* @param {String[]} functionList - List of function to build string
*
* @returns {String} The string, of all the various functions. Trace optimized if functionName given
*
*/
getStringFromFunctionNames(functionList) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const node = this.nodeMap[functionList[i]];
if (node) {
ret.push(this.nodeMap[functionList[i]].getFunctionString());
}
}
return ret.join('\n');
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name getPrototypeStringFromFunctionNames
*
* @desc Return string of all functions converted
*
* @param {String[]} functionList - List of function names to build the string.
* @param {Object} [opt - Settings object passed to functionNode. See functionNode for more details.
*
* @returns {Array} Prototypes of all functions converted
*
*/
getPrototypesFromFunctionNames(functionList, opt) {
const ret = [];
for (let i = 0; i < functionList.length; ++i) {
const functionName = functionList[i];
const node = this.nodeMap[functionName];
if (node) {
ret.push(node.getFunctionPrototypeString(opt));
} else if (this.nativeFunctions[functionName]) {
ret.push(this.nativeFunctions[functionName]);
}
}
return ret;
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name getPrototypeStringFromFunctionNames
*
* @desc Return string of all functions converted
*
* @param {String[]} functionList - List of function names to build the string.
* @param {Object} opt - Settings object passed to functionNode. See functionNode for more details.
*
* @returns {String} Prototype string of all functions converted
*
*/
getPrototypeStringFromFunctionNames(functionList, opt) {
return this.getPrototypesFromFunctionNames(functionList, opt).toString();
}
/**
* @memberOf FunctionBuilderBase#
* @function
* @name getString
*
* Get string for a particular function name
*
* @param {String} functionName - Function name to trace from. If null, it returns the WHOLE builder stack
*
* @returns {String} The string, of all the various functions. Trace optimized if functionName given
*
*/
getString(functionName, opt) {
if (opt === undefined) {
opt = {};
}
if (functionName) {
return this.getStringFromFunctionNames(this.traceFunctionCalls(functionName, [], opt).reverse(), opt);
}
return this.getStringFromFunctionNames(Object.keys(this.nodeMap), opt);
}
polyfillStandardFunctions() {
throw new Error('polyfillStandardFunctions not defined on base function builder');
}
};