PhoenixThread  1.0.0
Tools to ease parallel programming in C++
Loading...
Searching...
No Matches
PMultiThreadProgress Class Reference

Deal with Progress bar in mutlithread mode. More...

#include <PMultiThreadProgress.h>

Public Member Functions

size_t addProgressBar (const PString &name, int progressMax=1)
 Add a progress line.
 
void incrementProgress (size_t index)
 Increment the progress of the progress bar at index.
 
bool isFinished () const
 Say if the PMultiThreadProgress is finished.
 
PMultiThreadProgressoperator= (const PMultiThreadProgress &other)
 Definition of equal operator of PMultiThreadProgress.
 
 PMultiThreadProgress (const PMultiThreadProgress &other)
 Copy constructor of PMultiThreadProgress.
 
 PMultiThreadProgress (size_t nbExpectedProgressBar=1lu)
 Default constructor of PMultiThreadProgress.
 
void print ()
 Print the multithread progress bar.
 
void printSummary () const
 Print a summary of all the progress bars.
 
void setError (size_t index)
 Mark the progress bar at index finished with an error.
 
void setNbExpectedProgressBar (size_t nbExpectedProgressBar)
 Set the number of expected progress bars in the PMultiThreadProgress.
 
virtual ~PMultiThreadProgress ()
 Destructor of PMultiThreadProgress.
 

Protected Member Functions

void copyPMultiThreadProgress (const PMultiThreadProgress &other)
 Copy function of PMultiThreadProgress.
 

Private Member Functions

void eraseProgressBar (size_t nbColTerminal)
 Erase all progress bars.
 
size_t getNbColTerminal () const
 Get the number of columns in the current terminal.
 
void initialisationPMultiThreadProgress (size_t nbExpectedProgressBar)
 Initialisation function of the class PMultiThreadProgress.
 
bool isModified ()
 Check if the PMultiThreadProgress has been modified.
 
void printAllFinshedProgressBar ()
 Print all finished progress bars.
 
void printProgressBar (size_t nbColTerminal)
 Print all progress bars.
 

Private Attributes

size_t p_alreadyPrintedLine
 Nunmber of already printed line.
 
std::mutex p_currentMutex
 Mutex of the class to avoid conflict manipulation between threads.
 
size_t p_nbExpectedProgressBar
 Number of expected progress bars.
 
size_t p_previousNbProgressBar
 Previous number of progress bars.
 
VecProgressElement p_vecProgressElement
 Vector of pair (progress, between 0 and 100) and name of progress.
 

Detailed Description

Deal with Progress bar in mutlithread mode.

Definition at line 36 of file PMultiThreadProgress.h.

Constructor & Destructor Documentation

◆ PMultiThreadProgress() [1/2]

PMultiThreadProgress::PMultiThreadProgress ( size_t nbExpectedProgressBar = 1lu)

Default constructor of PMultiThreadProgress.

Parameters
nbExpectedProgressBar: number of expected progress bars

Definition at line 29 of file PMultiThreadProgress.cpp.

29 {
30 initialisationPMultiThreadProgress(nbExpectedProgressBar);
31}
void initialisationPMultiThreadProgress(size_t nbExpectedProgressBar)
Initialisation function of the class PMultiThreadProgress.

References initialisationPMultiThreadProgress().

Referenced by copyPMultiThreadProgress(), operator=(), and PMultiThreadProgress().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ PMultiThreadProgress() [2/2]

PMultiThreadProgress::PMultiThreadProgress ( const PMultiThreadProgress & other)

Copy constructor of PMultiThreadProgress.

Parameters
other: class to copy

Definition at line 36 of file PMultiThreadProgress.cpp.

36 {
38}
void copyPMultiThreadProgress(const PMultiThreadProgress &other)
Copy function of PMultiThreadProgress.

References copyPMultiThreadProgress(), and PMultiThreadProgress().

+ Here is the call graph for this function:

◆ ~PMultiThreadProgress()

PMultiThreadProgress::~PMultiThreadProgress ( )
virtual

Destructor of PMultiThreadProgress.

Definition at line 41 of file PMultiThreadProgress.cpp.

41 {
42
43}

Member Function Documentation

◆ addProgressBar()

size_t PMultiThreadProgress::addProgressBar ( const PString & name,
int progressMax = 1 )

Add a progress line.

