37 lines
1.1 KiB
Common Lisp

void kernel generation(global const int *previous, global int *new, global const int *size) {
int ID, Nthreads, n, ratio, start, stop, x, y, left, right, up, down, neighbors, i;
ID = get_global_id(0);
Nthreads = get_global_size(0);
n = size[0] * size[1];
ratio = (n / Nthreads); // number of elements for each thread
start = ratio * ID;
stop = ratio * (ID + 1);
for (i = start; i < stop; i++)
x = i % size[1];
y = i / size[0];
left = x - 1;
right = (x + 1) % size[0];
up = (y - 1 + size[1]) % size[1];
down = (y + 1) % size[1];
// Get the number of neighbors
neighbors =
previous[size[0] * up + left]
+ previous[size[0] * up + x]
+ previous[size[0] * up + right]
+ previous[size[0] * y + left]
+ previous[size[0] * y + right]
+ previous[size[0] * down + left]
+ previous[size[0] * down + x]
+ previous[size[0] * down + right];
// Update cell
new[size[0] * y + x] = (neighbors == 3) + previous[size[0] * y + x] * (neighbors == 2);
}