-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cl
More file actions
39 lines (31 loc) · 1.29 KB
/
kernel.cl
File metadata and controls
39 lines (31 loc) · 1.29 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
__kernel void GOL(const int dim,
__global int *grid,
__global int *gridSwp)
{
__private int x = get_global_id(0);
__private int y = get_global_id(1);
__private int id = x * dim + y;
__private int max = dim*dim;
__private int neighborCount = 0;
// Positions of neighbors
__private int positions[8] = {id-dim-1, id+dim-1, id-1, id+dim, id-dim, id+1, id-dim+1, id+dim+1};
// If we're on the left edge don't do left up, left or left down
__private int leftEdge = 3 * (id % dim == 0);
// If we're on the right edge don't do right up, right or right down
__private int rightEdge = 3 * ((id + 1) % dim == 0);
// Go through neighbors and check sum
for (__private int i = leftEdge; i < 8 - rightEdge; ++i)
if (positions[i] >= 0 && positions[i] < max) // Don't go out of bounds
neighborCount += grid[positions[i]];
__private int current = grid[id];
if (current == 1 && neighborCount < 2)
gridSwp[id] = 0;
else if (current == 1 && (neighborCount == 2 || neighborCount == 3))
gridSwp[id] = 1;
else if (current == 1 && neighborCount > 3)
gridSwp[id] = 0;
else if (current == 0 && neighborCount == 3)
gridSwp[id] = 1;
else
gridSwp[id] = current;
}