You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
9 years ago
|
__kernel void conway(__global unsigned char* front_grid, __global unsigned char* rear_grid, __global int* num_workers, __global int* grid_width, __global int* grid_height)
|
||
9 years ago
|
{
|
||
|
// Caclulate the start and end range that this worker will be calculating
|
||
|
|
||
9 years ago
|
int data_length = *grid_width * *grid_height;
|
||
9 years ago
|
|
||
9 years ago
|
int start_range = (data_length / *num_workers) * get_global_id(0) * 2; // * 2 = padding
|
||
|
int end_range = (data_length / *num_workers) * (get_global_id(0) + 1) * 2;
|
||
9 years ago
|
|
||
|
// x, y + 1
|
||
|
|
||
|
int neighbors = 0;
|
||
|
|
||
|
for (int i = start_range; i < end_range; i += 2){
|
||
|
|
||
|
// add all 8 blocks to neghbors
|
||
9 years ago
|
neighbors = 0;
|
||
9 years ago
|
// Top
|
||
9 years ago
|
neighbors += in[i - *grid_width * 2];
|
||
9 years ago
|
|
||
|
// Top right
|
||
9 years ago
|
neighbors += in[i - *grid_width * 2 + 2];
|
||
9 years ago
|
|
||
|
// Right
|
||
|
neighbors += in[i + 2];
|
||
|
|
||
|
// Bottom Right
|
||
9 years ago
|
neighbors += in[i + *grid_width * 2 + 2];
|
||
9 years ago
|
|
||
|
// Bottom
|
||
9 years ago
|
neighbors += in[i + *grid_width * 2];
|
||
9 years ago
|
|
||
|
// Bottom Left
|
||
9 years ago
|
neighbors += in[i + *grid_width * 2 - 2];
|
||
9 years ago
|
|
||
|
// Left
|
||
|
neighbors += in[i - 2];
|
||
|
|
||
|
// Top left
|
||
9 years ago
|
neighbors += in[i - *grid_width * 2 - 2];
|
||
9 years ago
|
|
||
|
// push living status to the padded second char
|
||
|
|
||
|
if (neighbors == 3 || (neighbors == 2 && in[i])){
|
||
|
in[i + 1] = 1;
|
||
|
}
|
||
|
|
||
|
else
|
||
|
in[i + 1] = 0;
|
||
|
}
|
||
|
}
|