openCARP
Doxygen code documentation for the open cardiac electrophysiology simulator openCARP
timers.cc
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) NumeriCor GmbH
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include "timers.h"
5 
6 #include <cstddef>
7 #include <cstdio>
8 #include <string>
9 #include "basics.h"
10 
11 namespace opencarp
12 {
13 
15 {
16  logger = f_open(filename, "w");
17 
18  const char* h1 = " ----- ----- ------- ----- ----- | ---- ----- --------- --------- --------- |";
19  const char* h2 = " mn it mx it avg it its ttits | ss ts st stps stpit |";
20 
21  if (logger == NULL)
22  log_msg(NULL, 3, 0, "%s error: Could not open file %s in %s. Turning off logging.\n",
23  __func__, filename);
24  else {
25  log_msg(logger, 0, 0, "%s", h1);
26  log_msg(logger, 0, 0, "%s", h2);
27  }
28 }
29 
30 void lin_solver_stats::log_stats(double tm, bool cflg)
31 {
32  if (!this->logger) return;
33 
34  // make sure this->solves is > 0
35  if (!this->solves) this->solves++;
36 
37  char itbuf[256];
38  char stbuf[64];
39 
40  // iterations in this period
41  float its = (this->tot - this->last_tot);
42 
43  // total solver time spent in this period
44  float tstm = this->slvtime - this->lastSlvtime;
45 
46  // solver time per solve
47  float stm = tstm / this->solves;
48 
49  // solver time per iteration
50  float itm = its ? tstm / its : 0.;
51 
52  if (!its)
53  this->min = 0;
54  this->totsolves += this->solves;
55 
56  snprintf(itbuf, sizeof itbuf, "%5d %5d %7.1f %6d %6d",
57  this->min, this->max, (float)(its / this->solves),
58  this->tot - this->last_tot, this->tot);
59  snprintf(stbuf, sizeof stbuf, "%4d %5d %9.4f %9.4f %9.4f", this->solves, this->totsolves,
60  (float)tstm, stm, itm);
61 
62  unsigned char flag = cflg ? ECHO : 0;
63  log_msg(this->logger, 0, flag | FLUSH | NONL, "%9.3f %s | %s |\n", tm, itbuf, stbuf);
64 
65  this->min = INT_MAX;
66  this->max = 0;
67  this->last_tot = this->tot;
68  this->solves = 0;
69  this->lastSlvtime = this->slvtime;
70 }
71 
72 void lin_solver_stats::update_iter(const int curiter)
73 {
74  if (curiter > max) max = curiter;
75  if (curiter < min) min = curiter;
76  tot += curiter;
77  solves++;
78 }
79 
81 {
82  logger = f_open(filename, "w");
83 
84  const char* h1 = " ----- ----- | --------- --------- |";
85  const char* h2 = " cls ttcls | time tot time |";
86 
87  if (logger == NULL)
88  log_msg(NULL, 3, 0, "%s error: Could not open file %s in %s. Turning off logging.\n",
89  __func__, filename);
90  else {
91  log_msg(logger, 0, 0, "%s", h1);
92  log_msg(logger, 0, 0, "%s", h2);
93  }
94 }
95 
96 void generic_timing_stats::log_stats(double tm, bool cflg)
97 {
98  if (!this->logger) return;
99 
100  char cbuf[256];
101  char tbuf[256];
102 
103  // time spent in this period
104  float ctm = this->tot_time - this->last_tot_time;
105  this->tot_calls += this->calls;
106 
107  snprintf(cbuf, sizeof cbuf, "%5d %5d", this->calls, this->tot_calls);
108  snprintf(tbuf, sizeof tbuf, "%9.4f %9.4f", ctm, this->tot_time);
109 
110  unsigned char flag = cflg ? ECHO : 0;
111  log_msg(this->logger, 0, flag | FLUSH | NONL, "%9.3f %s | %s |\n", tm, cbuf, tbuf);
112 
113  this->last_tot_time = this->tot_time;
114  this->calls = 0;
115 }
116 
117 void eikonal_solver_stats::init_logger(const char* filename, bool dream_mode)
118 {
119  logger = f_open(filename, "w");
120 
121  const char* h1 = " CycFIM (Step A) ------ ------ --------- --------- | Idiff (Step B) ------ | Repol. times (Step D)";
122  // Second column is maxAD, which means different things per technique, so it is
123  // labeled for the technique actually in use.
124  const char* h2 =
125  dream_mode
126  ? " mn AT mx AD its ttits time tttime | time tttime | time tttime"
127  : " mn AT mx AT its ttits time tttime | time tttime | time tttime";
128 
129  if (logger == NULL)
130  log_msg(NULL, 3, 0, "%s error: Could not open file %s in %s. Turning off logging.\n",
131  __func__, filename);
132  else {
133  log_msg(logger, 0, 0, "%s", h1);
134  log_msg(logger, 0, 0, "%s", h2);
135  }
136 }
137 
138 void eikonal_solver_stats::log_stats(double time, bool cflg)
139 {
140  if (!this->logger) return;
141 
142  char abuf[256];
143  char bbuf[256];
144  char dbuf[256];
145 
146  // iterations in this period
147  float its = (this->tot - this->last_tot);
148 
149  // total solver time spent in this period
150  float tstm_A = this->slvtime_A - this->lastSlvtime_A;
151  float tstm_B = this->slvtime_B - this->lastSlvtime_B;
152  float tstm_D = this->slvtime_D - this->lastSlvtime_D;
153 
154  this->totsolves += this->solves;
155 
156  snprintf(abuf, sizeof abuf, "%7.2f %7.2f %6d %6d %10.4f %10.4f", this->minAT, this->maxAD, this->tot - this->last_tot, this->tot, tstm_A, this->slvtime_A);
157  snprintf(bbuf, sizeof bbuf, "%10.4f %10.4f", tstm_B, this->slvtime_B);
158  snprintf(dbuf, sizeof dbuf, "%10.4f %10.4f", tstm_D, this->slvtime_D);
159 
160  unsigned char flag = cflg ? ECHO : 0;
161  log_msg(this->logger, 0, flag | FLUSH | NONL, "%9.3f %s | %s | %s\n", time, abuf, bbuf, dbuf);
162 
163  this->lastSlvtime_A = this->slvtime_A;
164  this->lastSlvtime_B = this->slvtime_B;
165  this->lastSlvtime_D = this->slvtime_D;
166  this->last_tot = this->tot;
167  this->solves = 0;
168 }
169 
170 void eikonal_solver_stats::update_iter(const int curiter)
171 {
172  tot += curiter;
173  solves++;
174 }
175 
176 void eikonal_solver_stats::update_cli(double time, bool cflg)
177 {
178  char buf[256];
179 
180  // total solver time spent in this period
181  float tstm_A = this->slvtime_A - this->lastSlvtime_A;
182  float tstm_B = this->slvtime_B - this->lastSlvtime_B;
183  float tstm_D = this->slvtime_D - this->lastSlvtime_D;
184 
185  snprintf(buf, sizeof buf, "EIK_CYC %d", this->cycle);
186  log_msg(NULL, 0, ECHO | FLUSH, "%.2f\t----\t%.1f\t%15s", time, tstm_A+tstm_B+tstm_D, buf);
187 
188  if (param_globals::output_level > 1) {
189  const char* end_out = "----\t----- \t----\t-------\t-------";
190  log_msg(NULL, 0, ECHO | FLUSH, "minAT:\t%.2f -> %.2f", last_minAT, minAT);
191  log_msg(NULL, 0, ECHO | FLUSH, "active list: %7i \tbc applied: %3s", this->activeList, this->bc_status ? "yes" : "no");
192  log_msg(NULL, 0, ECHO | FLUSH, "%s", end_out);
193  }
194 
195  this->cycle++;
196  this->bc_status = false;
197  this->last_minAT = this->minAT;
198 }
199 
200 } // namespace opencarp
Basic utility structs and functions, mostly IO related.
#define FLUSH
Definition: basics.h:305
#define ECHO
Definition: basics.h:302
#define NONL
Definition: basics.h:306
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:111
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:55
int solves
#solutions performed
Definition: timers.h:53
FILE_SPEC logger
file in which to write stats
Definition: timers.h:57
double slvtime_A
total time in Step A
Definition: timers.h:47
int totsolves
total # of solutions
Definition: timers.h:54
double lastSlvtime_D
last total time in Step D
Definition: timers.h:52
double maxAD
DREAM: time horizon this sweep batch may search up to. Pure Eikonal: actual latest activation time re...
Definition: timers.h:44
double last_minAT
previous minimum activation time
Definition: timers.h:42
void log_stats(double time, bool cflg)
Definition: timers.cc:138
double minAT
minimum activation time in current solve
Definition: timers.h:43
int last_tot
previous total # iterations
Definition: timers.h:46
int cycle
DREAM cycle.
Definition: timers.h:40
int tot
total # iterations
Definition: timers.h:45
int activeList
number of nodes currently in list
Definition: timers.h:41
double lastSlvtime_A
last total time in Step A
Definition: timers.h:48
void update_iter(const int curiter)
Definition: timers.cc:170
double slvtime_B
total time in Step B
Definition: timers.h:49
void init_logger(const char *filename, bool dream_mode)
Definition: timers.cc:117
double slvtime_D
total time in Step D
Definition: timers.h:51
bool bc_status
boundary conditions were applied?
Definition: timers.h:55
void update_cli(double time, bool cflg)
Definition: timers.cc:176
double lastSlvtime_B
last total time in Step B
Definition: timers.h:50
void log_stats(double tm, bool cflg)
Definition: timers.cc:96
FILE_SPEC logger
file in which to write stats
Definition: timers.h:80
void init_logger(const char *filename)
Definition: timers.cc:80
int tot_calls
total # calls
Definition: timers.h:76
double last_tot_time
last total time
Definition: timers.h:78
int calls
# calls for this interval, this is incremented externally
Definition: timers.h:75
double tot_time
total time, this is incremented externally
Definition: timers.h:77
void init_logger(const char *filename)
Definition: timers.cc:14
FILE_SPEC logger
file in which to write stats
Definition: timers.h:26
int last_tot
previous total #
Definition: timers.h:20
void log_stats(double tm, bool cflg)
Definition: timers.cc:30
int min
minimum #interations
Definition: timers.h:17
int max
maximum #iterations
Definition: timers.h:18
double lastSlvtime
total solver time
Definition: timers.h:25
void update_iter(const int curiter)
Definition: timers.cc:72
int solves
#solutions performed
Definition: timers.h:22
double slvtime
total solver time
Definition: timers.h:24
int totsolves
total # of solutions
Definition: timers.h:23