Skip to content

Commit

Permalink
Minor fix for 3d kernel maps and test
Browse files Browse the repository at this point in the history
  • Loading branch information
robertleeplummerjr committed Jul 11, 2018
1 parent 01a18ac commit a8d5d02
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 37 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.12
* @date Wed Jul 11 2018 15:20:52 GMT-0400 (EDT)
* @version 1.4.13
* @date Wed Jul 11 2018 18:51:55 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.

12 changes: 7 additions & 5 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.12
* @date Wed Jul 11 2018 15:20:52 GMT-0400 (EDT)
* @version 1.4.13
* @date Wed Jul 11 2018 18:51:56 GMT-0400 (EDT)
*
* @license MIT
* The MIT License
Expand Down Expand Up @@ -1024,11 +1024,13 @@ module.exports = function (_KernelBase) {
return ' ' + name + 'Z[this.thread.z][this.thread.y] = new Array(' + threadDim[0] + ');\n';
}).join('')) + '\n for (this.thread.x = 0; this.thread.x < ' + threadDim[0] + '; this.thread.x++) {\n var kernelResult;\n ' + kernel + '\n ret[this.thread.z][this.thread.y][this.thread.x] = kernelResult;\n' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + 'Z[this.thread.z][this.thread.y][this.thread.x] = ' + name + ';\n';
}).join('')) + '\n }\n }\n }\n \n if (this.graphical) {\n this._imageData.data.set(this._colorData);\n this._canvasCtx.putImageData(this._imageData, 0, 0);\n return;\n }\n \n if (this.output.length === 1) {\n ret = ret[0][0];\n' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n }\n }\n }\n \n if (this.graphical) {\n this._imageData.data.set(this._colorData);\n this._canvasCtx.putImageData(this._imageData, 0, 0);\n return;\n }\n \n if (this.output.length === 1) {\n ret = ret[0][0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z[0][0];\n';
}).join('')) + '\n \n } else if (this.output.length === 2) {\n ret = ret[0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n \n } else if (this.output.length === 2) {\n ret = ret[0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z[0];\n';
}).join('')) + '\n }\n \n ' + (this.subKernelOutputVariableNames === null ? 'return ret;\n' : this.subKernels !== null ? 'var result = [\n ' + this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n } else {\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z;\n';
}).join('')) + '\n }\n \n ' + (this.subKernelOutputVariableNames === null ? 'return ret;\n' : this.subKernels !== null ? 'var result = [\n ' + this.subKernelOutputVariableNames.map(function (name) {
return '' + name;
}).join(',\n') + '\n ];\n result.result = ret;\n return result;\n' : 'return {\n result: ret,\n ' + Object.keys(this.subKernelProperties).map(function (name, i) {
return name + ': ' + _this2.subKernelOutputVariableNames[i];
Expand Down
18 changes: 9 additions & 9 deletions bin/gpu.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions dist/backend/cpu/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@ module.exports = function (_KernelBase) {
return ' ' + name + 'Z[this.thread.z][this.thread.y] = new Array(' + threadDim[0] + ');\n';
}).join('')) + '\n for (this.thread.x = 0; this.thread.x < ' + threadDim[0] + '; this.thread.x++) {\n var kernelResult;\n ' + kernel + '\n ret[this.thread.z][this.thread.y][this.thread.x] = kernelResult;\n' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + 'Z[this.thread.z][this.thread.y][this.thread.x] = ' + name + ';\n';
}).join('')) + '\n }\n }\n }\n \n if (this.graphical) {\n this._imageData.data.set(this._colorData);\n this._canvasCtx.putImageData(this._imageData, 0, 0);\n return;\n }\n \n if (this.output.length === 1) {\n ret = ret[0][0];\n' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n }\n }\n }\n \n if (this.graphical) {\n this._imageData.data.set(this._colorData);\n this._canvasCtx.putImageData(this._imageData, 0, 0);\n return;\n }\n \n if (this.output.length === 1) {\n ret = ret[0][0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z[0][0];\n';
}).join('')) + '\n \n } else if (this.output.length === 2) {\n ret = ret[0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n \n } else if (this.output.length === 2) {\n ret = ret[0];\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z[0];\n';
}).join('')) + '\n }\n \n ' + (this.subKernelOutputVariableNames === null ? 'return ret;\n' : this.subKernels !== null ? 'var result = [\n ' + this.subKernelOutputVariableNames.map(function (name) {
}).join('')) + '\n } else {\n ' + (this.subKernelOutputVariableNames === null ? '' : this.subKernelOutputVariableNames.map(function (name) {
return ' ' + name + ' = ' + name + 'Z;\n';
}).join('')) + '\n }\n \n ' + (this.subKernelOutputVariableNames === null ? 'return ret;\n' : this.subKernels !== null ? 'var result = [\n ' + this.subKernelOutputVariableNames.map(function (name) {
return '' + name;
}).join(',\n') + '\n ];\n result.result = ret;\n return result;\n' : 'return {\n result: ret,\n ' + Object.keys(this.subKernelProperties).map(function (name, i) {
return name + ': ' + _this2.subKernelOutputVariableNames[i];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpu.js",
"version": "1.4.12",
"version": "1.4.13",
"description": "GPU Accelerated JavaScript",
"main": "./dist/index.js",
"directories": {
Expand Down
26 changes: 14 additions & 12 deletions src/backend/cpu/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,20 +263,22 @@ ${ this.subKernelOutputVariableNames === null
if (this.output.length === 1) {
ret = ret[0][0];
${ this.subKernelOutputVariableNames === null
? ''
: this.subKernelOutputVariableNames.map((name) => ` ${ name } = ${ name }Z[0][0];\n`).join('')
}
${ this.subKernelOutputVariableNames === null
? ''
: this.subKernelOutputVariableNames.map((name) => ` ${ name } = ${ name }Z[0][0];\n`).join('') }
} else if (this.output.length === 2) {
ret = ret[0];
${ this.subKernelOutputVariableNames === null
? ''
: this.subKernelOutputVariableNames.map((name) => ` ${ name } = ${ name }Z[0];\n`).join('')
}
}
} else if (this.output.length === 2) {
ret = ret[0];
${ this.subKernelOutputVariableNames === null
? ''
: this.subKernelOutputVariableNames.map((name) => ` ${ name } = ${ name }Z[0];\n`).join('') }
} else {
${ this.subKernelOutputVariableNames === null
? ''
: this.subKernelOutputVariableNames.map((name) => ` ${ name } = ${ name }Z;\n`).join('') }
}
${ this.subKernelOutputVariableNames === null
${ this.subKernelOutputVariableNames === null
? 'return ret;\n'
: this.subKernels !== null
? `var result = [
Expand Down
111 changes: 108 additions & 3 deletions test/features/create-kernel-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@
QUnit.assert.deepEqual(divideResult, [8]);
});

QUnit.test('createKernelMap (cpu)', function() {
QUnit.test('createKernelMap object 1 dimension 1 length (cpu)', function() {
var canvas = document.createElement('canvas');
var gpu = new GPU({mode: 'cpu', canvas: canvas});
var superKernel = createPropertyKernels(gpu, [1]);
var kernel = createKernel(gpu, [1]);
var output = superKernel([2], [2], [0.5]);
var result = QUnit.extend([], output.result);
var addResult = QUnit.extend([], output.addResult);
var divideResult = QUnit.extend([], output.divideResult);
var addResult = QUnit.extend([], kernel(output.addResult));
var divideResult = QUnit.extend([], kernel(output.divideResult));
QUnit.assert.deepEqual(result, [8]);
QUnit.assert.deepEqual(addResult, [4]);
QUnit.assert.deepEqual(divideResult, [8]);
Expand Down Expand Up @@ -226,6 +227,20 @@
QUnit.assert.deepEqual(divideResult, [2, 2, 2, 2, 2]);
});

QUnit.test('createKernelMap object 1 dimension 5 length (cpu)', function() {
var canvas = document.createElement('canvas');
var gpu = new GPU({mode: 'cpu', canvas: canvas});
var superKernel = createPropertyKernels(gpu, [5]);
var kernel = createKernel(gpu, [5]);
var output = superKernel([1,2,3,4,5], [1,2,3,4,5], [1,2,3,4,5]);
var result = QUnit.extend([], output.result);
var addResult = QUnit.extend([], kernel(output.addResult));
var divideResult = QUnit.extend([], kernel(output.divideResult));
QUnit.assert.deepEqual(result, [2, 2, 2, 2, 2]);
QUnit.assert.deepEqual(addResult, [2, 4, 6, 8, 10]);
QUnit.assert.deepEqual(divideResult, [2, 2, 2, 2, 2]);
});

QUnit.test('createKernelMap array (auto)', function() {
var canvas = document.createElement('canvas');
var gpu = new GPU({mode: null, canvas: canvas});
Expand Down Expand Up @@ -294,4 +309,94 @@
QUnit.assert.deepEqual(addResult, [2, 4, 6, 8, 10]);
QUnit.assert.deepEqual(divideResult, [2, 2, 2, 2, 2]);
});

QUnit.test('createKernelMap 3d (auto)', function() {
var kernel = new GPU().createKernelMap({
target: function saveTarget(value) {
return value;
}
}, function(value) {
return saveTarget(value);
}).setOutput([3,3,3]);
var result = kernel(1);
QUnit.assert.equal(result.result.length, 3);
QUnit.assert.equal(result.result[0].length, 3);
QUnit.assert.equal(result.result[0][0].length, 3);

QUnit.assert.equal(result.target.length, 3);
QUnit.assert.equal(result.target[0].length, 3);
QUnit.assert.equal(result.target[0][0].length, 3);
});

QUnit.test('createKernelMap 3d (gpu)', function() {
var kernel = new GPU({ mode: 'gpu' }).createKernelMap({
target: function saveTarget(value) {
return value;
}
}, function(value) {
return saveTarget(value);
}).setOutput([3,3,3]);
var result = kernel(1);
QUnit.assert.equal(result.result.length, 3);
QUnit.assert.equal(result.result[0].length, 3);
QUnit.assert.equal(result.result[0][0].length, 3);

QUnit.assert.equal(result.target.length, 3);
QUnit.assert.equal(result.target[0].length, 3);
QUnit.assert.equal(result.target[0][0].length, 3);
});

QUnit.test('createKernelMap 3d (webgl)', function() {
var kernel = new GPU({ mode: 'webgl' }).createKernelMap({
target: function saveTarget(value) {
return value;
}
}, function(value) {
return saveTarget(value);
}).setOutput([3,3,3]);
var result = kernel(1);
QUnit.assert.equal(result.result.length, 3);
QUnit.assert.equal(result.result[0].length, 3);
QUnit.assert.equal(result.result[0][0].length, 3);

QUnit.assert.equal(result.target.length, 3);
QUnit.assert.equal(result.target[0].length, 3);
QUnit.assert.equal(result.target[0][0].length, 3);
});

QUnit.test('createKernelMap 3d (webgl2)', function() {
var kernel = new GPU({ mode: 'webgl2' }).createKernelMap({
target: function saveTarget(value) {
return value;
}
}, function(value) {
return saveTarget(value);
}).setOutput([3,3,3]);
var result = kernel(1);
QUnit.assert.equal(result.result.length, 3);
QUnit.assert.equal(result.result[0].length, 3);
QUnit.assert.equal(result.result[0][0].length, 3);

QUnit.assert.equal(result.target.length, 3);
QUnit.assert.equal(result.target[0].length, 3);
QUnit.assert.equal(result.target[0][0].length, 3);
});

QUnit.test('createKernelMap 3d (cpu)', function() {
var kernel = new GPU({ mode: 'cpu' }).createKernelMap({
target: function saveTarget(value) {
return value;
}
}, function(value) {
return saveTarget(value);
}).setOutput([3,3,3]);
var result = kernel(1);
QUnit.assert.equal(result.result.length, 3);
QUnit.assert.equal(result.result[0].length, 3);
QUnit.assert.equal(result.result[0][0].length, 3);

QUnit.assert.equal(result.target.length, 3);
QUnit.assert.equal(result.target[0].length, 3);
QUnit.assert.equal(result.target[0][0].length, 3);
});
})();

0 comments on commit a8d5d02

Please sign in to comment.