Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 1fcd9c2

Browse files
iomaganarispramodk
authored andcommitted
Avoid large stdout with progress bar fixes #116 (#134)
1 parent 680749a commit 1fcd9c2

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

coreneuron/nrniv/nrn_setup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ void nrn_cleanup(bool clean_ion_global_map) {
974974
}
975975

976976
if (ml->_thread) {
977-
free (ml->_thread);
977+
free(ml->_thread);
978978
ml->_thread = NULL;
979979
}
980980

coreneuron/nrniv/nrnoptarg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static param_int param_int_args[] = {
7171
{"--extracon -x", 0, 0, 10000000,
7272
"Number of extra random connections in each thread to other duplicate models (int)."},
7373
{"--seed -s", -1, 0, 100000000, "Initialization seed for random number generator (int)."},
74-
{"--report-buffer-size", 4, 1, 8, "Size in MB of the report buffer (int)."},
74+
{"--report-buffer-size", 4, 1, 128, "Size in MB of the report buffer (int)."},
7575
{NULL, 0, 0, 0, NULL}};
7676

7777
static param_dbl param_dbl_args[] = {

coreneuron/nrnoc/eion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace coreneuron {
7272

7373
#define nparm 5
7474
static const char* mechanism[] = {/*just a template*/
75-
"0", "na_ion", "ena", "nao", "nai", 0, "ina", "dina_dv_", 0, 0};
75+
"0", "na_ion", "ena", "nao", "nai", 0, "ina", "dina_dv_", 0, 0};
7676

7777
void nrn_init_ion(NrnThread*, Memb_list*, int);
7878
void nrn_alloc_ion(double*, Datum*, int);

coreneuron/utils/progressbar/progressbar.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <assert.h>
1414
#include <limits.h>
15+
#include <unistd.h>
1516
#include "progressbar.h"
1617

1718
/// How wide we assume the screen is if termcap fails.
@@ -32,6 +33,12 @@ enum { WHITESPACE_LENGTH = 2 };
3233
/// The amount of width taken up by the border of the bar component.
3334
enum { BAR_BORDER_WIDTH = 2 };
3435

36+
/// The maximum number of bar redraws (to avoid frequent output in long runs)
37+
enum { BAR_DRAW_COUNT_MAX = 500 };
38+
39+
enum { BAR_DRAW_INTERVAL = 1,
40+
BAR_DRAW_INTERVAL_NOTTY = 5 };
41+
3542
/// Models a duration of time broken into hour/minute/second components. The number of seconds
3643
/// should be less than the
3744
/// number of seconds in one minute, and the number of minutes should be less than the number of
@@ -43,6 +50,7 @@ typedef struct {
4350
} progressbar_time_components;
4451

4552
static void progressbar_draw(const progressbar* bar);
53+
static int progressbar_remaining_seconds(const progressbar* bar);
4654

4755
/**
4856
* Create a new progress bar with the specified label, max number of steps, and format string.
@@ -57,6 +65,7 @@ progressbar* progressbar_new_with_format(const char* label, unsigned long max, c
5765

5866
new->max = max;
5967
new->value = 0;
68+
new->draw_time_interval = isatty(STDOUT_FILENO)? BAR_DRAW_INTERVAL : BAR_DRAW_INTERVAL_NOTTY;
6069
new->t = 0;
6170
new->start = time(NULL);
6271
assert(3 == strlen(format) && "format must be 3 characters in length");
@@ -66,6 +75,8 @@ progressbar* progressbar_new_with_format(const char* label, unsigned long max, c
6675

6776
progressbar_update_label(new, label);
6877
progressbar_draw(new);
78+
new->prev_t = difftime(time(NULL), new->start);
79+
new->drawn_count = 1;
6980

7081
return new;
7182
}
@@ -90,11 +101,45 @@ void progressbar_free(progressbar* bar) {
90101

91102
/**
92103
* Increment an existing progressbar by `value` steps.
104+
* Additionally issues a redraw in case a certain time interval has elapsed (min: 1sec)
105+
* Reasons for a larger interval are:
106+
* - Stdout is not TTY
107+
* - Respect BAR_DRAW_COUNT_MAX
93108
*/
94109
void progressbar_update(progressbar* bar, unsigned long value, double t) {
95110
bar->value = value;
96111
bar->t = t;
112+
int sim_time = difftime(time(NULL), bar->start);
113+
114+
// If there is not enough time passed to redraw the progress bar return
115+
if ((sim_time - bar->prev_t) < bar->draw_time_interval) {
116+
return;
117+
}
118+
97119
progressbar_draw(bar);
120+
121+
bar->drawn_count++;
122+
bar->prev_t = sim_time;
123+
124+
if (bar->drawn_count >= BAR_DRAW_COUNT_MAX || sim_time < 15) {
125+
// Dont change the interval after the limit. Simulation should be over any moment and
126+
// avoid the calc of draw_time_interval which could raise DIV/0
127+
// Also, dont do it the first 15sec to avoid really bad estimates which could potentially
128+
// delay a better estimate too far away in the future.
129+
return;
130+
}
131+
132+
// Sample ETA to calculate the next interval until the redraw of the progressbar
133+
int eta_s = progressbar_remaining_seconds(bar);
134+
bar->draw_time_interval = eta_s / (BAR_DRAW_COUNT_MAX - bar->drawn_count);
135+
136+
if (bar->draw_time_interval < BAR_DRAW_INTERVAL_NOTTY) {
137+
bar->draw_time_interval =
138+
isatty(STDOUT_FILENO)
139+
? ((bar->draw_time_interval < BAR_DRAW_INTERVAL)? BAR_DRAW_INTERVAL
140+
: bar->draw_time_interval)
141+
: BAR_DRAW_INTERVAL_NOTTY;
142+
}
98143
}
99144

100145
/**
@@ -191,6 +236,7 @@ static void progressbar_draw(const progressbar* bar) {
191236
fputc(' ', stdout);
192237
fprintf(stdout, ETA_FORMAT, bar->t, eta.hours, eta.minutes, eta.seconds);
193238
fputc('\r', stdout);
239+
fflush(stdout);
194240
}
195241

196242
/**

coreneuron/utils/progressbar/progressbar.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,21 @@ typedef struct _progressbar_t {
3232
/// current value
3333
unsigned long value;
3434

35+
/// value of the previous progress bar drawn in output
36+
unsigned long prev_sample_value;
37+
38+
/// time interval between consecutive bar redraws (seconds)
39+
unsigned long draw_time_interval;
40+
41+
/// number of redrawn bars
42+
unsigned long drawn_count;
43+
3544
/// time progressbar was started
3645
time_t start;
3746

47+
/// time progressbar was drawn for last time
48+
time_t prev_t;
49+
3850
/// label
3951
const char* label;
4052

0 commit comments

Comments
 (0)