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_ePBS.cxx : emulation of PBS 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_ePBS.hxx"
38 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)
44 BatchManager_ePBS::~BatchManager_ePBS()
49 // Methode pour le controle des jobs : soumet un job au gestionnaire
50 const JobId BatchManager_ePBS::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 += "; qsub " ;
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 pos = sline.find(".");
109 if(pos == string::npos)
112 strjob = sline.substr(0,pos);
114 JobId id(this, strjob);
118 // Methode pour le controle des jobs : retire un job du gestionnaire
119 void BatchManager_ePBS::deleteJob(const JobId & jobid)
123 istringstream iss(jobid.getReference());
126 // define command to submit batch
131 if (_username != ""){
132 command += _username;
136 command += _hostname;
137 command += " \"qdel " ;
138 command += iss.str();
140 cerr << command.c_str() << endl;
141 status = system(command.c_str());
143 throw EmulationException("Error of connection on remote host");
145 cerr << "jobId = " << ref << "killed" << endl;
148 // Methode pour le controle des jobs : suspend un job en file d'attente
149 void BatchManager_ePBS::holdJob(const JobId & jobid)
151 throw EmulationException("Not yet implemented");
154 // Methode pour le controle des jobs : relache un job suspendu
155 void BatchManager_ePBS::releaseJob(const JobId & jobid)
157 throw EmulationException("Not yet implemented");
161 // Methode pour le controle des jobs : modifie un job en file d'attente
162 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
164 throw EmulationException("Not yet implemented");
167 // Methode pour le controle des jobs : modifie un job en file d'attente
168 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
170 alterJob(jobid, param, Environnement());
173 // Methode pour le controle des jobs : modifie un job en file d'attente
174 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
176 alterJob(jobid, Parametre(), env);
179 // Methode pour le controle des jobs : renvoie l'etat du job
180 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
183 istringstream iss(jobid.getReference());
186 // define name of log file
187 string logFile="/tmp/logs/";
188 logFile += getenv("USER");
189 logFile += "/batchSalome_";
192 oss << this << "_" << id;
193 logFile += oss.str();
199 // define command to submit batch
203 if (_username != ""){
204 command += _username;
208 command += _hostname;
209 command += " \"qstat -f " ;
210 command += iss.str();
213 cerr << command.c_str() << endl;
214 status = system(command.c_str());
215 if(status && status != 153 && status != 256*153)
216 throw EmulationException("Error of connection on remote host");
218 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
222 // Methode pour le controle des jobs : teste si un job est present en machine
223 bool BatchManager_ePBS::isRunning(const JobId & jobid)
225 throw EmulationException("Not yet implemented");
228 void BatchManager_ePBS::buildBatchScript(const Job & job) throw(EmulationException)
231 Parametre params = job.getParametre();
232 const long nbproc = params[NBPROC];
233 const long edt = params[MAXWALLTIME];
234 const long mem = params[MAXRAMSIZE];
235 const string workDir = params[WORKDIR];
236 const std::string dirForTmpFiles = params[TMPDIR];
237 const string fileToExecute = params[EXECUTABLE];
238 const string home = params[HOMEDIR];
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 << "#PBS -l walltime=" << edt*60 << endl ;
255 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
256 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/runSalome.output.log." << filelogtemp << endl ;
257 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/runSalome.error.log." << filelogtemp << endl ;
258 if( workDir.size() > 0 )
259 tempOutputFile << "cd " << workDir << endl ;
260 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
261 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
262 tempOutputFile << _mpiImpl->halt();
263 tempOutputFile.flush();
264 tempOutputFile.close();
265 chmod(TmpFileName.c_str(), 0x1ED);
266 cerr << TmpFileName.c_str() << endl;
269 if( _protocol == "rsh" )
271 else if( _protocol == "ssh" )
274 throw EmulationException("Unknown protocol");
275 command += TmpFileName;
278 command += _username;
281 command += _hostname;
283 command += dirForTmpFiles ;
285 command += rootNameToExecute ;
286 command += "_Batch.sh" ;
287 cerr << command.c_str() << endl;
288 status = system(command.c_str());
290 throw EmulationException("Error of connection on remote host");
292 RmTmpFile(TmpFileName);