-
Notifications
You must be signed in to change notification settings - Fork 0
/
KMeans.c
263 lines (218 loc) · 7.94 KB
/
KMeans.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* KMeans algorithm with openmp and c
*/
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <jmorecfg.h>
int numPoints;
int numCenters;
int dimension;
int numIteration;
int bindThreads;
char *pointFile;
char *centerFile;
char *resultsFile;
double euclidean_distance(double *points1, double* points2, int offset1,
int offset2, int dim);
void addToSum(double *points, int pointOffset, double *sums, int nearestCenter, int dimension);
void resetToZero(double *array, int length);
void resetToZeroInt(int *array, int length);
int main(int argc, int **argv[]){
boolean PrintResults = TRUE;
int numproc, myid;
int ret = parse_args(argc,argv);
printf("######################################## Starting Kmeans ########################################\n");
clock_t start = clock(), diff;
int localNumPoints;
double *centers = malloc(sizeof(double) * numCenters * dimension);
int *centerCountsTotal = malloc((sizeof(int))*numCenters);
double *centerSumsTotal = malloc((sizeof(double))*numCenters*dimension);
resetToZero(centerSumsTotal,numCenters*dimension);
resetToZeroInt(centerCountsTotal,numCenters);
#pragma omp parallel private(myid)
{
numproc = omp_get_num_threads();
myid = omp_get_thread_num();
localNumPoints = numPoints/numproc;
double *localPoints = malloc(sizeof(double) * localNumPoints * dimension);
int *centerCounts = malloc((sizeof(int))*numCenters);
double *centerSums = malloc((sizeof(double))*numCenters*dimension);
resetToZero(centerSums,numCenters*dimension);
resetToZeroInt(centerCounts,numCenters);
if(myid == 0){
printf("Reading centers \n");
FILE *c = fopen(centerFile, "rb");
fread(centers, sizeof(double), numCenters * dimension, c);
fclose(c);
}
int startIdx = myid*localNumPoints;
FILE *f = fopen(pointFile, "rb");
printf("Reading points %d %d\n", myid, startIdx);
fseek(f, startIdx * dimension * sizeof(double), SEEK_SET);
fread(localPoints, sizeof(double), localNumPoints * dimension, f);
fclose(f);
while(numIteration > 0){
int i;
for(i = 0; i < localNumPoints; ++i){
int points_offset = i * dimension;
int nearest_center = find_nearest_center(localPoints, centers, numCenters,
dimension, points_offset);
++centerCounts[nearest_center];
addToSum(localPoints, points_offset, centerSums, nearest_center, dimension);
}
#pragma omp critical
{
int j;
for (j = 0; j < numCenters; ++j) {
centerCountsTotal[j] += centerCounts[j];
int k;
for (k = 0; k < dimension; ++k) {
centerSumsTotal[j*dimension + k] += centerSums[j*dimension + k];
}
}
}
#pragma omp barrier
if(myid == 0){
numIteration -= 1;
int j;
for (j = 0; j < numCenters; ++j) {
int k;
for (k = 0; k < dimension; ++k) {
centers[j*dimension + k] = centerSumsTotal[j*dimension + k]/centerCountsTotal[j];
}
}
resetToZero(centerSumsTotal,numCenters*dimension);
resetToZeroInt(centerCountsTotal,numCenters);
}
#pragma omp barrier
resetToZero(centerSums,numCenters*dimension);
resetToZeroInt(centerCounts,numCenters);
}
free(localPoints);
free(centerSums);
free(centerCounts);
}
diff = clock() - start;
int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds\n", msec/1000, msec%1000);
int i;
int sum = 0;
// for(i = 0; i < numCenters; ++i){
// printf("centers %d x y z value %f %f %f \n",i,centers[i*dimension],centers[i*dimension+1],centers[i*dimension+2]);
// }
free(centers);
free(centerCountsTotal);
free(centerSumsTotal);
printf("######################################## Ending Kmeans ########################################\n");
if(PrintResults == TRUE){
int localNumPoints = numPoints;
double *localPoints = malloc(sizeof(double) * numPoints * dimension);
int startIdx = 0;
FILE *fall = fopen(pointFile, "rb");
FILE *fout;
fout = fopen(resultsFile, "w+");
// printf("Reading points %d %d\n", myid, startIdx);
//fseek(fall, startIdx * dimension * sizeof(double), SEEK_SET);
fread(localPoints, sizeof(double), localNumPoints * dimension, fall);
fclose(fall);
for(i = 0; i < localNumPoints; ++i){
int points_offset = i * dimension;
int nearest_center = find_nearest_center(localPoints, centers, numCenters,
dimension, points_offset);
fprintf(fout, "%d %f %f %f %d %d\n",i, localPoints[i*dimension],localPoints[i*dimension + 1], localPoints[i*dimension + 2], nearest_center ,nearest_center);
}
free(localPoints);
fclose(fout);
}
return 0;
}
double euclidean_distance(double *points1, double* points2, int offset1,
int offset2, int dim) {
double d = 0.0;
double tmp;
int i;
for (i = 0; i < dim; ++i) {
tmp = points1[i + offset1] - points2[i + offset2];
d += tmp * tmp;
}
return sqrt(d);
}
void addToSum(double *points, int pointOffset, double *sums, int nearestCenter, int dimension){
int i;
for (i = 0; i < dimension; ++i) {
sums[nearestCenter*dimension + i] += points[pointOffset + i];
}
}
void resetToZero(double *array, int length) {
int i;
for (i = 0; i < length; ++i) {
array[i] = 0.0;
}
}
void resetToZeroInt(int *array, int length) {
int i;
for (i = 0; i < length; ++i) {
array[i] = 0;
}
}
int find_nearest_center(double *points, double *centers, int num_centers,
int dim, int points_offset) {
double min_dist = DBL_MAX;
int min_dist_idx = -1;
int i;
for (i = 0; i < num_centers; ++i) {
double dist = euclidean_distance(points, centers, points_offset,
i * dimension, dimension);
if (dist < min_dist) {
min_dist = dist;
min_dist_idx = i;
}
}
return min_dist_idx;
}
int parse_args(int argc, char **argv) {
int index;
int c;
opterr = 0;
while ((c = getopt(argc, argv, "n:d:k:m:o:c:p:b:v")) != -1)
switch (c) {
case 'n':
numPoints = atoi(optarg);
break;
case 'd':
dimension = atoi(optarg);
break;
case 'k':
numCenters = atoi(optarg);
break;
case 'm':
numIteration = atoi(optarg);
break;
case 'o':
resultsFile = optarg;
break;
case 'c':
centerFile = optarg;
break;
case 'p':
pointFile = optarg;
break;
case 'b':
bindThreads = atoi(optarg);
break;
default:
abort();
}
printf("Program Arguments\n");
printf(
" n = %d\n d = %d\n k = %d\n m = %d\n o = %s\n c = %s\n p = %s\n b = %d\n",
numPoints, dimension, numCenters, numIteration, resultsFile, centerFile, pointFile, bindThreads);
for (index = optind; index < argc; index++)
printf("Non-option argument %s\n", argv[index]);
return 0;
}