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 
118 {
119  logger = f_open(filename, "w");
120 
121  const char* h1 = " CycFIM (Step A) ------ ------ --------- --------- | Idiff (Step B) ------ | Repol. times (Step D)";
122  const char* h2 = " mn AT mx AT its ttits time tttime | time tttime | time tttime";
123 
124  if (logger == NULL)
125  log_msg(NULL, 3, 0, "%s error: Could not open file %s in %s. Turning off logging.\n",
126  __func__, filename);
127  else {
128  log_msg(logger, 0, 0, "%s", h1);
129  log_msg(logger, 0, 0, "%s", h2);
130  }
131 }
132 
133 void eikonal_solver_stats::log_stats(double time, bool cflg)
134 {
135  if (!this->logger) return;
136 
137  char abuf[256];
138  char bbuf[256];
139  char dbuf[256];
140 
141  // iterations in this period
142  float its = (this->tot - this->last_tot);
143 
144  // total solver time spent in this period
145  float tstm_A = this->slvtime_A - this->lastSlvtime_A;
146  float tstm_B = this->slvtime_B - this->lastSlvtime_B;
147  float tstm_D = this->slvtime_D - this->lastSlvtime_D;
148 
149  this->totsolves += this->solves;
150 
151  snprintf(abuf, sizeof abuf, "%7.2f %7.2f %6d %6d %10.4f %10.4f", this->minAT, this->maxAT, this->tot - this->last_tot, this->tot, tstm_A, this->slvtime_A);
152  snprintf(bbuf, sizeof bbuf, "%10.4f %10.4f", tstm_B, this->slvtime_B);
153  snprintf(dbuf, sizeof dbuf, "%10.4f %10.4f", tstm_D, this->slvtime_D);
154 
155  unsigned char flag = cflg ? ECHO : 0;
156  log_msg(this->logger, 0, flag | FLUSH | NONL, "%9.3f %s | %s | %s\n", time, abuf, bbuf, dbuf);
157 
158  this->lastSlvtime_A = this->slvtime_A;
159  this->lastSlvtime_B = this->slvtime_B;
160  this->lastSlvtime_D = this->slvtime_D;
161  this->last_tot = this->tot;
162  this->solves = 0;
163 }
164 
165 void eikonal_solver_stats::update_iter(const int curiter)
166 {
167  tot += curiter;
168  solves++;
169 }
170 
171 void eikonal_solver_stats::update_cli(double time, bool cflg)
172 {
173  char buf[256];
174 
175  // total solver time spent in this period
176  float tstm_A = this->slvtime_A - this->lastSlvtime_A;
177  float tstm_B = this->slvtime_B - this->lastSlvtime_B;
178  float tstm_D = this->slvtime_D - this->lastSlvtime_D;
179 
180  snprintf(buf, sizeof buf, "EIK_CYC %d", this->cycle);
181  log_msg(NULL, 0, ECHO | FLUSH, "%.2f\t----\t%.1f\t%15s", time, tstm_A+tstm_B+tstm_D, buf);
182 
183  if (param_globals::output_level > 1) {
184  const char* end_out = "----\t----- \t----\t-------\t-------";
185  log_msg(NULL, 0, ECHO | FLUSH, "minAT:\t%.2f -> %.2f", last_minAT, minAT);
186  log_msg(NULL, 0, ECHO | FLUSH, "maxAT:\t%.2f -> %.2f", last_maxAT, maxAT);
187  log_msg(NULL, 0, ECHO | FLUSH, "active list: %7i \tbc applied: %3s", this->activeList, this->bc_status ? "yes" : "no");
188  log_msg(NULL, 0, ECHO | FLUSH, "%s", end_out);
189  }
190 
191  this->cycle++;
192  this->bc_status = false;
193  this->last_minAT = this->minAT;
194  this->last_maxAT = this->maxAT;
195 }
196 
197 } // namespace opencarp
Basic utility structs and functions, mostly IO related.
#define FLUSH
Definition: basics.h:304
#define ECHO
Definition: basics.h:301
#define NONL
Definition: basics.h:305
FILE_SPEC f_open(const char *fname, const char *mode)
Open a FILE_SPEC.
Definition: basics.cc:123
void log_msg(FILE_SPEC out, int level, unsigned char flag, const char *fmt,...)
Definition: basics.cc:57
int solves
#solutions performed
Definition: timers.h:54
FILE_SPEC logger
file in which to write stats
Definition: timers.h:58
double slvtime_A
total time in Step A
Definition: timers.h:48
int totsolves
total # of solutions
Definition: timers.h:55
double lastSlvtime_D
last total time in Step D
Definition: timers.h:53
double last_minAT
previous minimum activation time
Definition: timers.h:42
void log_stats(double time, bool cflg)
Definition: timers.cc:133
double minAT
minimum activation time in current solve
Definition: timers.h:43
int last_tot
previous total # iterations
Definition: timers.h:47
double last_maxAT
previous maximum activation time
Definition: timers.h:44
double maxAT
maximum activation time in current solve
Definition: timers.h:45
int cycle
DREAM cycle.
Definition: timers.h:40
void init_logger(const char *filename)
Definition: timers.cc:117
int tot
total # iterations
Definition: timers.h:46
int activeList
number of nodes currently in list
Definition: timers.h:41
double lastSlvtime_A
last total time in Step A
Definition: timers.h:49
void update_iter(const int curiter)
Definition: timers.cc:165
double slvtime_B
total time in Step B
Definition: timers.h:50
double slvtime_D
total time in Step D
Definition: timers.h:52
bool bc_status
boundary conditions were applied?
Definition: timers.h:56
void update_cli(double time, bool cflg)
Definition: timers.cc:171
double lastSlvtime_B
last total time in Step B
Definition: timers.h:51
void log_stats(double tm, bool cflg)
Definition: timers.cc:96
FILE_SPEC logger
file in which to write stats
Definition: timers.h:78
void init_logger(const char *filename)
Definition: timers.cc:80
int tot_calls
total # calls
Definition: timers.h:74
double last_tot_time
last total time
Definition: timers.h:76
int calls
# calls for this interval, this is incremented externally
Definition: timers.h:73
double tot_time
total time, this is incremented externally
Definition: timers.h:75
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