-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPI.c
356 lines (305 loc) · 11.6 KB
/
MPI.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
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include "mpi.h"
#include <time.h>
#define MAX_ALIVE_CELLS 4194304
//--- Variables to calculate execution time ---
float execTime;
clock_t start, end;
struct coord {
int x;
int y;
};
struct alive_tab {
int length;
struct coord alive[4194304];
};
struct the_board {
int is_alive;
int point;
};
int main(int argc, char **argv) {
//--- initialize MPI ---
MPI_Status status; // required variable for receive routines
int noTasks = 2;
int rank = 0;
MPI_Init( &argc, &argv );
//--- get number of tasks, and make sure it's 2 ---
MPI_Comm_size( MPI_COMM_WORLD, &noTasks );
if ( noTasks != 2 ) {
printf( "Number of Processes/Tasks must be 2. Number = %d\n\n", noTasks );
MPI_Finalize();
return 1;
}
//--- get rank ---
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
int mpiRoot = 0;
int nRows, nCols, nIters;
if (rank == mpiRoot) {
nRows = 2048;
nCols = 2048;
nIters = 200;
}
int cpt = 0;
int x, y;
// each processor calls MPI_Bcast, data is broadcast from root processor and ends up in everyone value variable
// root process uses MPI_Bcast to broadcast the value, each other process uses MPI_Bcast to receive the broadcast value
MPI_Bcast(&nRows, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&nCols, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Bcast(&nIters, 1, MPI_INT, 0, MPI_COMM_WORLD);
int nRowsLocal = nRows / noTasks;
int nRowsLocalWithGhost = nRowsLocal + 2;
int nColsWithGhost = nCols + 2;
int i = 0;
static struct the_board board[2048][2048];
static struct the_board even_board[1026][2050];
static struct the_board odd_board[1026][2050];
static struct alive_tab AliveTab;
AliveTab.length = 0;
for (int iRow = 0; iRow < nRowsLocalWithGhost; ++iRow) {
for (int iCol = 0; iCol < nColsWithGhost; ++iCol) {
even_board[iRow][iCol].is_alive = 0;
even_board[iRow][iCol].point = 0;
odd_board[iRow][iCol].is_alive = 0;
odd_board[iRow][iCol].point = 0;
}
}
//--- create a type for struct the_board ---
const int nitems = 2;
int blocklengths[2] = {1,1};
MPI_Datatype types[2] = {MPI_INT, MPI_INT};
MPI_Datatype mpi_board_type;
MPI_Aint offsets[2];
offsets[0] = offsetof(struct the_board, is_alive);
offsets[1] = offsetof(struct the_board, point);
MPI_Type_create_struct(nitems, blocklengths, offsets, types, &mpi_board_type);
MPI_Type_commit(&mpi_board_type);
//--- init present board ---
FILE *fptr;
int num;
if ((fptr = fopen("./data", "r")) == NULL) {
printf("error opening file");
exit(1);
}
for (int iRow = 0; iRow < nRows; ++iRow) {
for (int iCol = 0; iCol < nCols; ++iCol) {
fscanf(fptr, "%d", &num);
//printf("%d\n", num);
board[iRow][iCol].is_alive = num;
if (num == 1) {
if (AliveTab.length < MAX_ALIVE_CELLS) {
if (iRow <= nRowsLocalWithGhost) {
AliveTab.alive[AliveTab.length].x = iRow + 1;
} else {
AliveTab.alive[AliveTab.length].x = iRow;
}
AliveTab.alive[AliveTab.length].y = iCol + 1;
AliveTab.length++;
}
}
board[iRow][iCol].point = 0;
}
}
fclose(fptr);
//--- init even & odd boards ---
if (rank == 0) {
for (int iRow = 1; iRow <= nRowsLocal; ++iRow) {
for (int iCol = 1; iCol <= nCols; ++iCol) {
even_board[iRow][iCol].is_alive = board[iRow-1][iCol-1].is_alive;
even_board[iRow][iCol].point = board[iRow-1][iCol-1].point;
}
}
} else {
for (int iRow = 1; iRow <= nRowsLocal; ++iRow) {
for (int iCol = 1; iCol <= nCols; ++iCol) {
odd_board[iRow][iCol].is_alive = board[iRow + nRowsLocal - 1][iCol - 1].is_alive;
odd_board[iRow][iCol].point = board[iRow + nRowsLocal - 1][iCol - 1].point;
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
start = clock();
}
//--- iterate ---
for (int iIter = 0; iIter < nIters; ++iIter) {
int send;
int recv;
if (rank == 0) {
//printf("-------- Iteration %d --------\n", iIter);
//--- ghost columns ---
for (int iRow = 1; iRow < nRowsLocalWithGhost; ++iRow) {
even_board[iRow][0].is_alive = board[iRow-1][nCols-1].is_alive;
even_board[iRow][0].point = board[iRow-1][nCols-1].point;
even_board[iRow][nCols + 1].is_alive = board[iRow-1][0].is_alive;
even_board[iRow][nCols + 1].point = board[iRow-1][0].point;
}
//--- Send ghost rows to rank 1 ---
send = MPI_Send(&even_board[1][0], nColsWithGhost, mpi_board_type, 1, 1, MPI_COMM_WORLD);
send = MPI_Send(&even_board[nRowsLocal][0], nColsWithGhost, mpi_board_type, 1, 1, MPI_COMM_WORLD);
// -- Recieve ghost rows from rank 1 ---
recv = MPI_Recv(&even_board[0][0], nColsWithGhost, mpi_board_type, 1, 1, MPI_COMM_WORLD, &status);
recv = MPI_Recv(&even_board[nRowsLocal + 1][0], nColsWithGhost, mpi_board_type, 1, 1, MPI_COMM_WORLD, &status);
} else {
//--- ghost columns ---
for (int iRow = 1; iRow <= nRowsLocal; ++iRow) {
odd_board[iRow][0].is_alive = board[iRow-1+nRowsLocal][nCols-1].is_alive;
odd_board[iRow][0].point = board[iRow-1+nRowsLocal][nCols-1].point;
odd_board[iRow][nCols + 1].is_alive = board[iRow-1+nRowsLocal][0].is_alive;
odd_board[iRow][nCols + 1].point = board[iRow-1+nRowsLocal][0].point;
}
// -- Recieve ghost rows from rank 0 ---
recv = MPI_Recv(&odd_board[nRowsLocal + 1][0], nColsWithGhost, mpi_board_type, 0, 1, MPI_COMM_WORLD, &status);
recv = MPI_Recv(&odd_board[0][0], nColsWithGhost, mpi_board_type, 0, 1, MPI_COMM_WORLD, &status);
//--- Send ghost rows to rank 0 ---
send = MPI_Send(&odd_board[nRowsLocal][0], nColsWithGhost, mpi_board_type, 0, 1, MPI_COMM_WORLD);
send = MPI_Send(&odd_board[1][0], nColsWithGhost, mpi_board_type, 0, 1, MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
//--- Update even & odd boards ---
for (int i = 0; i < AliveTab.length; i++) {
x = AliveTab.alive[i].x;
y = AliveTab.alive[i].y;
if (rank == 0) {
if (x <= nRowsLocal) {
even_board[x][y + 1].point = even_board[x][y + 1].point + 1;
even_board[x][y - 1].point = even_board[x][y - 1].point + 1;
even_board[x + 1][y - 1].point = even_board[x + 1][y - 1].point + 1;
even_board[x + 1][y].point = even_board[x + 1][y].point + 1;
even_board[x + 1][y + 1].point = even_board[x + 1][y + 1].point + 1;
even_board[x - 1][y - 1].point = even_board[x - 1][y - 1].point + 1;
even_board[x - 1][y].point = even_board[x - 1][y].point + 1;
even_board[x - 1][y + 1].point = even_board[x - 1][y + 1].point + 1;
}
} else {
if (x > nRowsLocal) {
x = (x % nRowsLocal) + 1;
odd_board[x][y + 1].point = odd_board[x][y + 1].point + 1;
odd_board[x][y - 1].point = odd_board[x][y - 1].point + 1;
odd_board[x + 1][y - 1].point = odd_board[x + 1][y - 1].point + 1;
odd_board[x + 1][y].point = odd_board[x + 1][y].point + 1;
odd_board[x + 1][y + 1].point = odd_board[x + 1][y + 1].point + 1;
odd_board[x - 1][y - 1].point = odd_board[x - 1][y - 1].point + 1;
odd_board[x - 1][y].point = odd_board[x - 1][y].point + 1;
odd_board[x - 1][y + 1].point = odd_board[x - 1][y + 1].point + 1;
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
AliveTab.length = 0; // We are going to reconstruct a new Table of living cells
for (int i = 1; i <= nRowsLocal; i++) {
for (int j = 1; j <= nCols; j++) {
if (rank == 0) {
if (even_board[i][j].is_alive == 1) {
if ((even_board[i][j].point > 3) || (even_board[i][j].point < 2)) {
even_board[i][j].is_alive = 0; // Die because of overload or less neighbor
} else {
if (AliveTab.length < MAX_ALIVE_CELLS) {
AliveTab.alive[AliveTab.length].x = i;
AliveTab.alive[AliveTab.length].y = j;
AliveTab.length++;
}
}
} else {
if (even_board[i][j].point == 3) {
if (AliveTab.length < MAX_ALIVE_CELLS) {
even_board[i][j].is_alive = 1; // Birth of the cell
AliveTab.alive[AliveTab.length].x = i;
AliveTab.alive[AliveTab.length].y = j;
AliveTab.length++;
}
}
}
even_board[i][j].point = 0;
} else {
if (odd_board[i][j].is_alive == 1) {
if ((odd_board[i][j].point > 3) || (odd_board[i][j].point < 2)) {
odd_board[i][j].is_alive = 0; // Die because of overload or less neighbor
} else {
if (AliveTab.length < MAX_ALIVE_CELLS) {
AliveTab.alive[AliveTab.length].x = i + nRowsLocal - 1;
AliveTab.alive[AliveTab.length].y = j;
AliveTab.length++;
}
}
} else {
if (odd_board[i][j].point == 3) {
if (AliveTab.length < MAX_ALIVE_CELLS) {
odd_board[i][j].is_alive = 1; // Birth of the cell
AliveTab.alive[AliveTab.length].x = i + nRowsLocal - 1;
AliveTab.alive[AliveTab.length].y = j;
AliveTab.length++;
}
}
}
odd_board[i][j].point = 0;
}
}
}
//--- Update board from even & odd boards ---
if (rank == 0) {
for (int iRow = 1; iRow <= nRowsLocal; ++iRow) {
for (int iCol = 1; iCol <= nCols; ++iCol) {
board[iRow - 1][iCol - 1].is_alive = even_board[iRow][iCol].is_alive;
even_board[iRow][iCol].point = 0;
board[iRow - 1][iCol - 1].point = 0;
}
}
} else {
for (int iRow = 1; iRow <= nRowsLocal; ++iRow) {
for (int iCol = 1; iCol <= nCols; ++iCol) {
board[iRow + nRowsLocal - 1][iCol - 1].is_alive = odd_board[iRow][iCol].is_alive;
odd_board[iRow][iCol].point = 0;
board[iRow + nRowsLocal - 1][iCol - 1].point = 0;
}
}
}
MPI_Barrier(MPI_COMM_WORLD);
//--- display board ---
/*if (rank == 0) {
printf("\n--- Rank %d ---\n", rank);
for (int iRow = 0; iRow < nRows; ++iRow) {
for (int iCol = 0; iCol < nCols; ++iCol) {
if (board[iRow][iCol].is_alive == 1) {
printf(" %d ", board[iRow][iCol].is_alive);
} else {
printf(" %d ", board[iRow][iCol].is_alive);
}
if (iCol == nCols - 1) {
printf("\n\n");
}
}
}
}
if (rank == 1) {
printf("\n--- Rank %d ---\n", rank);
for (int iRow = 0; iRow < nRows; ++iRow) {
for (int iCol = 0; iCol < nCols; ++iCol) {
if (board[iRow][iCol].is_alive == 1) {
printf(" %d ", board[iRow][iCol].is_alive);
} else {
printf(" %d ", board[iRow][iCol].is_alive);
}
if (iCol == nCols - 1) {
printf("\n\n");
}
}
}
}*/
}
//printf("Rank %d Finalize\n", rank);
//--- get time ---
if (rank == 0) {
end = clock();
execTime = (float)(end - start) / CLOCKS_PER_SEC;
printf("\n------------------------------------------------\n");
printf("Exectution Time with MPI - Game of life = %f s\n", execTime);
printf("For %ld iterations & board size = %ld*%ld.\n", nIters, nRows, nCols);
printf("------------------------------------------------\n");
}
MPI_Finalize();
return 0;
}