forked from fingerecho/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-custom-function.js
More file actions
156 lines (119 loc) · 3.85 KB
/
add-custom-function.js
File metadata and controls
156 lines (119 loc) · 3.85 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(function() {
function addCustomFunction_sumAB(mode) {
var gpu = new GPU({ mode: mode });
function custom_adder(a,b) {
return a+b;
}
var f = gpu.createKernel(function(a, b) {
return custom_adder(a[this.thread.x], b[this.thread.x]);
}, {
functions: [custom_adder],
output : [6]
});
QUnit.assert.ok( f !== null, 'function generated test');
var a = [1, 2, 3, 5, 6, 7];
var b = [4, 5, 6, 1, 2, 3];
var res = f(a,b);
var exp = [5, 7, 9, 6, 8, 10];
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( 'addCustomFunction_sumAB (auto)', function() {
addCustomFunction_sumAB(null);
});
QUnit.test( 'addCustomFunction_sumAB (gpu)', function() {
addCustomFunction_sumAB('gpu');
});
QUnit.test( 'addCustomFunction_sumAB (webgl)', function() {
addCustomFunction_sumAB('webgl');
});
QUnit.test( 'addCustomFunction_sumAB (webgl2)', function() {
addCustomFunction_sumAB('webgl2');
});
QUnit.test( 'addCustomFunction_sumAB (cpu)', function() {
addCustomFunction_sumAB('cpu');
});
function addCustomFunction_constantsWidth(mode) {
var gpu = new GPU({ mode: mode });
function custom_adder(a, b) {
var sum = 0;
for (var i = 0; i < this.constants.width; i++) {
sum += (a[this.thread.x] + b[this.thread.x]);
}
return sum;
}
var f = gpu.createKernel(function(a, b) {
return custom_adder(a, b);
}, {
functions: [custom_adder],
output : [6],
constants: { width: 6 }
});
QUnit.assert.ok( f !== null, 'function generated test');
var a = [1, 2, 3, 5, 6, 7];
var b = [1, 1, 1, 1, 1, 1];
var res = f(a,b);
var exp = [12, 18, 24, 36, 42, 48];
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('addCustomFunction_constantsWidth (auto)', function() {
addCustomFunction_constantsWidth(null);
});
QUnit.test('addCustomFunction_constantsWidth (gpu)', function() {
addCustomFunction_constantsWidth('gpu');
});
QUnit.test('addCustomFunction_constantsWidth (webgl)', function() {
addCustomFunction_constantsWidth('webgl');
});
QUnit.test('addCustomFunction_constantsWidth (webgl2)', function() {
addCustomFunction_constantsWidth('webgl2');
});
QUnit.test('addCustomFunction_constantsWidth (cpu)', function() {
addCustomFunction_constantsWidth('cpu');
});
function addCustomFunction_thisOutputX(mode) {
var gpu = new GPU({ mode: mode });
function custom_adder(a, b) {
var sum = 0;
for (var i = 0; i < this.output.x; i++) {
sum += (a[this.thread.x] + b[this.thread.x]);
}
return sum;
}
var f = gpu.createKernel(function(a, b) {
return custom_adder(a, b);
}, {
functions: [custom_adder],
output : [6]
});
QUnit.assert.ok( f !== null, 'function generated test');
var a = [1, 2, 3, 5, 6, 7];
var b = [1, 1, 1, 1, 1, 1];
var res = f(a,b);
var exp = [12, 18, 24, 36, 42, 48];
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('addCustomFunction_thisOutputX (auto)', function() {
addCustomFunction_thisOutputX(null);
});
QUnit.test('addCustomFunction_thisOutputX (gpu)', function() {
addCustomFunction_thisOutputX('gpu');
});
QUnit.test('addCustomFunction_thisOutputX (webgl)', function() {
addCustomFunction_thisOutputX('webgl');
});
QUnit.test('addCustomFunction_thisOutputX (webgl2)', function() {
addCustomFunction_thisOutputX('webgl2');
});
QUnit.test('addCustomFunction_thisOutputX (cpu)', function() {
addCustomFunction_thisOutputX('cpu');
});
})();