-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathk_means.c
275 lines (240 loc) · 6.67 KB
/
k_means.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
#include <stdio.h>
#include <math.h>
#include "util.h"
#include <string.h>
#include <mpi.h>
int mpirank, p;
int pN,d,C,N;
// from hpc2017 class started code
static int compare(const void *a, const void *b)
{
int *da = (int *)a;
int *db = (int *)b;
if (*da > *db)
return 1;
else if (*da < *db)
return -1;
else
return 0;
}
double **alloc_2d_double(int rows, int cols) {
// modified from http://stackoverflow.com/questions/5901476/sending-and-receiving-2d-array-over-mpi
int i;
double *data = (double *)malloc(rows*cols*sizeof(double));
double **array= (double **)malloc(rows*sizeof(double*));
for (i=0; i<rows; i++)
array[i] = &(data[cols*i]);
return array;
}
void printData(double **data,int row,int col){
int k,j;
for (k = 0; k < row ; k++)
{
for (j = 0; j<col; j++)
{
printf("%f ", data[k][j]);
}
printf("\n");
}
}
void readDataPartial(char *file_name,double **data){
int start = mpirank*pN;
int end = (mpirank+1)*pN;
//printf("Rank %d range, %d-%d\n",mpirank,start,end);
FILE *fp = fopen(file_name, "r");
const char s[1] = ",";
char *token;
int i;
int line_count=0;
int read_count=0;
if(fp != NULL)
{
char line[100];
while(fgets(line, sizeof line, fp) != NULL)
{
if (line_count>=start && line_count<end){
token = strtok(line, s);
for(i=0;i<d;i++)
{
data[read_count][i] = atof(token);
if(i<d-1)
{
token = strtok(NULL,s);
}
}
read_count++;
}
line_count++;
}
fclose(fp);
} else {
perror(file_name);
}
}
void getInitialClustersFromData(char *file_name,double **centers){
int indices[C];
FILE *fp = fopen(file_name, "r");
const char s[1] = ",";
char *token;
int i;
int line_count=0;
int c_i = 0;
for (i = 0; i < C ; i++){
indices[i]=rand() % N;
// printf("C%d is %d\n",i,indices[i]);
}
qsort(indices, C, sizeof(int), compare);
if(fp != NULL)
{
char line[100];
while(fgets(line, sizeof line, fp) != NULL)
{
if (line_count==indices[c_i]){
printf("Center %d from line %d\n",c_i,line_count);
token = strtok(line, s);
for(i=0;i<d;i++)
{
centers[c_i][i] = atof(token);
if(i<d-1)
{
token = strtok(NULL,s);
}
}
c_i++;
if (c_i == C){
break;
}
//simple duplicate index check .TODO make this at the beginning
else if (indices[c_i]==indices[c_i-1]){
indices[c_i]+=1;
}
}
line_count++;
}
fclose(fp);
} else {
perror(file_name);
}
}
int main(int argc, char * argv[])
{
// GLOBAL int mpirank, p;
// GLOBAL int pN,d,C,N;
int i,j,k,l;
char f_name[100];
int N_ITER;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* get name of host running MPI process */
char processor_name[MPI_MAX_PROCESSOR_NAME];
int name_len;
MPI_Get_processor_name(processor_name, &name_len);
//printf("Rank %d/%d running on %s.\n", mpirank, p, processor_name);
if (argc != 6 && mpirank == 0 ) {
fprintf(stderr, "Usage: ./k_means dim N C data n_iter\n");
fprintf(stderr, "dim: data dimension\n");
fprintf(stderr, "N: # data points\n");
fprintf(stderr, "C: number of clusters\n");
fprintf(stderr, "data: data file path\n");
fprintf(stderr, "n_iter: number of iterations\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
//READ arguments
sscanf(argv[1], "%d", &d);
sscanf(argv[2], "%d", &N);
sscanf(argv[3], "%d", &C);
sscanf(argv[5], "%d", &N_ITER);
if (((N % p)!=0) && mpirank == 0 ) {
printf("N: %d\n", N);
printf("p: %d\n", p);
printf("Exiting. N must be a power of p. Accepting this is trivial, but not implemented/tested.\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
pN = N / p;
strcpy(f_name, argv[4]);
if (mpirank==0) printf("%dd data,Total: %d points,%d points per process from file %s\n",d,N,pN,f_name);
double **centers;
double **class_data_sum;
int local_class_residual_count[C];
int global_class_residual_count[C];
centers = alloc_2d_double(C,d);
class_data_sum = alloc_2d_double(C,d);
if (mpirank==0){
getInitialClustersFromData(f_name,centers);
}
MPI_Bcast(&(centers[0][0]), C*d, MPI_DOUBLE,0,MPI_COMM_WORLD);
double min_res,c_res;
int min_i;
double **data;
data = alloc_2d_double(pN,d);
readDataPartial(f_name,data);
/* timing */
double start, end;
MPI_Barrier(MPI_COMM_WORLD);
start = MPI_Wtime();
for (i=0; i < N_ITER; i++){
//For each turn zero the sums
for (k=0; k<C; k++){
for (j=0;j<d;j++){
class_data_sum[k][j]=0;
}
local_class_residual_count[k]=0;
}
//For each data point get the clusters cluster center and add the residual
for (j=0; j< pN; j++ ){
min_res = INFINITY;
min_i = -1;
for (k=0; k<C; k++){
c_res=0.0;
//norm
for (l=0;l<d;l++){
c_res += pow(abs(data[j][l]-centers[k][l]),2);
}
c_res = sqrt(c_res);
if (c_res<min_res){
min_i = k;
min_res = c_res;
}
}
for (l=0; l<d; l++){
class_data_sum[min_i][l] += data[j][l];
}
local_class_residual_count[min_i] += 1;
}
//Share the sums of points and the countrs
MPI_Allreduce(&(class_data_sum[0][0]),&(centers[0][0]), C*d, MPI_DOUBLE, MPI_SUM,
MPI_COMM_WORLD);
MPI_Allreduce(local_class_residual_count,global_class_residual_count, C, MPI_INT, MPI_SUM,
MPI_COMM_WORLD);
for (k=0; k<C; k++){
for (l=0;l<d;l++){
centers[k][l] /= global_class_residual_count[k];
}
}
// if (mpirank==1){
// printf("rank1: centers\n");
// printData(centers,C,d);
// }
}
// if (mpirank==1){
// printf("rank1:\n");
// printData(centers,C,d);
// }
if (mpirank==0){
printf("centers:\n");
printData(centers,C,d);
}
free(data[0]);
free(data);
free(centers[0]);
free(centers);
/* timing */
MPI_Barrier(MPI_COMM_WORLD);
end = MPI_Wtime();
if (0 == mpirank) {
printf("Time elapsed is %f seconds.\n", end-start);
}
MPI_Finalize();
return 0;
}