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,
52 const char * protocol, const char * mpiImpl)
53 : BatchManager_eClient(parent, host, protocol, mpiImpl)
59 BatchManager_ePBS::~BatchManager_ePBS()
64 // Methode pour le controle des jobs : soumet un job au gestionnaire
65 const JobId BatchManager_ePBS::submitJob(const Job & job)
68 throw NotYetImplementedException("PBS emulation not supported on Windows platform yet");
71 Parametre params = job.getParametre();
72 const std::string dirForTmpFiles = params[TMPDIR];
73 const string fileToExecute = params[EXECUTABLE];
74 string::size_type p1 = fileToExecute.find_last_of("/");
75 string::size_type p2 = fileToExecute.find_last_of(".");
76 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
78 // export input files on cluster
79 exportInputFiles(job);
81 // build batch script for job
82 buildBatchScript(job);
84 // create log dir (local) and define name of log file
85 string logDir = "/tmp/logs/";
86 mkdir(logDir.c_str(), S_IRWXU);
87 logDir += getenv("USER");
88 mkdir(logDir.c_str(), S_IRWXU);
89 string logFile = logDir + "/batchSalome_";
99 // define command to submit batch
104 command += _username;
108 command += _hostname;
109 command += " \"cd " ;
110 command += dirForTmpFiles ;
111 command += "; qsub " ;
112 command += fileNameToExecute ;
113 command += "_Batch.sh\" > ";
115 cerr << command.c_str() << endl;
116 status = system(command.c_str());
118 throw EmulationException("Error of connection on remote host");
120 // read id of submitted job in log file
122 FILE *fp = fopen(logFile.c_str(),"r");
123 fgets( line, 128, fp);
127 size_t pos = sline.find(".");
129 if(pos == string::npos)
132 strjob = sline.substr(0,pos);
134 JobId id(this, strjob);
139 // Methode pour le controle des jobs : retire un job du gestionnaire
140 void BatchManager_ePBS::deleteJob(const JobId & jobid)
144 istringstream iss(jobid.getReference());
147 // define command to submit batch
152 if (_username != ""){
153 command += _username;
157 command += _hostname;
158 command += " \"qdel " ;
159 command += iss.str();
161 cerr << command.c_str() << endl;
162 status = system(command.c_str());
164 throw EmulationException("Error of connection on remote host");
166 cerr << "jobId = " << ref << "killed" << endl;
169 // Methode pour le controle des jobs : suspend un job en file d'attente
170 void BatchManager_ePBS::holdJob(const JobId & jobid)
172 throw EmulationException("Not yet implemented");
175 // Methode pour le controle des jobs : relache un job suspendu
176 void BatchManager_ePBS::releaseJob(const JobId & jobid)
178 throw EmulationException("Not yet implemented");
182 // Methode pour le controle des jobs : modifie un job en file d'attente
183 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
185 throw EmulationException("Not yet implemented");
188 // Methode pour le controle des jobs : modifie un job en file d'attente
189 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
191 alterJob(jobid, param, Environnement());
194 // Methode pour le controle des jobs : modifie un job en file d'attente
195 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
197 alterJob(jobid, Parametre(), env);
200 // Methode pour le controle des jobs : renvoie l'etat du job
201 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
204 istringstream iss(jobid.getReference());
207 // define name of log file
208 string logFile="/tmp/logs/";
209 logFile += getenv("USER");
210 logFile += "/batchSalome_";
213 oss << this << "_" << id;
214 logFile += oss.str();
220 // define command to submit batch
224 if (_username != ""){
225 command += _username;
229 command += _hostname;
230 command += " \"qstat -f " ;
231 command += iss.str();
234 cerr << command.c_str() << endl;
235 status = system(command.c_str());
236 if(status && status != 153 && status != 256*153)
237 throw EmulationException("Error of connection on remote host");
239 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
243 // Methode pour le controle des jobs : teste si un job est present en machine
244 bool BatchManager_ePBS::isRunning(const JobId & jobid)
246 throw EmulationException("Not yet implemented");
249 void BatchManager_ePBS::buildBatchScript(const Job & job)
251 #ifndef WIN32 //TODO: need for porting on Windows
253 Parametre params = job.getParametre();
254 Environnement env = job.getEnvironnement();
255 const long nbproc = params[NBPROC];
256 const long edt = params[MAXWALLTIME];
257 const long mem = params[MAXRAMSIZE];
258 const string workDir = params[WORKDIR];
259 const std::string dirForTmpFiles = params[TMPDIR];
260 const string fileToExecute = params[EXECUTABLE];
261 const string home = params[HOMEDIR];
262 const std::string queue = params[QUEUE];
263 std::string rootNameToExecute;
264 std::string fileNameToExecute;
265 std::string filelogtemp;
266 if( fileToExecute.size() > 0 ){
267 string::size_type p1 = fileToExecute.find_last_of("/");
268 string::size_type p2 = fileToExecute.find_last_of(".");
269 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
270 char* basec=strdup(fileToExecute.c_str());
271 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
274 int idx = dirForTmpFiles.find("Batch/");
275 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
278 rootNameToExecute = "command";
281 ofstream tempOutputFile;
282 std::string TmpFileName = createAndOpenTemporaryFile(tempOutputFile);
284 tempOutputFile << "#! /bin/sh -f" << endl;
286 tempOutputFile << "#BSUB -q " << queue << endl;
288 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl ;
290 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
291 if( fileToExecute.size() > 0 ){
292 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
293 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
296 tempOutputFile << "#PBS -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
297 tempOutputFile << "#PBS -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
299 if( workDir.size() > 0 )
300 tempOutputFile << "cd " << workDir << endl ;
301 if( fileToExecute.size() > 0 ){
302 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
303 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
304 tempOutputFile << _mpiImpl->halt();
307 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
308 tempOutputFile << env["COMMAND"];
311 tempOutputFile.flush();
312 tempOutputFile.close();
318 TmpFileName.c_str(), 0x1ED);
319 cerr << TmpFileName.c_str() << endl;
322 if( _protocol == "rsh" )
324 else if( _protocol == "ssh" )
327 throw EmulationException("Unknown protocol");
328 command += TmpFileName;
331 command += _username;
334 command += _hostname;
336 command += dirForTmpFiles ;
338 command += rootNameToExecute ;
339 command += "_Batch.sh" ;
340 cerr << command.c_str() << endl;
341 status = system(command.c_str());
343 throw EmulationException("Error of connection on remote host");
345 remove(TmpFileName.c_str());