libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/job.h
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef JOB_H_
00034 #define JOB_H_
00035 
00036 #include "configuration.h"
00037 #include "metaparser.h"
00038 #include "utils.h"
00039 
00040 #include <sstream>
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 
00044 #ifndef __WINDOWS__
00045 #include <sys/wait.h>
00046 #include <unistd.h>
00047 #endif
00048 
00049 using namespace std;
00050 
00051 namespace ebl {
00052 
00054   // job
00055   
00057   class EXPORT job {
00058   public:
00064     job(configuration &conf, const char *oconffname = NULL,
00065         bool resume = false);
00067     virtual ~job();
00068 
00070     virtual void run();
00071 
00073     virtual bool started();
00074 
00077     virtual bool write(bool reset_progress = false);
00078 
00082     virtual bool alive();
00083 
00087     virtual bool running();
00089     virtual int getpid();
00091     virtual string &name();
00093     virtual string shortname();
00094 
00096     virtual string get_root();
00098     virtual double minutes();
00100     virtual void force_started();
00102     virtual void reset_started();
00104     virtual string elapsed();
00106     virtual bool resumed();
00108     virtual bool finished();
00111     virtual int progress() const;
00112 
00114     // file-based resuming capabilities
00115 
00119     virtual bool declare_started();
00123     virtual bool declare_finished();
00125     virtual bool check_started();
00130     virtual bool check_running();
00137     virtual int check_progress();
00139     virtual bool check_finished();
00145     static void write_progress(uint i, uint total,
00146                                const char *additional = NULL,
00147                                const char *root = NULL);
00149     static string get_progress_filename(const char *root = NULL);
00153     static void write_finished(const char *root = NULL);
00156     virtual void figure_resume_from_weights(const char *dir = NULL);
00157     
00158   protected:
00160     virtual void run_child();
00164     virtual void figure_resume_out();
00165 
00167     // members
00168   protected:
00169     configuration       conf; 
00170     configuration       rconf; 
00171     string              exe;    
00172     string              outdir; 
00173     string              confname;       
00174     string              oconffname_;    
00175     timer               t;
00176     bool                _locally_started; 
00177     bool                _started; 
00178     bool                _running; 
00179     bool                _alive; 
00180     int                 pid;    
00181     bool                resumed_; 
00182     bool                _finished; 
00183     int                 _progress; 
00184     string              progress_fname; 
00185     string              finished_fname; 
00186   };
00187 
00189   EXPORT bool job_progress_cmp(const job *a, const job *b);
00190 
00192   // job manager
00193 
00195   class EXPORT job_manager {
00196   public:
00198     job_manager();
00199 
00201     virtual ~job_manager();
00202 
00212     virtual bool read_metaconf(const char *fname, const string *tstamp = NULL,
00213                                const char *resume_name = NULL, 
00214                                bool resumedir = false,
00215                                bool nomax = false, int maxjobs = -1);
00217     virtual void set_copy(const string &path);
00220     virtual void prepare(bool reset_progress = false);
00223     virtual void initialize_other(const string &other);
00225     virtual void run(bool force_start = false);
00228     virtual void monitor(const char *conffname);
00229 
00230   protected:
00231 
00233     virtual void release_dead_children();
00236     virtual void jobs_info();
00238     virtual void report();
00240     virtual void last_report();
00243     virtual list<string> *list_job_dirs(const char *conffname);
00244 
00246     // members
00247   protected:
00248     meta_configuration  mconf;     
00249     meta_configuration  rmconf;    
00250     string              mconf_fullfname;
00251     string              mconf_fname;    
00252     vector<job*>        jobs;      
00253     string              copy_path; 
00254     uint                max_jobs;  
00255     metaparser          parser;
00256     int                 maxiter;
00257     int                 maxiter_tmp;
00258     double              mintime;
00259     double              maxtime;
00260     uint                nalive;
00261     uint                nrunning;
00262     uint                unstarted;
00263     uint                finished;
00264     uint                unfinished;
00265     int                 ready_slots;
00266     ostringstream       infos;
00267     ostringstream       summary; 
00268     varmaplist          best; 
00269     varmaplist          besteach; 
00270     uint                swait; 
00271     timer               time; 
00272   };
00273 
00274 } // end namespace ebl
00275 
00276 #endif /* JOB_H_ */