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"
42 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)
48 BatchManager_eLSF::~BatchManager_eLSF()
53 // Methode pour le controle des jobs : soumet un job au gestionnaire
54 const JobId BatchManager_eLSF::submitJob(const Job & job)
57 Parametre params = job.getParametre();
58 const std::string dirForTmpFiles = params[TMPDIR];
59 const string fileToExecute = params[EXECUTABLE];
60 string::size_type p1 = fileToExecute.find_last_of("/");
61 string::size_type p2 = fileToExecute.find_last_of(".");
62 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
64 // export input files on cluster
65 exportInputFiles(job);
67 // build batch script for job
68 buildBatchScript(job);
70 // define name of log file
71 string logFile="/tmp/logs/";
72 logFile += getenv("USER");
73 logFile += "/batchSalome_";
83 // define command to submit batch
94 command += dirForTmpFiles ;
95 command += "; bsub < " ;
96 command += fileNameToExecute ;
97 command += "_Batch.sh\" > ";
99 cerr << command.c_str() << endl;
100 status = system(command.c_str());
102 throw EmulationException("Error of connection on remote host");
104 // read id of submitted job in log file
106 FILE *fp = fopen(logFile.c_str(),"r");
107 fgets( line, 128, fp);
111 int p10 = sline.find("<");
112 int p20 = sline.find(">");
113 string strjob = sline.substr(p10+1,p20-p10-1);
115 JobId id(this, strjob);
119 // Methode pour le controle des jobs : retire un job du gestionnaire
120 void BatchManager_eLSF::deleteJob(const JobId & jobid)
124 istringstream iss(jobid.getReference());
127 // define command to submit batch
132 if (_username != ""){
133 command += _username;
137 command += _hostname;
138 command += " \"bkill " ;
139 command += iss.str();
141 cerr << command.c_str() << endl;
142 status = system(command.c_str());
144 throw EmulationException("Error of connection on remote host");
146 cerr << "jobId = " << ref << "killed" << endl;
149 // Methode pour le controle des jobs : suspend un job en file d'attente
150 void BatchManager_eLSF::holdJob(const JobId & jobid)
152 throw EmulationException("Not yet implemented");
155 // Methode pour le controle des jobs : relache un job suspendu
156 void BatchManager_eLSF::releaseJob(const JobId & jobid)
158 throw EmulationException("Not yet implemented");
162 // Methode pour le controle des jobs : modifie un job en file d'attente
163 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
165 throw EmulationException("Not yet implemented");
168 // Methode pour le controle des jobs : modifie un job en file d'attente
169 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
171 alterJob(jobid, param, Environnement());
174 // Methode pour le controle des jobs : modifie un job en file d'attente
175 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
177 alterJob(jobid, Parametre(), env);
180 // Methode pour le controle des jobs : renvoie l'etat du job
181 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
184 istringstream iss(jobid.getReference());
187 // define name of log file
188 string logFile="/tmp/logs/";
189 logFile += getenv("USER");
190 logFile += "/batchSalome_";
192 srand ( time(NULL) );
196 logFile += oss.str();
202 // define command to submit batch
206 if (_username != ""){
207 command += _username;
211 command += _hostname;
212 command += " \"bjobs " ;
213 command += iss.str();
216 cerr << command.c_str() << endl;
217 status = system(command.c_str());
219 throw EmulationException("Error of connection on remote host");
221 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
227 // Methode pour le controle des jobs : teste si un job est present en machine
228 bool BatchManager_eLSF::isRunning(const JobId & jobid)
230 throw EmulationException("Not yet implemented");
233 void BatchManager_eLSF::buildBatchScript(const Job & job) throw(EmulationException)
235 #ifndef WIN32 //TODO: need for porting on Windows
237 Parametre params = job.getParametre();
238 const int nbproc = params[NBPROC];
239 const long edt = params[MAXWALLTIME];
240 const long mem = params[MAXRAMSIZE];
241 const string workDir = params[WORKDIR];
242 const std::string dirForTmpFiles = params[TMPDIR];
243 const string fileToExecute = params[EXECUTABLE];
244 string::size_type p1 = fileToExecute.find_last_of("/");
245 string::size_type p2 = fileToExecute.find_last_of(".");
246 std::string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
247 std::string fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(fileToExecute.c_str()));
249 int idx = dirForTmpFiles.find("Batch/");
250 std::string filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
252 std::string TmpFileName = BuildTemporaryFileName();
253 ofstream tempOutputFile;
254 tempOutputFile.open(TmpFileName.c_str(), ofstream::out );
256 tempOutputFile << "#! /bin/sh -f" << endl ;
258 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
260 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
261 tempOutputFile << "#BSUB -n " << nbproc << endl ;
262 tempOutputFile << "#BSUB -o runSalome.output.log." << filelogtemp << endl ;
263 tempOutputFile << "#BSUB -e runSalome.error.log." << filelogtemp << endl ;
264 if( workDir.size() > 0 )
265 tempOutputFile << "cd " << workDir << endl ;
266 tempOutputFile << _mpiImpl->boot("",nbproc);
267 tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
268 tempOutputFile << _mpiImpl->halt();
269 tempOutputFile.flush();
270 tempOutputFile.close();
276 TmpFileName.c_str(), 0x1ED);
277 cerr << TmpFileName.c_str() << endl;
280 if( _protocol == "rsh" )
282 else if( _protocol == "ssh" )
285 throw EmulationException("Unknown protocol");
286 command += TmpFileName;
289 command += _username;
292 command += _hostname;
294 command += dirForTmpFiles ;
296 command += rootNameToExecute ;
297 command += "_Batch.sh" ;
298 cerr << command.c_str() << endl;
299 status = system(command.c_str());
301 throw EmulationException("Error of connection on remote host");
303 RmTmpFile(TmpFileName);
308 std::string BatchManager_eLSF::getWallTime(const long edt)
315 oss << h << ":" << m;
317 oss << h << ":0" << m;