-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_mp_mpi_4.c
365 lines (313 loc) · 10.9 KB
/
app_mp_mpi_4.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
#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];
int n_threads = argv[8];
//Creating data structures
//Partial array of points
double *point_x = malloc(sizeof(double) * n_points / world_size);
double *point_y = malloc(sizeof(double) * n_points / world_size);
//Total array of points
double *p_x = malloc(sizeof(double) * n_points);
double *p_y = malloc(sizeof(double) * n_points);
//Total array of centroids
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);
}
//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
//Total array of the new centroids
double *new_centroid_x = malloc(n_centroids * sizeof(double));
double *new_centroid_y = malloc(n_centroids * sizeof(double));
//Counts the number of points associated to the new centroid
int *new_centroids_n_points = malloc(n_centroids * sizeof(int));
//For every points saves which centroid is the nearest
int *assign_centroid = malloc(n_points* sizeof(int));
//Partial array of new centroids
double *local_new_centroid_x = malloc(n_centroids/world_size * sizeof(double));
double *local_new_centroid_y = malloc(n_centroids/world_size * sizeof(double));
//Counts the number of points associated to the new local centroid
int *local_new_centroids_n_points = malloc(n_centroids/world_size * sizeof(int));
//Indicates for each node how many centroid it will receive
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;
//Initialize the Total new centroids array to 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;
}
//Initialize the Local new centroids array to 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 reduction(+: new_centroid_x[:n_centroids],new_centroid_y[:n_centroids], new_centroids_n_points[:n_centroids])
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
//{
//Sum the coordinates of the points asssociated to the closest_centroid
//(the new centroid will be calculated later by averaging this two arrays with the number of points associated to the closest_centroid)
new_centroid_x[closest_centroid] += point_x[i];
new_centroid_y[closest_centroid] += point_y[i];
//Count the points associated to the closest centroid
new_centroids_n_points[closest_centroid]++;
// }
}
//Reduce the partial computation.
//Scatter all new centroids across all nodes.
//Reduce all the new_centroid arrays into one and then scatter again across all nodes
//At the first iteration each node has a partition of the points and the full set of the centroids.
//In the other iterations each node will have a partition of the points and a partiton of the centroids.
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);
//offset indicates which portion of the new_centroid arrays is assigned to the current node.
//i.e the centroids are 20
//Node 1: new_centroids_*[0:9]
//Node 2: new_centroids_*[10:19]
int offset = world_rank * (n_centroids / world_size);
#pragma omp parallel for reduction(+: curr_error)
//Iterate over the partition of the new_centroids
for (int i = 0; i < n_centroids/world_size; i++)
{
if (local_new_centroids_n_points[i] != 0)
{
//Averaging the Local new centroids
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];
//Calculate the local distance from the old one
//centroid_* : Total array of centroids
//local_new_centroids_* : Local array of the new centroids
curr_error += calc_distance(centroid_x[offset + i], centroid_y[offset + i], local_new_centroid_x[i], local_new_centroid_y[i]);
} else {
//If no points are associated to the centroid, it will write the old value of the centroid
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 and send the total array to all nodes
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);
}