-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_imp.c
442 lines (432 loc) · 13.7 KB
/
mpi_imp.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#define min(a, b) ((a) + ((b) < (a) ? (b) - (a) : 0))
void read_file(char *file_name, int *width, int *height, int *maxval, int *type,
unsigned char ***red_buffer, unsigned char ***green_buffer, unsigned char ***blue_buffer)
{
*width = 0;
*height = 0;
*maxval = 0;
int i, j;
unsigned char c;
FILE *input_file = fopen(file_name, "rb");
fseek(input_file, 1, SEEK_CUR);
fread(&c, sizeof(char), 1, input_file);
if (c == '5')
*type = 0;
else
*type = 1;
fseek(input_file, 1, SEEK_CUR);
fread(&c, sizeof(char), 1, input_file);
while (c != '\n')
{
fread(&c, sizeof(char), 1, input_file);
}
fread(&c, sizeof(char), 1, input_file);
while (c != ' ')
{
*width = *width * 10 + (c - '0');
fread(&c, sizeof(char), 1, input_file);
}
fread(&c, sizeof(char), 1, input_file);
while (c != '\n')
{
*height = *height * 10 + (c - '0');
fread(&c, sizeof(char), 1, input_file);
}
fread(&c, sizeof(char), 1, input_file);
while (c != '\n')
{
*maxval = *maxval * 10 + (c - '0');
fread(&c, sizeof(char), 1, input_file);
}
*red_buffer = malloc((*height + 2) * sizeof(char *));
*green_buffer = malloc((*height + 2) * sizeof(char *));
*blue_buffer = malloc((*height + 2) * sizeof(char *));
for (i = 0; i < *height + 2; i++)
{
(*red_buffer)[i] = malloc((*width + 2) * sizeof(char));
(*green_buffer)[i] = malloc((*width + 2) * sizeof(char));
(*blue_buffer)[i] = malloc((*width + 2) * sizeof(char));
}
for (i = 0; i < *width + 2; i++)
{
(*red_buffer)[0][i] = 0;
(*green_buffer)[0][i] = 0;
(*blue_buffer)[0][i] = 0;
(*red_buffer)[*height + 1][i] = 0;
(*green_buffer)[*height + 1][i] = 0;
(*blue_buffer)[*height + 1][i] = 0;
}
for (i = 0; i < *height + 2; i++)
{
(*red_buffer)[i][0] = 0;
(*green_buffer)[i][0] = 0;
(*blue_buffer)[i][0] = 0;
(*red_buffer)[i][*width + 1] = 0;
(*green_buffer)[i][*width + 1] = 0;
(*blue_buffer)[i][*width + 1] = 0;
}
if (*type == 0)
{
for (i = 1; i <= *height; i++)
{
for (j = 1; j <= *width; j++)
{
fread(&((*red_buffer)[i][j]), sizeof(char), 1, input_file);
}
}
}
else if (*type == 1)
{
for (i = 1; i <= *height; i++)
{
for (j = 1; j <= *width; j++)
{
fread(&((*red_buffer)[i][j]), sizeof(char), 1, input_file);
fread(&((*green_buffer)[i][j]), sizeof(char), 1, input_file);
fread(&((*blue_buffer)[i][j]), sizeof(char), 1, input_file);
}
}
}
}
int num_digits(int n)
{
int res = 1;
while (n > 0)
{
res *= 10;
n /= 10;
}
return res / 10;
}
void write_file(char *file_name, int width, int height, int maxval, int type, unsigned char **red_buffer,
unsigned char **green_buffer, unsigned char **blue_buffer)
{
int i, j;
FILE *output_file = fopen(file_name, "wb");
unsigned char c;
c = 'P';
fwrite(&c, sizeof(char), 1, output_file);
if (type == 0)
c = '5';
else
c = '6';
fwrite(&c, sizeof(char), 1, output_file);
c = '\n';
fwrite(&c, sizeof(char), 1, output_file);
int n = num_digits(width);
while (n > 0)
{
c = '0' + width / n % 10;
fwrite(&c, sizeof(char), 1, output_file);
n /= 10;
}
c = ' ';
fwrite(&c, sizeof(char), 1, output_file);
n = num_digits(height);
while (n > 0)
{
c = '0' + height / n % 10;
fwrite(&c, sizeof(char), 1, output_file);
n /= 10;
}
c = '\n';
fwrite(&c, sizeof(char), 1, output_file);
n = num_digits(maxval);
while (n > 0)
{
c = '0' + maxval / n % 10;
fwrite(&c, sizeof(char), 1, output_file);
n /= 10;
}
c = '\n';
fwrite(&c, sizeof(char), 1, output_file);
if (type == 0)
{
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
{
fwrite(&red_buffer[i][j], sizeof(char), 1, output_file);
}
}
}
else if (type == 1)
{
for (i = 1; i <= height; i++)
{
for (j = 1; j <= width; j++)
{
fwrite(&red_buffer[i][j], sizeof(char), 1, output_file);
fwrite(&green_buffer[i][j], sizeof(char), 1, output_file);
fwrite(&blue_buffer[i][j], sizeof(char), 1, output_file);
}
}
}
}
unsigned char compute_filter(unsigned char i11, unsigned char i12, unsigned char i13,
unsigned char i21, unsigned char i22, unsigned char i23, unsigned char i31,
unsigned char i32, unsigned char i33)
{
unsigned char min_value;
min_value = min(i11, i12);
min_value = min(min_value, i13);
min_value = min(min_value, i21);
min_value = min(min_value, i22);
min_value = min(min_value, i23);
min_value = min(min_value, i31);
min_value = min(min_value, i32);
min_value = min(min_value, i33);
return min_value;
}
int main(int argc, char *argv[])
{
struct timeval start_time, end_time;
double elapsed;
int rank;
int nProcesses;
int i;
int type, width, height, maxval, nr_filters = 1;
if (argc != 3) {
printf("Not enough arguments! Usage: mpirun -np no_of_processes ./mpi file_in_name file_out_name\n");
return -1;
}
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
unsigned char **red_buffer, **green_buffer, **blue_buffer;
if (rank == 0)
{
read_file(argv[1], &width, &height, &maxval, &type, &red_buffer, &green_buffer, &blue_buffer);
for (i = 1; i < nProcesses; i++)
{
MPI_Send(&width, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&height, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
MPI_Send(&type, 1, MPI_INT, i, 0, MPI_COMM_WORLD);
}
}
else
{
MPI_Recv(&width, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&height, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(&type, 1, MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
float div = ((float)height) / nProcesses;
int start = rank * ceil(div) + 1;
int finish = fmin(height, (rank + 1) * (ceil(div)));
int expected_lines = finish - start + 3;
unsigned char **old_rb, **old_gb, **old_bb;
unsigned char **new_rb, **new_gb, **new_bb;
old_rb = malloc(expected_lines * sizeof(unsigned char *));
old_gb = malloc(expected_lines * sizeof(unsigned char *));
old_bb = malloc(expected_lines * sizeof(unsigned char *));
new_rb = malloc(expected_lines * sizeof(unsigned char *));
new_gb = malloc(expected_lines * sizeof(unsigned char *));
new_bb = malloc(expected_lines * sizeof(unsigned char *));
for (i = 0; i < expected_lines; i++)
{
old_rb[i] = malloc((width + 2) * sizeof(unsigned char));
old_gb[i] = malloc((width + 2) * sizeof(unsigned char));
old_bb[i] = malloc((width + 2) * sizeof(unsigned char));
new_rb[i] = malloc((width + 2) * sizeof(unsigned char));
new_gb[i] = malloc((width + 2) * sizeof(unsigned char));
new_bb[i] = malloc((width + 2) * sizeof(unsigned char));
}
if (rank == 0)
{
int proc;
gettimeofday(&start_time, 0);
for (proc = 1; proc < nProcesses; proc++)
{
int first = proc * ceil(div) + 1;
int last = fmin(height, (proc + 1) * (ceil(div)));
int line;
for (line = first - 1; line <= last + 1; line++)
{
MPI_Send(red_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(green_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, 0, MPI_COMM_WORLD);
MPI_Send(blue_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, 0, MPI_COMM_WORLD);
}
}
}
int line;
for (line = 0; line < expected_lines; line++)
{
memcpy(old_rb[line], red_buffer[line], width + 2);
if (type == 1)
{
memcpy(old_gb[line], green_buffer[line], width + 2);
memcpy(old_bb[line], blue_buffer[line], width + 2);
}
}
}
else
{
int line;
for (line = 0; line < expected_lines; line++)
{
MPI_Recv(old_rb[line], width + 2, MPI_UNSIGNED_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(old_gb[line], width + 2, MPI_UNSIGNED_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(old_bb[line], width + 2, MPI_UNSIGNED_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
}
int index;
for (index = 0; index < nr_filters; index++)
{
int line, collumn;
for (line = 1; line <= expected_lines - 2; line++)
{
for (collumn = 1; collumn <= width; collumn++)
{
new_rb[line][collumn] = compute_filter(old_rb[line - 1][collumn - 1], old_rb[line - 1][collumn], old_rb[line - 1][collumn + 1],
old_rb[line][collumn - 1], old_rb[line][collumn], old_rb[line][collumn + 1], old_rb[line + 1][collumn - 1],
old_rb[line + 1][collumn], old_rb[line + 1][collumn + 1]);
if (type == 1)
{
new_gb[line][collumn] = compute_filter(old_gb[line - 1][collumn - 1], old_gb[line - 1][collumn], old_gb[line - 1][collumn + 1],
old_gb[line][collumn - 1], old_gb[line][collumn], old_gb[line][collumn + 1], old_gb[line + 1][collumn - 1],
old_gb[line + 1][collumn], old_gb[line + 1][collumn + 1]);
new_bb[line][collumn] = compute_filter(old_bb[line - 1][collumn - 1], old_bb[line - 1][collumn], old_bb[line - 1][collumn + 1],
old_bb[line][collumn - 1], old_bb[line][collumn], old_bb[line][collumn + 1], old_bb[line + 1][collumn - 1],
old_bb[line + 1][collumn], old_bb[line + 1][collumn + 1]);
}
}
}
if (nProcesses >= 2)
{
if (rank == 0)
{
MPI_Send(new_rb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, 1, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(new_gb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, 1, 0, MPI_COMM_WORLD);
MPI_Send(new_bb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, 1, 0, MPI_COMM_WORLD);
}
MPI_Recv(new_rb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(new_gb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(new_bb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
else if (rank == nProcesses - 1)
{
MPI_Recv(new_rb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(new_gb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(new_bb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Send(new_rb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(new_gb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
MPI_Send(new_bb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
}
}
else
{
MPI_Recv(new_rb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(new_gb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(new_bb[0], width + 2, MPI_UNSIGNED_CHAR, rank - 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
MPI_Send(new_rb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(new_gb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
MPI_Send(new_bb[1], width + 2, MPI_UNSIGNED_CHAR, rank - 1, 0, MPI_COMM_WORLD);
}
MPI_Send(new_rb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, rank + 1, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(new_gb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, rank + 1, 0, MPI_COMM_WORLD);
MPI_Send(new_bb[expected_lines - 2], width + 2, MPI_UNSIGNED_CHAR, rank + 1, 0, MPI_COMM_WORLD);
}
MPI_Recv(new_rb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, rank + 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(new_gb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, rank + 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(new_bb[expected_lines - 1], width + 2, MPI_UNSIGNED_CHAR, rank + 1, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
}
unsigned char **aux;
aux = old_rb;
old_rb = new_rb;
new_rb = aux;
if (type == 1)
{
aux = old_gb;
old_gb = new_gb;
new_gb = aux;
aux = old_bb;
old_bb = new_bb;
new_bb = aux;
}
}
if (rank == 0)
{
int proc;
for (proc = 1; proc < nProcesses; proc++)
{
int first = proc * ceil(div) + 1;
int last = fmin(height, (proc + 1) * (ceil(div)));
int line;
for (line = first; line <= last; line++)
{
MPI_Recv(red_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (type == 1)
{
MPI_Recv(green_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Recv(blue_buffer[line], width + 2, MPI_UNSIGNED_CHAR, proc, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
}
}
int line;
for (line = 1; line <= expected_lines - 2; line++)
{
memcpy(red_buffer[line], old_rb[line], width + 2);
if (type == 1)
{
memcpy(green_buffer[line], old_gb[line], width + 2);
memcpy(blue_buffer[line], old_bb[line], width + 2);
}
}
}
else
{
int line;
for (line = 1; line <= expected_lines - 2; line++)
{
MPI_Send(old_rb[line], width + 2, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD);
if (type == 1)
{
MPI_Send(old_gb[line], width + 2, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD);
MPI_Send(old_bb[line], width + 2, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD);
}
}
}
if (rank == 0)
{
gettimeofday(&end_time, 0);
long seconds = end_time.tv_sec - start_time.tv_sec;
long microseconds = end_time.tv_usec - start_time.tv_usec;
elapsed = seconds + microseconds * 1e-6;
printf("\nMPI took %f seconds with %d processes\n", elapsed, nProcesses);
write_file(argv[2], width, height, maxval, type, red_buffer, green_buffer, blue_buffer);
}
MPI_Finalize();
return 0;
}