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"
38 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)
44 BatchManager_eLSF::~BatchManager_eLSF()
49 // Methode pour le controle des jobs : soumet un job au gestionnaire
50 const JobId BatchManager_eLSF::submitJob(const Job & job)
53 Parametre params = job.getParametre();
54 const std::string dirForTmpFiles = params[TMPDIR];
55 const string fileToExecute = params[EXECUTABLE];
56 string::size_type p1 = fileToExecute.find_last_of("/");
57 string::size_type p2 = fileToExecute.find_last_of(".");
58 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
60 // export input files on cluster
61 exportInputFiles(job);
63 // build batch script for job
64 buildBatchScript(job);
66 // define name of log file
67 string logFile="/tmp/logs/";
68 logFile += getenv("USER");
69 logFile += "/batchSalome_";
79 // define command to submit batch
90 command += dirForTmpFiles ;
91 command += "; bsub < " ;
92 command += fileNameToExecute ;
93 command += "_Batch.sh\" > ";
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 submit batch
128 if (_username != ""){
129 command += _username;
133 command += _hostname;
134 command += " \"bkill " ;
135 command += iss.str();
137 cerr << command.c_str() << endl;
138 status = system(command.c_str());
140 throw EmulationException("Error of connection on remote host");
142 cerr << "jobId = " << ref << "killed" << endl;
145 // Methode pour le controle des jobs : suspend un job en file d'attente
146 void BatchManager_eLSF::holdJob(const JobId & jobid)
148 throw EmulationException("Not yet implemented");
151 // Methode pour le controle des jobs : relache un job suspendu
152 void BatchManager_eLSF::releaseJob(const JobId & jobid)
154 throw EmulationException("Not yet implemented");
158 // Methode pour le controle des jobs : modifie un job en file d'attente
159 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
161 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)
167 alterJob(jobid, param, Environnement());
170 // Methode pour le controle des jobs : modifie un job en file d'attente
171 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
173 alterJob(jobid, Parametre(), env);
176 // Methode pour le controle des jobs : renvoie l'etat du job
177 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
180 istringstream iss(jobid.getReference());
183 // define name of log file
184 string logFile="/tmp/logs/";
185 logFile += getenv("USER");
186 logFile += "/batchSalome_";
188 srand ( time(NULL) );
192 logFile += oss.str();
198 // define command to submit batch
202 if (_username != ""){
203 command += _username;
207 command += _hostname;
208 command += " \"bjobs " ;
209 command += iss.str();
212 cerr << command.c_str() << endl;
213 status = system(command.c_str());
215 throw EmulationException("Error of connection on remote host");
217 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
223 // Methode pour le controle des jobs : teste si un job est present en machine
224 bool BatchManager_eLSF::isRunning(const JobId & jobid)
226 throw EmulationException("Not yet implemented");
229 void BatchManager_eLSF::buildBatchScript(const Job & job) throw(EmulationException)
232 Parametre params = job.getParametre();
233 const int nbproc = params[NBPROC];
234 const long edt = params[MAXWALLTIME];
235 const long mem = params[MAXRAMSIZE];
236 const string workDir = params[WORKDIR];
237 const std::string dirForTmpFiles = params[TMPDIR];
238 const string fileToExecute = params[EXECUTABLE];
239 string::size_type p1 = fileToExecute.find_last_of("/");
240 string::size_type p2 = fileToExecute.find_last_of(".");
241 std::string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
242 std::string fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(fileToExecute.c_str()));
244 int idx = dirForTmpFiles.find("Batch/");
245 std::string filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
247 std::string TmpFileName = BuildTemporaryFileName();
248 ofstream tempOutputFile;
249 tempOutputFile.open(TmpFileName.c_str(), ofstream::out );
251 tempOutputFile << "#! /bin/sh -f" << endl ;
253 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
255 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
256 tempOutputFile << "#BSUB -n " << nbproc << endl ;
257 tempOutputFile << "#BSUB -o runSalome.output.log." << filelogtemp << endl ;
258 tempOutputFile << "#BSUB -e runSalome.error.log." << filelogtemp << endl ;
259 if( workDir.size() > 0 )
260 tempOutputFile << "cd " << workDir << endl ;
261 tempOutputFile << _mpiImpl->boot("",nbproc);
262 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
263 tempOutputFile << _mpiImpl->halt();
264 tempOutputFile.flush();
265 tempOutputFile.close();
266 chmod(TmpFileName.c_str(), 0x1ED);
267 cerr << TmpFileName.c_str() << endl;
270 if( _protocol == "rsh" )
272 else if( _protocol == "ssh" )
275 throw EmulationException("Unknown protocol");
276 command += TmpFileName;
279 command += _username;
282 command += _hostname;
284 command += dirForTmpFiles ;
286 command += rootNameToExecute ;
287 command += "_Batch.sh" ;
288 cerr << command.c_str() << endl;
289 status = system(command.c_str());
291 throw EmulationException("Error of connection on remote host");
293 RmTmpFile(TmpFileName);
297 std::string BatchManager_eLSF::getWallTime(const long edt)
304 oss << h << ":" << m;
306 oss << h << ":0" << m;