-
Notifications
You must be signed in to change notification settings - Fork 1
/
slowpi.cl
55 lines (43 loc) · 988 Bytes
/
slowpi.cl
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
// slow PI cacllation, a fake compute intensive kernel
ulong xorshift128plus(ulong2* s)
{
ulong x = (*s).x;
ulong const y = (*s).y;
(*s).x = y;
x ^= x << 23;
(*s).y = x ^ y ^ (x >> 17) ^ (y >> 26);
return (*s).y + y;
}
__kernel void randonly(
ulong2 seed,
const ulong n,
__global ulong *restrict res)
{
int g_idx = get_global_id(0);
ulong tmp = 0;
ulong dist = 10000000;
ulong i;
seed.x += g_idx;
for (i = 0; i < n; i++) {
tmp = tmp ^ (xorshift128plus(&seed) % dist);
}
res[g_idx] = tmp;
}
__kernel void slowpi(
ulong2 seed,
const ulong n,
__global float *restrict res)
{
int g_idx = get_global_id(0);
ulong cnt = 0;
ulong dist = 10000000;
float x, y;
ulong i;
seed.x += g_idx;
for (i = 0; i < n; i++) {
x = (float)(xorshift128plus(&seed) % dist);
y = (float)(xorshift128plus(&seed) % dist);
if ( sqrt(x*x + y*y) < (float)dist ) cnt ++;
}
res[g_idx] = 4.0 * (float)cnt / (float)n;
}