-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlife.c
More file actions
120 lines (100 loc) · 2.89 KB
/
life.c
File metadata and controls
120 lines (100 loc) · 2.89 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
/*
Conway's Game of Life - OpenMP implementation
Jim Teresco
Williams College
Mount Holyoke College
Siena College
OpenMP parallelization, Fall 2021
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int gridsize;
double init_pct;
int num_iters, iter;
int **grid[2], curr, prev;
int i, j, neigh_count;
int live_count, birth_count, death_count;
if (argc != 4) {
fprintf(stderr,"Usage: %s gridsize init_pct num_iters\n",argv[0]);
exit(1);
}
/* seed the random number generator */
srand48(0);
/* read parameters from the command line */
gridsize=atoi(argv[1]);
init_pct=atof(argv[2]);
num_iters=atoi(argv[3]);
/* allocate the grids */
grid[0]=(int **)malloc((gridsize+2)*sizeof(int *));
for (i=0;i<=gridsize+1;i++)
grid[0][i]=(int *)malloc((gridsize+2)*sizeof(int));
grid[1]=(int **)malloc((gridsize+2)*sizeof(int *));
for (i=0;i<=gridsize+1;i++)
grid[1][i]=(int *)malloc((gridsize+2)*sizeof(int));
/* initialize the grids (incl boundary buffer all 0's) */
for (i=0;i<=gridsize+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<=gridsize;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("Initial grid has %d live cells out of %d\n",live_count,
gridsize*gridsize);
/* we can now start iterating */
for (iter=1; iter<=num_iters; iter++) {
/* swap the grids */
curr=1-curr; prev=1-prev;
printf("Iteration %d...\n",iter);
live_count=0; birth_count=0; death_count=0;
/* visit each grid cell */
#pragma omp parallel for private(i, j) shared(grid,gridsize,curr,prev) reduction(+:live_count) reduction(+:birth_count) reduction(+:death_count)
for (i=1;i<=gridsize;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(" Counters- living: %d, died: %d, born: %d\n",live_count,
death_count, birth_count);
}
/* free the grids */
for (i=0;i<=gridsize+1;i++)
free(grid[0][i]);
free(grid[0]);
for (i=0;i<=gridsize+1;i++)
free(grid[1][i]);
free(grid[1]);
}