forked from fingerecho/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmult-ab.js
More file actions
93 lines (81 loc) · 2.07 KB
/
mult-ab.js
File metadata and controls
93 lines (81 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function() {
function multABTest(mode) {
var gpu = new GPU({ mode });
var f = gpu.createKernel(function(a, b) {
var sum = 0;
sum += a[this.thread.y][0] * b[0][this.thread.x];
sum += a[this.thread.y][1] * b[1][this.thread.x];
sum += a[this.thread.y][2] * b[2][this.thread.x];
return sum;
}, {
output : [3, 3]
});
QUnit.assert.ok( f !== null, 'function generated test');
QUnit.assert.deepEqual(f(
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]).map(function(object) { return QUnit.extend([], object); }),
[
[30, 36, 42],
[66, 81, 96],
[102, 126, 150]
],
'basic mult function test'
);
gpu.destroy();
}
QUnit.test( 'multAB (auto)', function() {
multABTest(null);
});
QUnit.test( 'multAB (gpu)', function() {
multABTest('gpu');
});
QUnit.test( 'multAB (webgl)', function() {
multABTest('webgl');
});
QUnit.test( 'multAB (webgl2)', function() {
multABTest('webgl2');
});
QUnit.test( 'multAB (CPU)', function() {
multABTest('cpu');
});
function sqrtABTest(mode) {
var gpu = new GPU({ mode: mode });
var f = gpu.createKernel(function(a, b) {
return Math.sqrt(a[ this.thread.x ] * b[ this.thread.x ]);
}, {
output : [6]
});
QUnit.assert.ok(f !== null, 'function generated test');
var a = [3, 4, 5, 6, 7, 8];
var b = [3, 4, 5, 6, 7, 8];
var res = f(a,b);
var exp = [3, 4, 5, 6, 7, 8];
for(var i = 0; i < exp.length; ++i) {
QUnit.assert.close(res[i], exp[i], 0.1, 'Result arr idx: '+i);
}
gpu.destroy();
}
QUnit.test( 'sqrtAB (auto)', function() {
sqrtABTest(null);
});
QUnit.test( 'sqrtAB (gpu)', function() {
sqrtABTest('gpu');
});
QUnit.test( 'sqrtAB (webgl)', function() {
sqrtABTest('webgl');
});
QUnit.test( 'sqrtAB (webgl2)', function() {
sqrtABTest('webgl2');
});
QUnit.test( 'sqrtAB (CPU)', function() {
sqrtABTest('cpu');
});
})();