forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor-loop.js
More file actions
136 lines (104 loc) · 2.91 KB
/
for-loop.js
File metadata and controls
136 lines (104 loc) · 2.91 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
function for_loop_test(mode) {
var gpu = new GPU({ mode: mode });
var f = gpu.createKernel(function(a, b) {
var x = 0;
for(var i = 0; i < 10; i++) {
x = x + 1;
}
return (a[this.thread.x] + b[this.thread.x] + x);
}, {
dimensions : [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 = [15, 17, 19, 16, 18, 20];
for(var i = 0; i < exp.length; ++i) {
QUnit.assert.close(res[i], exp[i], 0.1, "Result arr idx: "+i);
}
}
QUnit.test( "for_loop (auto)", function() {
for_loop_test(null);
});
QUnit.test( "for_loop (WebGL)", function() {
for_loop_test("webgl");
});
QUnit.test( "for_loop (CPU)", function() {
for_loop_test("cpu");
});
// Prevent test function leak
(function() {
function evil_while_kernalFunction(a, b) {
var x = 0.0;
var i = 0;
//10000000 or 10 million is the approx upper limit on a chrome + GTX 780
while(i<100) {
x = x + 1.0;
++i;
}
return (a[this.thread.x] + b[this.thread.x] + x);
}
var evil_while_a = [1, 2, 3, 5, 6, 7];
var evil_while_b = [4, 5, 6, 1, 2, 3];
var evil_while_cpuRef = new GPU();
var evil_while_cpuRef_f = evil_while_cpuRef.createKernel(evil_while_kernalFunction, {
dimensions : [6],
mode : "cpu",
loopMaxIterations: 10000
});
var evil_while_exp = evil_while_cpuRef_f(evil_while_a,evil_while_b);
function evil_while_loop_test(mode ) {
var gpu = new GPU();
var f = gpu.createKernel(evil_while_kernalFunction, {
dimensions : [6],
mode : mode
});
QUnit.assert.ok( f !== null, "function generated test");
var res = f(evil_while_a,evil_while_b);
for(var i = 0; i < evil_while_exp.length; ++i) {
QUnit.assert.close(evil_while_exp[i], res[i], 0.1, "Result arr idx: "+i);
}
}
QUnit.test( "evil_while_loop (auto)", function() {
evil_while_loop_test(null);
});
QUnit.test( "evil_while_loop (WebGL)", function() {
evil_while_loop_test("webgl");
});
QUnit.test( "evil_while_loop (CPU)", function() {
evil_while_loop_test("cpu");
});
})();
function for_constant_loop_test(mode) {
var gpu = new GPU({ mode: mode });
var f = gpu.createKernel(function(a, b) {
var x = 0;
for(var i = 0; i < max; i++) {
x = x + 1;
}
return (a[this.thread.x] + b[this.thread.x] + x);
}, {
dimensions : [6],
constants: {
max: 10
}
});
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 = [15, 17, 19, 16, 18, 20];
for(var i = 0; i < exp.length; ++i) {
QUnit.assert.close(res[i], exp[i], 0.1, "Result arr idx: "+i);
}
}
QUnit.test( "for_constant_loop_test (auto)", function() {
for_constant_loop_test(null);
});
QUnit.test( "for_constant_loop_test (WebGL)", function() {
for_constant_loop_test("webgl");
});
QUnit.test( "for_constant_loop_test (CPU)", function() {
for_constant_loop_test("cpu");
});