--- /dev/null
+// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+/*
+ * Batch_Utils.cxx
+ *
+ * Created on: 30 jan. 2012
+ * Author : Renaud BARATE - EDF R&D
+ */
+
+#include <cstdio>
+
+#include <Batch_config.h>
+#include "Batch_Utils.hxx"
+
+#ifdef MSVC
+#define popen _popen
+#define pclose _pclose
+#endif
+
+using namespace std;
+namespace Batch {
+
+int Utils::getCommandOutput(const string & command, string & output)
+{
+ // Reinitialize output
+ output = "";
+
+ // Call command
+ FILE * fp = popen(command.c_str(), "r");
+ if (fp == NULL) {
+ return -1;
+ }
+
+ // Read the output and store it
+ char buf[1024];
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ output += buf;
+ }
+
+ // close and get status
+ int status = pclose(fp);
+ return status;
+}
+
+}
--- /dev/null
+// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+/*
+ * Batch_Utils.hxx
+ *
+ * Created on: 30 jan. 2012
+ * Author : Renaud BARATE - EDF R&D
+ */
+
+#ifndef BATCH_UTILS_HXX_
+#define BATCH_UTILS_HXX_
+
+#include <string>
+
+namespace Batch {
+
+class Utils {
+public:
+
+ /**
+ * Call a command with the system shell and stores its output in parameter "output".
+ * Returns the return code of the command.
+ */
+ static int getCommandOutput(const std::string & command, std::string & output);
+
+private:
+
+ // No instanciation possible as this class provides only static methods
+ Utils() { }
+
+};
+
+}
+
+#endif /* BATCH_UTILS_HXX_ */
Core/Batch_StringType
Core/Batch_TypeMismatchException
Core/Batch_Versatile
+ Core/Batch_Utils
)
APPEND_CLASSES_TO_SRC_FILES(${CLASS_LIST})
*
*/
-#include <stdlib.h>
-#include <string.h>
-
-#include <iostream>
+#include <cstdlib>
#include <fstream>
#include <sstream>
-#include <string>
-#include <sys/stat.h>
-
-#include <stdlib.h>
-#include <string.h>
-#ifdef WIN32
-#include <io.h>
-#else
-#include <libgen.h>
-#endif
-
-#include "Batch_Constants.hxx"
+#include <Batch_Constants.hxx>
+#include <Batch_Utils.hxx>
#include "Batch_BatchManager_eLSF.hxx"
#include "Batch_JobInfo_eLSF.hxx"
// Methode pour le controle des jobs : soumet un job au gestionnaire
const JobId BatchManager_eLSF::submitJob(const Job & job)
{
- int status;
Parametre params = job.getParametre();
const std::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
cerr << "Export des fichiers en entree" << endl;
// build batch script for job
cerr << "Construction du script de batch" << endl;
- buildBatchScript(job);
+ string scriptFile = buildSubmissionScript(job);
cerr << "Script envoye" << endl;
- // define name of log file (local)
- string logFile = generateTemporaryFileName("LSF-submitlog");
-
// define command to submit batch
- string subCommand = string("cd ") + workDir + "; bsub < " + fileNameToExecute + "_Batch.sh";
+ string subCommand = string("cd ") + workDir + "; bsub < " + scriptFile;
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
command += " 2>&1";
cerr << command.c_str() << endl;
- status = system(command.c_str());
- if(status)
- {
- ifstream error_message(logFile.c_str());
- std::string mess;
- std::string temp;
- while(std::getline(error_message, temp))
- mess += temp;
- error_message.close();
- throw EmulationException("Error of connection on remote host, error was: " + mess);
- }
- // read id of submitted job in log file
- char line[128];
- FILE *fp = fopen(logFile.c_str(),"r");
- fgets( line, 128, fp);
- fclose(fp);
+ string output;
+ int status = Utils::getCommandOutput(command, output);
+ cout << output;
+ if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
- string sline(line);
- int p10 = sline.find("<");
- int p20 = sline.find(">");
- string strjob = sline.substr(p10+1,p20-p10-1);
+ // read id of submitted job in output
+ int p10 = output.find("<");
+ int p20 = output.find(">");
+ string strjob = output.substr(p10+1,p20-p10-1);
JobId id(this, strjob);
return id;
istringstream iss(jobid.getReference());
iss >> id;
- // define name of log file (local)
- string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
-
// define command to query batch
string subCommand = string("bjobs ") + iss.str();
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
cerr << command.c_str() << endl;
- int status = system(command.c_str());
- if (status)
- throw EmulationException("Error of connection on remote host");
- JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
+ string output;
+ int status = Utils::getCommandOutput(command, output);
+ if (status) throw EmulationException("Error of connection on remote host");
+
+ JobInfo_eLSF ji = JobInfo_eLSF(id, output);
return ji;
}
throw EmulationException("Not yet implemented");
}
- void BatchManager_eLSF::buildBatchScript(const Job & job)
+ std::string BatchManager_eLSF::buildSubmissionScript(const Job & job)
{
-#ifndef WIN32 //TODO: need for porting on Windows
Parametre params = job.getParametre();
// Job Parameters
std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
tempOutputFile << "#! /bin/sh -f" << endl ;
+ if (params.find(NAME) != params.end())
+ tempOutputFile << "#BSUB -J " << params[NAME] << endl;
if (queue != "")
tempOutputFile << "#BSUB -q " << queue << endl;
if( edt > 0 )
tempOutputFile.flush();
tempOutputFile.close();
- BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
+ string remoteFileName = rootNameToExecute + "_Batch.sh";
int status = _protocol.copyFile(TmpFileName, "", "",
- workDir + "/" + rootNameToExecute + "_Batch.sh",
+ workDir + "/" + remoteFileName,
_hostname, _username);
if (status)
throw EmulationException("Error of connection on remote host");
-
-#endif
-
+ return remoteFileName;
}
std::string BatchManager_eLSF::getWallTime(const long edt)
virtual const Batch::JobId addJob(const Batch::Job & job, const std::string reference); // ajoute un nouveau job sans le soumettre
protected:
- void buildBatchScript(const Job & job);
+ std::string buildSubmissionScript(const Job & job);
std::string getWallTime(const long edt);
private:
*/
#include <cstdio>
-#include <iostream>
-#include <fstream>
#include <sstream>
-#include "Batch_Constants.hxx"
-#include "Batch_Parametre.hxx"
-#include "Batch_Environnement.hxx"
-#include "Batch_RunTimeException.hxx"
-#include "Batch_APIInternalFailureException.hxx"
+#include <Batch_Constants.hxx>
#include "Batch_JobInfo_eLSF.hxx"
using namespace std;
namespace Batch {
-
-
// Constructeurs
- JobInfo_eLSF::JobInfo_eLSF(int id, string logFile) : JobInfo()
+ JobInfo_eLSF::JobInfo_eLSF(int id, const std::string & queryOutput) : JobInfo()
{
- // On remplit les membres _param et _env
+ // Fill ID parameter
ostringstream oss;
oss << id;
_param[ID] = oss.str();
- // read status of job in log file
+ // read query output
string line;
- ifstream fp(logFile.c_str());
+ istringstream fp(queryOutput);
getline(fp, line);
// On some batch managers, the job is deleted soon after it is finished,
#ifndef _JOBINFO_LSF_H_
#define _JOBINFO_LSF_H_
-#include "Batch_RunTimeException.hxx"
#include "Batch_JobInfo.hxx"
#include <string>
public:
// Constructeurs et destructeur
JobInfo_eLSF() : _running(false) {};
- JobInfo_eLSF(int id,std::string logFile);
+ JobInfo_eLSF(int id, const std::string & queryOutput);
virtual ~JobInfo_eLSF();
// Constructeur par recopie
// ... and its parameters ...
Parametre p;
p[EXECUTABLE] = "./test-script.sh";
- p[NAME] = string("Test eLSF ") + argv[1];
+ p[NAME] = string("Test_eLSF_") + argv[1];
p[WORKDIR] = homedir + "/tmp/Batch";
p[INFILE] = Couple("seta.sh", "tmp/Batch/seta.sh");
p[INFILE] += Couple("setb.sh", "tmp/Batch/setb.sh");
p[TMPDIR] = "tmp/Batch/";
p[NBPROC] = 1;
p[MAXWALLTIME] = 1;
- p[MAXRAMSIZE] = 50;
+ p[MAXRAMSIZE] = 128;
p[HOMEDIR] = homedir;
p[EXCLUSIVE] = true;
job.setParametre(p);
*
*/
-#include <stdlib.h>
-#include <string.h>
-
-#include <iostream>
+#include <cstdlib>
#include <fstream>
#include <sstream>
-#include <sys/stat.h>
-
-#include <stdlib.h>
-#include <string.h>
-#include <Batch_config.h>
-#ifdef MSVC
-#include <io.h>
-#else
-#include <libgen.h>
-#endif
+#include <Batch_Constants.hxx>
+#include <Batch_Utils.hxx>
-#include "Batch_Constants.hxx"
#include "Batch_BatchManager_ePBS.hxx"
#include "Batch_JobInfo_ePBS.hxx"
CommunicationProtocolType protocolType, const char * mpiImpl,
int nb_proc_per_node)
: BatchManager(parent, host),
- BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
+ BatchManager_eClient(parent, host, username, protocolType, mpiImpl),
+ _nb_proc_per_node(nb_proc_per_node)
{
// Nothing to do
- _nb_proc_per_node = nb_proc_per_node;
}
// Destructeur
// Methode pour le controle des jobs : soumet un job au gestionnaire
const JobId BatchManager_ePBS::submitJob(const Job & job)
{
- int status;
Parametre params = job.getParametre();
const std::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
- buildBatchScript(job);
-
- // define name of log file (local)
- string logFile = generateTemporaryFileName("PBS-submitlog");
+ string scriptFile = buildSubmissionScript(job);
// define command to submit batch
- string subCommand = string("cd ") + workDir + "; qsub " + fileNameToExecute + "_Batch.sh";
+ string subCommand = string("cd ") + workDir + "; qsub " + scriptFile;
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
command += " 2>&1";
cerr << command.c_str() << endl;
- status = system(command.c_str());
- if(status)
- {
- ifstream error_message(logFile.c_str());
- std::string mess;
- std::string temp;
- while(std::getline(error_message, temp))
- mess += temp;
- error_message.close();
- throw EmulationException("Error of connection on remote host, error was: " + mess);
- }
- // read id of submitted job in log file
- ifstream idfile(logFile.c_str());
- string sline;
- idfile >> sline;
- idfile.close();
- if (sline.size() == 0)
- throw EmulationException("Error in the submission of the job on the remote host");
+ // submit job
+ string output;
+ int status = Utils::getCommandOutput(command, output);
+ cout << output;
+ if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
+
+ // normally output contains only id of submitted job, we just need to remove the final \n
+ string jobref = output.substr(0, output.size() - 1);
+ JobId id(this, jobref);
- JobId id(this, sline);
return id;
}
istringstream iss(jobid.getReference());
iss >> id;
- // define name of log file (local)
- string logFile = generateTemporaryFileName(string("PBS-querylog-id") + jobid.getReference());
-
// define command to query batch
string subCommand = string("qstat -f ") + iss.str();
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
cerr << command.c_str() << endl;
- int status = system(command.c_str());
+
+ string output;
+ int status = Utils::getCommandOutput(command, output);
if(status && status != 153 && status != 256*153)
throw EmulationException("Error of connection on remote host");
- JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
+ JobInfo_ePBS ji = JobInfo_ePBS(id, output);
return ji;
}
throw EmulationException("Not yet implemented");
}
- void BatchManager_ePBS::buildBatchScript(const Job & job)
+ std::string BatchManager_ePBS::buildSubmissionScript(const Job & job)
{
- std::cerr << "BuildBatchScript" << std::endl;
Parametre params = job.getParametre();
Environnement env = job.getEnvironnement();
std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
tempOutputFile << "#! /bin/sh -f" << endl;
+ if (params.find(NAME) != params.end()) {
+ tempOutputFile << "#PBS -N " << params[NAME] << endl;
+ }
+
if (nbproc > 0)
{
int nb_full_nodes = nbproc / _nb_proc_per_node;
tempOutputFile.flush();
tempOutputFile.close();
- BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
+ string remoteFileName = rootNameToExecute + "_Batch.sh";
int status = _protocol.copyFile(TmpFileName, "", "",
- workDir + "/" + rootNameToExecute + "_Batch.sh",
+ workDir + "/" + remoteFileName,
_hostname, _username);
if (status)
throw EmulationException("Error of connection on remote host, cannot copy batch submission file");
+ return remoteFileName;
}
}
virtual const Batch::JobId addJob(const Batch::Job & job, const std::string reference); // ajoute un nouveau job sans le soumettre
protected:
- void buildBatchScript(const Job & job);
+ std::string buildSubmissionScript(const Job & job);
private:
int _nb_proc_per_node;
#include <cstdio>
#include <iostream>
-#include <fstream>
#include <sstream>
-#include "Batch_Constants.hxx"
-#include "Batch_Parametre.hxx"
-#include "Batch_Environnement.hxx"
-#include "Batch_RunTimeException.hxx"
-#include "Batch_APIInternalFailureException.hxx"
+#include <Batch_Constants.hxx>
#include "Batch_JobInfo_ePBS.hxx"
using namespace std;
namespace Batch {
// Constructeurs
- JobInfo_ePBS::JobInfo_ePBS(int id, string logFile) : JobInfo()
+ JobInfo_ePBS::JobInfo_ePBS(int id, string queryOutput) : JobInfo()
{
- // On remplit les membres _param et _env
+ // Fill ID parameter
ostringstream oss;
oss << id;
_param[ID] = oss.str();
- // read of log file
- char line[128];
- ifstream fp(logFile.c_str(),ios::in);
-
- string sline;
+ // read query output
+ istringstream queryIss(queryOutput);
+ string line;
size_t pos = string::npos;
- while( (pos == string::npos) && fp.getline(line,80,'\n') ){
- sline = string(line);
- pos = sline.find("job_state");
- };
+ while( (pos == string::npos) && getline(queryIss, line) ) {
+ pos = line.find("job_state");
+ }
if(pos!=string::npos){
string status;
- istringstream iss(sline);
+ istringstream iss(line);
iss >> status;
iss >> status;
iss >> status;
public:
// Constructeurs et destructeur
JobInfo_ePBS() {};
- JobInfo_ePBS(int id,std::string logFile);
+ JobInfo_ePBS(int id, std::string queryOutput);
virtual ~JobInfo_ePBS();
// Constructeur par recopie
p[TMPDIR] = "tmp/Batch/";
p[NBPROC] = 1;
p[MAXWALLTIME] = 1;
- p[MAXRAMSIZE] = 1;
+ p[MAXRAMSIZE] = 128;
p[HOMEDIR] = homedir;
p[QUEUE] = queue;
job.setParametre(p);
*/
#include <cstdlib>
-#include <iostream>
#include <fstream>
#include <Batch_NotYetImplementedException.hxx>
#include <Batch_Constants.hxx>
+#include <Batch_Utils.hxx>
-#include "Batch_FactBatchManager_eSlurm.hxx"
#include "Batch_BatchManager_eSlurm.hxx"
#include "Batch_JobInfo_eSlurm.hxx"
// Method to submit a job to the batch manager
const JobId BatchManager_eSlurm::submitJob(const Job & job)
{
- int status;
Parametre params = job.getParametre();
const string workDir = params[WORKDIR];
// build command file to submit the job and copy it on the server
string cmdFile = buildCommandFile(job);
- // define name of log file (local)
- string logFile = generateTemporaryFileName("slurm-submitlog");
-
// define command to submit batch
string subCommand = string("cd ") + workDir + "; sbatch " + cmdFile;
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
- cerr << command.c_str() << endl;
- status = system(command.c_str());
- if (status)
- {
- ifstream error_message(logFile.c_str());
- string mess;
- string temp;
- while(getline(error_message, temp))
- mess += temp;
- error_message.close();
- throw EmulationException("Error of connection on remote host, error was: " + mess);
- }
-
- // read id of submitted job in log file
- string jobref;
- ifstream idfile(logFile.c_str());
- string line;
- while (idfile && line.compare(0, 20, "Submitted batch job ") != 0)
- getline(idfile, line);
- idfile.close();
- if (line.compare(0, 20, "Submitted batch job ") == 0)
- jobref = line.substr(20);
- if (jobref.size() == 0)
+ command += " 2>&1";
+ cout << command.c_str() << endl;
+
+ // submit job
+ string output;
+ int status = Utils::getCommandOutput(command, output);
+ cout << output;
+ if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
+
+ // find id of submitted job in output
+ string search = "Submitted batch job ";
+ string::size_type pos = output.find(search);
+ if (pos == string::npos)
throw EmulationException("Error in the submission of the job on the remote host");
+ pos += search.size();
+ string::size_type endl_pos = output.find('\n', pos);
+ string::size_type count = (endl_pos == string::npos)? string::npos : endl_pos - pos;
+ string jobref = output.substr(pos, count);
JobId id(this, jobref);
return id;
JobInfo BatchManager_eSlurm::queryJob(const JobId & jobid)
{
- // define name of log file (local)
- string logFile = generateTemporaryFileName("slurm-querylog-" + jobid.getReference());
-
// define command to query batch
string subCommand = "squeue -o %t -j " + jobid.getReference();
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
cerr << command.c_str() << endl;
- system(command.c_str());
+ string output;
+ Utils::getCommandOutput(command, output);
// We don't test the return code here because with jobs finished since a long time Slurm
// returns an error and a message like "slurm_load_jobs error: Invalid job id specified".
// So we consider that the job is finished when we get an error.
- JobInfo_eSlurm jobinfo = JobInfo_eSlurm(jobid.getReference(), logFile);
+ JobInfo_eSlurm jobinfo = JobInfo_eSlurm(jobid.getReference(), output);
return jobinfo;
}
* Author : Renaud BARATE - EDF R&D
*/
-#include <iostream>
-#include <fstream>
#include <sstream>
#include <Batch_RunTimeException.hxx>
namespace Batch {
- JobInfo_eSlurm::JobInfo_eSlurm(const std::string & id, const std::string & logFile)
+ JobInfo_eSlurm::JobInfo_eSlurm(const std::string & id, const std::string & queryOutput)
: JobInfo()
{
_param[ID] = id;
- // read log file
- ifstream log(logFile.c_str());
- string line;
-
- // status should be on the second line
- for (int i=0 ; i<2 ; i++)
- getline(log, line);
- log.close();
+ // read query output, status should be on the second line
+ istringstream iss(queryOutput);
string status;
- istringstream iss(line);
- iss >> status;
+ for (int i=0 ; i<2 ; i++)
+ getline(iss, status);
if (status.size() == 0) {
// On some batch managers, the job is deleted as soon as it is finished,
{
public:
- JobInfo_eSlurm(const std::string & id, const std::string & logFile);
+ JobInfo_eSlurm(const std::string & id, const std::string & queryOutput);
virtual ~JobInfo_eSlurm();
};