KSquare Utilities
KKJob.h
Go to the documentation of this file.
1 #ifndef _KKJOB_
2 #define _KKJOB_
3 
4 //***************************************************************************
5 //* *
6 //* Kurt Kramer *
7 //* The RandomSplits process uses this library. *
8 //* *
9 //* *
10 //* Kurt Kramer *
11 //* 2009-10-10 *
12 //* *
13 //* This code is currently not in use. I am toying with the idea to make *
14 //* a general KKJob Management Frame Work. The Idea is that 'KKJob' and *
15 //* 'KKJobManager' would be used to build specific KKJob Management code *
16 //* arround. In the case of 'FeatureSeletion' we would detrive sub- *
17 //* classes to manage the Major Speps and Jobs which would then in turn *
18 //* call a different set of Derved classes that would manage Binary, *
19 //* MultiClass, Parameter Tuning, and Feature Selection tasks. *
20 //* *
21 //* ex: KKJobManager would be replaced by. *
22 //* ParameterManager:: public KKJobManager *
23 //* FeatureSelectionManager:: public KKJobManager *
24 //* *
25 //* Would also need to create a set of classes to manage the RandomSplits *
26 //* *
27 //***************************************************************************
28 
29 
30 
31 
32 
33 
34 #include "KKQueue.h"
35 #include "OSservices.h"
36 #include "RunLog.h"
37 #include "KKStr.h"
38 
39 
40 
41 /**
42  *@Namespace KKJobManagment
43  *@brief A framework for managing a large number of processes(Jobs) in a multi-cpu/ multi-o/s environment.
44  *@details The Idea is that 'KKJob' and 'KKJobManager' would be used to build specific KKJob Management code
45  * arround. In the case of 'FeatureSeletion' we would detrive sub-classes to manage the Major Speps and
46  * Jobs which would then in turn call a different set of Derved classes that would manage Binary, MultiClass,
47  * Parameter Tuning, and Feature Selection tasks.
48  *@code
49  * ex: KKJobManager would be replaced by.
50  * ParameterManager:: public KKJobManager
51  * FeatureSelectionManager:: public KKJobManager
52  *@endcode
53  *
54  * The RandomSplits application makes use of the framework.
55  */
56 namespace KKJobManagment
57 {
58  class KKJobList;
59  typedef KKJobList* KKJobListPtr;
60 
61 
62  #if !defined(_JobManagerDefined_)
63  class KKJobManager;
64  typedef KKJobManager* JobManagerPtr;
65  #endif
66 
67 
68  class KKJob
69  {
70  public:
71  typedef enum {jsNULL, jsOpen, jsStarted, jsDone, jsExpanded} JobStatus;
72 
73  typedef enum {jtNULL, jtBinaryCombo, jtMultiClass, jtRandomSplit} JobTypes;
74 
75  typedef KKJob* KKJobPtr;
76 
77  KKJob (const KKJob& j);
78 
79  // To create a brand new job that has not been proceesed yet.
80  KKJob (JobManagerPtr _manager,
81  kkint32 _jobId,
82  kkint32 _parentId,
83  kkint32 _numPorcessesAllowed,
84  RunLog& _log
85  );
86 
87 
88  KKJob (JobManagerPtr _manager);
89 
90 
91  ~KKJob ();
92 
93 
94 
95  //*************************************************************************************************************************
96  typedef KKJobPtr (*ConstructorPtr)(JobManagerPtr _manager);
97 
99 
100  static ConstructorIndex registeredConstructors; /**< Track all the different constructors. Will be used to know wich constructor to
101  * use when reading the status file.
102  */
103 
104  static void RegisterConstructor (const KKStr& _name,
105  ConstructorPtr _constructor
106  );
107 
109  const KKStr& _jobTypeName,
110  const KKStr& _statusStr
111  );
112  //*************************************************************************************************************************
113 
114 
115 
116 
117  //***************************************** Access Methods **********************************************
118  kkint32 JobId () const {return jobId;}
119  kkint32 ParentId () const {return parentId;}
120  JobManagerPtr Manager () const {return manager;}
121  const VectorInt& Prerequisites () const {return prerequisites;}
122  RunLog& Log () {return log;}
123  JobStatus Status () const {return status;}
124  KKStr StatusStr () const;
125 
126  void JobId (kkint32 _jobId) {jobId = _jobId;}
127  void Status (JobStatus _status) {status = _status;}
128 
129 
130 
131  //************************** Methods that derived classes must implement ******************************
132  virtual KKJobPtr Duplicate () const; /**< Create a duplicate instance. */
133 
134 
135  virtual const char* JobType () const; /**< This will allow us to know which specific implementaion
136  * of 'KKJob' an instance really is.
137  */
138 
139  virtual void ProcessNode ();
140 
141  virtual void ReFresh (KKJob& j);
142 
143  virtual KKStr ToStatusStr (); /**< Any derived classes that implement this method must call its base class version 1st. */
144 
145 
146  /**
147  *@brief Imjpelmentation specific field processing.
148  *@details Any SubClass of KKJob needs to define this method. Whenever the 'ProcessStatusStr' method can not identify a field
149  * it will call this method to let the child Class process the field.
150  */
151  virtual
152  void ProcessStatusField (const KKStr& fieldName,
153  const KKStr& fieldValue
154  );
155 
156 
157  /**
158  *@brief Write out completed job results to status file.
159  *@details This method will get called right after the "KKJob" status line gets written when a KKJob is completed.
160  * See 'KKJobManager::GetNextSetOfJobs'. If a job needs to write more data to the Status file then you want to
161  * put on a single status line this is where you would do it. You write all the text in a format that you
162  * want to support. 'KKJobManager' will bracket it with <KKJob JobType=KKJob::JobType, JobId=####> and </KKJob>
163  *@code
164  * ex:
165  *<RandomSplitJob>
166  * KKJob Data;
167  * KKJob Data,
168  * etc. etc. etc.
169  *</RandomSplitJob>
170  *@endcode
171  */
172  virtual
173  void CompletedJobDataWrite (ostream& o);
174 
175 
176  /**@
177  *@brief Works with 'WriteCompletedJobData'; You use this to load in data written by 'WriteCompletedJobData'
178  */
179  virtual
180  void CompletedJobDataRead (istream& i);
181 
182 
183 
184  //*******************************************************************************************************
185 
186 
187  void AddPrerequisites (kkint32 _prerequisite);
189 
190  bool InPrerequisites (kkint32 _jobId); // Retuens true if '_jobId' is in 'prerequisites'.
191 
192 
193  static KKStr JobStatusToStr (JobStatus status);
194  static JobStatus JobStatusFromStr (const KKStr& statusStr);
195 
196  static KKStr JobTypeToStr (JobTypes jt);
197  static void JobTypeFromStr (const KKStr& s);
198 
199 
201 
202 
203  protected:
204  KKStr PrerequisitesToStr () const;
205  void PrerequisitesFromStr (const KKStr& s);
206 
207  void ProcessStatusStr (const KKStr& statusStr);
208 
212  kkint32 numProcessors; /**< Number of CPU's that are currently processing this node; That is
213  * the number that are currently calling the 'ProcessNode' method.
214  */
215 
216  kkint32 numPorcessesAllowed; /**< The number of Processes that are allowd to process at same time.
217  * that is ('numPorcessesAllowed' <= 'numActiveProcessors').
218  */
219 
220  VectorInt prerequisites; /**< List of JobId's that must complete (jobStatus == jsDone) before this
221  * process may be started.
222  */
223 
224  JobStatus status;
225  }; /* KKJob */
226 
227  typedef KKJob::KKJobPtr KKJobPtr;
228 
229  #define _KKJobDefined_
230 
231 
232 
233 
234 
235  class KKJobList: public KKQueue<KKJob>
236  {
237  public:
239 
240  KKJobList (const KKJobList& jobs);
241 
242  KKJobList (JobManagerPtr _manager);
243 
244  KKJobList (JobManagerPtr _manager,
245  KKStr _fileName
246  );
247 
248  bool AllPrequisitesDone (KKJobPtr job);
249 
250  bool AreAllJobsDone ();
251 
252  bool JobsStillRunning ();
253 
254  KKJobPtr LocateOpenJob ();
255 
256  RunLog& Log () {return log;}
257 
258  KKJobPtr LookUpByJobId (kkint32 jobId);
259 
260  void PushOnBack (KKJobPtr j);
261 
262 
263 
264  private:
265  RunLog& log;
266  JobManagerPtr manager;
267 
268  map<kkint32,KKJobPtr> jobIdLookUpTable;
269  map<kkint32,KKJobPtr>::iterator jobIdLookUpTableIdx;
270  typedef pair <kkint32, KKJobPtr> JobIdLookUpTablePair;
271 
272  }; /* KKJobList */
273 
275 
276  #define _JobListDefined_
277 } /* KKJobManagment */
278 
279 #endif
JobManagerPtr manager
Definition: KKJob.h:211
KKJob * KKJobPtr
Definition: KKJob.h:75
kkint32 numPorcessesAllowed
Definition: KKJob.h:216
static KKStr JobStatusToStr(JobStatus status)
Definition: KKJob.cpp:178
bool AllPrequisitesDone(KKJobPtr job)
Definition: KKJob.cpp:404
static void JobTypeFromStr(const KKStr &s)
virtual void ProcessNode()
Definition: KKJob.cpp:119
__int32 kkint32
Definition: KKBaseTypes.h:88
void PrerequisitesFromStr(const KKStr &s)
Definition: KKJob.cpp:243
KKJobList(JobManagerPtr _manager)
Definition: KKJob.cpp:359
const VectorInt & Prerequisites() const
Definition: KKJob.h:121
std::vector< int > VectorInt
Definition: KKBaseTypes.h:138
void AddPrerequisites(kkint32 _prerequisite)
KKJobManager * JobManagerPtr
Definition: KKJob.h:63
void Status(JobStatus _status)
Definition: KKJob.h:127
static void RegisterConstructor(const KKStr &_name, ConstructorPtr _constructor)
Definition: KKJob.cpp:135
KKStr StatusStr() const
Definition: KKJob.cpp:218
virtual KKJobPtr Duplicate() const
Definition: KKJob.cpp:112
KKJobList(JobManagerPtr _manager, KKStr _fileName)
map< KKStr, ConstructorPtr > ConstructorIndex
Definition: KKJob.h:98
static JobStatus JobStatusFromStr(const KKStr &statusStr)
Definition: KKJob.cpp:198
void AddPrerequisites(VectorInt _prerequisites)
virtual void CompletedJobDataWrite(ostream &o)
Write out completed job results to status file.
Definition: KKJob.cpp:344
void ProcessStatusStr(const KKStr &statusStr)
Definition: KKJob.cpp:278
static KKJobPtr CallAppropriateConstructor(JobManagerPtr _manager, const KKStr &_jobTypeName, const KKStr &_statusStr)
Definition: KKJob.cpp:145
static KKStr JobTypeToStr(JobTypes jt)
kkint32 ParentId() const
Definition: KKJob.h:119
virtual void CompletedJobDataRead(istream &i)
Works with &#39;WriteCompletedJobData&#39;; You use this to load in data written by &#39;WriteCompletedJobData&#39;.
Definition: KKJob.cpp:351
KKJob(JobManagerPtr _manager, kkint32 _jobId, kkint32 _parentId, kkint32 _numPorcessesAllowed, RunLog &_log)
JobStatus status
Definition: KKJob.h:224
virtual void ProcessStatusField(const KKStr &fieldName, const KKStr &fieldValue)
Imjpelmentation specific field processing.
Definition: KKJob.cpp:334
Responsable for keeping track of a list of jobs.
Definition: KKJobManager.h:29
KKJobPtr LocateOpenJob()
Definition: KKJob.cpp:420
kkint32 JobId() const
Definition: KKJob.h:118
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
kkint32 parentId
Definition: KKJob.h:210
void JobId(kkint32 _jobId)
Definition: KKJob.h:126
KKStr PrerequisitesToStr() const
Definition: KKJob.cpp:225
JobStatus Status() const
Definition: KKJob.h:123
KKJobList * KKJobListPtr
Definition: KKJob.h:238
virtual void ReFresh(KKJob &j)
Definition: KKJob.cpp:163
void PushOnBack(KKJobPtr j)
Definition: KKJob.cpp:456
KKJobList * KKJobListPtr
Definition: KKJob.h:58
virtual const char * JobType() const
Definition: KKJob.cpp:104
VectorInt prerequisites
Definition: KKJob.h:220
Used for logging messages.
Definition: RunLog.h:49
KKJob(const KKJob &j)
Definition: KKJob.cpp:42
JobManagerPtr Manager() const
Definition: KKJob.h:120
bool InPrerequisites(kkint32 _jobId)
A framework for managing a large number of processes(Jobs) in a multi-cpu/ multi-o/s environment...
Definition: KKJob.h:56
KKJob(JobManagerPtr _manager)
Definition: KKJob.cpp:82
kkint32 numProcessors
Definition: KKJob.h:212
RunLog & Log()
Definition: KKJob.h:122
KKJobList(const KKJobList &jobs)
Definition: KKJob.cpp:370
virtual KKStr ToStatusStr()
Definition: KKJob.cpp:261
static ConstructorIndex registeredConstructors
Definition: KKJob.h:100
KKJobPtr LookUpByJobId(kkint32 jobId)
Definition: KKJob.cpp:391