Parameters
name: name of the progress line
progressMax: maximum of progression bar
Returns
index of the created progress line

Definition at line 66 of file PMultiThreadProgress.cpp.

66 {
67 std::lock_guard<std::mutex> guard(p_currentMutex);
68 size_t index = p_vecProgressElement.size();
69 ProgressElement el;
70 el.progress = 0;
71 el.prevProgress = -1;
72 el.progressMax = progressMax;
73 el.name = name;
74 el.isEndError = false;
75 p_vecProgressElement.push_back(el);
76 return index;
77}
std::mutex p_currentMutex
Mutex of the class to avoid conflict manipulation between threads.
VecProgressElement p_vecProgressElement
Vector of pair (progress, between 0 and 100) and name of progress.
int prevProgress
Previous progression.
PString name
Name of the progress bar.
int progressMax
Maximum of the progression.
bool isEndError
True if the progress ends with an error.
int progress
Current progression.

References ProgressElement::isEndError, ProgressElement::name, p_currentMutex, p_vecProgressElement, ProgressElement::prevProgress, ProgressElement::progress, and ProgressElement::progressMax.

Referenced by phoenix_program_callVecProgress().

+ Here is the caller graph for this function:

◆ copyPMultiThreadProgress()

void PMultiThreadProgress::copyPMultiThreadProgress ( const PMultiThreadProgress & other)
protected

Copy function of PMultiThreadProgress.

Parameters
other: class to copy

Definition at line 127 of file PMultiThreadProgress.cpp.

127 {
131}
size_t p_nbExpectedProgressBar
Number of expected progress bars.
size_t p_alreadyPrintedLine
Nunmber of already printed line.

References p_alreadyPrintedLine, p_nbExpectedProgressBar, p_vecProgressElement, and PMultiThreadProgress().

Referenced by operator=(), and PMultiThreadProgress().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ eraseProgressBar()

void PMultiThreadProgress::eraseProgressBar ( size_t nbColTerminal)
private

Erase all progress bars.

Parameters
nbColTerminal: number of columns in the current terminal

Definition at line 161 of file PMultiThreadProgress.cpp.

161 {
162 if(p_previousNbProgressBar == 0lu){return;}
163 //Advances method but can't write anyting
164 std::cerr << "\33[2K\r"; //Remove a line where the cursor is
165 for(size_t i(p_alreadyPrintedLine + 1u); i < p_previousNbProgressBar; ++i){
166 std::cerr << "\033[A"; //Go to one line up
167 std::cerr << "\33[2K\r"; //Remove a line where the cursor is
168 }
169}
size_t p_previousNbProgressBar
Previous number of progress bars.

References p_alreadyPrintedLine, and p_previousNbProgressBar.

Referenced by print().

+ Here is the caller graph for this function:

◆ getNbColTerminal()

size_t PMultiThreadProgress::getNbColTerminal ( ) const
private

Get the number of columns in the current terminal.

Returns
number of columns in the current terminal

Definition at line 227 of file PMultiThreadProgress.cpp.

227 {
228 struct winsize w;
229 ioctl(0, TIOCGWINSZ, &w);
230 return w.ws_col;
231}

Referenced by print().

+ Here is the caller graph for this function:

◆ incrementProgress()

void PMultiThreadProgress::incrementProgress ( size_t index)

Increment the progress of the progress bar at index.

Parameters
index: index of the progress bar to be incremented

Definition at line 82 of file PMultiThreadProgress.cpp.

82 {
83 p_vecProgressElement[index].progress++;
84}

References p_vecProgressElement.

Referenced by phoenix_program_callVecProgress().

+ Here is the caller graph for this function:

◆ initialisationPMultiThreadProgress()

void PMultiThreadProgress::initialisationPMultiThreadProgress ( size_t nbExpectedProgressBar)
private

Initialisation function of the class PMultiThreadProgress.

Parameters
nbExpectedProgressBar: number of expected progress bars

Definition at line 136 of file PMultiThreadProgress.cpp.

136 {
139 p_nbExpectedProgressBar = nbExpectedProgressBar;
140 p_vecProgressElement.reserve(nbExpectedProgressBar);
141}

References p_alreadyPrintedLine, p_nbExpectedProgressBar, p_previousNbProgressBar, and p_vecProgressElement.

