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
39 #include "Batch_BatchManager_eLSF.hxx"
51 BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host, const char * protocol, const char * mpiImpl) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_eClient(parent,host,protocol,mpiImpl)
57 BatchManager_eLSF::~BatchManager_eLSF()
62 // Methode pour le controle des jobs : soumet un job au gestionnaire
63 const JobId BatchManager_eLSF::submitJob(const Job & job)
66 Parametre params = job.getParametre();
67 const std::string dirForTmpFiles = params[TMPDIR];
68 const string fileToExecute = params[EXECUTABLE];
69 std::string fileNameToExecute;
70 if( fileToExecute.size() > 0 ){
71 string::size_type p1 = fileToExecute.find_last_of("/");
72 string::size_type p2 = fileToExecute.find_last_of(".");
73 fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
76 fileNameToExecute = "command";
78 // export input files on cluster
79 exportInputFiles(job);
81 // build batch script for job
82 buildBatchScript(job);
84 // define name of log file
85 string logFile="/tmp/logs/";
86 logFile += getenv("USER");
87 logFile += "/batchSalome_";
97 // define command to submit batch
102 command += _username;
106 command += _hostname;
107 command += " \"cd " ;
108 command += dirForTmpFiles ;
109 command += "; bsub < " ;
110 command += fileNameToExecute ;
111 command += "_Batch.sh\" > ";
113 cerr << command.c_str() << endl;
114 status = system(command.c_str());
116 throw EmulationException("Error of connection on remote host");
118 // read id of submitted job in log file
120 FILE *fp = fopen(logFile.c_str(),"r");
121 fgets( line, 128, fp);
125 int p10 = sline.find("<");
126 int p20 = sline.find(">");
127 string strjob = sline.substr(p10+1,p20-p10-1);
129 JobId id(this, strjob);
133 // Methode pour le controle des jobs : retire un job du gestionnaire
134 void BatchManager_eLSF::deleteJob(const JobId & jobid)
138 istringstream iss(jobid.getReference());
141 // define command to submit batch
146 if (_username != ""){
147 command += _username;
151 command += _hostname;
152 command += " \"bkill " ;
153 command += iss.str();
155 cerr << command.c_str() << endl;
156 status = system(command.c_str());
158 throw EmulationException("Error of connection on remote host");
160 cerr << "jobId = " << ref << "killed" << endl;
163 // Methode pour le controle des jobs : suspend un job en file d'attente
164 void BatchManager_eLSF::holdJob(const JobId & jobid)
166 throw EmulationException("Not yet implemented");
169 // Methode pour le controle des jobs : relache un job suspendu
170 void BatchManager_eLSF::releaseJob(const JobId & jobid)
172 throw EmulationException("Not yet implemented");
176 // Methode pour le controle des jobs : modifie un job en file d'attente
177 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
179 throw EmulationException("Not yet implemented");
182 // Methode pour le controle des jobs : modifie un job en file d'attente
183 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
185 alterJob(jobid, param, Environnement());
188 // Methode pour le controle des jobs : modifie un job en file d'attente
189 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
191 alterJob(jobid, Parametre(), env);
194 // Methode pour le controle des jobs : renvoie l'etat du job
195 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
198 istringstream iss(jobid.getReference());
201 // define name of log file
202 string logFile="/tmp/logs/";
203 logFile += getenv("USER");
204 logFile += "/batchSalome_";
206 srand ( time(NULL) );
210 logFile += oss.str();
216 // define command to submit batch
220 if (_username != ""){
221 command += _username;
225 command += _hostname;
226 command += " \"bjobs " ;
227 command += iss.str();
230 cerr << command.c_str() << endl;
231 status = system(command.c_str());
233 throw EmulationException("Error of connection on remote host");
235 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
241 // Methode pour le controle des jobs : teste si un job est present en machine
242 bool BatchManager_eLSF::isRunning(const JobId & jobid)
244 throw EmulationException("Not yet implemented");
247 void BatchManager_eLSF::buildBatchScript(const Job & job)
249 #ifndef WIN32 //TODO: need for porting on Windows
251 Parametre params = job.getParametre();
252 Environnement env = job.getEnvironnement();
253 const int nbproc = params[NBPROC];
254 const long edt = params[MAXWALLTIME];
255 const long mem = params[MAXRAMSIZE];
256 const string workDir = params[WORKDIR];
257 const std::string dirForTmpFiles = params[TMPDIR];
258 const string fileToExecute = params[EXECUTABLE];
259 const string home = params[HOMEDIR];
260 const std::string queue = params[QUEUE];
261 std::string rootNameToExecute;
262 std::string fileNameToExecute;
263 std::string filelogtemp;
264 if( fileToExecute.size() > 0 ){
265 string::size_type p1 = fileToExecute.find_last_of("/");
266 string::size_type p2 = fileToExecute.find_last_of(".");
267 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
268 char* basec=strdup(fileToExecute.c_str());
269 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
272 int idx = dirForTmpFiles.find("Batch/");
273 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
276 rootNameToExecute = "command";
279 ofstream tempOutputFile;
280 std::string TmpFileName = createAndOpenTemporaryFile(tempOutputFile);
282 tempOutputFile << "#! /bin/sh -f" << endl ;
284 tempOutputFile << "#BSUB -q " << queue << endl;
286 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
288 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
289 tempOutputFile << "#BSUB -n " << nbproc << endl ;
290 if( fileToExecute.size() > 0 ){
291 tempOutputFile << "#BSUB -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
292 tempOutputFile << "#BSUB -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
295 tempOutputFile << "#BSUB -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
296 tempOutputFile << "#BSUB -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
298 if( workDir.size() > 0 )
299 tempOutputFile << "cd " << workDir << endl ;
300 if( fileToExecute.size() > 0 ){
301 tempOutputFile << _mpiImpl->boot("",nbproc);
302 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
303 tempOutputFile << _mpiImpl->halt();
306 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
307 tempOutputFile << env["COMMAND"];
310 tempOutputFile.flush();
311 tempOutputFile.close();
317 TmpFileName.c_str(), 0x1ED);
318 cerr << TmpFileName.c_str() << endl;
321 if( _protocol == "rsh" )
323 else if( _protocol == "ssh" )
326 throw EmulationException("Unknown protocol");
327 command += TmpFileName;
330 command += _username;
333 command += _hostname;
335 command += dirForTmpFiles ;
337 command += rootNameToExecute ;
338 command += "_Batch.sh" ;
339 cerr << command.c_str() << endl;
340 status = system(command.c_str());
342 throw EmulationException("Error of connection on remote host");
344 remove(TmpFileName.c_str());
349 std::string BatchManager_eLSF::getWallTime(const long edt)
356 oss << h << ":" << m;
358 oss << h << ":0" << m;