| 39 |
|
* [4] Vardeman, Stocker & Gezelter, in progress (2010). |
| 40 |
|
*/ |
| 41 |
|
|
| 42 |
< |
#include <string> |
| 43 |
< |
#include <sstream> |
| 42 |
> |
#include <iostream> |
| 43 |
> |
#include <cstdlib> |
| 44 |
> |
#include <cstdio> |
| 45 |
|
#include <sys/ioctl.h> |
| 45 |
– |
#define _POSIX_SOURCE |
| 46 |
– |
#include <unistd.h> |
| 46 |
|
#ifdef IS_MPI |
| 47 |
|
#include <mpi.h> |
| 48 |
< |
#endif //is_mpi |
| 48 |
> |
#endif |
| 49 |
|
#include "utils/ProgressBar.hpp" |
| 50 |
|
|
| 51 |
+ |
using namespace std; |
| 52 |
+ |
|
| 53 |
|
namespace OpenMD { |
| 54 |
|
|
| 55 |
|
const char * progressSpinner_ = "|/-\\"; |
| 61 |
|
#ifdef IS_MPI |
| 62 |
|
if (MPI::COMM_WORLD.Get_rank() == 0) { |
| 63 |
|
#endif |
| 64 |
< |
printf("\n"); |
| 65 |
< |
fflush(stdout); |
| 64 |
> |
cout << endl; |
| 65 |
> |
cout.flush(); |
| 66 |
|
#ifdef IS_MPI |
| 67 |
|
} |
| 68 |
|
#endif |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
void ProgressBar::update() { |
| 76 |
< |
int width = 80; |
| 76 |
> |
|
| 77 |
> |
int width; |
| 78 |
> |
|
| 79 |
|
#ifdef IS_MPI |
| 80 |
|
if (MPI::COMM_WORLD.Get_rank() == 0) { |
| 81 |
|
#endif |
| 82 |
< |
|
| 82 |
> |
|
| 83 |
|
// only do the progress bar if we are actually running in a tty: |
| 84 |
< |
if (isatty(fileno(stdout))) { |
| 84 |
> |
if (isatty(fileno(stdout)) && (getenv("SGE_TASK_ID")==NULL)) { |
| 85 |
|
// get the window width: |
| 86 |
|
struct winsize w; |
| 87 |
|
ioctl(fileno(stdout), TIOCGWINSZ, &w); |
| 88 |
|
width = w.ws_col; |
| 89 |
|
|
| 90 |
+ |
// handle the case when the width is returned as a nonsensical value. |
| 91 |
+ |
if (width <= 0) width = 80; |
| 92 |
+ |
|
| 93 |
|
// We'll use: |
| 94 |
|
// 31 characters for the completion estimate, |
| 95 |
|
// 6 for the % complete, |
| 96 |
|
// 2 characters for the open and closing brackets. |
| 97 |
|
|
| 98 |
< |
int avail = width - 31 - 6 - 2; |
| 99 |
< |
|
| 98 |
> |
int avail = width - 31 - 6 - 2; |
| 99 |
> |
|
| 100 |
|
++iteration_; |
| 101 |
< |
|
| 101 |
> |
|
| 102 |
|
if (maximum_ > 0.0) { |
| 103 |
|
|
| 104 |
|
// we know the maximum, so draw a progress bar |
| 105 |
|
|
| 106 |
|
RealType percent = value_ * 100.0 / maximum_; |
| 107 |
|
int hashes = int(percent * avail / 100.0); |
| 102 |
– |
std::string progressbar; |
| 103 |
– |
progressbar.assign(hashes, '#'); |
| 108 |
|
|
| 105 |
– |
// add the spinner to the end of the progress bar: |
| 106 |
– |
progressbar += progressSpinner_[iteration_ & 3]; |
| 107 |
– |
|
| 109 |
|
// compute the best estimate of the ending time: |
| 110 |
|
time_t current_ = time(NULL); |
| 111 |
|
time_t end_ = start_ + (current_ - start_) * (100.0/percent); |
| 112 |
|
struct tm * ender = localtime(&end_); |
| 113 |
< |
char buffer[24]; |
| 114 |
< |
strftime(buffer, 24, "%a %b %d @ %I:%M %p", ender); |
| 113 |
> |
char buffer[22]; |
| 114 |
> |
strftime(buffer, 22, "%a %b %d @ %I:%M %p", ender); |
| 115 |
|
|
| 116 |
< |
std::stringstream fmt; |
| 117 |
< |
fmt << "\r%3d%% [%-" << avail << "s] Estimate: %s"; |
| 118 |
< |
std::string st = fmt.str(); |
| 119 |
< |
|
| 120 |
< |
printf(st.c_str(), int(percent), |
| 121 |
< |
progressbar.c_str(), |
| 122 |
< |
buffer); |
| 123 |
< |
|
| 124 |
< |
} else { |
| 125 |
< |
// we don't know the maximum, so we can't draw a progress bar |
| 126 |
< |
int center = (iteration_ % 48) + 1; // 50 spaces, minus 2 |
| 127 |
< |
std::string before; |
| 128 |
< |
std::string after; |
| 129 |
< |
before.assign(std::max(center - 2, 0), ' '); |
| 130 |
< |
after.assign(std::min(center + 2, 50), ' '); |
| 131 |
< |
|
| 132 |
< |
printf("\r[%s###%s] ", |
| 133 |
< |
before.c_str(), after.c_str()); |
| 116 |
> |
cout << '\r'; |
| 117 |
> |
cout.width(3); |
| 118 |
> |
cout << right << int(percent); |
| 119 |
> |
cout.width(3); |
| 120 |
> |
cout << "% ["; |
| 121 |
> |
cout.fill('#'); |
| 122 |
> |
if (hashes+1 < avail) { |
| 123 |
> |
cout.width(hashes+1); |
| 124 |
> |
cout << progressSpinner_[iteration_ & 3]; |
| 125 |
> |
} else { |
| 126 |
> |
cout.width(avail); |
| 127 |
> |
cout << '#'; |
| 128 |
> |
} |
| 129 |
> |
cout.fill(' '); |
| 130 |
> |
if (avail - hashes - 1 > 0) { |
| 131 |
> |
cout.width(avail - hashes - 1); |
| 132 |
> |
cout << ' '; |
| 133 |
> |
} |
| 134 |
> |
cout.width(11); |
| 135 |
> |
cout << "] Estimate:"; |
| 136 |
> |
cout.width(22); |
| 137 |
> |
cout << buffer; |
| 138 |
> |
|
| 139 |
|
} |
| 140 |
< |
fflush(stdout); |
| 140 |
> |
cout.flush(); |
| 141 |
|
} |
| 142 |
|
#ifdef IS_MPI |
| 143 |
|
} |