summaryrefslogtreecommitdiff
path: root/openmp/src
diff options
context:
space:
mode:
Diffstat (limited to 'openmp/src')
-rw-r--r--openmp/src/create_grid.c2
-rw-r--r--openmp/src/file.c2
-rw-r--r--openmp/src/game.c5
-rw-r--r--openmp/src/main.c9
4 files changed, 17 insertions, 1 deletions
diff --git a/openmp/src/create_grid.c b/openmp/src/create_grid.c
index d6a5b94..a48317d 100644
--- a/openmp/src/create_grid.c
+++ b/openmp/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/openmp/src/file.c b/openmp/src/file.c
index 3ecb613..e10b667 100644
--- a/openmp/src/file.c
+++ b/openmp/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/openmp/src/game.c b/openmp/src/game.c
index b5e786d..cac6fa0 100644
--- a/openmp/src/game.c
+++ b/openmp/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, int threads) {
unsigned char** newGrid = malloc(sizeof(unsigned char*) * (game->height+(game->padding*2)));
int y,x,i,size;
@@ -30,10 +32,12 @@ void next(struct GAME* game, int threads) {
#pragma omp parallel num_threads(threads) shared(per_thread, threads, total_width, total_height, newGrid, game) private(y,x,i)
{
+ // Each thread gets a number of cells to compute
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++) {
+ // Iterate through each cell assigned for this thread
y = i / total_width;
x = i % total_width;
int my_neighbors = neighbors(game, x, y);
@@ -55,6 +59,7 @@ void next(struct GAME* game, int threads) {
game->grid = newGrid;
}
+//Rnadomly 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/openmp/src/main.c b/openmp/src/main.c
index 68ec0bd..50fb096 100644
--- a/openmp/src/main.c
+++ b/openmp/src/main.c
@@ -19,6 +19,7 @@
//#define VERBOSE 1
#define SEED 100
+// Do the simulation
void simulate(int argc, char** argv) {
srand(SEED);
char* filename;
@@ -26,6 +27,7 @@ void simulate(int argc, char** argv) {
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]);
@@ -51,6 +53,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 {
@@ -62,16 +65,19 @@ void simulate(int argc, char** argv) {
double 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 = 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" : " ");
@@ -80,6 +86,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);
}