Advertisement
Guest User

Pathfinding A* OpenCL Kernel

a guest
Jun 26th, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. float heuristic(int2 start, int2 end)
  2. {
  3.     float result = 0;
  4.    
  5.     int x, y;
  6.    
  7.     x = (int)(start.x - end.x);
  8.     y = (int)(start.y - end.y);
  9.    
  10.     if (x < 0)
  11.     {
  12.         x = -x;
  13.     }
  14.    
  15.     if (y < 0)
  16.     {
  17.         y = -y;
  18.     }
  19.    
  20.     result = sqrt((x * x) + (y * y));
  21.     return result;
  22. }
  23.  
  24. __kernel
  25. void findPath(__global char * output,
  26.               __global bool * input,
  27.               const    int    width,
  28.               const    int    height,
  29.               const    int    agentsNum,
  30.              __global int2 * startIndexes,
  31.              __global int2 * destIndexes)
  32. {  
  33.     uint globalIdx = get_global_id(0);
  34.  
  35.     /* wait until the whole block is filled */
  36.     barrier(CLK_LOCAL_MEM_FENCE);
  37.    
  38.     int2 closedList[1024];
  39.     int2 openedList[1024];
  40.    
  41.     int openedListLength = 0;
  42.     int closedListLength = 0;
  43.    
  44.     int2 start = (int2)startIndexes[globalIdx];
  45.     int2 end = (int2)destIndexes[globalIdx];
  46.    
  47.     if (start.x != end.x && start.y != end.y)
  48.     {
  49.         openedList[0] = (int2)start;
  50.         openedListLength++;
  51.     }
  52.  
  53.     int2 currentPoint, tempPoint;
  54.    
  55.     float currentH = 0;
  56.     float tempH = 0;
  57.    
  58.     int i = 0;
  59.     int x = 0;
  60.     int y = 0;
  61.    
  62.     bool found = false;
  63.     bool inClosedList;
  64.     bool inOpenedList;
  65.     bool betterPath;
  66.    
  67.     int stX, stY, enX, enY;
  68.     int index;
  69.     float hScore[1024];
  70.     float fScore[1024];
  71.     float gScore[1024];
  72.     int cameFrom[1024];
  73.    
  74.     gScore[start.x + (start.y * width)] = (float)0.0f;
  75.    
  76.     tempH = (float)heuristic(start, end);
  77.    
  78.     index = (int)(start.x + (start.y * width));
  79.    
  80.     hScore[index] = (float)tempH;
  81.     fScore[index] = (float)hScore[index];
  82.    
  83.     cameFrom[start.x + (start.y * width)] = (int)-1;
  84.    
  85.     output[index + ((width * height) * globalIdx)] = 'O';
  86.     output[(int)(end.x + (end.y * width)) + ((width * height) * globalIdx)] = 'X';
  87.    
  88.     while ((openedListLength > 0) && (!found))
  89.     {
  90.         currentH = -1.0f;
  91.         currentPoint = (int2)(-1,-1);
  92.        
  93.         for (i=0; i<openedListLength; i++)
  94.         {
  95.             tempPoint = (int2)openedList[i];
  96.             tempH = (float)heuristic(tempPoint, end);//(float)fScore[(int)(tempPoint.x + (width * tempPoint.y))]
  97.            
  98.             if ((currentH < 0.0f) || (currentH > tempH))
  99.             {
  100.                 currentH = tempH;
  101.                 currentPoint = tempPoint;
  102.             }
  103.         }
  104.        
  105.         openedListLength--;
  106.        
  107.         if (currentPoint.x == -1)
  108.         {
  109.             openedListLength = 0;
  110.             break;
  111.         }
  112.        
  113.         if ((end.x == currentPoint.x) && (end.y == currentPoint.y))
  114.         {
  115.             found = true;
  116.             break;
  117.         }
  118.        
  119.         closedList[closedListLength++] = (int2)currentPoint;
  120.        
  121.         stX = currentPoint.x - 1;
  122.         enX = currentPoint.x + 1;
  123.        
  124.         if (stX < 0)
  125.         {
  126.             stX = 0;
  127.         }
  128.         if(enX > (width - 1))
  129.         {
  130.             enX = width - 1;
  131.         }
  132.            
  133.         stY = currentPoint.y - 1;
  134.         enY = currentPoint.y + 1;
  135.        
  136.         if (stY < 0)
  137.         {
  138.             stY = 0;
  139.         }
  140.         if(enY > (height - 1))
  141.         {
  142.             enY = height - 1;
  143.         }
  144.  
  145.         for (x = stX; x <= enX; x++)
  146.         {
  147.             for (y = stY; y <= enY; y++)
  148.             {
  149.                 index = (int)(x + (width * y));
  150.            
  151.                 if (((x == currentPoint.x) && (y == currentPoint.y)) || (input[index]) == true)
  152.                 {
  153.                     continue;
  154.                 }
  155.  
  156.                 inClosedList = false;
  157.                 for (i=0; i<closedListLength; i++)
  158.                 {
  159.                     tempPoint = (int2)closedList[i];
  160.                    
  161.                     if ((tempPoint.x == x) && (tempPoint.y == y))
  162.                     {
  163.                         inClosedList = true;
  164.                         break;
  165.                     }
  166.                 }
  167.  
  168.                 if (inClosedList)
  169.                 {
  170.                     continue;
  171.                 }
  172.  
  173.                 inOpenedList = false;
  174.  
  175.                 for (i=0; i<openedListLength; i++)
  176.                 {
  177.                     tempPoint = (int2)openedList[i];
  178.  
  179.                     if ((tempPoint.x == x) && (tempPoint.y == y))
  180.                     {
  181.                         inOpenedList = true;
  182.                         break;
  183.                     }
  184.                 }
  185.                
  186.                 tempH = (float)(gScore[(int)(currentPoint.x + (currentPoint.y * width))] + heuristic(currentPoint, (int2)(x, y)));
  187.                
  188.                 if (!inOpenedList)
  189.                 {
  190.                     openedList[openedListLength++] = (int2)(x, y);
  191.                     betterPath = true;
  192.                 }
  193.                 else if (tempH < (float)gScore[index])
  194.                 {
  195.                     betterPath = true;
  196.                 }
  197.                 else
  198.                 {
  199.                     betterPath = false;
  200.                 }
  201.                
  202.                 if (betterPath)
  203.                 {
  204.                     cameFrom[index] = (int)(currentPoint.x + (currentPoint.y * width));
  205.                    
  206.                     gScore[index] = (float)tempH;
  207.                     tempH = (float)heuristic((int2)(x, y), end);
  208.                    
  209.                     hScore[index] = (float)tempH;
  210.                     fScore[index] = (float)gScore[index] + (float)hScore[index];
  211.                 }
  212.             }
  213.         }      
  214.     }
  215.    
  216.     if (found)
  217.     {
  218.         int currentNode = cameFrom[end.x + (end.y * width)];
  219.  
  220.         while (currentNode != -1)
  221.         {
  222.             if ((currentNode != (int)(start.x + (width * start.y))) && (currentNode != (int)(end.x + (width * end.y))))
  223.             {
  224.                 output[currentNode + ((width * height) * globalIdx)] = '*';
  225.             }
  226.            
  227.             currentNode = (int)cameFrom[currentNode];
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement