-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_mp_mpi_3.c
347 lines (302 loc) · 9.57 KB
/
app_mp_mpi_3.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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <sys/time.h>
#include <mpi.h>
#define BUFFER_SIZE 200
#define INVALID -1
#define MASTER 0
typedef struct
{
double x;
double y;
} point;
void *read_points(char *filepath, int n, double* x, double* y);
double calc_distance(double p1_x, double p1_y, double p2_x, double p2_y);
void write_new_centroids(double x[], double y[], int n_centroids, char *output);
void print_point(point *p);
/*
Arguments:
- Points file path.
- Number of points.
- Centroids file path.
- Number of centroids.
- Type of distance:
- 0, Manhattan
- 1, Euclidean
- 2, Euclidean no SQRT
- Error.
- Output file.
*/
int main(int argc, char *argv[])
{
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
//MPI Initiliazation
MPI_Init(NULL, NULL);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int done = 0;
//Parameters
int n_points = atoi(argv[2]);
int n_centroids = atoi(argv[4]);
int dist_algo = atoi(argv[5]);
double error = atof(argv[6]);
char *output_file = argv[7];
//Creating data structures
double *point_x = malloc(sizeof(double) * n_points / world_size);
double *point_y = malloc(sizeof(double) * n_points / world_size);
double *p_x = malloc(sizeof(double) * n_points);
double *p_y = malloc(sizeof(double) * n_points);
double *centroid_x = malloc(sizeof(double) * n_centroids);
double *centroid_y = malloc(sizeof(double) * n_centroids);
//Reading data
if (world_rank == MASTER)
{
read_points(argv[1], n_points, p_x, p_y);
read_points(argv[3], n_centroids, centroid_x, centroid_y);
/*
#pragma omp parallel for
for (int i = 0; i < n_points; i++)
{
p_x[i] = points[i].x;
p_y[i] = points[i].y;
}
#pragma omp parallel for
for (int i = 0; i < n_centroids; i++)
{
centroid_x[i] = centroids[i].x;
centroid_y[i] = centroids[i].y;
}
*/
}
//Scattering points
//Splits the points across all nodes
MPI_Scatter(
p_x,
n_points / world_size,
MPI_DOUBLE,
point_x,
n_points / world_size,
MPI_DOUBLE,
MASTER,
MPI_COMM_WORLD);
MPI_Scatter(
p_y,
n_points / world_size,
MPI_DOUBLE,
point_y,
n_points / world_size,
MPI_DOUBLE,
MASTER,
MPI_COMM_WORLD);
//Support data structures
double *new_centroid_x = malloc(n_centroids * sizeof(double));
double *new_centroid_y = malloc(n_centroids * sizeof(double));
int *assign_centroid = malloc(n_points* sizeof(int));
int *new_centroids_n_points = malloc(n_centroids * sizeof(int));
int *global_new_centroids_n_points = malloc(n_centroids * sizeof(int));
double *local_new_centroid_x = malloc(n_centroids/world_size * sizeof(double));
double *local_new_centroid_y = malloc(n_centroids/world_size * sizeof(double));
int *local_new_centroids_n_points = malloc(n_centroids/world_size * sizeof(int));
double *global_new_centroid_x = malloc(n_centroids * sizeof(double));
double *global_new_centroid_y = malloc(n_centroids * sizeof(double));
int *recvcounts = malloc(world_size * sizeof(int));
double curr_error;
//Initialization of the recv buffer
//It stands for how many centroids each node receives
for(int i = 0; i < world_size; i++) {
recvcounts[i] = n_centroids / world_size;
}
//Start computation
int iteration = 0;
//Broadcasting centroids
//All nodes will know the centroids
MPI_Bcast(
centroid_x,
n_centroids,
MPI_DOUBLE,
MASTER,
MPI_COMM_WORLD);
MPI_Bcast(
centroid_y,
n_centroids,
MPI_DOUBLE,
MASTER,
MPI_COMM_WORLD);
do
{
//Resetting support data structures
curr_error = 0;
#pragma omp parallel for
for (int i = 0; i < n_centroids; i++)
{
new_centroid_x[i] = 0;
new_centroid_y[i] = 0;
new_centroids_n_points[i] = 0;
}
#pragma omp parallel for
for (int i = 0; i < n_centroids / world_size; i++)
{
local_new_centroid_x[i] = 0;
local_new_centroid_y[i] = 0;
local_new_centroids_n_points[i] = 0;
}
//For each point, look for the closest centroid and
//assing the point to the centroid.
#pragma omp parallel for
for (int i = 0; i < n_points / world_size; i++)
{
double min_distance = INVALID;
int closest_centroid = INVALID;
for (int j = 0; j < n_centroids; j++)
{
double distance = calc_distance(point_x[i], point_y[i], centroid_x[j], centroid_y[j]);
if (distance < min_distance || min_distance == INVALID)
{
min_distance = distance;
closest_centroid = j;
}
}
//Increment the partial record.
//It will be averaged.
#pragma omp critical
{
new_centroid_x[closest_centroid] += point_x[i];
new_centroid_y[closest_centroid] += point_y[i];
new_centroids_n_points[closest_centroid]++;
}
}
//Reduce the partial computation.
//Scatter all new centroids across all nodes.
MPI_Reduce_scatter(
new_centroid_x,
local_new_centroid_x,
recvcounts,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
MPI_Reduce_scatter(
new_centroid_y,
local_new_centroid_y,
recvcounts,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD);
MPI_Reduce_scatter(
new_centroids_n_points,
local_new_centroids_n_points,
recvcounts,
MPI_INT,
MPI_SUM,
MPI_COMM_WORLD);
int offset = world_rank * (n_centroids / world_size);
#pragma omp parallel for reduction(+: curr_error)
for (int i = 0; i < n_centroids/world_size; i++)
{
if (local_new_centroids_n_points[i] != 0)
{
local_new_centroid_x[i] = local_new_centroid_x[i] / local_new_centroids_n_points[i];
local_new_centroid_y[i] = local_new_centroid_y[i] / local_new_centroids_n_points[i];
curr_error += calc_distance(centroid_x[offset + i], centroid_y[offset + i], local_new_centroid_x[i], local_new_centroid_y[i]);
} else {
local_new_centroid_x[i] = centroid_x[offset+i];
local_new_centroid_y[i] = centroid_y[offset+i];
}
}
//Sum all curr_error
//Broadcast the result to all node.
MPI_Allreduce(
&curr_error,
&curr_error,
1,
MPI_DOUBLE,
MPI_SUM,
MPI_COMM_WORLD
);
//Gather all the centroids to all node.
MPI_Allgather(
local_new_centroid_x,
n_centroids / world_size,
MPI_DOUBLE,
centroid_x,
n_centroids / world_size,
MPI_DOUBLE,
MPI_COMM_WORLD
);
MPI_Allgather(
local_new_centroid_y,
n_centroids / world_size,
MPI_DOUBLE,
centroid_y,
n_centroids / world_size,
MPI_DOUBLE,
MPI_COMM_WORLD
);
//Break if the computation reached enough precision
if (curr_error < error)
{
break;
}
iteration++;
if(world_rank == MASTER)
printf("Current error : %lf -- Curr iteration : %d\n", curr_error, iteration);
} while (done == 0);
gettimeofday(&end_time, NULL);
MPI_Finalize();
printf("Total number of iterations: %d\n", iteration);
printf("Time elapsed is %lu\n", end_time.tv_sec - start_time.tv_sec);
//Write the centroids to file
if(world_rank == MASTER){
//write_new_centroids(centroid_x, centroid_y, n_centroids, output_file);
}
return 0;
}
void write_new_centroids(double x[], double y[], int n_centroids, char *output) {
int i;
FILE *fp;
fp = fopen(output, "w");
if(fp == NULL) {
perror("Error while opening the file\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < n_centroids; i++) {
printf("x %lf y %lf iteration i %d\n", x[i], y[i], i);
fprintf(fp, "%lf %lf\n", x[i],y[i]);
}
fclose(fp);
}
void *read_points(char *filepath, int n, double* x, double* y)
{
int i;
FILE *fp;
fp = fopen(filepath, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
for ( i = 0; i < n; i++)
{
char string[BUFFER_SIZE];
fgets(string, BUFFER_SIZE, fp);
sscanf(string, "%lf %lf", &(x[i]), &(y[i]));
//print_point(&points[i]);
}
fclose(fp);
}
inline double calc_distance(double p1_x, double p1_y, double p2_x, double p2_y)
{
double delta_x = p1_x - p2_x;
double delta_y = p1_y - p2_y;
return sqrt((delta_x) * (delta_x) + (delta_y) * (delta_y));
}
void print_point(point *p)
{
printf("Printing point\n");
printf(" -- X coordinate : %lf\n", p->x);
printf(" -- Y coordinate : %lf\n", p->y);
}