-
Notifications
You must be signed in to change notification settings - Fork 94
/
progress.cc
64 lines (55 loc) · 1.47 KB
/
progress.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "caffe2/util/progress.h"
#include <iomanip>
#include <sstream>
namespace caffe2 {
bool Progress::update(int step, float interval) {
inc(step);
if (!mark_has() || mark_lapse() < interval) {
return false;
}
std::cerr << "\\|/-"[print_count++ % 4] << " " << string() << " \r"
<< std::flush;
mark();
return true;
}
void Progress::wipe() {
std::cerr << std::string(50, ' ') << '\r' << std::flush;
}
void Progress::summarize() {
wipe();
std::cout << report(true) << " " << std::endl;
}
std::string seconds_to_string(int seconds) {
std::ostringstream stream;
stream << std::setfill('0');
auto h = (int)seconds / 3600;
if (h != 0) {
stream << std::setw(2) << h << ":";
}
seconds -= h * 3600;
auto m = (int)seconds / 60;
stream << std::setw(2) << m << ":";
seconds -= m * 60;
stream << std::setw(2) << seconds;
return stream.str();
}
std::string Progress::report(bool past) const {
std::ostringstream stream;
stream << std::fixed << std::setprecision(1);
if (past) {
stream << "#" << index;
} else {
stream << index << "/" << size << " " << percent() << "%";
}
auto s = past ? avg_speed() : smooth_speed();
stream << " " << seconds_to_string(avg_lapse());
if (size > 0 && !past) {
stream << "+" << seconds_to_string(eta(s));
}
stream << " " << s << "/s";
return stream.str();
}
std::ostream &operator<<(std::ostream &os, Progress const &obj) {
return os << obj.string();
}
} // namespace caffe2