-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpilife.c
More file actions
287 lines (253 loc) · 7.26 KB
/
mpilife.c
File metadata and controls
287 lines (253 loc) · 7.26 KB
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
/* Conway's Game of Life - MPI implementation */
/* Jim Teresco, Tue Mar 18 22:42:46 EST 1997 */
/* Updated 20 years later for CSIS 400, Siena College, Fall 2017 */
/*
Preprocessor defines that will enable extra outputs:
OUTPUT_ALL: print out the world on every iteration
OUTPUT_END: print out the world at the end
USE_FILE: used in combination with the above, if defined, print to a file
(or files, when OUTPUT_ALL) with basename as defined in USE_FILE
*/
#define OUTPUT_END
//#define USE_FILE "lifeworld"
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <time.h>
int main(int argc, char *argv[]) {
int gridsize, myrows;
double init_pct;
int num_iters, iter;
int **grid[2], curr, prev;
int i, j, neigh_count;
long live_count, birth_count, death_count;
long global_live, global_birth, global_death;
int numprocs, mypid;
int msgcount;
MPI_Request requests[4];
MPI_Status status[4];
FILE *fp;
int proc_turn;
#ifdef USE_FILE
char filename[FILENAME_MAX];
#endif
MPI_Init(&argc,&argv);
if (argc != 4) {
fprintf(stderr,"Usage: %s gridsize init_pct num_iters\n",argv[0]);
MPI_Abort(MPI_COMM_WORLD, 1);
}
MPI_Comm_rank(MPI_COMM_WORLD,&mypid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
srand48(time(NULL)*(mypid+1));
/* read parameters from the command line */
gridsize=atoi(argv[1]);
init_pct=atof(argv[2]);
num_iters=atoi(argv[3]);
if (gridsize%numprocs) {
fprintf(stderr,"%s: grid size must be a multiple of number of procs\n", argv[0]);
MPI_Abort(MPI_COMM_WORLD,1);
}
myrows=gridsize/numprocs;
if (mypid == 0)
printf("Using grid size %d (%d rows on each of %d procs)\n",
gridsize,myrows,numprocs);
/* allocate the grids */
grid[0]=(int **)malloc((myrows+2)*sizeof(int *));
for (i=0;i<=myrows+1;i++)
grid[0][i]=(int *)malloc((gridsize+2)*sizeof(int));
grid[1]=(int **)malloc((myrows+2)*sizeof(int *));
for (i=0;i<=myrows+1;i++)
grid[1][i]=(int *)malloc((gridsize+2)*sizeof(int));
/* initialize the grids (incl boundary buffer all 0's) */
for (i=0;i<=myrows+1;i++)
for (j=0;j<=gridsize+1;j++) {
grid[0][i][j]=0;
grid[1][i][j]=0;
}
/* start current grid as 0 */
curr=0; prev=1;
/* initialize the current grid based on the desired percentage of
living cells specified on the command line */
live_count=0;
for (i=1;i<=myrows;i++)
for (j=1;j<=gridsize;j++) {
if (drand48()<init_pct) {
grid[curr][i][j]=1;
live_count++;
}
else grid[curr][i][j]=0;
}
printf("Proc %d: Initial grid has %ld live cells out of %ld\n",mypid,
live_count,(long)myrows*gridsize);
MPI_Reduce(&live_count, &global_live, 1, MPI_LONG, MPI_SUM, 0,
MPI_COMM_WORLD);
if (mypid == 0)
printf("Global: Initial grid has %ld live cells out of %ld\n",global_live,
(long)gridsize*gridsize);
#ifdef OUTPUT_ALL
for (proc_turn = 0; proc_turn < numprocs; proc_turn++) {
MPI_Barrier(MPI_COMM_WORLD);
if (mypid == proc_turn) {
#ifdef USE_FILE
sprintf(filename, "%s.000000.txt", USE_FILE);
fp = fopen(filename, "a");
#else
fp = stdout;
#endif
if (mypid == 0) {
fprintf(fp, "Initial grid:\n");
}
for (i=1; i<=myrows; i++) {
fprintf(fp, "[%3d] ", mypid);
for (j=1; j<=gridsize; j++) {
fprintf(fp, "%c", (grid[curr][i][j] ? '*' : '-'));
}
fprintf(fp, "\n");
}
#ifdef USE_FILE
fclose(fp);
#else
fflush(stdout);
#endif
}
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
/* we can now start iterating */
for (iter=1; iter<=num_iters; iter++) {
/* swap the grids */
curr=1-curr; prev=1-prev;
if (mypid == 0)
printf("Iteration %d...\n",iter);
/* update the local buffers of off-processor neighbors in prev grid */
/* send from proc to next highest and next lowest neighbor */
msgcount=0;
if (mypid < numprocs-1) {
MPI_Isend(grid[prev][myrows],gridsize+2,MPI_INT,mypid+1,1,
MPI_COMM_WORLD,&requests[msgcount]);
msgcount++;
MPI_Irecv(grid[prev][myrows+1],gridsize+2,MPI_INT,mypid+1,2,
MPI_COMM_WORLD,&requests[msgcount]);
msgcount++;
}
if (mypid > 0) {
MPI_Isend(grid[prev][1],gridsize+2,MPI_INT,mypid-1,2,
MPI_COMM_WORLD,&requests[msgcount]);
msgcount++;
MPI_Irecv(grid[prev][0],gridsize+2,MPI_INT,mypid-1,1,
MPI_COMM_WORLD,&requests[msgcount]);
msgcount++;
}
/* wait for communication to complete */
MPI_Waitall(msgcount,requests,status);
/* perform the actual iteration */
live_count=0; birth_count=0; death_count=0;
/* visit each grid cell */
for (i=1;i<=myrows;i++)
for (j=1;j<=gridsize;j++) {
neigh_count=
(grid[prev][i-1][j-1]+grid[prev][i-1][j]+grid[prev][i-1][j+1]+
grid[prev][i][j-1]+grid[prev][i][j+1]+
grid[prev][i+1][j-1]+grid[prev][i+1][j]+grid[prev][i+1][j+1]);
switch (neigh_count) {
case 2:
/* no change */
grid[curr][i][j]=grid[prev][i][j];
break;
case 3:
/* birth */
if (!grid[prev][i][j]) birth_count++;
grid[curr][i][j]=1;
break;
default:
/* death of loneliness or overcrowding */
if (grid[prev][i][j]) death_count++;
grid[curr][i][j]=0;
break;
}
live_count+=grid[curr][i][j];
}
/* print the stats */
printf("Proc %d Counters- living: %ld, died: %ld, born: %ld\n",mypid,
live_count, death_count, birth_count);
MPI_Reduce(&live_count, &global_live, 1, MPI_LONG, MPI_SUM, 0,
MPI_COMM_WORLD);
MPI_Reduce(&death_count, &global_death, 1, MPI_LONG, MPI_SUM, 0,
MPI_COMM_WORLD);
MPI_Reduce(&birth_count, &global_birth, 1, MPI_LONG, MPI_SUM, 0,
MPI_COMM_WORLD);
if (mypid == 0)
printf("Global Counters- living: %ld, died: %ld, born: %ld\n",
global_live, global_death, global_birth);
#ifdef OUTPUT_ALL
for (proc_turn = 0; proc_turn < numprocs; proc_turn++) {
MPI_Barrier(MPI_COMM_WORLD);
if (mypid == proc_turn) {
#ifdef USE_FILE
sprintf(filename, "%s.%06d.txt", USE_FILE, iter);
fp = fopen(filename, "a");
#else
fp = stdout;
#endif
if (mypid == 0) {
fprintf(fp, "Grid at iter %d:\n", iter);
}
for (i=1; i<=myrows; i++) {
fprintf(fp, "[%3d] ", mypid);
for (j=1; j<=gridsize; j++) {
fprintf(fp, "%c", (grid[curr][i][j] ? '*' : '-'));
}
fprintf(fp, "\n");
}
#ifdef USE_FILE
fclose(fp);
#else
fflush(stdout);
#endif
}
}
#endif
/* end of iteration - synchronize */
/* this is not strictly necessary, as the reduce collective
communication operations will synchronize the processes */
MPI_Barrier(MPI_COMM_WORLD);
}
#ifdef OUTPUT_END
for (proc_turn = 0; proc_turn < numprocs; proc_turn++) {
MPI_Barrier(MPI_COMM_WORLD);
if (mypid == proc_turn) {
#ifdef USE_FILE
sprintf(filename, "%s.final.txt", USE_FILE);
fp = fopen(filename, "a");
#else
fp = stdout;
#endif
if (mypid == 0) {
fprintf(fp, "Final grid:\n");
}
for (i=1; i<=myrows; i++) {
fprintf(fp, "[%3d] ", mypid);
for (j=1; j<=gridsize; j++) {
fprintf(fp, "%c", (grid[curr][i][j] ? '*' : '-'));
}
fprintf(fp, "\n");
}
#ifdef USE_FILE
fclose(fp);
#else
fflush(stdout);
#endif
}
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
/* free the grids */
for (i=0;i<=myrows+1;i++)
free(grid[0][i]);
free(grid[0]);
for (i=0;i<=myrows+1;i++)
free(grid[1][i]);
free(grid[1]);
MPI_Finalize();
return 0;
}