forked from swayfreeda/ImageBasedModellingEduV1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress_counter.h
executable file
·89 lines (73 loc) · 2.29 KB
/
progress_counter.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (C) 2015, Nils Moehrle
* TU Darmstadt - Graphics, Capture and Massively Parallel Computing
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#ifndef TEX_PROGRESSCOUNTER_HEADER
#define TEX_PROGRESSCOUNTER_HEADER
#include <fstream>
#include <iostream>
#include <sstream>
#include "util/timer.h"
#include <cmath>
enum ProgressCounterStyle {
ETA,
SIMPLE
};
static const std::string clear = "\r" + std::string(80,' ') + "\r";
class ProgressCounter {
private:
std::ofstream tty;
util::WallTimer timer;
std::string task;
std::size_t max;
std::size_t count;
public:
ProgressCounter(std::string const & _task, std::size_t max);
template <ProgressCounterStyle T> void progress(void);
void inc(void);
void reset(std::string const & _task);
};
inline
ProgressCounter::ProgressCounter(std::string const & _task, std::size_t _max)
: tty("/dev/tty", std::ios_base::out), timer(),
task(_task), max(_max), count(0) {}
inline void
ProgressCounter::inc(void) {
std::size_t tmp;
#pragma omp atomic capture
tmp = ++count;
if(tmp == max) {
std::stringstream ss;
ss << clear << task << " 100%... done. (Took "
<< timer.get_elapsed_sec() << "s)";
#pragma omp critical(progress_counter_inc)
std::cout << ss.rdbuf() << std::endl;
}
}
inline void
ProgressCounter::reset(std::string const & _task) {
timer.reset();
count = 0;
task = _task;
}
template <ProgressCounterStyle T> void
ProgressCounter::progress(void) {
if ((max > 100 && count % (max / 100) == 0) || max <= 100) {
float percent = static_cast<float>(count) / max;
int ipercent = std::floor(percent * 100.0f + 0.5f);
std::stringstream ss;
ss << clear << task << " " << ipercent << "%...";
if (T == ETA && ipercent > 3){
std::size_t const elapsed = timer.get_elapsed();
std::size_t eta = (elapsed / percent - elapsed) / 1000;
ss << " eta ~ " << eta << " s";
}
#pragma omp critical(progress_counter_progress)
tty << ss.rdbuf() << std::flush;
}
}
#endif /* TEX_PROGRESSCOUNTER_HEADER */