Referenced by PMultiThreadProgress().

+ Here is the caller graph for this function:

◆ isFinished()

bool PMultiThreadProgress::isFinished ( ) const

Say if the PMultiThreadProgress is finished.

Returns
true if the PMultiThreadProgress is finished, false otherwise

Definition at line 96 of file PMultiThreadProgress.cpp.

References p_alreadyPrintedLine, p_nbExpectedProgressBar, and p_vecProgressElement.

Referenced by phoenix_print_parallel_progress().

+ Here is the caller graph for this function:

◆ isModified()

bool PMultiThreadProgress::isModified ( )
private

Check if the PMultiThreadProgress has been modified.

Returns
true if the PMultiThreadProgress has been modified, false otherwise

Definition at line 146 of file PMultiThreadProgress.cpp.

146 {
147 std::lock_guard<std::mutex> guard(p_currentMutex);
148 bool isModif(false);
149 size_t i(p_alreadyPrintedLine);
150 while(!isModif && i < p_vecProgressElement.size()){
151 ProgressElement & el = p_vecProgressElement[i];
152 isModif = el.progress != el.prevProgress || el.isEndError;
153 ++i;
154 }
155 return isModif;
156}

References ProgressElement::isEndError, p_alreadyPrintedLine, p_currentMutex, p_vecProgressElement, ProgressElement::prevProgress, and ProgressElement::progress.

Referenced by print().

+ Here is the caller graph for this function:

◆ operator=()

PMultiThreadProgress & PMultiThreadProgress::operator= ( const PMultiThreadProgress & other)

Definition of equal operator of PMultiThreadProgress.

Parameters
other: class to copy
Returns
copied class

Definition at line 49 of file PMultiThreadProgress.cpp.

49 {
51 return *this;
52}

References copyPMultiThreadProgress(), and PMultiThreadProgress().

+ Here is the call graph for this function:

◆ print()

void PMultiThreadProgress::print ( )

Print the multithread progress bar.

Definition at line 101 of file PMultiThreadProgress.cpp.

101 {
102 if(!isModified()){return;} //No need to print
103 size_t nbColTerminal = getNbColTerminal();
104 //Clear all the progress bars on screen
105 eraseProgressBar(nbColTerminal);
106 //Check if we can print first finshed lines definitely
108 //Update the rest of the progress bars to print
109 printProgressBar(nbColTerminal);
110 //We save the previous number of progress bars, in case we print some and add other later
112}
bool isModified()
Check if the PMultiThreadProgress has been modified.
void printProgressBar(size_t nbColTerminal)
Print all progress bars.
void eraseProgressBar(size_t nbColTerminal)
Erase all progress bars.
void printAllFinshedProgressBar()
Print all finished progress bars.
size_t getNbColTerminal() const
Get the number of columns in the current terminal.

References eraseProgressBar(), getNbColTerminal(), isModified(), p_previousNbProgressBar, p_vecProgressElement, printAllFinshedProgressBar(), and printProgressBar().

Referenced by phoenix_print_parallel_progress().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ printAllFinshedProgressBar()

void PMultiThreadProgress::printAllFinshedProgressBar ( )
private

Print all finished progress bars.

Definition at line 204 of file PMultiThreadProgress.cpp.

204 {
205 std::lock_guard<std::mutex> guard(p_currentMutex);
206 size_t i(p_alreadyPrintedLine);
207 while(i < p_vecProgressElement.size()){
208 ProgressElement & el = p_vecProgressElement[i];
209 if(el.progress >= el.progressMax || el.isEndError){ //This is a finished progress bar
210 std::cout << el.name << " : ";
211 if(el.isEndError){
212 std::cout << "\033[31mError\033[0m" << std::endl;
213 }else{
214 std::cout << "\033[32mDone\033[0m" << std::endl;
215 }
217 }else{
218 break;
219 }
220 ++i;
221 }
222}

References ProgressElement::isEndError, ProgressElement::name, p_alreadyPrintedLine, p_currentMutex, p_vecProgressElement, ProgressElement::progress, and ProgressElement::progressMax.

Referenced by print().

+ Here is the caller graph for this function:

◆ printProgressBar()

void PMultiThreadProgress::printProgressBar ( size_t nbColTerminal)
private

Print all progress bars.

Parameters
nbColTerminal: number of columns in the current terminal

