MCPcopy Create free account
hub / github.com/gpujs/gpu.js / sumAB

Function sumAB

test/features/add-custom-function.js:198–229  ·  view source on GitHub ↗
(mode)

Source from the content-addressed store, hash-verified

196describe('features: add custom function with `this.constants.width` in loop');
197
198function sumAB(mode) {
199 const gpu = new GPU({mode});
200
201 function customAdder(a, b) {
202 let sum = 0;
203 for (let i = 0; i < this.constants.width; i++) {
204 sum += a[this.thread.x] + b[this.thread.x];
205 }
206 return sum;
207 }
208
209 gpu.addFunction(customAdder);
210
211 const kernel = gpu.createKernel(function (a, b) {
212 return customAdder(a, b);
213 }, {
214 output: [6],
215 constants: {width: 6},
216 precision: 'unsigned',
217 });
218
219 assert.ok(kernel !== null, 'function generated test');
220
221 const a = [1, 2, 3, 5, 6, 7];
222 const b = [1, 1, 1, 1, 1, 1];
223
224 const result = kernel(a, b);
225 const expected = [12, 18, 24, 36, 42, 48];
226
227 assert.deepEqual(Array.from(result), expected);
228 gpu.destroy();
229}
230
231test('sumAB auto', () => {
232 sumAB(null);

Callers 1

Calls 5

addFunctionMethod · 0.95
createKernelMethod · 0.95
destroyMethod · 0.95
kernelFunction · 0.85
customAdderFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…