particle_double.cl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #if defined(cl_amd_fp64) || defined(cl_khr_fp64)
  2. #if defined(cl_amd_fp64)
  3. #pragma OPENCL EXTENSION cl_amd_fp64 : enable
  4. #elif defined(cl_khr_fp64)
  5. #pragma OPENCL EXTENSION cl_khr_fp64 : enable
  6. #endif
  7. /** added this function. was missing in original double version.
  8. * Takes in a double and returns an integer that approximates to that double
  9. * @return if the mantissa < .5 => return value < input value; else return value > input value
  10. */
  11. double dev_round_double(double value) {
  12. int newValue = (int) (value);
  13. if (value - newValue < .5f)
  14. return newValue;
  15. else
  16. return newValue++;
  17. }
  18. /********************************
  19. * CALC LIKELIHOOD SUM
  20. * DETERMINES THE LIKELIHOOD SUM BASED ON THE FORMULA: SUM( (IK[IND] - 100)^2 - (IK[IND] - 228)^2)/ 100
  21. * param 1 I 3D matrix
  22. * param 2 current ind array
  23. * param 3 length of ind array
  24. * returns a double representing the sum
  25. ********************************/
  26. double calcLikelihoodSum(__global unsigned char * I, __global int * ind, int numOnes, int index){
  27. double likelihoodSum = 0.0;
  28. int x;
  29. for(x = 0; x < numOnes; x++)
  30. likelihoodSum += (pow((double)(I[ind[index*numOnes + x]] - 100),2) - pow((double)(I[ind[index*numOnes + x]]-228),2))/50.0;
  31. return likelihoodSum;
  32. }
  33. /****************************
  34. CDF CALCULATE
  35. CALCULATES CDF
  36. param1 CDF
  37. param2 weights
  38. param3 Nparticles
  39. *****************************/
  40. void cdfCalc(__global double * CDF, __global double * weights, int Nparticles){
  41. int x;
  42. CDF[0] = weights[0];
  43. for(x = 1; x < Nparticles; x++){
  44. CDF[x] = weights[x] + CDF[x-1];
  45. }
  46. }
  47. /*****************************
  48. * RANDU
  49. * GENERATES A UNIFORM DISTRIBUTION
  50. * returns a double representing a randomily generated number from a uniform distribution with range [0, 1)
  51. ******************************/
  52. double d_randu(__global int * seed, int index)
  53. {
  54. int M = INT_MAX;
  55. int A = 1103515245;
  56. int C = 12345;
  57. int num = A*seed[index] + C;
  58. seed[index] = num % M;
  59. return fabs(seed[index] / ((double) M));
  60. }
  61. /**
  62. * Generates a normally distributed random number using the Box-Muller transformation
  63. * @note This function is thread-safe
  64. * @param seed The seed array
  65. * @param index The specific index of the seed to be advanced
  66. * @return a double representing random number generated using the Box-Muller algorithm
  67. * @see http://en.wikipedia.org/wiki/Normal_distribution, section computing value for normal random distribution
  68. */
  69. double d_randn(__global int * seed, int index){
  70. //Box-Muller algortihm
  71. double pi = 3.14159265358979323846;
  72. double u = d_randu(seed, index);
  73. double v = d_randu(seed, index);
  74. double cosine = cos(2*pi*v);
  75. double rt = -2*log(u);
  76. return sqrt(rt)*cosine;
  77. }
  78. /****************************
  79. UPDATE WEIGHTS
  80. UPDATES WEIGHTS
  81. param1 weights
  82. param2 likelihood
  83. param3 Nparticles
  84. ****************************/
  85. double updateWeights(__global double * weights, __global double * likelihood, int Nparticles){
  86. int x;
  87. double sum = 0;
  88. for(x = 0; x < Nparticles; x++){
  89. weights[x] = weights[x] * exp(likelihood[x]);
  90. sum += weights[x];
  91. }
  92. return sum;
  93. }
  94. int findIndexBin(__global double * CDF, int beginIndex, int endIndex, double value)
  95. {
  96. if(endIndex < beginIndex)
  97. return -1;
  98. int middleIndex;
  99. while(endIndex > beginIndex)
  100. {
  101. middleIndex = beginIndex + ((endIndex-beginIndex)/2);
  102. if(CDF[middleIndex] >= value)
  103. {
  104. if(middleIndex == 0)
  105. return middleIndex;
  106. else if(CDF[middleIndex-1] < value)
  107. return middleIndex;
  108. else if(CDF[middleIndex-1] == value)
  109. {
  110. while(CDF[middleIndex] == value && middleIndex >= 0)
  111. middleIndex--;
  112. middleIndex++;
  113. return middleIndex;
  114. }
  115. }
  116. if(CDF[middleIndex] > value)
  117. endIndex = middleIndex-1;
  118. else
  119. beginIndex = middleIndex+1;
  120. }
  121. return -1;
  122. }
  123. /*****************************
  124. * CUDA Find Index Kernel Function to replace FindIndex
  125. * param1: arrayX
  126. * param2: arrayY
  127. * param3: CDF
  128. * param4: u
  129. * param5: xj
  130. * param6: yj
  131. * param7: weights
  132. * param8: Nparticles
  133. *****************************/
  134. __kernel void find_index_kernel(__global double * arrayX, __global double * arrayY,
  135. __global double * CDF, __global double * u, __global double * xj,
  136. __global double * yj, __global double * weights, int Nparticles
  137. ){
  138. int i = get_global_id(0);
  139. if(i < Nparticles){
  140. int index = -1;
  141. int x;
  142. for(x = 0; x < Nparticles; x++){
  143. if(CDF[x] >= u[i]){
  144. index = x;
  145. break;
  146. }
  147. }
  148. if(index == -1){
  149. index = Nparticles-1;
  150. }
  151. xj[i] = arrayX[index];
  152. yj[i] = arrayY[index];
  153. //weights[i] = 1 / ((double) (Nparticles)); //moved this code to the beginning of likelihood kernel
  154. }
  155. barrier(CLK_GLOBAL_MEM_FENCE);
  156. }
  157. __kernel void normalize_weights_kernel(__global double * weights, int Nparticles, __global double * partial_sums, __global double * CDF, __global double * u, __global int * seed)
  158. {
  159. int i = get_global_id(0);
  160. int local_id = get_local_id(0);
  161. __local double u1;
  162. __local double sumWeights;
  163. if(0 == local_id)
  164. sumWeights = partial_sums[0];
  165. barrier(CLK_LOCAL_MEM_FENCE);
  166. if(i < Nparticles) {
  167. weights[i] = weights[i]/sumWeights;
  168. }
  169. barrier(CLK_GLOBAL_MEM_FENCE);
  170. if(i == 0) {
  171. cdfCalc(CDF, weights, Nparticles);
  172. u[0] = (1/((double)(Nparticles))) * d_randu(seed, i); // do this to allow all threads in all blocks to use the same u1
  173. }
  174. barrier(CLK_GLOBAL_MEM_FENCE);
  175. if(0 == local_id)
  176. u1 = u[0];
  177. barrier(CLK_LOCAL_MEM_FENCE);
  178. if(i < Nparticles)
  179. {
  180. u[i] = u1 + i/((double)(Nparticles));
  181. }
  182. }
  183. __kernel void sum_kernel(__global double* partial_sums, int Nparticles)
  184. {
  185. int i = get_global_id(0);
  186. size_t THREADS_PER_BLOCK = get_local_size(0);
  187. if(i == 0)
  188. {
  189. int x;
  190. double sum = 0;
  191. int num_blocks = ceil((double) Nparticles / (double) THREADS_PER_BLOCK);
  192. for (x = 0; x < num_blocks; x++) {
  193. sum += partial_sums[x];
  194. }
  195. partial_sums[0] = sum;
  196. }
  197. }
  198. /*****************************
  199. * OpenCL Likelihood Kernel Function to replace FindIndex
  200. * param1: arrayX
  201. * param2: arrayY
  202. * param2.5: CDF
  203. * param3: ind
  204. * param4: objxy
  205. * param5: likelihood
  206. * param6: I
  207. * param6.5: u
  208. * param6.75: weights
  209. * param7: Nparticles
  210. * param8: countOnes
  211. * param9: max_size
  212. * param10: k
  213. * param11: IszY
  214. * param12: Nfr
  215. *****************************/
  216. __kernel void likelihood_kernel(__global double * arrayX, __global double * arrayY,__global double * xj, __global double * yj, __global double * CDF, __global int * ind, __global int * objxy, __global double * likelihood, __global unsigned char * I, __global double * u, __global double * weights, const int Nparticles, const int countOnes, const int max_size, int k, const int IszY, const int Nfr, __global int *seed, __global double * partial_sums, __local double* buffer){
  217. int block_id = get_group_id(0);
  218. int thread_id = get_local_id(0);
  219. int i = get_global_id(0);
  220. size_t THREADS_PER_BLOCK = get_local_size(0);
  221. int y;
  222. int indX, indY;
  223. if(i < Nparticles){
  224. arrayX[i] = xj[i];
  225. arrayY[i] = yj[i];
  226. weights[i] = 1 / ((double) (Nparticles)); //Donnie - moved this line from end of find_index_kernel to prevent all weights from being reset before calculating position on final iteration.
  227. arrayX[i] = arrayX[i] + 1.0 + 5.0*d_randn(seed, i);
  228. arrayY[i] = arrayY[i] - 2.0 + 2.0*d_randn(seed, i);
  229. }
  230. barrier(CLK_GLOBAL_MEM_FENCE);
  231. if(i < Nparticles)
  232. {
  233. for(y = 0; y < countOnes; y++){
  234. indX = dev_round_double(arrayX[i]) + objxy[y*2 + 1];
  235. indY = dev_round_double(arrayY[i]) + objxy[y*2];
  236. ind[i*countOnes + y] = abs(indX*IszY*Nfr + indY*Nfr + k);
  237. if(ind[i*countOnes + y] >= max_size)
  238. ind[i*countOnes + y] = 0;
  239. }
  240. likelihood[i] = calcLikelihoodSum(I, ind, countOnes, i);
  241. likelihood[i] = likelihood[i]/countOnes;
  242. weights[i] = weights[i] * exp(likelihood[i]); //Donnie Newell - added the missing exponential function call
  243. }
  244. buffer[thread_id] = 0.0; // DEBUG!!!!!!!!!!!!!!!!!!!!!!!!
  245. //buffer[thread_id] = i;
  246. barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
  247. if(i < Nparticles){
  248. buffer[thread_id] = weights[i];
  249. }
  250. barrier(CLK_LOCAL_MEM_FENCE);
  251. /* for some reason the get_local_size(0) call was not returning 512. */
  252. //for(unsigned int s=get_local_size(0)/2; s>0; s>>=1)
  253. for(unsigned int s=THREADS_PER_BLOCK/2; s>0; s>>=1)
  254. {
  255. if(thread_id < s)
  256. {
  257. buffer[thread_id] += buffer[thread_id + s];
  258. }
  259. barrier(CLK_LOCAL_MEM_FENCE);
  260. }
  261. if(thread_id == 0)
  262. {
  263. partial_sums[block_id] = buffer[0];
  264. }
  265. }//*/
  266. #endif