summaryrefslogtreecommitdiff
path: root/openmp/src
diff options
context:
space:
mode:
Diffstat (limited to 'openmp/src')
-rw-r--r--openmp/src/create_grid.c38
-rw-r--r--openmp/src/file.c17
-rw-r--r--openmp/src/game.c64
-rw-r--r--openmp/src/main.c106
4 files changed, 225 insertions, 0 deletions
diff --git a/openmp/src/create_grid.c b/openmp/src/create_grid.c
new file mode 100644
index 0000000..d6a5b94
--- /dev/null
+++ b/openmp/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/openmp/src/file.c b/openmp/src/file.c
new file mode 100644
index 0000000..3ecb613
--- /dev/null
+++ b/openmp/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/openmp/src/game.c b/openmp/src/game.c
new file mode 100644
index 0000000..b5e786d
--- /dev/null
+++ b/openmp/src/game.c
@@ -0,0 +1,64 @@
+#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, int threads) {
+ unsigned char** newGrid = malloc(sizeof(unsigned char*) * (game->height+(game->padding*2)));
+ int y,x,i,size;
+ size = sizeof(unsigned char) * (game->width+(game->padding*2));
+ for (y = 0; y < game->height+(game->padding*2); y++) {
+ newGrid[y] = malloc(size);
+ memset(newGrid[y], 0, size);
+ }
+ int total_width = game->width+(game->padding*2);
+ int total_height = game->height+(game->padding*2);
+
+ int per_thread = (total_width * total_height) / threads;
+
+#pragma omp parallel num_threads(threads) shared(per_thread, threads, total_width, total_height, newGrid, game) private(y,x,i)
+ {
+ int me = omp_get_thread_num();
+ int thread_start = per_thread * me;
+ int thread_end = thread_start + per_thread + (me == threads-1 ? (total_width*total_height) % per_thread : 0);
+ for (i = thread_start; i < thread_end; i++) {
+ y = i / total_width;
+ x = i % total_width;
+ 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/openmp/src/main.c b/openmp/src/main.c
new file mode 100644
index 0000000..68ec0bd
--- /dev/null
+++ b/openmp/src/main.c
@@ -0,0 +1,106 @@
+#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
+
+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) {
+ 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));
+ }
+
+ 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++) {
+ if (i > 0) {
+ // Iteration 0 is just the input board
+ start = omp_get_wtime();
+ next(&game, threads);
+ end = omp_get_wtime();
+ time_computing_life += ((double) (end - start));
+ }
+ 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) (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;
+}