use this.constants to achieve constant lookup

This commit is contained in:
Robert Plummer 2017-08-09 12:56:45 -04:00
parent 1f3cc9d3a8
commit ec7c8a3e4e
6 changed files with 7 additions and 12 deletions

View File

@ -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

View File

@ -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';

View File

@ -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;

View File

@ -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('')

View File

@ -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;
}

View File

@ -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)));