-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkmeansTest.c
382 lines (296 loc) · 9.48 KB
/
kmeansTest.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/********************************************************************/
/* Parallel KMeans using MPI project 2013 */
/* */
/* Implemented by Nikos Katirtzis (nikos912000) */
/********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "kmeans.h"
#include "cluster.h"
#include <sys/time.h>
#include <math.h>
#include <mpi.h>
#define max_iterations 50
#define threshold 0.001
void error_message()
{
char *help = "Error using kmeans: Three arguments required\n"
"First: number of elements\n"
"Second: number of attributes (dimensions)\n"
"Third: numder of clusters\n";
printf("%s", help);
}
void random_initialization(data_struct *data_in)
{
int i, j = 0;
int n = data_in->leading_dim;
int m = data_in->secondary_dim;
double *tmp_dataset = data_in->dataset;
unsigned int *tmp_Index = data_in->members;
//srand(time(NULL));
srand(0);
for (i = 0; i < m; i++)
{
tmp_Index[i] = 0;
for (j = 0; j < n; j++)
{
tmp_dataset[i * n + j] = (double) rand() / RAND_MAX;
}
}
}
void initialize_clusters(data_struct *data_in,data_struct *cluster_in)
{
int i, j, pick = 0;
int n = cluster_in->leading_dim;
int m = cluster_in->secondary_dim;
int Objects = data_in->secondary_dim;
double *tmp_Centroids = cluster_in->dataset;
double *tmp_dataset = data_in->dataset;
unsigned int *tmp_Sizes = data_in->members;
int step = Objects / m;
/*randomly pick initial cluster centers*/
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
tmp_Centroids[i * n + j] = tmp_dataset[pick * n + j];
}
pick += step;
}
}
void print(data_struct* data2print)
{
int i, j = 0;
int n = data2print->leading_dim;
int m = data2print->secondary_dim;
double *tmp_dataset = data2print->dataset;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%f ", tmp_dataset[i * n + j]);
}
printf("\n");
}
}
void save(data_struct* data2save, char *filename1, char *filename2)
{
int i, j = 0;
FILE *outfile;
int n = data2save->leading_dim;
int m = data2save->secondary_dim;
double *tmp_dataset = data2save->dataset;
unsigned int *tmp_members = data2save->members;
printf("Saving data to files: ");
printf("%s and %s \n", filename1, filename2);
/*===========Save to file 1===========*/
if((outfile=fopen(filename1, "wb")) == NULL)
{
printf("Can't open output file\n");
}
fwrite(tmp_dataset, sizeof(double), m * n, outfile);
fclose(outfile);
/*===========Save to file 2========*/
if ((outfile = fopen(filename2, "wb")) == NULL)
{
printf("Can't open output file\n");
}
fwrite(tmp_members, sizeof(unsigned int), m, outfile);
fclose(outfile);
}
void clean(data_struct* data1){
free(data1->dataset);
free(data1->members);
}
int main(int argc, char **argv)
{
int i, processors, rank;
struct timeval first, second, lapsed;
struct timezone tzp;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &processors);
if (argc<4 && rank == 0)
{
error_message();
return 0;
}
int numObjects = atoi(argv[1]);
int numAttributes = atoi(argv[2]);
int numClusters = atoi(argv[3]);
char *file1_0 = "centroids.bin";
char *file1_1 = "ClusterSize.bin";
char *file2_0 = "dataset.bin";
char *file2_1 = "Index.bin";
data_struct data_in;
data_struct clusters;
/*=======Memory Allocation=========*/
data_in.leading_dim = numAttributes;
data_in.secondary_dim = numObjects;
data_in.dataset = (double*)malloc(numObjects * numAttributes * sizeof(double));
data_in.members = (unsigned int*)malloc(numObjects * sizeof(unsigned int));
clusters.leading_dim = numAttributes;
clusters.secondary_dim = numClusters;
clusters.dataset = (double*)malloc(numClusters * numAttributes * sizeof(double));
clusters.members = (unsigned int*)malloc(numClusters * sizeof(unsigned int));
/*=============initialize ==========*/
random_initialization(&data_in);
if (rank == 0)
{
initialize_clusters(&data_in, &clusters);
}
MPI_Barrier(MPI_COMM_WORLD);
// Broadcasts a message from the process with rank "root" to all other processes of the communicator
MPI_Bcast(clusters.dataset, numClusters*numAttributes, MPI_DOUBLE, 0, MPI_COMM_WORLD);
// Create a struct for each process. Each process has its own dataset (a subdata of the initial one)
data_struct p_data;
p_data.leading_dim = numAttributes;
// The elements associated with each process are n/p
double n_split = numObjects / processors;
double p_objects = ceil(n_split);
int n_temp = p_objects * processors;
if (rank != 0)
{
p_data.secondary_dim = p_objects;
p_data.dataset = (double*)malloc(p_objects * numAttributes * sizeof(double));
p_data.members = (unsigned int*)malloc(p_objects * sizeof(unsigned int));
}
else
{
p_data.secondary_dim = p_objects + (numObjects - n_temp);
p_data.dataset = (double*)malloc(p_data.secondary_dim * numAttributes * sizeof(double));
p_data.members = (unsigned int*)malloc(p_data.secondary_dim * sizeof(unsigned int));
}
printf("\nI am process %d and my size is = %d", rank, p_data.secondary_dim);
// Here, every process creates a sub-dataset of its elements
for (i = 0; i < p_data.secondary_dim * p_data.leading_dim;i++)
{
p_data.dataset[i] = data_in.dataset[rank * p_data.secondary_dim * p_data.leading_dim+i];
}
// Get timestamp (only once!)
if (rank == 0)
{
gettimeofday(&first, &tzp);
}
/************************* Clustering *************************/
/***** Initializations *****/
int iter, j, k;
// SumOfDist is the sum of the old iteration
// new_SumOfDist is the sum of the current iteration
// The difference of them is compared with the threshold
double SumOfDist = 0, new_SumOfDist=0, temp_SumOfDist=0;
double* newCentroids;
int* temp_clusterSize;
unsigned int*temp_dataMembers;
// Temporary array for cluster sizes
// Used in reduction, as we can't use the same argument for send and receive
temp_clusterSize = (int*)malloc(numClusters * sizeof(int));
// Temporary array for data members -in which cluster they belong to- (used in reduction)
temp_dataMembers = (unsigned int*)malloc(numObjects * sizeof(unsigned int));
// Our new centroids (used in kmeans_process)
newCentroids = (double*)malloc(numAttributes * numClusters * sizeof(double));
// Initialize all cluster sizes to zero
for (i = 0; i < numClusters; i++)
{
temp_clusterSize[i] = 0;
}
// Make sure that all processes start clustering at the same time
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0)
{
printf("\n\nClastering is going to start!\n");
}
/***** Start clustering *****/
for (iter = 0; iter < max_iterations; iter++)
{
new_SumOfDist = 0;
temp_SumOfDist = 0;
for (i = 0; i < numClusters * numAttributes; i++)
{
newCentroids[i] = 0;
}
kmeans_process(&p_data, &clusters, newCentroids, &new_SumOfDist);
// "Pass" centroids to the struct
MPI_Allreduce(newCentroids, clusters.dataset, numClusters*numAttributes, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
// Each process has its own cluster sizes, so we have to sum them up!
// Actually, we could do that only once, at the end of clustering
MPI_Allreduce(clusters.members, temp_clusterSize, numClusters, MPI_UNSIGNED, MPI_SUM, MPI_COMM_WORLD);
// "Pass" cluster sizes to the struct
for (i = 0; i < numClusters; i++)
{
clusters.members[i] = temp_clusterSize[i];
}
// Update cluster centers
for (i = 0; i < numClusters; i++)
{
for (j = 0; j < numAttributes; j++)
{
clusters.dataset[i * numAttributes + j] /= (double) clusters.members[i];
}
}
// Calculate total sum from local sums of processes
MPI_Allreduce(&new_SumOfDist, &temp_SumOfDist, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
// If we have reached threshold, stop clustering
if (fabs(SumOfDist - temp_SumOfDist) < threshold)
{
break;
}
// Get the new sum
SumOfDist = temp_SumOfDist;
// And print it (once only)
if (rank == 0)
{
printf("\nSum of Distances of iteration %d: %f", iter + 1, SumOfDist);
}
}
// Free some space
free(newCentroids);
free(temp_clusterSize);
MPI_Barrier(MPI_COMM_WORLD);
/* We need some code for saving data members now*/
// Each process saves its own data in a temporary array
for (i = 0; i < p_data.secondary_dim; i++)
{
temp_dataMembers[rank * p_data.secondary_dim + i] = p_data.members[i];
}
// Merge the above data
MPI_Allreduce(temp_dataMembers, data_in.members, numObjects, MPI_UNSIGNED, MPI_SUM, MPI_COMM_WORLD);
free(temp_dataMembers);
// Wait for all processes to reach this point
MPI_Barrier(MPI_COMM_WORLD);
// We have finished!
// Print some stuff and save results in files
if (rank == 0)
{
printf("\n\nFinished after %d iterations\n", iter);
gettimeofday(&second, &tzp);
if (first.tv_usec > second.tv_usec)
{
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
printf("Time elapsed: %d.%06dsec\n\n", (int)lapsed.tv_sec, (int)lapsed.tv_usec);
printf("Cluster sizes\n");
for (i = 0; i < numClusters; i++)
{
printf("Cluster%d: %d\n", i,clusters.members[i]);
}
printf("\n");
/*========save data============*/
save(&clusters, file1_0, file1_1);
save(&data_in, file2_0, file2_1);
printf("\nSaved!\n");
}
/*============clean memory===========*/
clean(&p_data);
clean(&data_in);
clean(&clusters);
if (rank == 0)
{
printf("Program has finished!\n");
}
MPI_Finalize();
}