Skip to content

Commit

Permalink
grid: Fix OpenMP in grid_library.c
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Aug 27, 2020
1 parent 976dad7 commit dd8d7d6
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/grid/common/grid_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@ void grid_library_init() {
printf("Error: Grid library was already initialized.\n");
abort();
}

per_thread_stats =
malloc(sizeof(grid_library_stats *) * omp_get_max_threads());

// Using parallel regions to ensure memory is allocated near a thread's core.
#pragma omp parallel
#pragma omp parallel default(none) shared(per_thread_stats)
{
#pragma omp master
per_thread_stats =
malloc(sizeof(grid_library_stats *) * omp_get_num_threads());
#pragma omp barrier
const int ithread = omp_get_thread_num();
per_thread_stats[ithread] = malloc(sizeof(grid_library_stats));
memset(per_thread_stats[ithread], 0, sizeof(grid_library_stats));
}

library_initialized = true;
}

Expand All @@ -51,8 +52,7 @@ void grid_library_finalize() {
abort();
}

#pragma omp parallel master
for (int i = 0; i < omp_get_num_threads(); i++) {
for (int i = 0; i < omp_get_max_threads(); i++) {
free(per_thread_stats[i]);
}
free(per_thread_stats);
Expand Down Expand Up @@ -124,8 +124,7 @@ void grid_library_print_stats(void (*mpi_sum_func)(long *),
grid_library_stats totals;
memset(&totals, 0, sizeof(grid_library_stats));

#pragma omp parallel master
for (int i = 0; i < omp_get_num_threads(); i++) {
for (int i = 0; i < omp_get_max_threads(); i++) {
sum_stats(*per_thread_stats[i], &totals);
}

Expand Down

0 comments on commit dd8d7d6

Please sign in to comment.