From ec7c8a3e4e993450e7cd08fefeeb428f6ba3770a Mon Sep 17 00:00:00 2001 From: Robert Plummer Date: Wed, 9 Aug 2017 12:56:45 -0400 Subject: [PATCH] use `this.constants` to achieve constant lookup --- bin/gpu-core.js | 2 +- bin/gpu.js | 9 +++------ src/backend/cpu/function-builder.js | 2 +- src/backend/cpu/kernel.js | 2 -- test/src/features/for-loop.js | 2 +- test/src/issues/147-missing-constant.js | 2 +- 6 files changed, 7 insertions(+), 12 deletions(-) diff --git a/bin/gpu-core.js b/bin/gpu-core.js index b77dc89c..bdd2354d 100644 --- a/bin/gpu-core.js +++ b/bin/gpu-core.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 0.0.0 - * @date Fri Jul 28 2017 11:56:49 GMT-0400 (EDT) + * @date Tue Aug 08 2017 09:33:07 GMT-0400 (EDT) * * @license MIT * The MIT License diff --git a/bin/gpu.js b/bin/gpu.js index d90c8b44..34aabe6f 100644 --- a/bin/gpu.js +++ b/bin/gpu.js @@ -5,7 +5,7 @@ * GPU Accelerated JavaScript * * @version 0.0.0 - * @date Fri Jul 28 2017 11:56:49 GMT-0400 (EDT) + * @date Tue Aug 08 2017 09:33:07 GMT-0400 (EDT) * * @license MIT * The MIT License @@ -52,7 +52,7 @@ module.exports = function (_FunctionBuilderBase) { if (node.isSubKernel) { ret += 'var ' + node.functionName + ' = ' + node.jsFunctionString.replace('return', 'return ' + node.functionName + 'Result[this.thread.z][this.thread.y][this.thread.x] =') + '.bind(this);\n'; } else { - ret += 'var ' + node.functionName + ' = ' + node.jsFunctionString + ';\n'; + ret += 'var ' + node.functionName + ' = ' + node.jsFunctionString + '.bind(this);\n'; } } return ret; @@ -266,7 +266,6 @@ module.exports = function (_KernelBase) { if (this._kernelString !== null) return this._kernelString; - var paramNames = this.paramNames; var builder = this.functionBuilder; var threadDim = this.threadDim || (this.threadDim = utils.clone(this.dimensions)); @@ -294,9 +293,7 @@ module.exports = function (_KernelBase) { } } - return this._kernelString = '\n ' + (this.constants ? Object.keys(this.constants).map(function (key) { - return 'var ' + key + ' = ' + _this2.constants[key]; - }).join(';\n') + ';\n' : '') + '\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) { + return this._kernelString = '\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) { return ' var ' + name + ' = null;\n'; }).join('')) + '\n ' + builder.getPrototypeString() + '\n var fn = function fn(' + this.paramNames.join(', ') + ') { ' + this._fnBody + ' }.bind(this);\n return function (' + this.paramNames.join(', ') + ') {\n var ret = new Array(' + threadDim[2] + ');\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) { return ' ' + name + ' = new Array(' + threadDim[2] + ');\n'; diff --git a/src/backend/cpu/function-builder.js b/src/backend/cpu/function-builder.js index 8e996ddf..24e34e0c 100644 --- a/src/backend/cpu/function-builder.js +++ b/src/backend/cpu/function-builder.js @@ -39,7 +39,7 @@ module.exports = class CPUFunctionBuilder extends FunctionBuilderBase { if (node.isSubKernel) { ret += `var ${ node.functionName } = ` + node.jsFunctionString.replace('return', `return ${ node.functionName }Result[this.thread.z][this.thread.y][this.thread.x] =`) + '.bind(this);\n'; } else { - ret += `var ${ node.functionName } = ${ node.jsFunctionString };\n`; + ret += `var ${ node.functionName } = ${ node.jsFunctionString }.bind(this);\n`; } } return ret; diff --git a/src/backend/cpu/kernel.js b/src/backend/cpu/kernel.js index f0fe7be9..ab23a28a 100644 --- a/src/backend/cpu/kernel.js +++ b/src/backend/cpu/kernel.js @@ -174,7 +174,6 @@ module.exports = class CPUKernel extends KernelBase { getKernelString() { if (this._kernelString !== null) return this._kernelString; - const paramNames = this.paramNames; const builder = this.functionBuilder; // Thread dim fix (to make compilable) @@ -205,7 +204,6 @@ module.exports = class CPUKernel extends KernelBase { } return this._kernelString = ` - ${ this.constants ? Object.keys(this.constants).map((key) => { return `var ${ key } = ${ this.constants[key] }`; }).join(';\n') + ';\n' : '' } ${ this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map((name) => ` var ${ name } = null;\n`).join('') diff --git a/test/src/features/for-loop.js b/test/src/features/for-loop.js index 2a865b79..0eb417aa 100644 --- a/test/src/features/for-loop.js +++ b/test/src/features/for-loop.js @@ -96,7 +96,7 @@ function for_constant_loop_test(mode) { var gpu = new GPU({ mode: mode }); var f = gpu.createKernel(function(a, b) { var x = 0; - for(var i = 0; i < max; i++) { + for(var i = 0; i < this.constants.max; i++) { x = x + 1; } diff --git a/test/src/issues/147-missing-constant.js b/test/src/issues/147-missing-constant.js index e786795d..5f00cfae 100644 --- a/test/src/issues/147-missing-constant.js +++ b/test/src/issues/147-missing-constant.js @@ -9,7 +9,7 @@ function typedArrayTest(mode) { .setConstants({ pi: Math.PI }); gpu.addFunction(function getPi() { - return pi; + return this.constants.pi; }); console.log(QUnit.assert.equal((kernel()[0]).toFixed(7), Math.PI.toFixed(7)));