1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 * BatchManager_eLSF.cxx : emulation of LSF client
23 * Auteur : Bernard SECHER - CEA DEN
24 * Mail : mailto:bernard.secher@cea.fr
25 * Date : Thu Apr 24 10:17:22 2008
34 #include "Batch_BatchManager_eLSF.hxx"
44 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)
50 BatchManager_eLSF::~BatchManager_eLSF()
55 // Methode pour le controle des jobs : soumet un job au gestionnaire
56 const JobId BatchManager_eLSF::submitJob(const Job & job)
59 Parametre params = job.getParametre();
60 const std::string dirForTmpFiles = params[TMPDIR];
61 const string fileToExecute = params[EXECUTABLE];
62 string::size_type p1 = fileToExecute.find_last_of("/");
63 string::size_type p2 = fileToExecute.find_last_of(".");
64 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
66 // export input files on cluster
67 exportInputFiles(job);
69 // build batch script for job
70 buildBatchScript(job);
72 // define name of log file
73 string logFile="/tmp/logs/";
74 logFile += getenv("USER");
75 logFile += "/batchSalome_";
85 // define command to submit batch
96 command += dirForTmpFiles ;
97 command += "; bsub < " ;
98 command += fileNameToExecute ;
99 command += "_Batch.sh\" > ";
101 cerr << command.c_str() << endl;
102 status = system(command.c_str());
104 throw EmulationException("Error of connection on remote host");
106 // read id of submitted job in log file
108 FILE *fp = fopen(logFile.c_str(),"r");
109 fgets( line, 128, fp);
113 int p10 = sline.find("<");
114 int p20 = sline.find(">");
115 string strjob = sline.substr(p10+1,p20-p10-1);
117 JobId id(this, strjob);
121 // Methode pour le controle des jobs : retire un job du gestionnaire
122 void BatchManager_eLSF::deleteJob(const JobId & jobid)
126 istringstream iss(jobid.getReference());
129 // define command to submit batch
134 if (_username != ""){
135 command += _username;
139 command += _hostname;
140 command += " \"bkill " ;
141 command += iss.str();
143 cerr << command.c_str() << endl;
144 status = system(command.c_str());
146 throw EmulationException("Error of connection on remote host");
148 cerr << "jobId = " << ref << "killed" << endl;
151 // Methode pour le controle des jobs : suspend un job en file d'attente
152 void BatchManager_eLSF::holdJob(const JobId & jobid)
154 throw EmulationException("Not yet implemented");
157 // Methode pour le controle des jobs : relache un job suspendu
158 void BatchManager_eLSF::releaseJob(const JobId & jobid)
160 throw EmulationException("Not yet implemented");
164 // Methode pour le controle des jobs : modifie un job en file d'attente
165 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
167 throw EmulationException("Not yet implemented");
170 // Methode pour le controle des jobs : modifie un job en file d'attente
171 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
173 alterJob(jobid, param, Environnement());
176 // Methode pour le controle des jobs : modifie un job en file d'attente
177 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
179 alterJob(jobid, Parametre(), env);
182 // Methode pour le controle des jobs : renvoie l'etat du job
183 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
186 istringstream iss(jobid.getReference());
189 // define name of log file
190 string logFile="/tmp/logs/";
191 logFile += getenv("USER");
192 logFile += "/batchSalome_";
194 srand ( time(NULL) );
198 logFile += oss.str();
204 // define command to submit batch
208 if (_username != ""){
209 command += _username;
213 command += _hostname;
214 command += " \"bjobs " ;
215 command += iss.str();
218 cerr << command.c_str() << endl;
219 status = system(command.c_str());
221 throw EmulationException("Error of connection on remote host");
223 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
229 // Methode pour le controle des jobs : teste si un job est present en machine
230 bool BatchManager_eLSF::isRunning(const JobId & jobid)
232 throw EmulationException("Not yet implemented");
235 void BatchManager_eLSF::buildBatchScript(const Job & job) throw(EmulationException)
237 #ifndef WIN32 //TODO: need for porting on Windows
239 Parametre params = job.getParametre();
240 const int nbproc = params[NBPROC];
241 const long edt = params[MAXWALLTIME];
242 const long mem = params[MAXRAMSIZE];
243 const string workDir = params[WORKDIR];
244 const std::string dirForTmpFiles = params[TMPDIR];
245 const string fileToExecute = params[EXECUTABLE];
246 string::size_type p1 = fileToExecute.find_last_of("/");
247 string::size_type p2 = fileToExecute.find_last_of(".");
248 std::string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
249 std::string fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(fileToExecute.c_str()));
251 int idx = dirForTmpFiles.find("Batch/");
252 std::string filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
254 std::string TmpFileName = BuildTemporaryFileName();
255 ofstream tempOutputFile;
256 tempOutputFile.open(TmpFileName.c_str(), ofstream::out );
258 tempOutputFile << "#! /bin/sh -f" << endl ;
260 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
262 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
263 tempOutputFile << "#BSUB -n " << nbproc << endl ;
264 tempOutputFile << "#BSUB -o runSalome.output.log." << filelogtemp << endl ;
265 tempOutputFile << "#BSUB -e runSalome.error.log." << filelogtemp << endl ;
266 if( workDir.size() > 0 )
267 tempOutputFile << "cd " << workDir << endl ;
268 tempOutputFile << _mpiImpl->boot("",nbproc);
269 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
270 tempOutputFile << _mpiImpl->halt();
271 tempOutputFile.flush();
272 tempOutputFile.close();
278 TmpFileName.c_str(), 0x1ED);
279 cerr << TmpFileName.c_str() << endl;
282 if( _protocol == "rsh" )
284 else if( _protocol == "ssh" )
287 throw EmulationException("Unknown protocol");
288 command += TmpFileName;
291 command += _username;
294 command += _hostname;
296 command += dirForTmpFiles ;
298 command += rootNameToExecute ;
299 command += "_Batch.sh" ;
300 cerr << command.c_str() << endl;
301 status = system(command.c_str());
303 throw EmulationException("Error of connection on remote host");
305 RmTmpFile(TmpFileName);
310 std::string BatchManager_eLSF::getWallTime(const long edt)
317 oss << h << ":" << m;
319 oss << h << ":0" << m;