kmeans_clustering.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*****************************************************************************/
  2. /*IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. */
  3. /*By downloading, copying, installing or using the software you agree */
  4. /*to this license. If you do not agree to this license, do not download, */
  5. /*install, copy or use the software. */
  6. /* */
  7. /* */
  8. /*Copyright (c) 2005 Northwestern University */
  9. /*All rights reserved. */
  10. /*Redistribution of the software in source and binary forms, */
  11. /*with or without modification, is permitted provided that the */
  12. /*following conditions are met: */
  13. /* */
  14. /*1 Redistributions of source code must retain the above copyright */
  15. /* notice, this list of conditions and the following disclaimer. */
  16. /* */
  17. /*2 Redistributions in binary form must reproduce the above copyright */
  18. /* notice, this list of conditions and the following disclaimer in the */
  19. /* documentation and/or other materials provided with the distribution.*/
  20. /* */
  21. /*3 Neither the name of Northwestern University nor the names of its */
  22. /* contributors may be used to endorse or promote products derived */
  23. /* from this software without specific prior written permission. */
  24. /* */
  25. /*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS */
  26. /*IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
  27. /*TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT AND */
  28. /*FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL */
  29. /*NORTHWESTERN UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, */
  30. /*INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  31. /*(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR */
  32. /*SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
  33. /*HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
  34. /*STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN */
  35. /*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  36. /*POSSIBILITY OF SUCH DAMAGE. */
  37. /******************************************************************************/
  38. /*************************************************************************/
  39. /** File: kmeans_clustering.c **/
  40. /** Description: Implementation of regular k-means clustering **/
  41. /** algorithm **/
  42. /** Author: Wei-keng Liao **/
  43. /** ECE Department, Northwestern University **/
  44. /** email: wkliao@ece.northwestern.edu **/
  45. /** **/
  46. /** Edited by: Jay Pisharath **/
  47. /** Northwestern University. **/
  48. /** **/
  49. /** ================================================================ **/
  50. /** **/
  51. /** Edited by: Shuai Che, David Tarjan, Sang-Ha Lee **/
  52. /** University of Virginia **/
  53. /** **/
  54. /** Description: No longer supports fuzzy c-means clustering; **/
  55. /** only regular k-means clustering. **/
  56. /** No longer performs "validity" function to analyze **/
  57. /** compactness and separation crietria; instead **/
  58. /** calculate root mean squared error. **/
  59. /** **/
  60. /*************************************************************************/
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <float.h>
  64. #include <math.h>
  65. #include <omp.h>
  66. #include "kmeans.h"
  67. #define RANDOM_MAX 2147483647
  68. extern double wtime(void);
  69. /*----< kmeans_clustering() >---------------------------------------------*/
  70. float** kmeans_clustering(float **feature, /* in: [npoints][nfeatures] */
  71. int nfeatures,
  72. int npoints,
  73. int nclusters,
  74. float threshold,
  75. int *membership) /* out: [npoints] */
  76. {
  77. int i, j, n = 0; /* counters */
  78. int loop=0, temp;
  79. int *new_centers_len; /* [nclusters]: no. of points in each cluster */
  80. float delta; /* if the point moved */
  81. float **clusters; /* out: [nclusters][nfeatures] */
  82. float **new_centers; /* [nclusters][nfeatures] */
  83. int *initial; /* used to hold the index of points not yet selected
  84. prevents the "birthday problem" of dual selection (?)
  85. considered holding initial cluster indices, but changed due to
  86. possible, though unlikely, infinite loops */
  87. int initial_points;
  88. int c = 0;
  89. /* nclusters should never be > npoints
  90. that would guarantee a cluster without points */
  91. if (nclusters > npoints)
  92. nclusters = npoints;
  93. /* allocate space for and initialize returning variable clusters[] */
  94. clusters = (float**) malloc(nclusters * sizeof(float*));
  95. clusters[0] = (float*) malloc(nclusters * nfeatures * sizeof(float));
  96. for (i=1; i<nclusters; i++)
  97. clusters[i] = clusters[i-1] + nfeatures;
  98. /* initialize the random clusters */
  99. initial = (int *) malloc (npoints * sizeof(int));
  100. for (i = 0; i < npoints; i++)
  101. {
  102. initial[i] = i;
  103. }
  104. initial_points = npoints;
  105. /* randomly pick cluster centers */
  106. for (i=0; i<nclusters && initial_points >= 0; i++) {
  107. //n = (int)rand() % initial_points;
  108. for (j=0; j<nfeatures; j++)
  109. clusters[i][j] = feature[initial[n]][j]; // remapped
  110. /* swap the selected index to the end (not really necessary,
  111. could just move the end up) */
  112. temp = initial[n];
  113. initial[n] = initial[initial_points-1];
  114. initial[initial_points-1] = temp;
  115. initial_points--;
  116. n++;
  117. }
  118. /* initialize the membership to -1 for all */
  119. for (i=0; i < npoints; i++)
  120. membership[i] = -1;
  121. /* allocate space for and initialize new_centers_len and new_centers */
  122. new_centers_len = (int*) calloc(nclusters, sizeof(int));
  123. new_centers = (float**) malloc(nclusters * sizeof(float*));
  124. new_centers[0] = (float*) calloc(nclusters * nfeatures, sizeof(float));
  125. for (i=1; i<nclusters; i++)
  126. new_centers[i] = new_centers[i-1] + nfeatures;
  127. /* iterate until convergence */
  128. do {
  129. delta = 0.0;
  130. // CUDA
  131. delta = (float) kmeansOCL(feature, /* in: [npoints][nfeatures] */
  132. nfeatures, /* number of attributes for each point */
  133. npoints, /* number of data points */
  134. nclusters, /* number of clusters */
  135. membership, /* which cluster the point belongs to */
  136. clusters, /* out: [nclusters][nfeatures] */
  137. new_centers_len, /* out: number of points in each cluster */
  138. new_centers /* sum of points in each cluster */
  139. );
  140. /* replace old cluster centers with new_centers */
  141. /* CPU side of reduction */
  142. for (i=0; i<nclusters; i++) {
  143. for (j=0; j<nfeatures; j++) {
  144. if (new_centers_len[i] > 0)
  145. clusters[i][j] = new_centers[i][j] / new_centers_len[i]; /* take average i.e. sum/n */
  146. new_centers[i][j] = 0.0; /* set back to 0 */
  147. }
  148. new_centers_len[i] = 0; /* set back to 0 */
  149. }
  150. c++;
  151. } while ((delta > threshold) && (loop++ < 500)); /* makes sure loop terminates */
  152. printf("iterated %d times\n", c);
  153. free(new_centers[0]);
  154. free(new_centers);
  155. free(new_centers_len);
  156. return clusters;
  157. }