Skip to content

Commit 8e3b937

Browse files
committed
Update kernels to not output same value
1 parent 7ec1571 commit 8e3b937

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

test/features/return-arrays.js

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ test('return Array(2) from kernel cpu', () => {
4040
function returnArray2D2FromKernel(mode) {
4141
const gpu = new GPU({ mode });
4242
const kernel = gpu.createKernel(function() {
43-
return [1, 2];
44-
}, { output: [2, 2], precision: 'single' });
45-
const result = kernel();
46-
assert.deepEqual(result.map(matrix => matrix.map(row => Array.from(row))), [[[1, 2], [1, 2]],[[1, 2], [1, 2]]]);
43+
return [this.thread.x, this.thread.y];
44+
}, { output : [3, 7], precision: 'single' });
45+
const res = kernel();
46+
for (let y = 0; y < 7; ++y) {
47+
for (let x = 0; x < 3; ++x) {
48+
assert.equal(res[y][x][0], x);
49+
assert.equal(res[y][x][1], y);
50+
}
51+
}
4752
gpu.destroy();
4853
}
4954

@@ -73,11 +78,18 @@ test('return Array2D(2) from kernel cpu', () => {
7378

7479
function returnArray3D2FromKernel(mode) {
7580
const gpu = new GPU({ mode });
76-
const kernel = gpu.createKernel(function() {
77-
return [1, 2];
78-
}, { output: [2, 2, 2], precision: 'single' });
79-
const result = kernel();
80-
assert.deepEqual(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row)))), [[[[1, 2], [1, 2]],[[1, 2], [1, 2]]],[[[1, 2], [1, 2]],[[1, 2], [1, 2]]]]);
81+
const kernel = gpu.createKernel(function() {
82+
return [this.thread.y, this.thread.z];
83+
}, { output : [3, 5, 7], precision: 'single' });
84+
const res = kernel();
85+
for (let z = 0; z < 7; ++z) {
86+
for (let y = 0; y < 5; ++y) {
87+
for (let x = 0; x < 3; ++x) {
88+
assert.equal(res[z][y][x][0], y);
89+
assert.equal(res[z][y][x][1], z);
90+
}
91+
}
92+
}
8193
gpu.destroy();
8294
}
8395

@@ -143,10 +155,16 @@ test('return Array(3) from kernel cpu', () => {
143155
function returnArray2D3FromKernel(mode) {
144156
const gpu = new GPU({ mode });
145157
const kernel = gpu.createKernel(function() {
146-
return [1, 2, 3];
147-
}, { output: [2,2], precision: 'single' });
148-
const result = kernel();
149-
assert.deepEqual(Array.from(result.map(matrix => matrix.map(row => Array.from(row)))), [[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]]);
158+
return [this.thread.x, this.thread.y, this.thread.x * this.thread.y];
159+
}, { output : [3, 7] ,precision: 'single' });
160+
const res = kernel();
161+
for (let y = 0; y < 7; ++y) {
162+
for (let x = 0; x < 3; ++x) {
163+
assert.equal(res[y][x][0], x);
164+
assert.equal(res[y][x][1], y);
165+
assert.equal(res[y][x][2], x * y);
166+
}
167+
}
150168
gpu.destroy();
151169
}
152170

@@ -177,10 +195,18 @@ test('return Array2D(3) from kernel cpu', () => {
177195
function returnArray3D3FromKernel(mode) {
178196
const gpu = new GPU({ mode });
179197
const kernel = gpu.createKernel(function() {
180-
return [1, 2, 3];
181-
}, { output: [2,2,2], precision: 'single' });
182-
const result = kernel();
183-
assert.deepEqual(Array.from(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row))))), [[[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]],[[[1, 2, 3],[1, 2, 3]],[[1, 2, 3],[1, 2, 3]]]]);
198+
return [this.thread.x, this.thread.y, this.thread.z];
199+
}, { output : [3, 5, 7] ,precision: 'single' });
200+
const res = kernel();
201+
for (let z = 0; z < 7; ++z) {
202+
for (let y = 0; y < 5; ++y) {
203+
for (let x = 0; x < 3; ++x) {
204+
assert.equal(res[z][y][x][0], x);
205+
assert.equal(res[z][y][x][1], y);
206+
assert.equal(res[z][y][x][2], z);
207+
}
208+
}
209+
}
184210
gpu.destroy();
185211
}
186212

@@ -245,10 +271,17 @@ test('return Array(4) from kernel cpu', () => {
245271
function returnArray2D4FromKernel(mode) {
246272
const gpu = new GPU({ mode });
247273
const kernel = gpu.createKernel(function() {
248-
return [1, 2, 3, 4];
249-
}, { output: [2,2], precision: 'single' });
250-
const result = kernel();
251-
assert.deepEqual(result.map(matrix => matrix.map(row => Array.from(row))), [[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]]);
274+
return [this.thread.x, this.thread.y, this.thread.x * this.thread.y, this.thread.x - this.thread.y];
275+
}, { output : [3, 7], precision: 'single' });
276+
const res = kernel();
277+
for (let y = 0; y < 3; ++y) {
278+
for (let x = 0; x < 3; ++x) {
279+
assert.equal(res[y][x][0], x);
280+
assert.equal(res[y][x][1], y);
281+
assert.equal(res[y][x][2], x * y);
282+
assert.equal(res[y][x][3], x - y);
283+
}
284+
}
252285
gpu.destroy();
253286
}
254287

@@ -279,10 +312,19 @@ test('return Array2D(4) from kernel cpu', () => {
279312
function returnArray3D4FromKernel(mode) {
280313
const gpu = new GPU({ mode });
281314
const kernel = gpu.createKernel(function() {
282-
return [1, 2, 3, 4];
283-
}, { output: [2,2,2], precision: 'single' });
284-
const result = kernel();
285-
assert.deepEqual(result.map(cube => cube.map(matrix => matrix.map(row => Array.from(row)))), [[[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]],[[[1, 2, 3, 4],[1, 2, 3, 4]],[[1, 2, 3, 4],[1, 2, 3, 4]]]]);
315+
return [this.thread.x, this.thread.y, this.thread.z, this.thread.x * this.thread.y * this.thread.z];
316+
}, { output : [3, 5, 7], precision: 'single' });
317+
const res = kernel();
318+
for (let z = 0; z < 7; ++z) {
319+
for (let y = 0; y < 5; ++y) {
320+
for (let x = 0; x < 3; ++x) {
321+
assert.equal(res[z][y][x][0], x);
322+
assert.equal(res[z][y][x][1], y);
323+
assert.equal(res[z][y][x][2], z);
324+
assert.equal(res[z][y][x][3], x * y * z);
325+
}
326+
}
327+
}
286328
gpu.destroy();
287329
}
288330

0 commit comments

Comments
 (0)