KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVBatchSystem.cpp
Go to the documentation of this file.
1 /*
2 $Id: KVBatchSystem.cpp,v 1.12 2008/04/14 08:49:11 franklan Exp $
3 $Revision: 1.12 $
4 $Date: 2008/04/14 08:49:11 $
5 */
6 
7 //Created by KVClassFactory on Thu Apr 13 13:07:59 2006
8 //Author: John Frankland
9 
10 #include "KVBatchSystem.h"
11 #include "KVBase.h"
12 #include "TEnv.h"
13 #include "TPluginManager.h"
14 #include "TSystem.h"
15 #include "KVDataAnalyser.h"
16 #include "KVDataAnalysisTask.h"
17 
18 using namespace std;
19 
21 
23 
24 
35 
37  : KVBase(name), fAnalyser(nullptr)
38 {
39  // Constructor with name of batch system. Name will be used to retrieve
40  // resources from configuration file, e.g. batch system title, job submission
41  // command, name of batch script, default job options, runs per job in multijob mode:
42  //
43  // [batch system name].BatchSystem.Title:
44  // [batch system name].BatchSystem.JobSubCmd:
45  // [batch system name].BatchSystem.JobScript:
46  // [batch system name].BatchSystem.DefaultJobOptions:
47 
48  //set title of batch system
49  SetTitle(gEnv->GetValue(Form("%s.BatchSystem.Title", name), ""));
50  //command for job submission
51  fJobSubCmd = gEnv->GetValue(Form("%s.BatchSystem.JobSubCmd", name), "");
52  //check command is valid
53  if (!KVBase::FindExecutable(fJobSubCmd)) {
54  Warning("KVBatchSystem", "Batch system %s has unknown job submission command: %s",
55  name, fJobSubCmd.Data());
56  }
57  //script for batch job
58  SetJobScript(gEnv->GetValue(Form("%s.BatchSystem.JobScript", name), ""));
59  //set default job options
60  SetDefaultJobOptions(gEnv->GetValue(Form("%s.BatchSystem.DefaultJobOptions", name), ""));
61 }
62 
63 
64 
67 
68 KVBatchSystem::~KVBatchSystem()
69 {
70  //Destructor
71  if (gBatchSystem == this)
72  gBatchSystem = 0;
73 }
74 
75 
76 
99 
101 {
102  //Builds and returns static string containing full command line necessary to submit a batch
103  //job. This is constructed as follows:
104  //
105  // [JobSubCmd] [default options] [par1 val1] [par2 val2] ... [JobScript]
106  //
107  //The 'default options' can be set with SetDefaultJobOptions().
108  //
109  //The 'par1,val1' pairs are the named parameters held in the fParList KVParameterList.
110  //For each parameter in the list, the name of the parameter is used for 'par1' and the value
111  //of the parameter is used for 'val1' etc. etc.
112  //e.g. if the job submission command requires an option to be passed with the following
113  //syntax
114  // [JobSubCmd] -Noption
115  //one of the parameters in the list should be called "-N" and its value should be 'option'
116  //
117  //In order to add a simple flag such as
118  // [JobSubCmd] -V
119  //add a parameter called "-V" with value "".
120  //
121  //The special variable #JobName# can be used anywhere and will be replaced by the
122  //jobname returned by GetJobName() at the moment of job submission.
123 
124  static TString command_line;
125  command_line.Form("%s %s ", fJobSubCmd.Data(), fDefOpt.Data());
126  if (fParList.GetNpar()) {
127  for (int i = 0; i < fParList.GetNpar(); i++) {
128  KVNamedParameter* par = fParList.GetParameter(i);
129  command_line += par->GetName();
130  if (par->GetTString() != "") {
131  command_line += par->GetString();
132  }
133  command_line += " ";
134  }
135  }
136  command_line += fJobScript;
137  //replace #JobName# with name of current job
138  command_line.ReplaceAll("#JobName#", GetJobName());
139  return command_line.Data();
140 }
141 
142 
143 
144 
148 
150 {
151  //Clear previously set parameters in order to create a new job submission command
152  //(default options are not affected: use SetDefaultJobOptions to change them)
153 
154  fJobName = "";
155  fParList.Clear();
156  fAnalyser = nullptr;
157 }
158 
159 
160 
161 
164 
166 {
167  //Make this the default batch system
168  gBatchSystem = this;
169 }
170 
171 
172 
173 
180 
182 {
183  //Submits a job to batch system, i.e. executes the string formed by GetJobSubCmdLine,
184  //if all necessary parameters have been given (any missing ones will be asked for).
185  //Parameters specific to a given batch system can be added my modifying the
186  //CheckJobParameters() method for the associated child class.
187 
188  //set environment variables required by KaliVedaAnalysis batch executable
189  gSystem->Setenv("KVBATCHNAME", GetJobName());
190  gSystem->Setenv("KVLAUNCHDIR", gSystem->WorkingDirectory());
191  cout << GetJobSubCmdLine() << endl;
192  if (fAnalyser) {
193  fAnalyser->WriteBatchEnvFile(GetJobName());
194  gSystem->Setenv("KVANALYSER", fAnalyser->ClassName());
195  }
196  gSystem->Exec(GetJobSubCmdLine());
197 }
198 
199 
200 
201 
206 
208 {
209  //Processes the job requests for the batch system.
210  //In normal mode, this submits one job for the data analyser fAnalyser
211  //In multijobs mode, this submits one job for each run in the runlist associated to fAnalyser
212 
213  if (!CheckJobParameters()) return;
214 
215  SubmitJob();
216 }
217 
218 
219 
220 
226 
228 {
229  // Create batch system object defined as a plugin in .kvrootrc
230  // If no plugin is found, we create a new KVBatchSystem base object which
231  // will be initialised from resources defined in .kvrootrc using its name.
232 
233  //check and load plugin library
234  TPluginHandler* ph = KVBase::LoadPlugin("KVBatchSystem", plugin);
235  if (!ph)
236  return new KVBatchSystem(plugin);
237 
238  //execute constructor/macro for multidetector
239  return ((KVBatchSystem*) ph->ExecPlugin(1, plugin));
240 }
241 
242 
243 
244 
250 
252 {
253  // Submit data analysis task described by KVDataAnalyser object to the batch system.
254  //
255  // Note that the default options for this batch system may be changed just before
256  // job submission depending on the current environment, see ChangeDefJobOpt().
257 
258  Info("SubmitTask", "Task submission for analyser class : %s", da->ClassName());
259  SetAnalyser(da);
260  //change job submission options depending on task, environment, etc.
261  ChangeDefJobOpt(da);
262  Run();
263 }
264 
265 
266 
267 
280 
282 {
283  // PRIVATE method called by SubmitTask() at moment of job submission.
284  // Depending on the current environment, the default job submission options
285  // may be changed by this method.
286  // For example, default options may be changed depending on the analysis
287  // task to be performed. We look for an environment variable of the form:
288  //
289  // [batch system name].BatchSystem.DefaultJobOptions.[analysis task name]
290  //
291  // and if found we use the options defined by it. If not, we use the default
292  // job options defined by
293  // [batch system name].BatchSystem.DefaultJobOptions
294 
295  TString tmp = gEnv->GetValue(Form("%s.BatchSystem.DefaultJobOptions.%s",
296  GetName(), da->GetAnalysisTask()->GetName()), "");
297  if (tmp.Length()) {
298  Info("ChangeDefJobOpt", "Changing default batch options for task %s.", da->GetAnalysisTask()->GetName());
299  Info("ChangeDefJobOpt", "Batch job options for this job are : %s", tmp.Data());
300  SetDefaultJobOptions(tmp.Data());
301  }
302  else {
303  tmp = gEnv->GetValue(Form("%s.BatchSystem.DefaultJobOptions", GetName()), "");
304  SetDefaultJobOptions(tmp.Data());
305  }
306 }
307 
308 
309 
310 
318 
320 {
321  //Returns name of batch job, either during submission of batch jobs or when an analysis
322  //task is running in batch mode (access through gBatchSystem global pointer).
323  //
324  // Depending on the batch system, some sanitization of the jobname may be required
325  // e.g. to remove "illegal" characters from the jobname. This is done by SanitizeJobName()
326  // before the jobname is returned.
327 
328  if (!fAnalyser) {
329  //stand-alone batch submission ?
330  fCurrJobName = fJobName;
331  }
332  else {
333  //replace any special symbols with their current values
334  fCurrJobName = fAnalyser->ExpandAutoBatchName(fJobName.Data());
335  }
336  SanitizeJobName();
337  return fCurrJobName.Data();
338 }
339 
340 
341 
342 
345 
347 {
348  // Checks the job and ask for the job name if needed
349  KVString jobName = fJobName;
350  while (!jobName.Length()) {
351  cout << "Please enter the job name : ";
352  cout.flush();
353  jobName.ReadToDelim(cin);
354  if (jobName.Length()) {
355  SetJobName(jobName.Data());
356  }
357  }
358  return kTRUE;
359 }
360 
361 
362 
363 
367 
369 {
370  //Store any useful information on batch system in the TEnv
371  //(this method is used by KVDataAnalyser::WriteBatchEnvFile)
372  env->SetValue("BatchSystem.JobName", GetJobName());
373 }
374 
375 
376 
377 
381 
383 {
384  //Read any useful information on batch system from the TEnv
385  //(this method is used by KVDataAnalyser::ReadBatchEnvFile)
386  fJobName = env->GetValue("BatchSystem.JobName", "");
387 }
388 
389 
390 
391 
395 
396 void KVBatchSystem::Print(Option_t* option) const
397 {
398  //if option="log", print infos for batch log file
399  //if option="all", print detailed info on batch system
400  if (!strcmp(option, "log")) {
401  cout << "Job " << GetJobName()
402  << " executed by batch system " << GetName() << endl;
403  }
404  else if (!strcmp(option, "all")) {
405  cout << ClassName() << " : Name = " << GetName() << endl << " Title = " << GetTitle() << endl;
406  cout << " fJobSubCmd = " << fJobSubCmd.Data() << endl;
407  cout << " fJobScript = " << fJobScript.Data() << endl;
408  cout << " fDefOpt = " << fDefOpt.Data() << endl;
409  fParList.Print(); //list of parameters/switches to be passed on job submission command line
410  }
411  else
412  KVBase::Print(option);
413 }
414 
415 
416 
423 
425 {
426  // Create and fill list with KVBatchJob objects, one for each job currently
427  // handled by the batch system.
428  //
429  // Needs to be implemented for specific systems in child classes.
430  // This method returns 0x0.
431  return 0x0;
432 }
433 
434 
435 
444 
446 {
447  // Fill the list with all relevant parameters for batch system,
448  // set to their default values.
449  //
450  // Parameters defined here are:
451  // JobName [string]
452  // AutoJobName [bool]
453  // AutoJobNameFormat [string]
454 
455  nl.Clear();
456  nl.SetTitle(GetTitle());
457  nl.SetValue("JobName", "");
458  nl.SetValue("AutoJobName", kTRUE);
459  nl.SetValue("AutoJobNameFormat", "$UserClass");
460 }
461 
462 
463 
466 
468 {
469  // Use the parameters in the list to set all relevant parameters for batch system.
470 
471  if (nl.GetBoolValue("AutoJobName"))
472  SetJobName(nl.GetStringValue("AutoJobNameFormat"));
473  else
474  SetJobName(nl.GetStringValue("JobName"));
475  Info("SetBatchSystemParameters", "JobName = %s", GetJobName());
476 }
477 
478 
479 
480 
488 
490 {
491  //Set the job name. In MultiJobsMode this will be used as the base name for all jobs;
492  //each individual job will have the name 'basejobname_Rxxxx", with xxxx=run number for job.
493  //
494  //The job name can be generated automatically by replacing certain special symbols
495  //in the name given here depending on the characteristics of the job. See
496  //KVDataAnalyser::ExpandAutoBatchName for allowed symbols.
497  fJobName = name;
498 }
499 
500 
KVBatchSystem * gBatchSystem
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
bool Bool_t
const Bool_t kTRUE
const char Option_t
R__EXTERN TEnv * gEnv
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:135
static Bool_t FindExecutable(TString &exec, const Char_t *path="$(PATH)")
Definition: KVBase.cpp:964
virtual void Print(Option_t *option="") const
Definition: KVBase.cpp:413
static TPluginHandler * LoadPlugin(const Char_t *base, const Char_t *uri="0")
Definition: KVBase.cpp:756
Base class for interface to a batch job management system.
Definition: KVBatchSystem.h:77
virtual void SubmitTask(KVDataAnalyser *da)
virtual void WriteBatchEnvFile(TEnv *)
virtual const Char_t * GetJobSubCmdLine()
virtual void SetJobName(const Char_t *name)
void cd()
Make this the default batch system.
virtual void SubmitJob()
virtual const Char_t * GetJobName() const
virtual void Run()
virtual KVList * GetListOfJobs()
virtual void Print(Option_t *="") const
virtual void ChangeDefJobOpt(KVDataAnalyser *da)
static KVBatchSystem * GetBatchSystem(const Char_t *plugin)
virtual void ReadBatchEnvFile(TEnv *)
virtual void SetBatchSystemParameters(const KVNameValueList &)
Use the parameters in the list to set all relevant parameters for batch system.
virtual void GetBatchSystemParameterList(KVNameValueList &)
virtual void Clear(Option_t *opt="")
virtual Bool_t CheckJobParameters()
Checks the job and ask for the job name if needed.
Manager class which sets up and runs data analysis tasks.
KVDataAnalysisTask * GetAnalysisTask() const
Extended TList class which owns its objects by default.
Definition: KVList.h:27
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
void SetValue(const Char_t *name, value_type value)
virtual void Clear(Option_t *opt="")
Bool_t GetBoolValue(const Char_t *name) const
const Char_t * GetStringValue(const Char_t *name) const
A generic named parameter storing values of different types.
const Char_t * GetString() const
TString GetTString() const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
void Print(const char *="") const
virtual const char * GetValue(const char *name, const char *dflt) const
virtual void SetValue(const char *name, const char *value, EEnvLevel level=kEnvChange, const char *type=nullptr)
virtual const char * GetName() const
virtual void SetTitle(const char *title="")
virtual const char * ClassName() const
Longptr_t ExecPlugin(int nargs, const T &... params)
Ssiz_t Length() const
std::istream & ReadToDelim(std::istream &str, char delim='\n')
const char * Data() const
void Form(const char *fmt,...)
TString & ReplaceAll(const char *s1, const char *s2)
virtual Int_t Exec(const char *shellcmd)
virtual const char * WorkingDirectory()
virtual void Setenv(const char *name, const char *value)
RooCmdArg ClassName(const char *name)
void Info(const char *location, const char *va_(fmt),...)
void Warning(const char *location, const char *va_(fmt),...)