Skip to content

Commit

Permalink
Merge pull request #328 from gpujs/243-cpu-input
Browse files Browse the repository at this point in the history
Fix #243 cpu input
  • Loading branch information
robertleeplummerjr authored Jun 29, 2018
2 parents 81ac3f2 + 2d57f2a commit c3d148a
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 87 deletions.
4 changes: 2 additions & 2 deletions bin/gpu-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 1.4.3
* @date Fri Jun 22 2018 10:06:04 GMT-0400 (EDT)
* @version 1.4.4
* @date Thu Jun 28 2018 22:26:24 GMT-0400 (EDT)
*
* @license MIT
* The MIT License
Expand Down
4 changes: 2 additions & 2 deletions bin/gpu-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 107 additions & 23 deletions bin/gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*
* GPU Accelerated JavaScript
*
* @version 1.4.3
* @date Fri Jun 22 2018 10:06:04 GMT-0400 (EDT)
* @version 1.4.4
* @date Thu Jun 28 2018 22:26:24 GMT-0400 (EDT)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -550,30 +550,62 @@ module.exports = function (_BaseFunctionNode) {
if (mNode.object.type === 'Identifier') {
this.astGeneric(mNode.object, retArr);
retArr.push('[');
if (this.paramTypes[this.paramNames.indexOf(mNode.object.name)] === 'Input') {
var indexArray = ['(this.output.z) * ('];
this.astGeneric(mNode.property, indexArray);
indexArray.push('+ this.output.x)');
debugger;
retArr.push.apply(retArr, indexArray);
if (this.isInput(mNode.object.name)) {
this.astGeneric(mNode.property, retArr);
} else {
this.astGeneric(mNode.property, retArr);
}
retArr.push(']');
} else {
this.astGeneric(mNode.object, retArr);
var last = retArr.pop();
if (this.paramTypes[this.paramNames.indexOf(mNode.object.object.name)] === 'Input') {
var _indexArray = [' * ('];
this.astGeneric(mNode.property, _indexArray);
_indexArray.push(' + this.output.y)');
retArr.push.apply(retArr, _indexArray);
debugger;
if (mNode.object.object) {
if (mNode.object.object.object && this.isInput(mNode.object.object.object.name)) {
this.pushState('input-index-z');
this.astGeneric(mNode.object, retArr);
var last = retArr.pop();
retArr.push(' + ');
this.popState('input-index-z');
this.pushState('input-index');
this.astGeneric(mNode.property, retArr);
this.popState('input-index');
retArr.push(last);
} else if (this.isInput(mNode.object.object.name)) {
if (!this.isState('input-index-z')) {
this.pushState('input-index-y');
}
this.astGeneric(mNode.object, retArr);
var _last = retArr.pop();
retArr.push(' + ');
if (!this.isState('input-index-z')) {
this.popState('input-index-y');
}

var isInputIndexZ = this.isState('input-index-z');
if (isInputIndexZ) {
this.pushState('input-index-y');
} else {
this.pushState('input-index');
}
this.astGeneric(mNode.property, retArr);
if (isInputIndexZ) {
this.popState('input-index-y');
} else {
this.popState('input-index');
}
retArr.push(_last);
} else {
this.astGeneric(mNode.object, retArr);
var _last2 = retArr.pop();
retArr.push('][');
this.astGeneric(mNode.property, retArr);
retArr.push(_last2);
}
} else {
this.astGeneric(mNode.object, retArr);
var _last3 = retArr.pop();
retArr.push('][');
this.astGeneric(mNode.property, retArr);
retArr.push(_last3);
}
retArr.push(last);
}
} else {
var unrolled = this.astMemberExpressionUnroll(mNode);
Expand All @@ -585,7 +617,6 @@ module.exports = function (_BaseFunctionNode) {
unrolled = '_' + unrolled;
}

console.log(unrolled);
switch (unrolled) {
case '_this.output.x':
retArr.push(this.output[0]);
Expand All @@ -596,6 +627,31 @@ module.exports = function (_BaseFunctionNode) {
case '_this.output.z':
retArr.push(this.output[2]);
break;
case '_this.thread.x':
if (this.isState('input-index-y')) {
retArr.push('(_this.thread.x * _this.threadDim[1])');
} else if (this.isState('input-index-z')) {
retArr.push('(_this.thread.x * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
case '_this.thread.y':
if (this.isState('input-index-y')) {
retArr.push('(_this.thread.y * _this.threadDim[0])');
} else if (this.isState('input-index-z')) {
retArr.push('(_this.thread.y * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
case '_this.thread.z':
if (this.isState('input-index-z')) {
retArr.push('(_this.thread.z * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
default:
retArr.push(unrolled);
}
Expand Down Expand Up @@ -713,7 +769,7 @@ function removeNoise(str) {
}

module.exports = function (cpuKernel, name) {
return '() => {\n ' + kernelRunShortcut.toString() + ';\n const utils = {\n allPropertiesOf: ' + removeNoise(utils.allPropertiesOf.toString()) + ',\n clone: ' + removeNoise(utils.clone.toString()) + '\n };\n const Utils = utils;\n class ' + (name || 'Kernel') + ' {\n constructor() { \n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON.stringify(cpuKernel.paramNames) + ';\n this.paramTypes = ' + JSON.stringify(cpuKernel.paramTypes) + ';\n this.texSize = ' + JSON.stringify(cpuKernel.texSize) + ';\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n this._kernelString = `' + cpuKernel._kernelString + '`;\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n\t\t this.run = function() {\n this.run = null;\n this.build();\n return this.run.apply(this, arguments);\n }.bind(this);\n this.thread = {\n x: 0,\n y: 0,\n z: 0\n };\n }\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise(cpuKernel.build.toString()) + '\n ' + removeFnNoise(cpuKernel.setupParams.toString()) + '\n run () { ' + cpuKernel.kernelString + ' }\n getKernelString() { return this._kernelString; }\n };\n return kernelRunShortcut(new Kernel());\n };';
return '() => {\n ' + kernelRunShortcut.toString() + ';\n const utils = {\n allPropertiesOf: ' + removeNoise(utils.allPropertiesOf.toString()) + ',\n clone: ' + removeNoise(utils.clone.toString()) + '\n };\n const Utils = utils;\n class ' + (name || 'Kernel') + ' {\n constructor() { \n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON.stringify(cpuKernel.paramNames) + ';\n this.paramTypes = ' + JSON.stringify(cpuKernel.paramTypes) + ';\n this.texSize = ' + JSON.stringify(cpuKernel.texSize) + ';\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n this._kernelString = `' + cpuKernel._kernelString + '`;\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n\t\t this.run = function() {\n this.run = null;\n this.build();\n return this.run.apply(this, arguments);\n }.bind(this);\n this.thread = {\n x: 0,\n y: 0,\n z: 0\n };\n }\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise(cpuKernel.build.toString()) + '\n ' + removeFnNoise(cpuKernel.setupParams.toString()) + '\n run () { ' + cpuKernel.kernelString + ' }\n getKernelString() { return this._kernelString; }\n ' + removeFnNoise(cpuKernel.validateOptions.toString()) + '\n };\n return kernelRunShortcut(new Kernel());\n };';
};
},{"../../core/utils":32,"../kernel-run-shortcut":9}],4:[function(require,module,exports){
'use strict';
Expand Down Expand Up @@ -785,6 +841,7 @@ module.exports = function (_KernelBase) {
key: 'build',
value: function build() {
this.setupParams(arguments);
this.validateOptions();
var threadDim = this.threadDim = utils.clone(this.output);

while (threadDim.length < 3) {
Expand Down Expand Up @@ -1123,7 +1180,6 @@ module.exports = function () {
}, {
key: 'traceFunctionCalls',
value: function traceFunctionCalls(functionName, retList, parent) {
debugger;
functionName = functionName || 'kernel';
retList = retList || [];

Expand Down Expand Up @@ -1215,7 +1271,6 @@ module.exports = function () {
}, {
key: 'getPrototypesFromFunctionNames',
value: function getPrototypesFromFunctionNames(functionList, opt) {
debugger;
var ret = [];
for (var i = 0; i < functionList.length; ++i) {
var functionName = functionList[i];
Expand Down Expand Up @@ -1281,6 +1336,7 @@ module.exports = function () {
this.constants = null;
this.output = null;
this.declarations = {};
this.states = [];

if (options) {
if (options.hasOwnProperty('debug')) {
Expand Down Expand Up @@ -1362,16 +1418,39 @@ module.exports = function () {
if (!this.constants) return false;
return this.constants.hasOwnProperty(paramName);
}
}, {
key: 'isInput',
value: function isInput(paramName) {
return this.paramTypes[this.paramNames.indexOf(paramName)] === 'Input';
}
}, {
key: 'setAddFunction',
value: function setAddFunction(fn) {
this.addFunction = fn;
return this;
}


}, {
key: 'pushState',
value: function pushState(state) {
this.states.push(state);
}
}, {
key: 'popState',
value: function popState(state) {
if (this.state !== state) {
throw new Error('Cannot popState ' + state + ' when in ' + this.state);
}
this.states.pop();
}
}, {
key: 'isState',
value: function isState(state) {
return this.state === state;
}
}, {
key: 'getJsFunction',


value: function getJsFunction() {
if (this.jsFunction) {
return this.jsFunction;
Expand Down Expand Up @@ -1724,6 +1803,11 @@ module.exports = function () {
value: function astArrayExpression(ast, retArr) {
return retArr;
}
}, {
key: 'state',
get: function get() {
return this.states[this.states.length - 1];
}
}]);

return BaseFunctionNode;
Expand Down
18 changes: 9 additions & 9 deletions bin/gpu.min.js

Large diffs are not rendered by default.

88 changes: 72 additions & 16 deletions dist/backend/cpu/function-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,30 +845,62 @@ module.exports = function (_BaseFunctionNode) {
if (mNode.object.type === 'Identifier') {
this.astGeneric(mNode.object, retArr);
retArr.push('[');
if (this.paramTypes[this.paramNames.indexOf(mNode.object.name)] === 'Input') {
var indexArray = ['(this.output.z) * ('];
this.astGeneric(mNode.property, indexArray);
indexArray.push('+ this.output.x)');
debugger;
retArr.push.apply(retArr, indexArray);
if (this.isInput(mNode.object.name)) {
this.astGeneric(mNode.property, retArr);
} else {
this.astGeneric(mNode.property, retArr);
}
retArr.push(']');
} else {
this.astGeneric(mNode.object, retArr);
var last = retArr.pop();
if (this.paramTypes[this.paramNames.indexOf(mNode.object.object.name)] === 'Input') {
var _indexArray = [' * ('];
this.astGeneric(mNode.property, _indexArray);
_indexArray.push(' + this.output.y)');
retArr.push.apply(retArr, _indexArray);
debugger;
if (mNode.object.object) {
if (mNode.object.object.object && this.isInput(mNode.object.object.object.name)) {
this.pushState('input-index-z');
this.astGeneric(mNode.object, retArr);
var last = retArr.pop();
retArr.push(' + ');
this.popState('input-index-z');
this.pushState('input-index');
this.astGeneric(mNode.property, retArr);
this.popState('input-index');
retArr.push(last);
} else if (this.isInput(mNode.object.object.name)) {
if (!this.isState('input-index-z')) {
this.pushState('input-index-y');
}
this.astGeneric(mNode.object, retArr);
var _last = retArr.pop();
retArr.push(' + ');
if (!this.isState('input-index-z')) {
this.popState('input-index-y');
}

var isInputIndexZ = this.isState('input-index-z');
if (isInputIndexZ) {
this.pushState('input-index-y');
} else {
this.pushState('input-index');
}
this.astGeneric(mNode.property, retArr);
if (isInputIndexZ) {
this.popState('input-index-y');
} else {
this.popState('input-index');
}
retArr.push(_last);
} else {
this.astGeneric(mNode.object, retArr);
var _last2 = retArr.pop();
retArr.push('][');
this.astGeneric(mNode.property, retArr);
retArr.push(_last2);
}
} else {
this.astGeneric(mNode.object, retArr);
var _last3 = retArr.pop();
retArr.push('][');
this.astGeneric(mNode.property, retArr);
retArr.push(_last3);
}
retArr.push(last);
}
} else {
var unrolled = this.astMemberExpressionUnroll(mNode);
Expand All @@ -881,7 +913,6 @@ module.exports = function (_BaseFunctionNode) {
unrolled = '_' + unrolled;
}

console.log(unrolled);
switch (unrolled) {
case '_this.output.x':
retArr.push(this.output[0]);
Expand All @@ -892,6 +923,31 @@ module.exports = function (_BaseFunctionNode) {
case '_this.output.z':
retArr.push(this.output[2]);
break;
case '_this.thread.x':
if (this.isState('input-index-y')) {
retArr.push('(_this.thread.x * _this.threadDim[1])');
} else if (this.isState('input-index-z')) {
retArr.push('(_this.thread.x * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
case '_this.thread.y':
if (this.isState('input-index-y')) {
retArr.push('(_this.thread.y * _this.threadDim[0])');
} else if (this.isState('input-index-z')) {
retArr.push('(_this.thread.y * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
case '_this.thread.z':
if (this.isState('input-index-z')) {
retArr.push('(_this.thread.z * _this.threadDim[0] * _this.threadDim[1])');
} else {
retArr.push(unrolled);
}
break;
default:
retArr.push(unrolled);
}
Expand Down
2 changes: 1 addition & 1 deletion dist/backend/cpu/kernel-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ function removeNoise(str) {
}

module.exports = function (cpuKernel, name) {
return '() => {\n ' + kernelRunShortcut.toString() + ';\n const utils = {\n allPropertiesOf: ' + removeNoise(utils.allPropertiesOf.toString()) + ',\n clone: ' + removeNoise(utils.clone.toString()) + '\n };\n const Utils = utils;\n class ' + (name || 'Kernel') + ' {\n constructor() { \n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON.stringify(cpuKernel.paramNames) + ';\n this.paramTypes = ' + JSON.stringify(cpuKernel.paramTypes) + ';\n this.texSize = ' + JSON.stringify(cpuKernel.texSize) + ';\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n this._kernelString = `' + cpuKernel._kernelString + '`;\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n\t\t this.run = function() {\n this.run = null;\n this.build();\n return this.run.apply(this, arguments);\n }.bind(this);\n this.thread = {\n x: 0,\n y: 0,\n z: 0\n };\n }\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise(cpuKernel.build.toString()) + '\n ' + removeFnNoise(cpuKernel.setupParams.toString()) + '\n run () { ' + cpuKernel.kernelString + ' }\n getKernelString() { return this._kernelString; }\n };\n return kernelRunShortcut(new Kernel());\n };';
return '() => {\n ' + kernelRunShortcut.toString() + ';\n const utils = {\n allPropertiesOf: ' + removeNoise(utils.allPropertiesOf.toString()) + ',\n clone: ' + removeNoise(utils.clone.toString()) + '\n };\n const Utils = utils;\n class ' + (name || 'Kernel') + ' {\n constructor() { \n this.argumentsLength = 0;\n this._canvas = null;\n this._webGl = null;\n this.built = false;\n this.program = null;\n this.paramNames = ' + JSON.stringify(cpuKernel.paramNames) + ';\n this.paramTypes = ' + JSON.stringify(cpuKernel.paramTypes) + ';\n this.texSize = ' + JSON.stringify(cpuKernel.texSize) + ';\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n this._kernelString = `' + cpuKernel._kernelString + '`;\n this.output = ' + JSON.stringify(cpuKernel.output) + ';\n\t\t this.run = function() {\n this.run = null;\n this.build();\n return this.run.apply(this, arguments);\n }.bind(this);\n this.thread = {\n x: 0,\n y: 0,\n z: 0\n };\n }\n setCanvas(canvas) { this._canvas = canvas; return this; }\n setWebGl(webGl) { this._webGl = webGl; return this; }\n ' + removeFnNoise(cpuKernel.build.toString()) + '\n ' + removeFnNoise(cpuKernel.setupParams.toString()) + '\n run () { ' + cpuKernel.kernelString + ' }\n getKernelString() { return this._kernelString; }\n ' + removeFnNoise(cpuKernel.validateOptions.toString()) + '\n };\n return kernelRunShortcut(new Kernel());\n };';
};
1 change: 1 addition & 0 deletions dist/backend/cpu/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module.exports = function (_KernelBase) {
key: 'build',
value: function build() {
this.setupParams(arguments);
this.validateOptions();
var threadDim = this.threadDim = utils.clone(this.output);

while (threadDim.length < 3) {
Expand Down
2 changes: 0 additions & 2 deletions dist/backend/function-builder-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ module.exports = function () {
}, {
key: 'traceFunctionCalls',
value: function traceFunctionCalls(functionName, retList, parent) {
debugger;
functionName = functionName || 'kernel';
retList = retList || [];

Expand Down Expand Up @@ -296,7 +295,6 @@ module.exports = function () {
}, {
key: 'getPrototypesFromFunctionNames',
value: function getPrototypesFromFunctionNames(functionList, opt) {
debugger;
var ret = [];
for (var i = 0; i < functionList.length; ++i) {
var functionName = functionList[i];
Expand Down
Loading

0 comments on commit c3d148a

Please sign in to comment.