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
|
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <omp.h>
#include "file.h"
#include "game.h"
#include "create_grid.h"
/*
Rules for life:
Any live cell with fewer than two live neighbors dies (underpopulation).
Any live cell with two or three live neighbors continues to live.
Any live cell with more than three live neighbors dies (overpopulation).
Any dead cell with exactly three live neighbors becomes a live cell (reproduction).
*/
#define PADDING 10
//#define VERBOSE 1
#define SEED 100
// Do the simulation
void simulate(int argc, char** argv) {
srand(SEED);
char* filename;
struct GAME game;
game.padding = PADDING;
int iterations, log_each_step, threads;
if (argc == 8) {
// Parse the arguments
filename = argv[2];
game.width = atoi(argv[3]);
game.height = atoi(argv[4]);
iterations = atoi(argv[5]);
log_each_step = atoi(argv[6]);
threads = atoi(argv[7]);
} else {
printf("Usage: ./gol simulate <filename | random> <width> <height> <iterations> <log-each-step?1:0> <threads>\n");
filename = "output/out.bin";
game.height = 10;
game.width = 10;
iterations = 5;
log_each_step = 0;
threads = 1;
}
double global_start = omp_get_wtime();
// Allocate space for current grid (1 byte per tile)
game.grid = malloc(sizeof(unsigned char*) * (game.height+(2*game.padding)));
for (int i = 0; i < game.height+(2*game.padding); i++) {
game.grid[i] = malloc(sizeof(unsigned char) * (game.width+(2*game.padding)));
memset(game.grid[i], 0, game.width+(2*game.padding));
}
// Choose where to read initial position
if (strcmp(filename, "random") == 0) {
randomize(&game);
} else {
read_in(filename, &game);
}
char iteration_file[1024];
double time_computing_life = 0;
double start, end;
for (int i = 0; i <= iterations; i++) {
// Iteration 0 will just be the initial grid
if (i > 0) {
start = omp_get_wtime();
// Compute the next grid with threads
next(&game, threads);
end = omp_get_wtime();
time_computing_life += ((double) (end - start));
}
if (log_each_step) {
// If we are logging each step, perform IO operations
#if VERBOSE == 1
printf("\n===Iteration %i===\n", i);
// Print the board without the padding elements
for (int y = game.padding; y < game.height+game.padding; y++) {
for (int x = game.padding; x < game.width+game.padding; x++) {
printf("%s ", game.grid[y][x] ? "X" : " ");
}
printf("\n");
}
printf("===End iteration %i===\n", i);
#endif
// Save to a file
sprintf(iteration_file, "output/iteration-%07d.bin", i);
write_out(iteration_file, &game);
}
}
double total_clock_time = ((double) (omp_get_wtime() - global_start));
printf("\n===Timing===\nTime computing life: %f\nClock time: %f\n", time_computing_life, total_clock_time);
}
int main(int argc, char** argv) {
if (argc >= 2) {
if (strcmp(argv[1], "simulate") == 0) {
simulate(argc, argv);
} else if (strcmp(argv[1], "create-grid") == 0) {
create_grid(argc, argv);
} else {
printf("Unknown input: %s\n", argv[1]);
exit(1);
}
} else {
printf("Usage: ./gol <simulate | create-grid>\n");
exit(1);
}
return 0;
}
|