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"
42 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)
48 BatchManager_ePBS::~BatchManager_ePBS()
53 // Methode pour le controle des jobs : soumet un job au gestionnaire
54 const JobId BatchManager_ePBS::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 += "; qsub " ;
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 pos = sline.find(".");
113 if(pos == string::npos)
116 strjob = sline.substr(0,pos);
118 JobId id(this, strjob);
122 // Methode pour le controle des jobs : retire un job du gestionnaire
123 void BatchManager_ePBS::deleteJob(const JobId & jobid)
127 istringstream iss(jobid.getReference());
130 // define command to submit batch
135 if (_username != ""){
136 command += _username;
140 command += _hostname;
141 command += " \"qdel " ;
142 command += iss.str();
144 cerr << command.c_str() << endl;
145 status = system(command.c_str());
147 throw EmulationException("Error of connection on remote host");
149 cerr << "jobId = " << ref << "killed" << endl;
152 // Methode pour le controle des jobs : suspend un job en file d'attente
153 void BatchManager_ePBS::holdJob(const JobId & jobid)
155 throw EmulationException("Not yet implemented");
158 // Methode pour le controle des jobs : relache un job suspendu
159 void BatchManager_ePBS::releaseJob(const JobId & jobid)
161 throw EmulationException("Not yet implemented");
165 // Methode pour le controle des jobs : modifie un job en file d'attente
166 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
168 throw EmulationException("Not yet implemented");
171 // Methode pour le controle des jobs : modifie un job en file d'attente
172 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
174 alterJob(jobid, param, Environnement());
177 // Methode pour le controle des jobs : modifie un job en file d'attente
178 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
180 alterJob(jobid, Parametre(), env);
183 // Methode pour le controle des jobs : renvoie l'etat du job
184 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
187 istringstream iss(jobid.getReference());
190 // define name of log file
191 string logFile="/tmp/logs/";
192 logFile += getenv("USER");
193 logFile += "/batchSalome_";
196 oss << this << "_" << id;
197 logFile += oss.str();
203 // define command to submit batch
207 if (_username != ""){
208 command += _username;
212 command += _hostname;
213 command += " \"qstat -f " ;
214 command += iss.str();
217 cerr << command.c_str() << endl;
218 status = system(command.c_str());
219 if(status && status != 153 && status != 256*153)
220 throw EmulationException("Error of connection on remote host");
222 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
226 // Methode pour le controle des jobs : teste si un job est present en machine
227 bool BatchManager_ePBS::isRunning(const JobId & jobid)
229 throw EmulationException("Not yet implemented");
232 void BatchManager_ePBS::buildBatchScript(const Job & job) throw(EmulationException)
234 #ifndef WIN32 //TODO: need for porting on Windows
236 Parametre params = job.getParametre();
237 const long nbproc = params[NBPROC];
238 const long edt = params[MAXWALLTIME];
239 const long mem = params[MAXRAMSIZE];
240 const string workDir = params[WORKDIR];
241 const std::string dirForTmpFiles = params[TMPDIR];
242 const string fileToExecute = params[EXECUTABLE];
243 const string home = params[HOMEDIR];
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 << "#PBS -l walltime=" << edt*60 << endl ;
260 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
261 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/runSalome.output.log." << filelogtemp << endl ;
262 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/runSalome.error.log." << filelogtemp << endl ;
263 if( workDir.size() > 0 )
264 tempOutputFile << "cd " << workDir << endl ;
265 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
266 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
267 tempOutputFile << _mpiImpl->halt();
268 tempOutputFile.flush();
269 tempOutputFile.close();
275 TmpFileName.c_str(), 0x1ED);
276 cerr << TmpFileName.c_str() << endl;
279 if( _protocol == "rsh" )
281 else if( _protocol == "ssh" )
284 throw EmulationException("Unknown protocol");
285 command += TmpFileName;
288 command += _username;
291 command += _hostname;
293 command += dirForTmpFiles ;
295 command += rootNameToExecute ;
296 command += "_Batch.sh" ;
297 cerr << command.c_str() << endl;
298 status = system(command.c_str());
300 throw EmulationException("Error of connection on remote host");
302 RmTmpFile(TmpFileName);