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
39 #include "Batch_BatchManager_ePBS.hxx"
51 BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host, const char * protocol, const char * mpiImpl) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_eClient(parent,host,protocol,mpiImpl)
57 BatchManager_ePBS::~BatchManager_ePBS()
62 // Methode pour le controle des jobs : soumet un job au gestionnaire
63 const JobId BatchManager_ePBS::submitJob(const Job & job)
66 Parametre params = job.getParametre();
67 const std::string dirForTmpFiles = params[TMPDIR];
68 const string fileToExecute = params[EXECUTABLE];
69 string::size_type p1 = fileToExecute.find_last_of("/");
70 string::size_type p2 = fileToExecute.find_last_of(".");
71 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
73 // export input files on cluster
74 exportInputFiles(job);
76 // build batch script for job
77 buildBatchScript(job);
79 // define name of log file
80 string logFile="/tmp/logs/";
81 logFile += getenv("USER");
82 logFile += "/batchSalome_";
92 // define command to submit batch
101 command += _hostname;
102 command += " \"cd " ;
103 command += dirForTmpFiles ;
104 command += "; qsub " ;
105 command += fileNameToExecute ;
106 command += "_Batch.sh\" > ";
108 cerr << command.c_str() << endl;
109 status = system(command.c_str());
111 throw EmulationException("Error of connection on remote host");
113 // read id of submitted job in log file
115 FILE *fp = fopen(logFile.c_str(),"r");
116 fgets( line, 128, fp);
120 int pos = sline.find(".");
122 if(pos == string::npos)
125 strjob = sline.substr(0,pos);
127 JobId id(this, strjob);
131 // Methode pour le controle des jobs : retire un job du gestionnaire
132 void BatchManager_ePBS::deleteJob(const JobId & jobid)
136 istringstream iss(jobid.getReference());
139 // define command to submit batch
144 if (_username != ""){
145 command += _username;
149 command += _hostname;
150 command += " \"qdel " ;
151 command += iss.str();
153 cerr << command.c_str() << endl;
154 status = system(command.c_str());
156 throw EmulationException("Error of connection on remote host");
158 cerr << "jobId = " << ref << "killed" << endl;
161 // Methode pour le controle des jobs : suspend un job en file d'attente
162 void BatchManager_ePBS::holdJob(const JobId & jobid)
164 throw EmulationException("Not yet implemented");
167 // Methode pour le controle des jobs : relache un job suspendu
168 void BatchManager_ePBS::releaseJob(const JobId & jobid)
170 throw EmulationException("Not yet implemented");
174 // Methode pour le controle des jobs : modifie un job en file d'attente
175 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
177 throw EmulationException("Not yet implemented");
180 // Methode pour le controle des jobs : modifie un job en file d'attente
181 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
183 alterJob(jobid, param, Environnement());
186 // Methode pour le controle des jobs : modifie un job en file d'attente
187 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
189 alterJob(jobid, Parametre(), env);
192 // Methode pour le controle des jobs : renvoie l'etat du job
193 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
196 istringstream iss(jobid.getReference());
199 // define name of log file
200 string logFile="/tmp/logs/";
201 logFile += getenv("USER");
202 logFile += "/batchSalome_";
205 oss << this << "_" << id;
206 logFile += oss.str();
212 // define command to submit batch
216 if (_username != ""){
217 command += _username;
221 command += _hostname;
222 command += " \"qstat -f " ;
223 command += iss.str();
226 cerr << command.c_str() << endl;
227 status = system(command.c_str());
228 if(status && status != 153 && status != 256*153)
229 throw EmulationException("Error of connection on remote host");
231 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
235 // Methode pour le controle des jobs : teste si un job est present en machine
236 bool BatchManager_ePBS::isRunning(const JobId & jobid)
238 throw EmulationException("Not yet implemented");
241 void BatchManager_ePBS::buildBatchScript(const Job & job)
243 #ifndef WIN32 //TODO: need for porting on Windows
245 Parametre params = job.getParametre();
246 Environnement env = job.getEnvironnement();
247 const long nbproc = params[NBPROC];
248 const long edt = params[MAXWALLTIME];
249 const long mem = params[MAXRAMSIZE];
250 const string workDir = params[WORKDIR];
251 const std::string dirForTmpFiles = params[TMPDIR];
252 const string fileToExecute = params[EXECUTABLE];
253 const string home = params[HOMEDIR];
254 const std::string queue = params[QUEUE];
255 std::string rootNameToExecute;
256 std::string fileNameToExecute;
257 std::string filelogtemp;
258 if( fileToExecute.size() > 0 ){
259 string::size_type p1 = fileToExecute.find_last_of("/");
260 string::size_type p2 = fileToExecute.find_last_of(".");
261 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
262 char* basec=strdup(fileToExecute.c_str());
263 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
266 int idx = dirForTmpFiles.find("Batch/");
267 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
270 rootNameToExecute = "command";
273 ofstream tempOutputFile;
274 std::string TmpFileName = createAndOpenTemporaryFile(tempOutputFile);
276 tempOutputFile << "#! /bin/sh -f" << endl;
278 tempOutputFile << "#BSUB -q " << queue << endl;
280 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl ;
282 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
283 if( fileToExecute.size() > 0 ){
284 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
285 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
288 tempOutputFile << "#PBS -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
289 tempOutputFile << "#PBS -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
291 if( workDir.size() > 0 )
292 tempOutputFile << "cd " << workDir << endl ;
293 if( fileToExecute.size() > 0 ){
294 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
295 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
296 tempOutputFile << _mpiImpl->halt();
299 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
300 tempOutputFile << env["COMMAND"];
303 tempOutputFile.flush();
304 tempOutputFile.close();
310 TmpFileName.c_str(), 0x1ED);
311 cerr << TmpFileName.c_str() << endl;
314 if( _protocol == "rsh" )
316 else if( _protocol == "ssh" )
319 throw EmulationException("Unknown protocol");
320 command += TmpFileName;
323 command += _username;
326 command += _hostname;
328 command += dirForTmpFiles ;
330 command += rootNameToExecute ;
331 command += "_Batch.sh" ;
332 cerr << command.c_str() << endl;
333 status = system(command.c_str());
335 throw EmulationException("Error of connection on remote host");
337 remove(TmpFileName.c_str());