00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
#include "ProgressBar.h"
00047
#include "stringutils.h"
00048
00049
#if USING_MPI
00050
#include <plearn/sys/PLMPI.h>
00051
#endif //USING_MPI
00052
00053
namespace PLearn {
00054
using namespace std;
00055
00056
00057 PP<ProgressBarPlugin> ProgressBar::plugin;
00058
00059 PP<ProgressBarPlugin> ProgressBar::getCurrentPlugin()
00060 {
00061
if (
plugin == NULL)
00062
plugin =
new TextProgressBarPlugin(cerr);
00063
return plugin;
00064 }
00065
00066 ProgressBar::ProgressBar(
string _title,
unsigned long the_maxpos)
00067 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false)
00068 {
00069
if (
plugin == NULL)
00070
plugin =
new TextProgressBarPlugin(cerr);
00071
00072
plugin->addProgressBar(
this);
00073 }
00074
00075 ProgressBar::ProgressBar(ostream& _out,
string _title,
unsigned long the_maxpos)
00076 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false)
00077 {
00078
if (
plugin == NULL)
00079
plugin =
new TextProgressBarPlugin(cerr);
00080
00081
plugin->addProgressBar(
this);
00082 }
00083 ProgressBar::ProgressBar(
PStream& _out,
string _title,
unsigned long the_maxpos)
00084 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false)
00085 {
00086
if (
plugin == NULL)
00087
plugin =
new TextProgressBarPlugin(cerr);
00088
00089
plugin->addProgressBar(
this);
00090 }
00091
00092 ProgressBar::~ProgressBar()
00093 {
00094
close();
00095 }
00096
00097 void ProgressBar::close()
00098 {
00099
if(
closed)
00100
return;
00101
closed=
true;
00102
if(
currentpos<
maxpos)
00103
operator()(
maxpos);
00104
plugin->killProgressBar(
this);
00105 }
00106
00107 TextProgressBarPlugin::TextProgressBarPlugin(ostream& _out)
00108 :out(&_out)
00109 {
00110
out.
outmode=PStream::raw_ascii;
00111 }
00112
00113 TextProgressBarPlugin::TextProgressBarPlugin(
PStream& _out)
00114 :out(_out)
00115 {
00116 }
00117
00118 void TextProgressBarPlugin::addProgressBar(
ProgressBar * pb)
00119 {
00120
#if USING_MPI
00121
if(PLMPI::rank==0)
00122 {
00123
#endif
00124
string fulltitle =
string(
" ") + pb->
title +
" (" +
tostring(pb->
maxpos) +
") ";
00125
out <<
"[" +
center(fulltitle,100,
'-') +
"]\n[";
00126
out.
flush();
00127
#if USING_MPI
00128
}
00129
#endif
00130
}
00131
00132 void TextProgressBarPlugin::update(
ProgressBar * pb,
unsigned long newpos)
00133 {
00134
#if USING_MPI
00135
if(PLMPI::rank==0)
00136 {
00137
#endif
00138
00139
if(newpos < pb->
currentpos)
00140 {
00141 pb->
currentpos=0;
00142
string fulltitle =
string(
" ") + pb->
title +
" (" +
tostring(pb->
maxpos) +
") ";
00143
out <<
"\n[" +
center(fulltitle,100,
'-') +
"]\n[";
00144
out.
flush();
00145 }
00146
00147
if(!pb->
maxpos || newpos>pb->
maxpos)
00148
return;
00149
int ndots =
int(newpos / (
double(pb->
maxpos) / 100)) - int(pb->
currentpos / (
double(pb->
maxpos) / 100));
00150
if (ndots < 0)
00151
PLERROR(
"In TextProgressBarPlugin::update - Trying to plot an infinite number of dots");
00152
while(ndots--)
00153
out <<
'.';
00154
out.
flush();
00155 pb->
currentpos = newpos;
00156
if(pb->
currentpos==pb->
maxpos)
00157 {
00158
out <<
"]";
00159
out <<
endl;
00160 }
00161
#if USING_MPI
00162
}
00163
#endif
00164
}
00165
00166
00167 }
00168
00169
00170
00171