Add awesome progress bar

parent 31a38b75
......@@ -232,10 +232,6 @@ elif env['solver'] == 'hybrid':
elif env['solver'] == 'fwavevec':
env.Append(CPPDEFINES=['WAVE_PROPAGATION_SOLVER=4', 'PHYSICAL_VECTOR_SIZE='+str(env['physicalVectorSize'])])
# set the precompiler flags for serial version
if env['parallelization'] in ['none', 'cuda']:
env.Append(CPPDEFINES=['NOMPI'])
# set the precompiler flags for CUDA
if env['parallelization'] in ['cuda', 'mpi_with_cuda']:
env.Append(CPPDEFINES=['CUDA'])
......
......@@ -61,6 +61,7 @@
#include "../tools/Logger.hpp"
#include "tools/ProgressBar.hh"
/**
* Compute the number of block rows from the total number of processes.
......@@ -368,8 +369,12 @@ int main( int argc, char** argv ) {
l_topNeighborRank, l_topInflow, l_topOutflow,
l_mpiRow );
// Init fancy progressbar
tools::ProgressBar progressBar(l_endSimulation, l_mpiRank);
// write the output at time zero
tools::Logger::logger.printOutputTime(0);
progressBar.update(0.);
std::string l_fileName = generateBaseFileName(l_baseName,l_blockPositionX,l_blockPositionY);
//boundary size of the ghost layers
......@@ -396,17 +401,17 @@ int main( int argc, char** argv ) {
l_wavePropgationBlock.getDischarge_hu(),
l_wavePropgationBlock.getDischarge_hv(),
(float) 0.);
/**
* Simulation.
*/
// print the start message and reset the wall clock time
progressBar.clear();
tools::Logger::logger.printStartMessage();
tools::Logger::logger.initWallClockTime(time(NULL));
//! simulation time.
float l_t = 0.0;
progressBar.update(l_t);
// loop over checkpoints
for(int c=1; c<=l_numberOfCheckPoints; c++) {
......@@ -449,7 +454,9 @@ int main( int argc, char** argv ) {
l_t += l_maxTimeStepWidthGlobal;
// print the current simulation time
progressBar.clear();
tools::Logger::logger.printSimulationTime(l_t);
progressBar.update(l_t);
}
// update and reset the cpu time in the logger
......@@ -457,7 +464,9 @@ int main( int argc, char** argv ) {
tools::Logger::logger.resetCpuClockToCurrentTime();
// print current simulation time
progressBar.clear();
tools::Logger::logger.printOutputTime(l_t);
progressBar.update(l_t);
// write output
l_writer.writeTimeStep( l_wavePropgationBlock.getWaterHeight(),
......@@ -474,6 +483,8 @@ int main( int argc, char** argv ) {
l_scenario.deleteGrids();
#endif
progressBar.clear();
// write the statistics message
tools::Logger::logger.printStatisticsMessage();
......
......@@ -61,6 +61,7 @@
#include "../tools/Logger.hpp"
static tools::Logger s_sweLogger;
#endif
#include "tools/ProgressBar.hh"
/**
* Main program for the simulation on a single SWE_WavePropagationBlock.
......@@ -178,9 +179,12 @@ int main( int argc, char** argv ) {
l_checkPoints[cp] = cp*(l_endSimulation/l_numberOfCheckPoints);
}
// Init fancy progressbar
tools::ProgressBar progressBar(l_endSimulation);
// write the output at time zero
s_sweLogger.printOutputTime((float) 0.);
progressBar.update(0.);
std::string l_fileName = generateBaseFileName(l_baseName,0,0);
//boundary size of the ghost layers
......@@ -212,11 +216,13 @@ int main( int argc, char** argv ) {
* Simulation.
*/
// print the start message and reset the wall clock time
progressBar.clear();
s_sweLogger.printStartMessage();
s_sweLogger.initWallClockTime(time(NULL));
//! simulation time.
float l_t = 0.0;
progressBar.update(l_t);
// loop over checkpoints
for(int c=1; c<=l_numberOfCheckPoints; c++) {
......@@ -246,14 +252,18 @@ int main( int argc, char** argv ) {
l_t += l_maxTimeStepWidth;
// print the current simulation time
progressBar.clear();
s_sweLogger.printSimulationTime(l_t);
progressBar.update(l_t);
}
// update the cpu time in the logger
s_sweLogger.updateCpuTime();
// print current simulation time of the output
progressBar.clear();
s_sweLogger.printOutputTime(l_t);
progressBar.update(l_t);
// write output
l_writer.writeTimeStep( l_wavePropgationBlock.getWaterHeight(),
......@@ -266,6 +276,7 @@ int main( int argc, char** argv ) {
* Finalize.
*/
// write the statistics message
progressBar.clear();
s_sweLogger.printStatisticsMessage();
// print the cpu time
......
/**
* @file
* This file is part of SWE.
*
* @author Sebastian Rettenberger (rettenbs AT in.tum.de, http://www5.in.tum.de/wiki/index.php/Sebastian_Rettenberger,_M.Sc.)
*
* @section LICENSE
*
* SWE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SWE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SWE. If not, see <http://www.gnu.org/licenses/>.
*
*
* @section DESCRIPTION
*
* A simple progress bar using stdout
*/
#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H
#include <cassert>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <limits>
#include <unistd.h>
#include <sys/ioctl.h>
namespace tools
{
class ProgressBar
{
private:
/** Local rank (we only do work on rank 0) */
int m_rank;
/** Total amount of work */
float m_totalWork;
/** Progress bar initialization time */
time_t m_startTime;
/** Terminal size */
unsigned int m_terminalSize;
/** Rotating bar char */
unsigned char m_rotatingBar;
public:
ProgressBar(float totalWork = 1., int rank = 0)
: m_rank(rank),
m_totalWork(totalWork),
m_rotatingBar(0),
m_startTime(time(0))
{
if (rank != 0)
return;
#ifdef TIOCGSIZE
struct ttysize ts;
ioctl(STDIN_FILENO, TIOCGSIZE, &ts);
m_terminalSize = ts.ts_cols;
#elif defined(TIOCGWINSZ)
struct winsize ts;
ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);
m_terminalSize = ts.ws_col;
#else
m_terminalSize = 0;
#endif
}
/**
* @param done The amount of work already done
*/
void update(float done)
{
if (m_rank != 0 || m_terminalSize < MIN_TERM_SIZE)
return;
unsigned int printed = 2;
std::cout << '\r';
printed += printTimeLeft(done);
std::cout << ' ';
printed += printPercentage(done);
std::cout << ' ';
printProgressBar(done, m_terminalSize-printed-2);
std::cout << ' ';
printRotatingBar();
std::cout << std::flush;
}
void clear()
{
if (m_rank != 0 || m_terminalSize < MIN_TERM_SIZE)
return;
std::cout << '\r';
for (int i = 0; i < m_terminalSize; i++)
std::cout << ' ';
std::cout << '\r';
}
private:
/**
* @return Number of characters printed
*/
unsigned int printTimeLeft(float done)
{
float timeLeft;
if (done <= 0)
timeLeft = std::numeric_limits<float>::max();
else
timeLeft = (time(0) - m_startTime) * (m_totalWork - done) / done;
std::cout << "Time left: ";
if (timeLeft < 1) {
for (int i = 3; i < TIME_SIZE; i++)
std::cout << ' ';
std::cout << "< 1";
} else {
int digits = ceil(log(timeLeft)/log(10));
if (digits > TIME_SIZE) {
// Maximum number we can show
for (int i = 0; i < TIME_SIZE; i++)
std::cout << '9';
} else {
streamsize oldPrec = std::cout.precision();
std::ios::fmtflags oldFlags = std::cout.flags();
streamsize oldWidth = std::cout.width();
std::cout.precision(std::max(0, TIME_SIZE-digits-2));
std::cout.setf(std::ios::fixed);
std::cout.width(TIME_SIZE);
std::cout << timeLeft;
std::cout.precision(oldPrec);
std::cout.flags(oldFlags);
std::cout.width(oldWidth);
}
}
std::cout << " sec";
return 11+TIME_SIZE+4;
}
/**
* @return Number of characters printed
*/
unsigned int printPercentage(float done)
{
int per = floor(done/m_totalWork*100);
std::cout << '(';
streamsize oldWidth = std::cout.width();
std::cout.width(3);
std::cout << per;
std::cout.width(oldWidth);
std::cout << "% done)";
return 1+3+7;
}
void printProgressBar(float done, int size)
{
if (size < 3)
return;
size -= 2; // leave space for []
unsigned int per = floor(done/m_totalWork * size);
std::cout << '[';
for (int i = 0; i < per; i++)
std::cout << '=';
if (per < size) {
std::cout << '>';
per++;
}
for (int i = per; i < size; i++)
std::cout << ' ';
std::cout << ']';
}
void printRotatingBar()
{
static const char* CHARS = "|/-\\";
std::cout << CHARS[m_rotatingBar];
m_rotatingBar = (m_rotatingBar + 1) % 4;
}
static const int MIN_TERM_SIZE = 20;
static const int TIME_SIZE = 8;
};
}
#endif // PROGRESSBAR_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment