kmeans.cl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #ifndef FLT_MAX
  2. #define FLT_MAX 3.40282347e+38
  3. #endif
  4. __kernel void
  5. kmeans_kernel_c(__global float *feature,
  6. __global float *clusters,
  7. __global int *membership,
  8. int npoints,
  9. int nclusters,
  10. int nfeatures,
  11. int offset,
  12. int size
  13. )
  14. {
  15. unsigned int point_id = get_global_id(0);
  16. int index = 0;
  17. //const unsigned int point_id = get_global_id(0);
  18. if (point_id < npoints)
  19. {
  20. float min_dist=FLT_MAX;
  21. for (int i=0; i < nclusters; i++) {
  22. float dist = 0;
  23. float ans = 0;
  24. for (int l=0; l<nfeatures; l++){
  25. ans += (feature[l * npoints + point_id]-clusters[i*nfeatures+l])*
  26. (feature[l * npoints + point_id]-clusters[i*nfeatures+l]);
  27. }
  28. dist = ans;
  29. if (dist < min_dist) {
  30. min_dist = dist;
  31. index = i;
  32. }
  33. }
  34. //printf("%d\n", index);
  35. membership[point_id] = index;
  36. }
  37. return;
  38. }
  39. __kernel void
  40. kmeans_swap(__global float *feature,
  41. __global float *feature_swap,
  42. int npoints,
  43. int nfeatures
  44. ){
  45. unsigned int tid = get_global_id(0);
  46. //for(int i = 0; i < nfeatures; i++)
  47. // feature_swap[i * npoints + tid] = feature[tid * nfeatures + i];
  48. //Lingjie Zhang modificated at 11/05/2015
  49. if (tid < npoints){
  50. for(int i = 0; i < nfeatures; i++)
  51. feature_swap[i * npoints + tid] = feature[tid * nfeatures + i];
  52. }
  53. // end of Lingjie Zhang's modification
  54. }