summaryrefslogtreecommitdiff
path: root/serial
diff options
context:
space:
mode:
Diffstat (limited to 'serial')
-rw-r--r--serial/Makefile54
-rw-r--r--serial/include/create_grid.h12
-rw-r--r--serial/include/file.h11
-rw-r--r--serial/include/game.h20
-rw-r--r--serial/src/create_grid.c38
-rw-r--r--serial/src/file.c17
-rw-r--r--serial/src/game.c53
-rw-r--r--serial/src/main.c103
8 files changed, 308 insertions, 0 deletions
diff --git a/serial/Makefile b/serial/Makefile
new file mode 100644
index 0000000..e90dc23
--- /dev/null
+++ b/serial/Makefile
@@ -0,0 +1,54 @@
+CC ?= gcc
+
+SRC_PATH = src
+BUILD_PATH = build
+BIN_PATH = $(BUILD_PATH)/bin
+
+BIN_NAME = gol
+
+SRC_EXT = c
+
+SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-)
+OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o)
+DEPS = $(OBJECTS:.o=.d)
+
+COMPILE_FLAGS = -Wall -g -std=c99
+INCLUDES = -I include/
+LIBS =
+
+.PHONY: default_target
+default_target: release
+
+.PHONY: release
+release: dirs
+ @$(MAKE) all
+
+.PHONY: dirs
+dirs:
+ @echo "Creating directories"
+ @mkdir -p $(dir $(OBJECTS))
+ @mkdir -p $(BIN_PATH)
+
+.PHONY: clean
+clean:
+ @echo "Deleting $(BIN_NAME) symlink"
+ @$(RM) $(BIN_NAME)
+ @echo "Deleting directories"
+ @$(RM) -r $(BUILD_PATH)
+ @$(RM) -r $(BIN_PATH)
+
+.PHONY: all
+all: $(BIN_PATH)/$(BIN_NAME)
+ @echo "Making symlink: $(BIN_NAME) -> $<"
+ @$(RM) $(BIN_NAME)
+ @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME)
+
+$(BIN_PATH)/$(BIN_NAME): $(OBJECTS)
+ @echo "Linking: $@"
+ $(CC) $(OBJECTS) -o $@ ${LIBS}
+
+-include $(DEPS)
+
+$(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT)
+ @echo "Compiling: $< -> $@"
+ $(CC) $(COMPILE_FLAGS) $(INCLUDES) -MP -MMD -c $< -o $@
diff --git a/serial/include/create_grid.h b/serial/include/create_grid.h
new file mode 100644
index 0000000..45ac891
--- /dev/null
+++ b/serial/include/create_grid.h
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "file.h"
+#include "game.h"
+
+#ifndef CREATE_GRID_H
+#define CREATE_GRID_H
+
+void print_grid(struct GAME* game);
+void create_grid(int argc, char** argv);
+
+#endif // CREATE_GRID_H
diff --git a/serial/include/file.h b/serial/include/file.h
new file mode 100644
index 0000000..b2db380
--- /dev/null
+++ b/serial/include/file.h
@@ -0,0 +1,11 @@
+#include "game.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef FILE_H
+#define FILE_H
+
+void read_in(char* filename, struct GAME* game);
+void write_out(char* filename, struct GAME* game);
+
+#endif //FILE_H
diff --git a/serial/include/game.h b/serial/include/game.h
new file mode 100644
index 0000000..1896daa
--- /dev/null
+++ b/serial/include/game.h
@@ -0,0 +1,20 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#ifndef GAME_H
+#define GAME_H
+
+struct GAME {
+ unsigned char** grid;
+ int padding;
+ int width;
+ int height;
+};
+
+int neighbors(struct GAME* game, int x, int y);
+void next(struct GAME* game);
+void update(struct GAME* game, int x1, int x2, int y1, int y2);
+void randomize(struct GAME* game);
+
+#endif // GAME_H
diff --git a/serial/src/create_grid.c b/serial/src/create_grid.c
new file mode 100644
index 0000000..d6a5b94
--- /dev/null
+++ b/serial/src/create_grid.c
@@ -0,0 +1,38 @@
+#include "create_grid.h"
+
+void print_grid(struct GAME* game) {
+ printf("\n===GRID===\n");
+ for (int y = 0; y < game->height; y++) {
+ for (int x = 0; x < game->width; x++) {
+ printf("%i ", game->grid[y][x]);
+ }
+ printf("\n");
+ }
+}
+
+void create_grid(int argc, char** argv) {
+ char* filename;
+ struct GAME game;
+ game.padding = 0;
+ if (argc == 5) {
+ game.width = atoi(argv[2]);
+ game.height = atoi(argv[3]);
+ filename = argv[4];
+ } else {
+ printf("Usage: ./gol create-grid <width> <height> <filename>\n");
+ exit(1);
+ }
+
+ unsigned char** grid = malloc(sizeof(unsigned char*) * game.height);
+ for (int y = 0; y < game.height; y++) {
+ grid[y] = malloc(sizeof(unsigned char*) * game.width);
+ printf("Row %i: ", y);
+ for (int x = 0; x < game.width; x++) {
+ char temp;
+ scanf("%i%c", (unsigned int*)&grid[y][x],&temp);
+ }
+ }
+ game.grid = grid;
+ write_out(filename, &game);
+ print_grid(&game);
+}
diff --git a/serial/src/file.c b/serial/src/file.c
new file mode 100644
index 0000000..3ecb613
--- /dev/null
+++ b/serial/src/file.c
@@ -0,0 +1,17 @@
+#include "file.h"
+
+void read_in(char* filename, struct GAME* game) {
+ FILE* file = fopen(filename, "rb");
+ for (int i = game->padding; i < game->height+game->padding; i++) {
+ fread(game->grid[i] + game->padding, sizeof(unsigned char), game->width, file);
+ }
+ fclose(file);
+}
+
+void write_out(char* filename, struct GAME* game) {
+ FILE* file = fopen(filename, "w+");
+ for (int i = game->padding; i < game->height+game->padding; i++) {
+ fwrite(game->grid[i] + game->padding, sizeof(unsigned char), game->width, file);
+ }
+ fclose(file);
+}
diff --git a/serial/src/game.c b/serial/src/game.c
new file mode 100644
index 0000000..2921e3c
--- /dev/null
+++ b/serial/src/game.c
@@ -0,0 +1,53 @@
+#include "game.h"
+
+int neighbors(struct GAME* game, int x, int y) {
+ int n = 0;
+ for (int dy = -1; dy <= 1; dy++) {
+ for (int dx = -1; dx <= 1; dx++) {
+ if (!(dx == 0 && dy == 0) && (x+dx) > 0 && (y+dy) > 0 && (x+dx) < game->width+(game->padding*2) && (y+dy) < game->height+(game->padding*2)) {
+ if (game->grid[y+dy][x+dx]) {
+ n++;
+ }
+ }
+ }
+ }
+
+ return n;
+}
+
+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));
+ for (int y = 0; y < game->height+(game->padding*2); y++) {
+ newGrid[y] = malloc(size);
+ memset(newGrid[y], 0, size);
+ }
+
+ 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);
+ if (game->grid[y][x]) {
+ if (my_neighbors < 2 || my_neighbors > 3) {
+ newGrid[y][x] = 0;
+ } else {
+ newGrid[y][x] = 1;
+ }
+ } else {
+ if (my_neighbors == 3) {
+ newGrid[y][x] = 1;
+ }
+ }
+ }
+ }
+
+ free(game->grid);
+ game->grid = newGrid;
+}
+
+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++) {
+ game->grid[y][x] = rand() & 1;
+ }
+ }
+}
diff --git a/serial/src/main.c b/serial/src/main.c
new file mode 100644
index 0000000..f4c8141
--- /dev/null
+++ b/serial/src/main.c
@@ -0,0 +1,103 @@
+#include <stdlib.h>
+#include <time.h>
+#include <stdio.h>
+#include <string.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
+
+void simulate(int argc, char** argv) {
+ srand(SEED);
+ char* filename;
+ struct GAME game;
+ game.padding = PADDING;
+ int iterations, log_each_step;
+ if (argc == 7) {
+ filename = argv[2];
+ game.width = atoi(argv[3]);
+ game.height = atoi(argv[4]);
+ iterations = atoi(argv[5]);
+ log_each_step = atoi(argv[6]);
+ } else {
+ printf("Usage: ./gol simulate <filename | random> <width> <height> <iterations> <log-each-step?1:0>\n");
+ filename = "output/out.bin";
+ game.height = 10;
+ game.width = 10;
+ iterations = 5;
+ log_each_step = 0;
+ }
+
+ clock_t global_start = clock();
+
+ // 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));
+ }
+
+ if (strcmp(filename, "random") == 0) {
+ randomize(&game);
+ } else {
+ read_in(filename, &game);
+ }
+
+ char iteration_file[1024];
+ double time_computing_life = 0;
+ clock_t start, end;
+
+ for (int i = 0; i <= iterations; i++) {
+ if (i > 0) {
+ // Iteration 0 is just the input board
+ start = clock();
+ next(&game);
+ end = clock();
+ time_computing_life += ((double) (end - start)) / CLOCKS_PER_SEC;
+ }
+ if (log_each_step) {
+ #if VERBOSE == 1
+ printf("\n===Iteration %i===\n", i);
+ 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
+ sprintf(iteration_file, "output/iteration-%07d.bin", i);
+ write_out(iteration_file, &game);
+ }
+ }
+ double total_clock_time = ((double) (clock() - global_start)) / CLOCKS_PER_SEC;
+ 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;
+}