Skip to content

Commit

Permalink
grid: Use macros for counter dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
oschuett committed Mar 17, 2021
1 parent 6ab7065 commit 6d312f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
34 changes: 25 additions & 9 deletions src/grid/common/grid_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#include "grid_constants.h"
#include "grid_library.h"

// counter dimensions
#define GRID_NBACKENDS 3
#define GRID_NKERNELS 4
#define GRID_MAX_LP 20

typedef struct {
grid_sphere_cache sphere_cache;
long counters[GRID_NBACKENDS * GRID_NKERNELS * GRID_MAX_LP];
} grid_library_globals;

static grid_library_globals **per_thread_globals = NULL;
static bool library_initialized = false;
static int max_threads = 0;
Expand Down Expand Up @@ -109,8 +119,11 @@ void grid_library_counter_add(const int lp, const enum grid_backend backend,
const enum grid_library_kernel kernel,
const int increment) {
assert(lp >= 0);
assert(kernel < GRID_NKERNELS);
const int back = backend - GRID_BACKEND_REF;
const int idx = back * 4 * 20 + kernel * 20 + imin(lp, 19);
assert(back < GRID_NBACKENDS);
const int idx = back * GRID_NKERNELS * GRID_MAX_LP + kernel * GRID_MAX_LP +
imin(lp, GRID_MAX_LP - 1);
const int ithread = omp_get_thread_num();
assert(ithread < max_threads);
per_thread_globals[ithread]->counters[idx] += increment;
Expand Down Expand Up @@ -138,10 +151,12 @@ void grid_library_print_stats(void (*mpi_sum_func)(long *, int),
}

// Sum all counters across threads and mpi ranks.
long counters[320][2] = {0};
const int ncounters = GRID_NBACKENDS * GRID_NKERNELS * GRID_MAX_LP;
long counters[ncounters][2];
memset(counters, 0, ncounters * 2 * sizeof(long));
double total = 0.0;
for (int i = 0; i < 320; i++) {
counters[i][1] = i;
for (int i = 0; i < ncounters; i++) {
counters[i][1] = i; // needed as inverse index after qsort
for (int j = 0; j < max_threads; j++) {
counters[i][0] += per_thread_globals[j]->counters[i];
}
Expand All @@ -150,7 +165,7 @@ void grid_library_print_stats(void (*mpi_sum_func)(long *, int),
}

// Sort counters.
qsort(counters, 320, 2 * sizeof(long), &compare_counters);
qsort(counters, ncounters, 2 * sizeof(long), &compare_counters);

// Print counters.
print_func("\n", output_unit);
Expand All @@ -177,14 +192,15 @@ void grid_library_print_stats(void (*mpi_sum_func)(long *, int),
"collocate general", "integrate general"};
const char *backend_names[] = {"REF", "CPU", "GPU"};

for (int i = 0; i < 320; i++) {
for (int i = 0; i < ncounters; i++) {
if (counters[i][0] == 0)
continue; // skip empty counters
const double percent = 100.0 * counters[i][0] / total;
const int idx = counters[i][1];
const int back = idx / 80;
const int kern = (idx % 80) / 20;
const int lp = (idx % 80) % 20;
const int backend_stride = GRID_NKERNELS * GRID_MAX_LP;
const int back = idx / backend_stride;
const int kern = (idx % backend_stride) / GRID_MAX_LP;
const int lp = (idx % backend_stride) % GRID_MAX_LP;
char buffer[100];
snprintf(buffer, sizeof(buffer), " %-5i %-17s %-6s %34li %10.2f%%\n", lp,
kernel_names[kern], backend_names[back], counters[i][0], percent);
Expand Down
10 changes: 0 additions & 10 deletions src/grid/common/grid_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ grid_library_config grid_library_get_config(void);
void grid_library_print_stats(void (*mpi_sum_func)(long *, int), int mpi_comm,
void (*print_func)(char *, int), int output_unit);

/*******************************************************************************
* \brief All exiting counters. When adding a counter also update functions
* internal_add_stats() and grid_library_print_counters().
* \author Ole Schuett
******************************************************************************/
typedef struct {
grid_sphere_cache sphere_cache;
long counters[4 * 4 * 20]; // [backend][kernel][lp]
} grid_library_globals;

/*******************************************************************************
* \brief Various kernels provided by the grid library.
* \author Ole Schuett
Expand Down

0 comments on commit 6d312f7

Please sign in to comment.