summaryrefslogtreecommitdiff
path: root/serial/src
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2021-12-08 01:50:12 -0700
committerLogan Hunt <loganhunt@simponic.xyz>2021-12-08 01:50:12 -0700
commitc846568cf28b4d128cf893dc0abb6ccb5ccdcc32 (patch)
tree01ad0e303ea59103f2af9b9aa007785b88fa80ca /serial/src
parent253b267f1cee377a834860fb4deac54ef9a78b7a (diff)
downloadgol-c846568cf28b4d128cf893dc0abb6ccb5ccdcc32.tar.gz
gol-c846568cf28b4d128cf893dc0abb6ccb5ccdcc32.zip
Timing study
Diffstat (limited to 'serial/src')
-rw-r--r--serial/src/create_grid.c2
-rw-r--r--serial/src/file.c2
-rw-r--r--serial/src/game.c4
-rw-r--r--serial/src/main.c11
4 files changed, 18 insertions, 1 deletions
diff --git a/serial/src/create_grid.c b/serial/src/create_grid.c
index d6a5b94..a48317d 100644
--- a/serial/src/create_grid.c
+++ b/serial/src/create_grid.c
@@ -1,5 +1,6 @@
#include "create_grid.h"
+// Print entirety of a grid to verify input
void print_grid(struct GAME* game) {
printf("\n===GRID===\n");
for (int y = 0; y < game->height; y++) {
@@ -10,6 +11,7 @@ void print_grid(struct GAME* game) {
}
}
+// Go through user input
void create_grid(int argc, char** argv) {
char* filename;
struct GAME game;
diff --git a/serial/src/file.c b/serial/src/file.c
index 3ecb613..e10b667 100644
--- a/serial/src/file.c
+++ b/serial/src/file.c
@@ -1,5 +1,6 @@
#include "file.h"
+// Read a grid from a binary file into the space without padding
void read_in(char* filename, struct GAME* game) {
FILE* file = fopen(filename, "rb");
for (int i = game->padding; i < game->height+game->padding; i++) {
@@ -8,6 +9,7 @@ void read_in(char* filename, struct GAME* game) {
fclose(file);
}
+// Write a grid to a binary file into the space without padding
void write_out(char* filename, struct GAME* game) {
FILE* file = fopen(filename, "w+");
for (int i = game->padding; i < game->height+game->padding; i++) {
diff --git a/serial/src/game.c b/serial/src/game.c
index 2921e3c..ebaab1b 100644
--- a/serial/src/game.c
+++ b/serial/src/game.c
@@ -1,5 +1,6 @@
#include "game.h"
+// Calculate the number of live neighbors a cell has
int neighbors(struct GAME* game, int x, int y) {
int n = 0;
for (int dy = -1; dy <= 1; dy++) {
@@ -15,6 +16,7 @@ int neighbors(struct GAME* game, int x, int y) {
return n;
}
+// Compute the next iteration of a board
void next(struct GAME* game) {
unsigned char** newGrid = malloc(sizeof(unsigned char*) * (game->height+(game->padding*2)));
int size = sizeof(unsigned char) * (game->width+(game->padding*2));
@@ -23,6 +25,7 @@ void next(struct GAME* game) {
memset(newGrid[y], 0, size);
}
+ // Iterate through each cell
for (int y = 0; y < game->height+(game->padding*2); y++) {
for (int x = 0; x < game->width+(game->padding*2); x++) {
int my_neighbors = neighbors(game, x, y);
@@ -44,6 +47,7 @@ void next(struct GAME* game) {
game->grid = newGrid;
}
+// Randomly assign life value to each cell
void randomize(struct GAME* game) {
for (int y = game->padding; y < game->height+game->padding; y++) {
for (int x = game->padding; x < game->width+game->padding; x++) {
diff --git a/serial/src/main.c b/serial/src/main.c
index f4c8141..c7cdacf 100644
--- a/serial/src/main.c
+++ b/serial/src/main.c
@@ -18,6 +18,7 @@
//#define VERBOSE 1
#define SEED 100
+// Do the simulation
void simulate(int argc, char** argv) {
srand(SEED);
char* filename;
@@ -25,6 +26,7 @@ void simulate(int argc, char** argv) {
game.padding = PADDING;
int iterations, log_each_step;
if (argc == 7) {
+ // Parse the arguments
filename = argv[2];
game.width = atoi(argv[3]);
game.height = atoi(argv[4]);
@@ -48,6 +50,7 @@ void simulate(int argc, char** argv) {
memset(game.grid[i], 0, game.width+(2*game.padding));
}
+ // Choose where to read initial position
if (strcmp(filename, "random") == 0) {
randomize(&game);
} else {
@@ -55,20 +58,25 @@ void simulate(int argc, char** argv) {
}
char iteration_file[1024];
+
+ // Timing code
double time_computing_life = 0;
clock_t start, end;
for (int i = 0; i <= iterations; i++) {
+ // Iteration 0 will just be the initial grid
if (i > 0) {
- // Iteration 0 is just the input board
start = clock();
+ // Compute the next grid
next(&game);
end = clock();
time_computing_life += ((double) (end - start)) / CLOCKS_PER_SEC;
}
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" : " ");
@@ -77,6 +85,7 @@ void simulate(int argc, char** argv) {
}
printf("===End iteration %i===\n", i);
#endif
+ // Save to a file
sprintf(iteration_file, "output/iteration-%07d.bin", i);
write_out(iteration_file, &game);
}