PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc. 00007 // 00008 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 00039 00040 /* ******************************************************* 00041 * $Id: ProgressBar.cc 9789 2008-12-16 21:35:14Z nouiz $ 00042 * AUTHORS: Pascal Vincent 00043 * This file is part of the PLearn library. 00044 ******************************************************* */ 00045 00046 //#include <iomanip> 00047 #include "RemoteDeclareMethod.h" 00048 #include "ProgressBar.h" 00049 #include "stringutils.h" 00050 #include <math.h> 00051 #include <plearn/base/tostring.h> 00052 #include <plearn/math/pl_math.h> 00053 00054 #if USING_MPI 00055 #include <plearn/sys/PLMPI.h> 00056 #elif _OPENMP 00057 #include <omp.h> 00058 #endif //USING_MPI 00059 00060 namespace PLearn { 00061 using namespace std; 00062 00063 00064 PP<ProgressBarPlugin> ProgressBar::plugin; 00065 00066 PP<ProgressBarPlugin> ProgressBar::getCurrentPlugin() 00067 { 00068 if (plugin == NULL) 00069 plugin = new TextProgressBarPlugin(cerr); 00070 return plugin; 00071 } 00072 00073 ProgressBar::ProgressBar(string _title, uint32_t the_maxpos) 00074 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00075 { 00076 if (plugin == NULL) 00077 plugin = new TextProgressBarPlugin(cerr); 00078 00079 plugin->addProgressBar(this); 00080 } 00081 00082 ProgressBar::ProgressBar(ostream& _out, string _title, uint32_t the_maxpos) 00083 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00084 { 00085 if (plugin == NULL) 00086 plugin = new TextProgressBarPlugin(cerr); 00087 00088 plugin->addProgressBar(this); 00089 } 00090 ProgressBar::ProgressBar(PStream& _out, string _title, uint32_t the_maxpos) 00091 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00092 { 00093 if (plugin == NULL) 00094 plugin = new TextProgressBarPlugin(cerr); 00095 00096 plugin->addProgressBar(this); 00097 } 00098 00099 ProgressBar::~ProgressBar() 00100 { 00101 close(); 00102 } 00103 00104 void ProgressBar::close() 00105 { 00106 if(closed) 00107 return; 00108 closed=true; 00109 if(currentpos<maxpos) 00110 operator()(maxpos); 00111 plugin->killProgressBar(this); 00112 } 00113 00114 00115 int TextProgressBarPlugin::width = 100; 00116 00117 00118 TextProgressBarPlugin::TextProgressBarPlugin(ostream& _out) 00119 :out(&_out) 00120 { 00121 out.outmode=PStream::raw_ascii; 00122 } 00123 00124 TextProgressBarPlugin::TextProgressBarPlugin(PStream& _out) 00125 :out(_out) 00126 { 00127 } 00128 00129 void TextProgressBarPlugin::addProgressBar(ProgressBar * pb) 00130 { 00131 #if USING_MPI 00132 if(PLMPI::rank==0) 00133 #elif _OPENMP 00134 if(omp_get_thread_num()==0) 00135 #endif 00136 { 00137 00138 string fulltitle = string(" ") + pb->title + " (" + tostring(pb->maxpos) + ") "; 00139 out << "[" + center(fulltitle,width,'-') + "]\n["; 00140 out.flush(); 00141 } 00142 } 00143 00144 void TextProgressBarPlugin::update(ProgressBar * pb, uint32_t newpos) 00145 { 00146 #if USING_MPI 00147 if(PLMPI::rank==0) 00148 #elif _OPENMP 00149 if(omp_get_thread_num()==0) 00150 #endif 00151 { 00152 // this handles the case where we reuse the same progress bar 00153 if(newpos < pb->currentpos) 00154 { 00155 pb->currentpos=0; 00156 string fulltitle = string(" ") + pb->title + " (" + tostring(pb->maxpos) + ") "; 00157 out << "\n[" + center(fulltitle,width,'-') + "]\n["; 00158 out.flush(); 00159 } 00160 00161 if(!pb->maxpos || newpos>pb->maxpos) 00162 return; 00163 int ndots = int(round( newpos / (double(pb->maxpos) / width) )) - 00164 int(round( pb->currentpos / (double(pb->maxpos) / width) )); 00165 if (ndots < 0) 00166 PLERROR("In TextProgressBarPlugin::update - Trying to plot an infinite number of dots"); 00167 while(ndots--) 00168 out << '.'; 00169 out.flush(); 00170 pb->currentpos = newpos; 00171 if(pb->currentpos==pb->maxpos) 00172 { 00173 out << "]"; 00174 out << endl; 00175 } 00176 } 00177 } 00178 00179 00180 //*************************/ 00181 // RemoteProgressBarPlugin 00182 00183 RemoteProgressBarPlugin::RemoteProgressBarPlugin(ostream& _out, unsigned int nticks_) 00184 :TextProgressBarPlugin(_out), nticks(nticks_) 00185 {} 00186 00187 RemoteProgressBarPlugin::RemoteProgressBarPlugin(PStream& _out, unsigned int nticks_) 00188 :TextProgressBarPlugin(_out), nticks(nticks_) 00189 {} 00190 00191 void RemoteProgressBarPlugin::addProgressBar(ProgressBar* pb) 00192 { printTitle(pb); } 00193 00194 map<ProgressBar*, unsigned int> RemoteProgressBarPlugin::pb_ids;//init 00195 unsigned int RemoteProgressBarPlugin::next_pb_id= 0;//init 00196 00197 unsigned int RemoteProgressBarPlugin::getPBarID(ProgressBar* pb) 00198 { 00199 if(pb_ids.find(pb) == pb_ids.end()) 00200 pb_ids[pb]= ++next_pb_id; 00201 return pb_ids[pb]; 00202 } 00203 00204 00205 void RemoteProgressBarPlugin::update(ProgressBar* pb, uint32_t newpos) 00206 { 00207 // this handles the case where we reuse the same progress bar 00208 if(newpos < pb->currentpos) 00209 { 00210 pb->currentpos=0; 00211 printTitle(pb); 00212 } 00213 if(0 < (int( newpos / (double(pb->maxpos) / nticks) ) - 00214 int(round( pb->currentpos / (double(pb->maxpos) / nticks) )))) 00215 { 00216 out.write("*PU "); 00217 out << getPBarID(pb) << newpos << endl; 00218 pb->currentpos = newpos; 00219 } 00220 } 00221 00222 void RemoteProgressBarPlugin::printTitle(ProgressBar* pb) 00223 { 00224 string fulltitle = string(" ") + pb->title + " (" + tostring(pb->maxpos) + ") "; 00225 out.write("*PA "); 00226 out << getPBarID(pb) << pb->maxpos << fulltitle << endl; 00227 } 00228 00229 void RemoteProgressBarPlugin::killProgressBar(ProgressBar* pb) 00230 { 00231 out.write("*PK "); 00232 out << getPBarID(pb) << endl; 00233 } 00234 00235 //*************************/ 00236 // LineOutputProgressBarPlugin 00237 00238 LineOutputProgressBarPlugin::LineOutputProgressBarPlugin(ostream& _out, unsigned int nticks_) 00239 :TextProgressBarPlugin(_out), nticks(nticks_) 00240 {} 00241 00242 LineOutputProgressBarPlugin::LineOutputProgressBarPlugin(PStream& _out, unsigned int nticks_) 00243 :TextProgressBarPlugin(_out), nticks(nticks_) 00244 {} 00245 00246 void LineOutputProgressBarPlugin::addProgressBar(ProgressBar* pb) 00247 { out << "In progress: " << pbInfo(pb) << endl; } 00248 00249 void LineOutputProgressBarPlugin::update(ProgressBar* pb, uint32_t newpos) 00250 { 00251 // this handles the case where we reuse the same progress bar 00252 if(newpos < pb->currentpos) 00253 { 00254 pb->currentpos= newpos; 00255 out << "In progress: ";//to be continued... 00256 } 00257 else if(0 < (int( newpos / (double(pb->maxpos) / nticks) ) - 00258 int(round( pb->currentpos / (double(pb->maxpos) / nticks) )))) 00259 pb->currentpos= newpos; 00260 out << pbInfo(pb) << endl; 00261 } 00262 00263 void LineOutputProgressBarPlugin::killProgressBar(ProgressBar* pb) 00264 { out << pbInfo(pb) << " Finished" << endl; } 00265 00266 string LineOutputProgressBarPlugin::pbInfo(ProgressBar* pb) 00267 { 00268 unsigned int curpos= pb->currentpos, 00269 maxpos= pb->maxpos; 00270 return string("[") + pb->title + "] " 00271 + tostring(curpos) + '/' + tostring(maxpos) 00272 + " (" + tostring(static_cast<double>(curpos)*100.0 / static_cast<double>(maxpos)) + "%)"; 00273 } 00274 00275 00276 void setProgressBarPlugin(string pb_type) 00277 { 00278 if(pb_type == "none") 00279 ProgressBar::setPlugin(new NullProgressBarPlugin); 00280 else if(pb_type == "text") 00281 ProgressBar::setPlugin(new TextProgressBarPlugin(cerr)); 00282 else 00283 PLERROR("Invalid ProgressBar type: %s", pb_type.c_str()); 00284 } 00285 00286 BEGIN_DECLARE_REMOTE_FUNCTIONS 00287 00288 declareFunction("setProgressBarPlugin", &setProgressBarPlugin, 00289 (BodyDoc("Sets the progress bar plugin.\n"), 00290 ArgDoc ("pb_type", "one of: 'none','text'."))); 00291 00292 END_DECLARE_REMOTE_FUNCTIONS 00293 00294 00295 00296 } // end of namespace PLearn 00297 00298 00299 /* 00300 Local Variables: 00301 mode:c++ 00302 c-basic-offset:4 00303 c-file-style:"stroustrup" 00304 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00305 indent-tabs-mode:nil 00306 fill-column:79 00307 End: 00308 */ 00309 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :