1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * BatchManager_eLSF.cxx : emulation of LSF client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
44 #include "Batch_BatchManager_eLSF.hxx"
45 #include "Batch_JobInfo_eLSF.hxx"
51 BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
52 CommunicationProtocolType protocolType, const char * mpiImpl)
53 : BatchManager_eClient(parent, host, protocolType, mpiImpl)
59 BatchManager_eLSF::~BatchManager_eLSF()
64 // Methode pour le controle des jobs : soumet un job au gestionnaire
65 const JobId BatchManager_eLSF::submitJob(const Job & job)
68 Parametre params = job.getParametre();
69 const std::string dirForTmpFiles = params[TMPDIR];
70 const string fileToExecute = params[EXECUTABLE];
71 std::string fileNameToExecute;
72 if( fileToExecute.size() > 0 ){
73 string::size_type p1 = fileToExecute.find_last_of("/");
74 string::size_type p2 = fileToExecute.find_last_of(".");
75 fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
78 fileNameToExecute = "command";
80 // export input files on cluster
81 exportInputFiles(job);
83 // build batch script for job
84 buildBatchScript(job);
86 // define name of log file (local)
87 string logFile = generateTemporaryFileName("LSF-submitlog");
89 // define command to submit batch
90 string subCommand = string("cd ") + dirForTmpFiles + "; bsub < " +
91 fileNameToExecute + "_Batch.sh";
92 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
95 cerr << command.c_str() << endl;
96 status = system(command.c_str());
98 throw EmulationException("Error of connection on remote host");
100 // read id of submitted job in log file
102 FILE *fp = fopen(logFile.c_str(),"r");
103 fgets( line, 128, fp);
107 int p10 = sline.find("<");
108 int p20 = sline.find(">");
109 string strjob = sline.substr(p10+1,p20-p10-1);
111 JobId id(this, strjob);
115 // Methode pour le controle des jobs : retire un job du gestionnaire
116 void BatchManager_eLSF::deleteJob(const JobId & jobid)
120 istringstream iss(jobid.getReference());
123 // define command to delete batch
124 string subCommand = string("bkill ") + iss.str();
125 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
126 cerr << command.c_str() << endl;
127 status = system(command.c_str());
129 throw EmulationException("Error of connection on remote host");
131 cerr << "jobId = " << ref << "killed" << endl;
134 // Methode pour le controle des jobs : suspend un job en file d'attente
135 void BatchManager_eLSF::holdJob(const JobId & jobid)
137 throw EmulationException("Not yet implemented");
140 // Methode pour le controle des jobs : relache un job suspendu
141 void BatchManager_eLSF::releaseJob(const JobId & jobid)
143 throw EmulationException("Not yet implemented");
147 // Methode pour le controle des jobs : modifie un job en file d'attente
148 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
150 throw EmulationException("Not yet implemented");
153 // Methode pour le controle des jobs : modifie un job en file d'attente
154 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
156 alterJob(jobid, param, Environnement());
159 // Methode pour le controle des jobs : modifie un job en file d'attente
160 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
162 alterJob(jobid, Parametre(), env);
165 // Methode pour le controle des jobs : renvoie l'etat du job
166 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
169 istringstream iss(jobid.getReference());
172 // define name of log file (local)
173 string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
175 // define command to query batch
176 string subCommand = string("bjobs ") + iss.str();
177 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
180 cerr << command.c_str() << endl;
181 int status = system(command.c_str());
183 throw EmulationException("Error of connection on remote host");
185 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
191 // Methode pour le controle des jobs : teste si un job est present en machine
192 bool BatchManager_eLSF::isRunning(const JobId & jobid)
194 throw EmulationException("Not yet implemented");
197 void BatchManager_eLSF::buildBatchScript(const Job & job)
199 #ifndef WIN32 //TODO: need for porting on Windows
200 Parametre params = job.getParametre();
201 Environnement env = job.getEnvironnement();
202 const int nbproc = params[NBPROC];
203 const long edt = params[MAXWALLTIME];
204 const long mem = params[MAXRAMSIZE];
205 const string workDir = params[WORKDIR];
206 const std::string dirForTmpFiles = params[TMPDIR];
207 const string fileToExecute = params[EXECUTABLE];
208 const string home = params[HOMEDIR];
209 const std::string queue = params[QUEUE];
210 std::string rootNameToExecute;
211 std::string fileNameToExecute;
212 std::string filelogtemp;
213 if( fileToExecute.size() > 0 ){
214 string::size_type p1 = fileToExecute.find_last_of("/");
215 string::size_type p2 = fileToExecute.find_last_of(".");
216 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
217 char* basec=strdup(fileToExecute.c_str());
218 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
221 int idx = dirForTmpFiles.find("Batch/");
222 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
225 rootNameToExecute = "command";
228 ofstream tempOutputFile;
229 std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
231 tempOutputFile << "#! /bin/sh -f" << endl ;
233 tempOutputFile << "#BSUB -q " << queue << endl;
235 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
237 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
238 tempOutputFile << "#BSUB -n " << nbproc << endl ;
239 if( fileToExecute.size() > 0 ){
240 tempOutputFile << "#BSUB -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
241 tempOutputFile << "#BSUB -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
244 tempOutputFile << "#BSUB -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
245 tempOutputFile << "#BSUB -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
247 if( workDir.size() > 0 )
248 tempOutputFile << "cd " << workDir << endl ;
249 if( fileToExecute.size() > 0 ){
250 tempOutputFile << _mpiImpl->boot("",nbproc);
251 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
252 tempOutputFile << _mpiImpl->halt();
255 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
256 tempOutputFile << env["COMMAND"];
259 tempOutputFile.flush();
260 tempOutputFile.close();
266 TmpFileName.c_str(), 0x1ED);
267 cerr << TmpFileName.c_str() << endl;
269 int status = _protocol.copyFile(TmpFileName, "", "",
270 dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
271 _hostname, _username);
273 throw EmulationException("Error of connection on remote host");
275 remove(TmpFileName.c_str());
280 std::string BatchManager_eLSF::getWallTime(const long edt)
287 oss << h << ":" << m;
289 oss << h << ":0" << m;