Definition at line 174 of file PMultiThreadProgress.cpp.

174 {
175 for(size_t i(p_alreadyPrintedLine); i < p_vecProgressElement.size(); ++i){
176 ProgressElement & el = p_vecProgressElement[i];
177 std::cerr << el.name << " : ";
178 if(el.progress >= el.progressMax || el.isEndError){ //This is a finished progress bar but not finaly drawed
179 if(el.isEndError){
180 std::cerr << "\033[31mError\033[0m";
181 for(size_t j(el.name.size() + 8lu); j < nbColTerminal; ++j){
182 std::cerr << " ";
183 }
184 }else{
185 std::cerr << "\033[32mDone\033[0m";
186 for(size_t j(el.name.size() + 7lu); j < nbColTerminal; ++j){
187 std::cerr << " ";
188 }
189 }
190 }else{
191 int avencement = (100*el.progress)/el.progressMax;
192 std::stringstream strAvencement;
193 strAvencement << avencement;
194 std::cerr << avencement << "%";
195 for(size_t j(el.name.size() + 4lu + strAvencement.str().size()); j < nbColTerminal; ++j){
196 std::cerr << " ";
197 }
198 el.prevProgress = el.progress; //Update previous progress
199 }
200 }
201}

References ProgressElement::isEndError, ProgressElement::name, p_alreadyPrintedLine, p_vecProgressElement, ProgressElement::prevProgress, ProgressElement::progress, and ProgressElement::progressMax.

Referenced by print().

+ Here is the caller graph for this function:

◆ printSummary()

void PMultiThreadProgress::printSummary ( ) const

Print a summary of all the progress bars.

Definition at line 115 of file PMultiThreadProgress.cpp.

115 {
116 size_t nbFail(0lu);
117 for(VecProgressElement::const_iterator it(p_vecProgressElement.begin()); it != p_vecProgressElement.end(); ++it){
118 nbFail += it->isEndError;
119 }
120 size_t nbSuccess(p_vecProgressElement.size() - nbFail);
121 std::cout << "PMultiThreadProgress : " << p_vecProgressElement.size() << " computations, " << nbSuccess << " success, " << nbFail << " fail" << std::endl;
122}

References p_vecProgressElement.

Referenced by phoenix_print_parallel_progress().

+ Here is the caller graph for this function:

◆ setError()

void PMultiThreadProgress::setError ( size_t index)

Mark the progress bar at index finished with an error.

Parameters
index: index of the progress bar to be modified

Definition at line 89 of file PMultiThreadProgress.cpp.

89 {
90 p_vecProgressElement[index].isEndError = true;
91}

References p_vecProgressElement.

◆ setNbExpectedProgressBar()

void PMultiThreadProgress::setNbExpectedProgressBar ( size_t nbExpectedProgressBar)

Set the number of expected progress bars in the PMultiThreadProgress.

Parameters
nbExpectedProgressBar: number of expected progress bars

Definition at line 57 of file PMultiThreadProgress.cpp.

57 {
58 p_nbExpectedProgressBar = nbExpectedProgressBar;
59}

References p_nbExpectedProgressBar.

Member Data Documentation

◆ p_alreadyPrintedLine

size_t PMultiThreadProgress::p_alreadyPrintedLine
private

◆ p_currentMutex

std::mutex PMultiThreadProgress::p_currentMutex
private

Mutex of the class to avoid conflict manipulation between threads.

Definition at line 69 of file PMultiThreadProgress.h.

Referenced by addProgressBar(), isModified(), and printAllFinshedProgressBar().

◆ p_nbExpectedProgressBar

size_t PMultiThreadProgress::p_nbExpectedProgressBar
private

Number of expected progress bars.

Definition at line 73 of file PMultiThreadProgress.h.

Referenced by copyPMultiThreadProgress(), initialisationPMultiThreadProgress(), isFinished(), and setNbExpectedProgressBar().

◆ p_previousNbProgressBar

size_t PMultiThreadProgress::p_previousNbProgressBar
private

Previous number of progress bars.

Definition at line 71 of file PMultiThreadProgress.h.

Referenced by eraseProgressBar(), initialisationPMultiThreadProgress(), and print().

◆ p_vecProgressElement

VecProgressElement PMultiThreadProgress::p_vecProgressElement
private

The documentation for this class was generated from the following files: