From 5fad6377e6ff439bf379d34716a7271822a306ce Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 28 Dec 2018 12:50:38 +0100 Subject: Make logging configurable through the parfile. --- param.ccl | 12 +++++++++ src/maximal_slicing_axi_mg.c | 60 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/param.ccl b/param.ccl index 1718290..0795ac4 100644 --- a/param.ccl +++ b/param.ccl @@ -12,3 +12,15 @@ CCTK_INT fd_stencil "finite differencing stencil" { 1: :: "" } 1 + +RESTRICTED: +CCTK_INT stats_every "print elliptic solver stats every coarsest-level steps" +{ + 0: :: "" +} 0 + +RESTRICTED: +STRING loglevel "logging level" +{ + :: "" +} "info" diff --git a/src/maximal_slicing_axi_mg.c b/src/maximal_slicing_axi_mg.c index ae851db..79127db 100644 --- a/src/maximal_slicing_axi_mg.c +++ b/src/maximal_slicing_axi_mg.c @@ -29,6 +29,18 @@ #define LOGDEBUG #endif +static const struct { + const char *str; + enum MG2DLogLevel level; +} log_levels[] = { + { "fatal", MG2D_LOG_FATAL }, + { "error", MG2D_LOG_ERROR }, + { "warning", MG2D_LOG_WARNING }, + { "info", MG2D_LOG_INFO }, + { "verbose", MG2D_LOG_VERBOSE }, + { "debug", MG2D_LOG_DEBUG }, +}; + #include static inline int64_t gettime(void) { @@ -74,6 +86,8 @@ typedef struct MSMGContext { MaximalSlicingContext *ps_solver; + int log_level; + /* timings */ int64_t time_solve; int64_t count_solve; @@ -133,6 +147,17 @@ static void coord_patch_free(CoordPatch *cp) mg2d_solver_free(&cp->solver); } +static void log_callback(const MG2DContext *ctx, int level, + const char *fmt, va_list vl) +{ + MSMGContext *ms = ctx->opaque; + int target_level = ms->log_level; + if (level > target_level) + return; + fprintf(stderr, "[%d] t=%g ", ctz(ms->gh->cctk_levfac[0]), ms->gh->cctk_time); + vfprintf(stderr, fmt, vl); +} + static CoordPatch *get_coord_patch(MSMGContext *ms, int level) { cGH *gh = ms->gh; @@ -216,6 +241,10 @@ static CoordPatch *get_coord_patch(MSMGContext *ms, int level) cp->solver->tol = 1e-10; cp->solver->nb_cycles = 2; + cp->solver->opaque = ms; + cp->solver->log_callback = log_callback; + + /* initialize boundary values to zero, * non-zero values on the outer boundaries of refined levels are filled in elsewhere */ for (int i = 0; i < 4; i++) { @@ -235,6 +264,7 @@ static CoordPatch *get_coord_patch(MSMGContext *ms, int level) static void print_stats(MSMGContext *ms) { + int orig_log_level; int64_t total = ms->time_solve + ms->time_eval; fprintf(stderr, "%2.2f%% eval: %ld runs; %g s total; %g ms avg per run\n", @@ -271,6 +301,9 @@ static void print_stats(MSMGContext *ms) (double)ms->time_solve_export * 100.0 / ms->time_solve_mg, (double)ms->time_solve_history * 100.0 / ms->time_solve_mg); + orig_log_level = ms->log_level; + ms->log_level = MG2D_LOG_VERBOSE; + for (int i = 0; i < ms->nb_patches; i++) { CoordPatch *cp = get_coord_patch(ms, i); char indent_buf[64]; @@ -279,12 +312,14 @@ static void print_stats(MSMGContext *ms) mg2d_print_stats(cp->solver, indent_buf); } + ms->log_level = orig_log_level; } -static int context_init(cGH *gh, int fd_stencil, MSMGContext **ctx) +static int context_init(cGH *gh, int fd_stencil, const char *loglevel_str, + MSMGContext **ctx) { MSMGContext *ms; - int ret; + int loglevel = -1; ms = calloc(1, sizeof(*ms)); if (!ms) @@ -293,6 +328,19 @@ static int context_init(cGH *gh, int fd_stencil, MSMGContext **ctx) ms->gh = gh; ms->fd_stencil = fd_stencil; + for (int i = 0; i < ARRAY_ELEMS(log_levels); i++) { + if (!strcmp(loglevel_str, log_levels[i].str)) { + loglevel = log_levels[i].level; + break; + } + } + if (loglevel < 0) { + fprintf(stderr, "Invalid loglevel: %s\n", loglevel_str); + return -EINVAL; + } + + ms->log_level = loglevel; + *ctx = ms; return 0; @@ -731,7 +779,8 @@ finish: ms->time_solve += gettime() - total_start; ms->count_solve++; - if (reflevel == 0 && ms->count_solve > 0 && ms->count_eval > 0) + if (stats_every > 0 && reflevel == 0 && ms->count_solve > 0 && ms->count_eval > 0 && + !(ms->count_solve % stats_every)) print_stats(ms); } @@ -815,8 +864,11 @@ void msa_mg_init(CCTK_ARGUMENTS) { DECLARE_CCTK_ARGUMENTS; DECLARE_CCTK_PARAMETERS; + int ret; - context_init(cctkGH, fd_stencil, &ms); + ret = context_init(cctkGH, fd_stencil, loglevel, &ms); + if (ret < 0) + CCTK_WARN(0, "Error initializing the solver context"); } void msa_mg_inithist(CCTK_ARGUMENTS) -- cgit v1.2.3