-
Notifications
You must be signed in to change notification settings - Fork 0
/
t1_game_of_life.c
145 lines (128 loc) · 3.12 KB
/
t1_game_of_life.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
#define CELL(I,J) (field[size*(I)+(J)])
#define ALIVE(I,J) t[size*(I)+(J)] = 1
#define DEAD(I,J) t[size*(I)+(J)] = 0
//**** Referência do código: https://rosettacode.org/wiki/Conway%27s_Game_of_Life/C <-------
int count_alive(const char *field, int i, int j, int size)
{
int x, y, a=0;
for(x=i-1; x <= (i+1) ; x++)
{
for(y=j-1; y <= (j+1) ; y++)
{
if ( (x==i) && (y==j) ) continue;
if ( (y<size) && (x<size) &&
(x>=0) && (y>=0) )
{
a += CELL(x,y);
}
}
}
return a;
}
void evolve(const char *field, char *t, int size)
{
int i, j, alive, cs;
/*
Caso de laço de repetição perfeitamente aninhado
Collapse: trata o for aninhado como um único loop de tamanho sizeXsize
e paraleliza a estrutura
*/
#pragma omp parallel for collapse(2) //Número de loops a serem paralelizados, contando de fora
for(i=0; i < size; i++)
{
for(j=0; j < size; j++)
{
alive = count_alive(field, i, j, size);
cs = CELL(i,j);
if ( cs )
{
if ( (alive > 3) || ( alive < 2 ) )
DEAD(i,j);
else
ALIVE(i,j);
} else {
if ( alive == 3 )
ALIVE(i,j);
else
DEAD(i,j);
}
}
}
}
#include <stdio.h>
/* some additional header needed to use the function evolve provided
previously, or just copy/paste the given code here */
#define BLINKER_SIZE 3
#define BLINKER_GEN 3
char small_blinker[] = {
0,0,0,
1,1,1,
0,0,0
};
char temp_blinker[BLINKER_SIZE*BLINKER_SIZE];
#define FIELD_SIZE 45
#define FIELD_GEN 175
char field[FIELD_SIZE * FIELD_SIZE];
char temp_field[FIELD_SIZE*FIELD_SIZE];
/* set the cell i,j as alive */
#define SCELL(I,J) field[FIELD_SIZE*(I)+(J)] = 1
void dump_field(const char *f, int size)
{
int i;
for (i=0; i < (size*size); i++)
{
if ( (i % size) == 0 ) printf("\n");
printf("%c", f[i] ? 'X' : '.');
}
printf("\n");
}
int main(int argc, char **argv)
{
int i;
char *fa, *fb, *tt, op;
/* fast and dirty selection option */
if ( argc > 1 )
{
op = *argv[1];
} else {
op = 'b';
}
switch ( op )
{
case 'B':
case 'b':
/* blinker test */
fa = small_blinker;
fb = temp_blinker;
for(i=0; i< BLINKER_GEN; i++)
{
dump_field(fa, BLINKER_SIZE);
evolve(fa, fb, BLINKER_SIZE);
tt = fb; fb = fa; fa = tt;
}
return 0;
case 'G':
case 'g':
for(i=0; i < (FIELD_SIZE*FIELD_SIZE) ; i++) field[i]=0;
/* prepare the glider */
SCELL(0, 1);
SCELL(1, 2);
SCELL(2, 0);
SCELL(2, 1);
SCELL(2, 2);
/* evolve */
fa = field;
fb = temp_field;
for (i=0; i < FIELD_GEN; i++)
{
dump_field(fa, FIELD_SIZE);
evolve(fa, fb, FIELD_SIZE);
tt = fb; fb = fa; fa = tt;
}
return 0;
default:
fprintf(stderr, "no CA for this\n");
break;
}
return 1;
}