1 /* =============================================================================
4 * -- Implementation of normal k-means clustering algorithm
6 * =============================================================================
11 * ECE Department, Northwestern University
12 * email: wkliao@ece.northwestern.edu
18 * Northwestern University.
24 * University of California, Irvine
27 * =============================================================================
29 * For the license of bayes/sort.h and bayes/sort.c, please see the header
32 * ------------------------------------------------------------------------
34 * For the license of kmeans, please see kmeans/LICENSE.kmeans
36 * ------------------------------------------------------------------------
38 * For the license of ssca2, please see ssca2/COPYRIGHT
40 * ------------------------------------------------------------------------
42 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
43 * header of the files.
45 * ------------------------------------------------------------------------
47 * For the license of lib/rbtree.h and lib/rbtree.c, please see
48 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
50 * ------------------------------------------------------------------------
52 * Unless otherwise noted, the following license applies to STAMP files:
54 * Copyright (c) 2007, Stanford University
55 * All rights reserved.
57 * Redistribution and use in source and binary forms, with or without
58 * modification, are permitted provided that the following conditions are
61 * * Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
64 * * Redistributions in binary form must reproduce the above copyright
65 * notice, this list of conditions and the following disclaimer in
66 * the documentation and/or other materials provided with the
69 * * Neither the name of Stanford University nor the names of its
70 * contributors may be used to endorse or promote products derived
71 * from this software without specific prior written permission.
73 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
74 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
76 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
77 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
78 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
79 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
80 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
81 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
82 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
83 * THE POSSIBILITY OF SUCH DAMAGE.
85 * =============================================================================
95 /* =============================================================================
97 * =============================================================================
99 public static void work(int myId, GlobalArgs args) {
107 intwrapper[] new_centers_len;
108 float[][] new_centers;
111 feature = args.feature;
112 nfeatures = args.nfeatures;
113 npoints = args.npoints;
114 nclusters = args.nclusters;
115 membership = args.membership;
116 clusters = args.clusters;
117 new_centers_len = args.new_centers_len;
118 new_centers = args.new_centers;
122 int index, start, stop;
124 start = myId * CHUNK;
126 //System.out.println("myId= " + myId + " start= " + start + " npoints= " + npoints);
127 while (start < npoints) {
128 stop = (((start + CHUNK) < npoints) ? (start + CHUNK) : npoints);
129 for (int i = start; i < stop; i++) {
131 index = Common.common_findNearestPoint(feature[i],
136 // If membership changes, increase delta by 1.
137 // membership[i] cannot be changed by other threads
139 if (membership[i] != index) {
143 // Assign the membership to object i
144 // membership[i] can't be changed by other thread
145 membership[i] = index;
147 // Update new cluster centers : sum of objects located within
148 new_centers_len[index] = new_centers_len[index] + 1;
149 for (int j = 0; j < nfeatures; j++) {
150 new_centers[index][j] = new_centers[index][j] + feature[i][j];
156 if (start + CHUNK < npoints) {
158 start = args.global_i;
159 args.global_i = start + CHUNK;
167 args.global_delta = args.global_delta + delta;
171 /* =============================================================================
173 * =============================================================================
175 public float[][] normal_exec (
177 float[][] feature, /* in: [npoints][nfeatures] */
183 Random randomPtr, /* out: [npoints] */
187 float[][] clusters; /* out: [nclusters][nfeatures] */
190 /* Randomly pick cluster centers */
192 /* Allocate space for returning variable clusters[] */
193 clusters = global new float[nclusters][nfeatures];
195 for (int i = 0; i < nclusters; i++) {
196 int n = (int)(randomPtr.random_generate() % npoints);
197 for (int j = 0; j < nfeatures; j++) {
198 clusters[i][j] = feature[n][j];
201 for (int i = 0; i < npoints; i++) {
207 * Need to initialize new_centers_len and new_centers[0] to all 0.
208 * Allocate clusters on different cache lines to reduce false sharing.
210 intwrapper[] new_centers_len;
211 float[][] new_centers;
213 new_centers_len = global new intwrapper[nclusters];
214 new_centers = global new float[nclusters][nfeatures];
224 Barrier barr = new Barrier("128.195.136.162");
229 tmp_args.feature = feature;
230 tmp_args.clusters = clusters;
231 tmp_args.new_centers_len = new_centers_len;
232 tmp_args.new_centers = new_centers;
234 tmp_args.nfeatures = nfeatures;
235 tmp_args.npoints = npoints;
236 tmp_args.nclusters = nclusters;
237 tmp_args.membership = membership;
238 tmp_args.global_i = nthreads * CHUNK;
239 tmp_args.global_delta = delta;
242 //Work in parallel with other threads
243 thread_work(tmp_args, barr);
246 delta = tmp_args.global_delta;
248 // Replace old cluster centers with new_centers
249 for (int i = 0; i < nclusters; i++) {
250 for (int j = 0; j < nfeatures; j++) {
251 if (new_centers_len[i] > 0) {
252 clusters[i][j] = new_centers[i][j] / new_centers_len[i];
254 new_centers[i][j] = (float)0.0; // set back to 0
256 new_centers_len[i] = 0; // set back to 0
261 //System.out.println("delta= " + delta + " loop= " + loop + " threshold= " + threshold);
262 } while ((delta > threshold) && (loop++ < 500));
268 * Work done by primary thread in parallel with other threads
270 void thread_work(GlobalArgs args, Barrier barr) {
271 Barrier.enterBarrier(barr);
272 Normal.work(0, args); //threadId = 0 because primary thread
273 Barrier.enterBarrier(barr);
277 /* =============================================================================
281 * =============================================================================