function testSameSourceDestinationFromResultThrows(error, precision, mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(value) { return value[this.thread.x] + 1; }, { output: [1], pipeline: true, immutable: false, precision, }); let result = kernel([0]); assert.equal((result.toArray ? result.toArray() : result)[0], 1); assert.throws(() => kernel(result), error); gpu.destroy(); }
function clearClonedTexture(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function() { return 1; }, { output: [1], pipeline: true, immutable: true }); const result = kernel(); assert.equal(result.toArray()[0], 1); const result2 = result.clone(); const result3 = result2.clone(); assert.equal(result2.toArray()[0], 1); assert.equal(result3.toArray()[0], 1); result2.clear(); assert.equal(result2.toArray()[0], 0); assert.equal(result3.toArray()[0], 1); gpu.destroy(); }
function readFromTextureKernels(mode) { const gpu = new GPU({ mode }); function add(m, n) { return m + n; } const kernels = gpu.createKernelMap({ addResult: add }, function (a, b) { return add(a[this.thread.x], b[this.thread.x]); }) .setPipeline(true) .setOutput([5]); const result = kernels([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]); const textureResult = result.addResult; assert.deepEqual(Array.from(result.result.toArray()), [2, 4, 6, 8, 10]); assert.deepEqual(Array.from(textureResult.toArray()), [2, 4, 6, 8, 10]); gpu.destroy(); }
function copy3DTexture(precision, mode) { const gpu = new GPU({ mode }); function makeTexture() { return (gpu.createKernel(function() { return this.thread.x + (this.thread.y * this.output.x) * (this.output.x * this.output.y * this.thread.z); }, { output: [5, 5, 5], pipeline: true, precision }))(); } const texture = makeTexture(); const clone = texture.clone(); assert.notEqual(texture, clone); assert.equal(texture.texture, clone.texture); assert.deepEqual(texture.toArray(), clone.toArray()); gpu.destroy(); }
function testArray2D3(mode) { const gpu = new GPU({ mode }); const texture = ( gpu.createKernel(function() { return [this.thread.x, this.thread.y, this.thread.x * this.thread.y]; }) .setOutput([10, 10]) .setPipeline(true) .setPrecision('single') )(); const expected = texture.toArray(); const kernel = gpu.createKernel(function(value) { return value[this.thread.y][this.thread.x]; }) .setArgumentTypes({ value: 'Array2D(3)' }) .setOutput([10, 10]) .setPipeline(false) .setPrecision('single'); assert.notEqual(texture.constructor, Array); assert.equal(expected.constructor, Array); assert.deepEqual(kernel(texture), expected); assert.deepEqual(kernel(expected), expected); gpu.destroy(); }
function testArrayWithoutTypeDefined(mode) { const gpu = new GPU({ mode }); const texture = ( gpu.createKernel(function() { return this.thread.x; }) .setOutput([10]) .setPipeline(true) .setPrecision('single') )(); const expected = texture.toArray(); const kernel = gpu.createKernel(function(value) { return value[this.thread.x]; }) .setOutput([10]) .setPipeline(false) .setPrecision('single'); assert.notEqual(texture.constructor, Array); assert.equal(expected.constructor, Float32Array); assert.deepEqual(kernel(texture), expected); assert.deepEqual(kernel(expected), expected); gpu.destroy(); }
function clearTexture(precision, mode) { const gpu = new GPU({ mode }); function makeTexture() { return (gpu.createKernel(function() { return this.thread.x; }, { output: [5], pipeline: true, precision }))(); } const texture = makeTexture(); assert.deepEqual(texture.toArray(), new Float32Array([0,1,2,3,4])); texture.clear(); const texture2 = makeTexture(); // put another texture in the way assert.deepEqual(texture.toArray(), new Float32Array([0,0,0,0,0])); assert.deepEqual(texture2.toArray(), new Float32Array([0,1,2,3,4])); gpu.destroy(); }
function floatPipeline2DOutput(mode) { const gpu = new GPU({ mode }); const matrix = [ [1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], ]; const kernel = gpu.createKernel(function(matrix) { return matrix[this.thread.y][this.thread.x]; }, { output: [5, 3], optimizeFloatMemory: true, precision: 'single', pipeline: true, }); const texture = kernel(matrix); const result = texture.toArray(); assert.deepEqual(result.map(row => Array.from(row)), matrix); gpu.destroy(); }
function copy2DTexture(precision, mode) { const gpu = new GPU({ mode }); function makeTexture() { return (gpu.createKernel(function() { return this.thread.x + (this.thread.y * this.output.x); }, { output: [5, 5], pipeline: true, precision }))(); } const texture = makeTexture(); const clone = texture.clone(); assert.notEqual(texture, clone); assert.equal(texture.texture, clone.texture); assert.deepEqual(texture.toArray(), clone.toArray()); gpu.destroy(); }
function testArrayWithTypeDefined(mode) { const gpu = new GPU({ mode }); const texture = ( gpu.createKernel(function() { return this.thread.x; }) .setOutput([10]) .setPipeline(true) .setPrecision('single') )(); const expected = texture.toArray(); const kernel = gpu.createKernel(function(value) { return value[this.thread.x]; }) .setArgumentTypes({ value: 'Array' }) .setOutput([10]) .setPipeline(false) .setPrecision('single'); assert.notEqual(texture.constructor, Array); assert.equal(expected.constructor, Float32Array); assert.deepEqual(kernel(texture), expected); assert.deepEqual(kernel(expected), expected); gpu.destroy(); }
function testOutputTextureIsClonedWhenRecompiling(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(value) { return value[this.thread.x] + 1; }, { output: [1], immutable: true, pipeline: true }); const result1 = kernel([1]); assert.equal(result1.toArray()[0], 2); const result2 = kernel(result1); result1.delete(); assert.equal(result2.toArray()[0], 3); result2.delete(); const result3 = kernel([3]); assert.equal(result3.toArray()[0], 4); const result4 = kernel(result3); result3.delete(); assert.equal(result4.toArray()[0], 5); result4.delete(); gpu.destroy(); }
function copy1DTexture(precision, mode) { const gpu = new GPU({ mode }); function makeTexture() { return (gpu.createKernel(function() { return this.thread.x; }, { output: [5], pipeline: true, precision }))(); } const texture = makeTexture(); const clone = texture.clone(); assert.notEqual(texture, clone); assert.equal(texture.texture, clone.texture); assert.deepEqual(texture.toArray(), clone.toArray()); gpu.destroy(); }
function immutableKernelWithFloats(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function (v) { return v[this.thread.x] + 1; }, { output: [1], immutable: true, pipeline: true, precision: 'single', }); // start with a value on CPU // reuse that output, simulating that this value will be monitored, and updated via the same kernel // this is often used in neural networks const output1 = kernel([1]); const output2 = kernel(output1); const output3 = kernel(output2); assert.equal(output1.toArray()[0], 2); assert.equal(output2.toArray()[0], 3); assert.equal(output3.toArray()[0], 4); gpu.destroy(); }
function testImmutableDoesNotCollideWithKernelTexture(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(v) { return v[this.thread.x] + 1; }, { output: [1], precision: 'unsigned', pipeline: true, immutable: true, }); const v = [1]; const result1 = kernel(v); assert.deepEqual(result1.toArray(), new Float32Array([2])); // kernel is getting ready to recompile, because a new type of input const result2 = kernel(result1); assert.deepEqual(result2.toArray(), new Float32Array([3])); // now the kernel textures match, this would fail, and this is that this test is testing const result3 = kernel(result2); assert.deepEqual(result3.toArray(), new Float32Array([4])); gpu.destroy(); }
function testImmutableDoesNotCollideWithKernelTexture(mode) { const gpu = new GPU({ mode }); const kernel = gpu.createKernel(function(v) { return v[this.thread.x] + 1; }, { output: [1], precision: 'single', pipeline: true, immutable: true, }); const v = [1]; const result1 = kernel(v); assert.deepEqual(result1.toArray(), new Float32Array([2])); // kernel is getting ready to recompile, because a new type of input const result2 = kernel(result1); assert.deepEqual(result2.toArray(), new Float32Array([3])); // now the kernel textures match, this would fail, and this is that this test is testing const result3 = kernel(result2); assert.deepEqual(result3.toArray(), new Float32Array([4])); gpu.destroy(); }