Salome HOME
Add preprocess feature.
[tools/libbatch.git] / src / OAR / BatchManager_OAR.cxx
index 503ad8dfdea51b05d618159cdf9da907be57b98f..0989bf2889a8445edc2f105ecb3bb468bfa1c9e0 100644 (file)
@@ -31,36 +31,33 @@ using namespace std;
 
 namespace Batch
 {
-       BatchManager_OAR::BatchManager_OAR(const FactBatchManager * parent, const char * host,
-                       const char * username,
-                       CommunicationProtocolType protocolType, const char * mpiImpl)
-               : BatchManager(parent, host, username, protocolType, mpiImpl)
-       {
-       }
-
-       BatchManager_OAR::~BatchManager_OAR()
-       {
-       }
-
-       // Soumet un job au gestionnaire
-       const JobId BatchManager_OAR::submitJob(const Job & job)
-       {
-               Parametre params = job.getParametre();
-               const string workDir = params[WORKDIR];
-               const string fileToExecute = params[EXECUTABLE];
-               string::size_type p1 = fileToExecute.find_last_of("/");
-               string::size_type p2 = fileToExecute.find_last_of(".");
-               std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
-
-               // export input files on cluster
-               exportInputFiles(job);
-
-               // build batch script for job
-               string scriptFile = buildBatchScript(job);
+  BatchManager_OAR::BatchManager_OAR(const FactBatchManager * parent, const char * host,
+                  const char * username,
+                  CommunicationProtocolType protocolType, const char * mpiImpl)
+          : BatchManager(parent, host, username, protocolType, mpiImpl)
+  {
+  }
+
+  BatchManager_OAR::~BatchManager_OAR()
+  {
+  }
+
+  // Soumet un job au gestionnaire
+  const JobId BatchManager_OAR::runJob(const Job & job)
+  {
+    Parametre params = job.getParametre();
+    const string workDir = params[WORKDIR];
+    const string fileToExecute = params[EXECUTABLE];
+    string::size_type p1 = fileToExecute.find_last_of("/");
+    string::size_type p2 = fileToExecute.find_last_of(".");
+    std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
+
+    // build batch script for job
+    string scriptFile = buildBatchScript(job);
 
     // define command to submit batch
-               string subCommand = string("oarsub -t allow_classic_ssh -d ") + workDir + " -S " + workDir + "/" + scriptFile;
-               string command = _protocol.getExecCommand(subCommand, _hostname, _username);
+    string subCommand = string("oarsub -t allow_classic_ssh -d ") + workDir + " -S " + workDir + "/" + scriptFile;
+    string command = _protocol.getExecCommand(subCommand, _hostname, _username);
     command += " 2>&1";
     LOG(command);
 
@@ -70,33 +67,33 @@ namespace Batch
     LOG(output);
     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
 
-               // read id of submitted job in output
-               istringstream logfile(output);
-               string sline, idline, id;
-
-               if (logfile)
-               {
-                       while (getline(logfile, sline) && sline != "")
-                       {
-                               idline = sline;
-                       }
-
-                       vector<string> tokens;
-                       JobInfo::Tokenize(idline, tokens, "=");
-                       id = tokens[1];
-               }
-               else
-               {
-                       throw RunTimeException("Error in the submission of the job on the remote host");
-               }
-
-               JobId jobid(this, id);
-               return jobid;
-       }
-
-       // retire un job du gestionnaire
-       void BatchManager_OAR::deleteJob(const JobId & jobid)
-       {
+    // read id of submitted job in output
+    istringstream logfile(output);
+    string sline, idline, id;
+
+    if (logfile)
+    {
+      while (getline(logfile, sline) && sline != "")
+      {
+        idline = sline;
+      }
+
+      vector<string> tokens;
+      JobInfo::Tokenize(idline, tokens, "=");
+      id = tokens[1];
+    }
+    else
+    {
+      throw RunTimeException("Error in the submission of the job on the remote host");
+    }
+
+    JobId jobid(this, id);
+    return jobid;
+  }
+
+  // retire un job du gestionnaire
+  void BatchManager_OAR::deleteJob(const JobId & jobid)
+  {
     // define command to delete job
     string subCommand = "oardel " + jobid.getReference();
     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
@@ -107,11 +104,11 @@ namespace Batch
       throw RunTimeException("Can't delete job " + jobid.getReference());
 
     LOG("job " << jobid.getReference() << " killed");
-       }
+  }
 
-       // Renvoie l'etat du job
-       JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
-       {
+  // Renvoie l'etat du job
+  JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
+  {
     // define command to query batch
     string subCommand = "oarstat -fj " + jobid.getReference();
     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
@@ -123,19 +120,19 @@ namespace Batch
 
     JobInfo_OAR jobinfo = JobInfo_OAR(jobid.getReference(), output);
     return jobinfo;
-       }
+  }
 
-       string BatchManager_OAR::buildBatchScript(const Job & job)
-       {
-               Parametre params = job.getParametre();
+  string BatchManager_OAR::buildBatchScript(const Job & job)
+  {
+    Parametre params = job.getParametre();
 
-               // Job Parameters
-               string workDir       = "";
-               string fileToExecute = "";
-               string tmpDir = "";
-               int nbproc               = 0;
-               int mem              = 0;
-               string queue         = "";
+    // Job Parameters
+    string workDir       = "";
+    string fileToExecute = "";
+    string tmpDir = "";
+    int nbproc          = 0;
+    int mem              = 0;
+    string queue         = "";
 
     // Mandatory parameters
     if (params.find(WORKDIR) != params.end())
@@ -147,85 +144,85 @@ namespace Batch
     else
       throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
 
-               // Optional parameters
-               if (params.find(NBPROC) != params.end()) 
-                       nbproc = params[NBPROC];
+    // Optional parameters
+    if (params.find(NBPROC) != params.end())
+            nbproc = params[NBPROC];
     int nbprocpernode = 1;
     if (params.find(NBPROCPERNODE) != params.end())
       nbprocpernode = params[NBPROCPERNODE];
     long walltimeSecs = 0;
-               if (params.find(MAXWALLTIME) != params.end()) 
-                 walltimeSecs = (long)params[MAXWALLTIME] * 60;
-               if (params.find(MAXRAMSIZE) != params.end()) 
-                       mem = params[MAXRAMSIZE];
-               if (params.find(QUEUE) != params.end()) 
-                       queue = params[QUEUE].str();
-
-               string::size_type p1 = fileToExecute.find_last_of("/");
-               string::size_type p2 = fileToExecute.find_last_of(".");
-               string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
-               string fileNameToExecute = fileToExecute.substr(p1+1);
-
-               // Create batch submit file
+    if (params.find(MAXWALLTIME) != params.end())
+      walltimeSecs = (long)params[MAXWALLTIME] * 60;
+    if (params.find(MAXRAMSIZE) != params.end())
+            mem = params[MAXRAMSIZE];
+    if (params.find(QUEUE) != params.end())
+            queue = params[QUEUE].str();
+
+    string::size_type p1 = fileToExecute.find_last_of("/");
+    string::size_type p2 = fileToExecute.find_last_of(".");
+    string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
+    string fileNameToExecute = fileToExecute.substr(p1+1);
+
+    // Create batch submit file
     ofstream tempOutputFile;
     string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
 
-               tempOutputFile << "#!/bin/sh -f" << endl;
-
-               int nb_full_nodes(0);
-               int nb_proc_on_last_node(0);
-
-               if (nbproc > 0)
-               {
-                       nb_full_nodes = nbproc / nbprocpernode;
-                       nb_proc_on_last_node = nbproc % nbprocpernode;
-
-                       // In exclusive mode, we reserve all procs on the nodes
-                       if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0)
-                       {
-                               nb_full_nodes += 1;
-                               nb_proc_on_last_node = 0;
-                       }
-               }
-
-               if (nb_full_nodes > 0)
-               {
-                       tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
-                       if (walltimeSecs > 0)
-                       {
-                               tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
-                       }
-                       else
-                       {
-                               tempOutputFile << endl;
-                       }
-               }
-               else
-               {
-                       if (walltimeSecs > 0)
-                       {
-                               tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
-                       }
-               }
-
-               if (queue != "")
-               {
-                       tempOutputFile << "#OAR -q " << queue << endl;
-               }
-
-               tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
-               tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log."  << rootNameToExecute << endl;
-
-               tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
-
-               // Launch the executable
-               tempOutputFile << "cd " << tmpDir << endl;
-               tempOutputFile << "./" + fileNameToExecute << endl;
-               tempOutputFile.flush();
-               tempOutputFile.close();
-
-               Utils::chmod(tmpFileName.c_str(), 0x1ED);
-               LOG("Batch script file generated is: " << tmpFileName);
+    tempOutputFile << "#!/bin/sh -f" << endl;
+
+    int nb_full_nodes(0);
+    int nb_proc_on_last_node(0);
+
+    if (nbproc > 0)
+    {
+      nb_full_nodes = nbproc / nbprocpernode;
+      nb_proc_on_last_node = nbproc % nbprocpernode;
+
+      // In exclusive mode, we reserve all procs on the nodes
+      if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0)
+      {
+        nb_full_nodes += 1;
+        nb_proc_on_last_node = 0;
+      }
+    }
+
+    if (nb_full_nodes > 0)
+    {
+      tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
+      if (walltimeSecs > 0)
+      {
+        tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
+      }
+      else
+      {
+        tempOutputFile << endl;
+      }
+    }
+    else
+    {
+      if (walltimeSecs > 0)
+      {
+        tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
+      }
+    }
+
+    if (queue != "")
+    {
+      tempOutputFile << "#OAR -q " << queue << endl;
+    }
+
+    tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
+    tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log."  << rootNameToExecute << endl;
+
+    tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
+
+    // Launch the executable
+    tempOutputFile << "cd " << tmpDir << endl;
+    tempOutputFile << "./" + fileNameToExecute << endl;
+    tempOutputFile.flush();
+    tempOutputFile.close();
+
+    Utils::chmod(tmpFileName.c_str(), 0x1ED);
+    LOG("Batch script file generated is: " << tmpFileName);
 
     string remoteFileName = rootNameToExecute + "_Batch.sh";
     int status = _protocol.copyFile(tmpFileName, "", "",
@@ -235,17 +232,17 @@ namespace Batch
       throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
 
     return remoteFileName;
-       }
-
-       string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
-       {
-               int h(seconds / 3600);
-               int m((seconds % 3600) / 60);
-               int s((seconds % 3600) % 60);
-
-               stringstream ss;
-               ss << h << ":" << m << ":" << s;
-               
-               return ss.str();
-       }
+  }
+
+  string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
+  {
+    int h(seconds / 3600);
+    int m((seconds % 3600) / 60);
+    int s((seconds % 3600) % 60);
+
+    stringstream ss;
+    ss << h << ":" << m << ":" << s;
+
+    return ss.str();
+  }
 }