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_ePBS.cxx : emulation of PBS client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
38 #include "Batch_BatchManager_ePBS.hxx"
39 #include "Batch_config.h"
45 BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host,
46 const char * protocol, const char * mpiImpl)
47 : BatchManager_eClient(parent, host, protocol, mpiImpl)
53 BatchManager_ePBS::~BatchManager_ePBS()
58 // Methode pour le controle des jobs : soumet un job au gestionnaire
59 const JobId BatchManager_ePBS::submitJob(const Job & job)
62 Parametre params = job.getParametre();
63 const std::string dirForTmpFiles = params[TMPDIR];
64 const string fileToExecute = params[EXECUTABLE];
65 string::size_type p1 = fileToExecute.find_last_of("/");
66 string::size_type p2 = fileToExecute.find_last_of(".");
67 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
69 // export input files on cluster
70 exportInputFiles(job);
72 // build batch script for job
73 buildBatchScript(job);
75 // define name of log file (local)
76 string logFile = generateTemporaryFileName("PBS-submitlog");
78 // define command to submit batch
79 string command = "\"";
82 if( _protocol == "rsh" )
84 else if( _protocol == "ssh" )
87 throw EmulationException("Unknown protocol : only rsh and ssh are known !");
92 command += _username + "@";
95 command += _hostname + " ";
100 command += dirForTmpFiles ;
101 command += "; qsub " ;
102 command += fileNameToExecute ;
103 command += "_Batch.sh";
109 cerr << command.c_str() << endl;
110 status = system(command.c_str());
112 throw EmulationException("Error of connection on remote host");
114 // read id of submitted job in log file
116 FILE *fp = fopen(logFile.c_str(),"r");
117 fgets( line, 128, fp);
121 size_t pos = sline.find(".");
123 if(pos == string::npos)
126 strjob = sline.substr(0,pos);
128 JobId id(this, strjob);
132 // Methode pour le controle des jobs : retire un job du gestionnaire
133 void BatchManager_ePBS::deleteJob(const JobId & jobid)
137 istringstream iss(jobid.getReference());
140 // define command to submit batch
145 if (_username != ""){
146 command += _username;
150 command += _hostname;
151 command += " \"qdel " ;
152 command += iss.str();
154 cerr << command.c_str() << endl;
155 status = system(command.c_str());
157 throw EmulationException("Error of connection on remote host");
159 cerr << "jobId = " << ref << "killed" << endl;
162 // Methode pour le controle des jobs : suspend un job en file d'attente
163 void BatchManager_ePBS::holdJob(const JobId & jobid)
165 throw EmulationException("Not yet implemented");
168 // Methode pour le controle des jobs : relache un job suspendu
169 void BatchManager_ePBS::releaseJob(const JobId & jobid)
171 throw EmulationException("Not yet implemented");
175 // Methode pour le controle des jobs : modifie un job en file d'attente
176 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
178 throw EmulationException("Not yet implemented");
181 // Methode pour le controle des jobs : modifie un job en file d'attente
182 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
184 alterJob(jobid, param, Environnement());
187 // Methode pour le controle des jobs : modifie un job en file d'attente
188 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
190 alterJob(jobid, Parametre(), env);
193 // Methode pour le controle des jobs : renvoie l'etat du job
194 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
197 istringstream iss(jobid.getReference());
200 // define name of log file (local)
201 string logFile = generateTemporaryFileName(string("PBS-querylog-id") + jobid.getReference());
203 // define command to query batch
204 string command = "\"";
207 if( _protocol == "rsh" )
209 else if( _protocol == "ssh" )
212 throw EmulationException("Unknown protocol : only rsh and ssh are known !");
216 if (_username != ""){
217 command += _username + "@";
220 command += _hostname + " ";
224 command += "qstat -f " ;
225 command += iss.str();
231 cerr << command.c_str() << endl;
232 int status = system(command.c_str());
233 if(status && status != 153 && status != 256*153)
234 throw EmulationException("Error of connection on remote host");
236 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
240 // Methode pour le controle des jobs : teste si un job est present en machine
241 bool BatchManager_ePBS::isRunning(const JobId & jobid)
243 throw EmulationException("Not yet implemented");
246 void BatchManager_ePBS::buildBatchScript(const Job & job)
249 Parametre params = job.getParametre();
250 Environnement env = job.getEnvironnement();
251 const long nbproc = params[NBPROC];
252 const long edt = params[MAXWALLTIME];
253 const long mem = params[MAXRAMSIZE];
254 const string workDir = params[WORKDIR];
255 const std::string dirForTmpFiles = params[TMPDIR];
256 const string fileToExecute = params[EXECUTABLE];
257 const string home = params[HOMEDIR];
258 const std::string queue = params[QUEUE];
259 std::string rootNameToExecute;
260 std::string fileNameToExecute;
261 std::string filelogtemp;
262 if( fileToExecute.size() > 0 ){
263 string::size_type p1 = fileToExecute.find_last_of("/");
264 string::size_type p2 = fileToExecute.find_last_of(".");
265 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
266 char* basec=strdup(fileToExecute.c_str());
267 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
270 int idx = dirForTmpFiles.find("Batch/");
271 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
274 rootNameToExecute = "command";
277 ofstream tempOutputFile;
278 std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
280 tempOutputFile << "#! /bin/sh -f" << endl;
282 tempOutputFile << "#BSUB -q " << queue << endl;
284 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl ;
286 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
287 if( fileToExecute.size() > 0 ){
288 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
289 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
292 tempOutputFile << "#PBS -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
293 tempOutputFile << "#PBS -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
295 if( workDir.size() > 0 )
296 tempOutputFile << "cd " << workDir << endl ;
297 if( fileToExecute.size() > 0 ){
298 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
299 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
300 tempOutputFile << _mpiImpl->halt();
303 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
304 tempOutputFile << env["COMMAND"];
307 tempOutputFile.flush();
308 tempOutputFile.close();
314 TmpFileName.c_str(), 0x1ED);
315 cerr << TmpFileName.c_str() << endl;
317 string command = "\"";
320 if( _protocol == "rsh" ) {
322 } else if( _protocol == "ssh" ) {
325 throw EmulationException("Unknown protocol : only rsh and ssh are known !");
329 command += TmpFileName;
332 command += _username;
335 command += _hostname;
337 command += dirForTmpFiles ;
339 command += rootNameToExecute ;
340 command += "_Batch.sh" ;
341 cerr << command.c_str() << endl;
342 status = system(command.c_str());
344 throw EmulationException("Error of connection on remote host");
346 remove(TmpFileName.c_str());