Advertisement
Guest User

Game of Life OpenCL

a guest
Oct 29th, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #define LOCAL_CELL_SIZE 14
  2. #define FINAL_LOCAL_INDEX 15
  3. #define LOCAL_ACTUAL_SIZE 16
  4.  
  5. __inline int shouldIBeAlive(int currentStatus, int aliveNeighbours) {
  6.     return aliveNeighbours == 3 || (currentStatus == 1 && aliveNeighbours == 2);
  7. }
  8.  
  9. int getGlobalPixelIndex(int x, int y, int sizeX, int sizeY) {
  10.     x = (x == -1) ? sizeX-1 : x;
  11.     y = (y == -1) ? sizeY-1 : y;
  12.  
  13.     x = (x == sizeX) ? 0 : x;
  14.     y = (y == sizeY) ? 0 : y;
  15.     return y*sizeX + x;
  16. }
  17.  
  18. __inline int getLocalPixelIndex(int x, int y) {
  19.     return y*LOCAL_ACTUAL_SIZE + x;
  20. }
  21.  
  22. __kernel void game_step(
  23.     __global int *global_input,
  24.     __global int *output,
  25.     int sizeX,
  26.     int sizeY)
  27. {
  28.     int x = get_global_id(0) - 2 * get_group_id(0) - 1;
  29.     int y = get_global_id(1) - 2 * get_group_id(1) - 1;
  30.  
  31.     int group_x_start = get_group_id(0) * LOCAL_CELL_SIZE;
  32.     int group_y_start = get_group_id(1) * LOCAL_CELL_SIZE;
  33.  
  34.     int local_x = get_local_id(0);
  35.     int local_y = get_local_id(1);
  36.  
  37.     __local int local_input[LOCAL_ACTUAL_SIZE*LOCAL_ACTUAL_SIZE];
  38.  
  39.     if(x <= sizeX && y <= sizeY){
  40.         local_input[getLocalPixelIndex(local_x, local_y)]
  41.                 = global_input[getGlobalPixelIndex(group_x_start + local_x - 1,
  42.                                                    group_y_start + local_y - 1,
  43.                                                    sizeX,
  44.                                                    sizeY)];
  45.     }
  46.  
  47.     barrier(CLK_LOCAL_MEM_FENCE);
  48.  
  49.     //index 0 and final doesn't process cells
  50.     if(local_x != 0 && local_x != FINAL_LOCAL_INDEX &&
  51.        local_y != 0 && local_y != FINAL_LOCAL_INDEX &&
  52.        x < sizeX && y < sizeY){
  53.        int aliveNeighbours = 0;
  54.        int currentStatus = local_input[getLocalPixelIndex(local_x, local_y)];
  55.  
  56.        aliveNeighbours += local_input[getLocalPixelIndex(local_x - 1, local_y - 1)];
  57.        aliveNeighbours += local_input[getLocalPixelIndex(local_x, local_y - 1)];
  58.        aliveNeighbours += local_input[getLocalPixelIndex(local_x + 1, local_y - 1)];
  59.  
  60.        aliveNeighbours += local_input[getLocalPixelIndex(local_x - 1, local_y)];
  61.        aliveNeighbours += local_input[getLocalPixelIndex(local_x + 1, local_y)];
  62.  
  63.        aliveNeighbours += local_input[getLocalPixelIndex(local_x - 1, local_y + 1)];
  64.        aliveNeighbours += local_input[getLocalPixelIndex(local_x, local_y + 1)];
  65.        aliveNeighbours += local_input[getLocalPixelIndex(local_x + 1, local_y + 1)];
  66.  
  67.        output[y*sizeX + x] = shouldIBeAlive(currentStatus, aliveNeighbours);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement