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
47 #include "Batch_BatchManager_eLSF.hxx"
48 #include "Batch_JobInfo_eLSF.hxx"
54 BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
55 CommunicationProtocolType protocolType, const char * mpiImpl)
56 : BatchManager_eClient(parent, host, protocolType, mpiImpl)
62 BatchManager_eLSF::~BatchManager_eLSF()
67 // Methode pour le controle des jobs : soumet un job au gestionnaire
68 const JobId BatchManager_eLSF::submitJob(const Job & job)
71 Parametre params = job.getParametre();
72 const std::string dirForTmpFiles = params[TMPDIR];
73 const string fileToExecute = params[EXECUTABLE];
74 std::string fileNameToExecute;
75 if( fileToExecute.size() > 0 ){
76 string::size_type p1 = fileToExecute.find_last_of("/");
77 string::size_type p2 = fileToExecute.find_last_of(".");
78 fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
81 fileNameToExecute = "command";
83 // export input files on cluster
84 exportInputFiles(job);
86 // build batch script for job
87 buildBatchScript(job);
89 // define name of log file (local)
90 string logFile = generateTemporaryFileName("LSF-submitlog");
92 // define command to submit batch
93 string subCommand = string("cd ") + dirForTmpFiles + "; bsub < " +
94 fileNameToExecute + "_Batch.sh";
95 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
98 cerr << command.c_str() << endl;
99 status = system(command.c_str());
101 throw EmulationException("Error of connection on remote host");
103 // read id of submitted job in log file
105 FILE *fp = fopen(logFile.c_str(),"r");
106 fgets( line, 128, fp);
110 int p10 = sline.find("<");
111 int p20 = sline.find(">");
112 string strjob = sline.substr(p10+1,p20-p10-1);
114 JobId id(this, strjob);
118 // Methode pour le controle des jobs : retire un job du gestionnaire
119 void BatchManager_eLSF::deleteJob(const JobId & jobid)
123 istringstream iss(jobid.getReference());
126 // define command to delete batch
127 string subCommand = string("bkill ") + iss.str();
128 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
129 cerr << command.c_str() << endl;
130 status = system(command.c_str());
132 throw EmulationException("Error of connection on remote host");
134 cerr << "jobId = " << ref << "killed" << endl;
137 // Methode pour le controle des jobs : suspend un job en file d'attente
138 void BatchManager_eLSF::holdJob(const JobId & jobid)
140 throw EmulationException("Not yet implemented");
143 // Methode pour le controle des jobs : relache un job suspendu
144 void BatchManager_eLSF::releaseJob(const JobId & jobid)
146 throw EmulationException("Not yet implemented");
150 // Methode pour le controle des jobs : modifie un job en file d'attente
151 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
153 throw EmulationException("Not yet implemented");
156 // Methode pour le controle des jobs : modifie un job en file d'attente
157 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
159 alterJob(jobid, param, Environnement());
162 // Methode pour le controle des jobs : modifie un job en file d'attente
163 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
165 alterJob(jobid, Parametre(), env);
168 // Methode pour le controle des jobs : renvoie l'etat du job
169 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
172 istringstream iss(jobid.getReference());
175 // define name of log file (local)
176 string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
178 // define command to query batch
179 string subCommand = string("bjobs ") + iss.str();
180 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
183 cerr << command.c_str() << endl;
184 int status = system(command.c_str());
186 throw EmulationException("Error of connection on remote host");
188 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
194 // Methode pour le controle des jobs : teste si un job est present en machine
195 bool BatchManager_eLSF::isRunning(const JobId & jobid)
197 throw EmulationException("Not yet implemented");
200 void BatchManager_eLSF::buildBatchScript(const Job & job)
202 #ifndef WIN32 //TODO: need for porting on Windows
203 Parametre params = job.getParametre();
204 Environnement env = job.getEnvironnement();
205 const int nbproc = params[NBPROC];
206 const long edt = params[MAXWALLTIME];
207 const long mem = params[MAXRAMSIZE];
208 const string workDir = params[WORKDIR];
209 const std::string dirForTmpFiles = params[TMPDIR];
210 const string fileToExecute = params[EXECUTABLE];
211 const string home = params[HOMEDIR];
212 const std::string queue = params[QUEUE];
213 std::string rootNameToExecute;
214 std::string fileNameToExecute;
215 std::string filelogtemp;
216 if( fileToExecute.size() > 0 ){
217 string::size_type p1 = fileToExecute.find_last_of("/");
218 string::size_type p2 = fileToExecute.find_last_of(".");
219 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
220 char* basec=strdup(fileToExecute.c_str());
221 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
224 int idx = dirForTmpFiles.find("Batch/");
225 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
228 rootNameToExecute = "command";
231 ofstream tempOutputFile;
232 std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
234 tempOutputFile << "#! /bin/sh -f" << endl ;
236 tempOutputFile << "#BSUB -q " << queue << endl;
238 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
240 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
241 tempOutputFile << "#BSUB -n " << nbproc << endl ;
242 if( fileToExecute.size() > 0 ){
243 tempOutputFile << "#BSUB -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
244 tempOutputFile << "#BSUB -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
247 tempOutputFile << "#BSUB -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
248 tempOutputFile << "#BSUB -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
250 if( workDir.size() > 0 )
251 tempOutputFile << "cd " << workDir << endl ;
252 if( fileToExecute.size() > 0 ){
253 tempOutputFile << _mpiImpl->boot("",nbproc);
254 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
255 tempOutputFile << _mpiImpl->halt();
258 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
259 tempOutputFile << env["COMMAND"];
262 tempOutputFile.flush();
263 tempOutputFile.close();
269 TmpFileName.c_str(), 0x1ED);
270 cerr << TmpFileName.c_str() << endl;
272 int status = _protocol.copyFile(TmpFileName, "", "",
273 dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
274 _hostname, _username);
276 throw EmulationException("Error of connection on remote host");
278 remove(TmpFileName.c_str());
283 std::string BatchManager_eLSF::getWallTime(const long edt)
290 oss << h << ":" << m;
292 oss << h << ":0" << m;