diff options
Diffstat (limited to 'openmp/src/game.c')
-rw-r--r-- | openmp/src/game.c | 5 |
1 files changed, 5 insertions, 0 deletions
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++) { |