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_eSGE.cxx : emulation of SGE client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
36 #include "Batch_BatchManager_eSGE.hxx"
49 BatchManager_eSGE::BatchManager_eSGE(const FactBatchManager * parent, const char * host, const char * protocol, const char * mpiImpl) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_eClient(parent,host,protocol,mpiImpl)
55 BatchManager_eSGE::~BatchManager_eSGE()
60 // Methode pour le controle des jobs : soumet un job au gestionnaire
61 const JobId BatchManager_eSGE::submitJob(const Job & job)
64 Parametre params = job.getParametre();
65 const std::string dirForTmpFiles = params[TMPDIR];
66 const string fileToExecute = params[EXECUTABLE];
67 string::size_type p1 = fileToExecute.find_last_of("/");
68 string::size_type p2 = fileToExecute.find_last_of(".");
69 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
71 // export input files on cluster
72 exportInputFiles(job);
74 // build batch script for job
75 buildBatchScript(job);
77 // define name of log file
78 string logFile="/tmp/logs/";
79 logFile += getenv("USER");
80 logFile += "/batchSalome_";
90 // define command to submit batch
100 command += " \"cd " ;
101 command += dirForTmpFiles ;
102 command += "; qsub " ;
103 command += fileNameToExecute ;
104 command += "_Batch.sh\" > ";
106 cerr << command.c_str() << endl;
107 status = system(command.c_str());
109 throw EmulationException("Error of connection on remote host");
111 // read id of submitted job in log file
113 FILE *fp = fopen(logFile.c_str(),"r");
114 fgets( line, 128, fp);
118 istringstream iss(line);
119 iss >> strjob >> strjob >> strjob;
121 JobId id(this, strjob);
125 // Methode pour le controle des jobs : retire un job du gestionnaire
126 void BatchManager_eSGE::deleteJob(const JobId & jobid)
130 istringstream iss(jobid.getReference());
133 // define command to submit batch
138 if (_username != ""){
139 command += _username;
143 command += _hostname;
144 command += " \"qdel " ;
145 command += iss.str();
147 cerr << command.c_str() << endl;
148 status = system(command.c_str());
150 throw EmulationException("Error of connection on remote host");
152 cerr << "jobId = " << ref << "killed" << endl;
155 // Methode pour le controle des jobs : suspend un job en file d'attente
156 void BatchManager_eSGE::holdJob(const JobId & jobid)
158 throw EmulationException("Not yet implemented");
161 // Methode pour le controle des jobs : relache un job suspendu
162 void BatchManager_eSGE::releaseJob(const JobId & jobid)
164 throw EmulationException("Not yet implemented");
168 // Methode pour le controle des jobs : modifie un job en file d'attente
169 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
171 throw EmulationException("Not yet implemented");
174 // Methode pour le controle des jobs : modifie un job en file d'attente
175 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param)
177 alterJob(jobid, param, Environnement());
180 // Methode pour le controle des jobs : modifie un job en file d'attente
181 void BatchManager_eSGE::alterJob(const JobId & jobid, const Environnement & env)
183 alterJob(jobid, Parametre(), env);
186 // Methode pour le controle des jobs : renvoie l'etat du job
187 JobInfo BatchManager_eSGE::queryJob(const JobId & jobid)
190 istringstream iss(jobid.getReference());
193 // define name of log file
194 string logFile="/tmp/logs/";
195 logFile += getenv("USER");
196 logFile += "/batchSalome_";
199 oss << this << "_" << id;
200 logFile += oss.str();
206 // define command to submit batch
210 if (_username != ""){
211 command += _username;
215 command += _hostname;
216 command += " \"qstat | grep " ;
217 command += iss.str();
220 cerr << command.c_str() << endl;
221 status = system(command.c_str());
222 if(status && status != 256)
223 throw EmulationException("Error of connection on remote host");
225 JobInfo_eSGE ji = JobInfo_eSGE(id,logFile);
229 // Methode pour le controle des jobs : teste si un job est present en machine
230 bool BatchManager_eSGE::isRunning(const JobId & jobid)
232 throw EmulationException("Not yet implemented");
235 void BatchManager_eSGE::buildBatchScript(const Job & job)
238 //TODO porting on Win32 platform
240 Parametre params = job.getParametre();
241 Environnement env = job.getEnvironnement();
242 const long nbproc = params[NBPROC];
243 const long edt = params[MAXWALLTIME];
244 const long mem = params[MAXRAMSIZE];
245 const string workDir = params[WORKDIR];
246 const std::string dirForTmpFiles = params[TMPDIR];
247 const string fileToExecute = params[EXECUTABLE];
248 const string home = params[HOMEDIR];
249 const std::string queue = params[QUEUE];
250 std::string rootNameToExecute;
251 std::string fileNameToExecute;
252 std::string filelogtemp;
253 if( fileToExecute.size() > 0 ){
254 string::size_type p1 = fileToExecute.find_last_of("/");
255 string::size_type p2 = fileToExecute.find_last_of(".");
256 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
257 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename((char *) fileToExecute.c_str()));
259 int idx = dirForTmpFiles.find("Batch/");
260 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
263 rootNameToExecute = "command";
266 ofstream tempOutputFile;
267 std::string TmpFileName = createAndOpenTemporaryFile(tempOutputFile);
269 tempOutputFile << "#! /bin/sh -f" << endl;
271 tempOutputFile << "#$ -q " << queue << endl;
272 tempOutputFile << "#$ -pe mpich " << nbproc << endl;
274 tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
276 tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
277 if( fileToExecute.size() > 0 ){
278 tempOutputFile << "#$ -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
279 tempOutputFile << "#$ -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
282 tempOutputFile << "#$ -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
283 tempOutputFile << "#$ -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
285 if( workDir.size() > 0 )
286 tempOutputFile << "cd " << workDir << endl ;
287 if( fileToExecute.size() > 0 ){
288 tempOutputFile << _mpiImpl->boot("",nbproc);
289 tempOutputFile << _mpiImpl->run("${TMPDIR}/machines",nbproc,fileNameToExecute);
290 tempOutputFile << _mpiImpl->halt();
293 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
294 tempOutputFile << env["COMMAND"];
297 tempOutputFile.flush();
298 tempOutputFile.close();
299 chmod(TmpFileName.c_str(), 0x1ED);
300 cerr << TmpFileName.c_str() << endl;
303 if( _protocol == "rsh" )
305 else if( _protocol == "ssh" )
308 throw EmulationException("Unknown protocol");
309 command += TmpFileName;
312 command += _username;
315 command += _hostname;
317 command += dirForTmpFiles ;
319 command += rootNameToExecute ;
320 command += "_Batch.sh" ;
321 cerr << command.c_str() << endl;
322 status = system(command.c_str());
324 throw EmulationException("Error of connection on remote host");
326 remove(TmpFileName.c_str());
330 std::string BatchManager_eSGE::getWallTime(const long edt)
337 oss << h << ":" << m;
339 oss << h << ":0" << m;