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
45 #include "Batch_BatchManager_eSGE.hxx"
46 #include "Batch_JobInfo_eSGE.hxx"
52 BatchManager_eSGE::BatchManager_eSGE(const FactBatchManager * parent, const char * host,
53 CommunicationProtocolType protocolType, const char * mpiImpl)
54 : BatchManager_eClient(parent, host, protocolType, mpiImpl)
60 BatchManager_eSGE::~BatchManager_eSGE()
65 // Methode pour le controle des jobs : soumet un job au gestionnaire
66 const JobId BatchManager_eSGE::submitJob(const Job & job)
69 Parametre params = job.getParametre();
70 const std::string dirForTmpFiles = params[TMPDIR];
71 const string fileToExecute = params[EXECUTABLE];
72 string::size_type p1 = fileToExecute.find_last_of("/");
73 string::size_type p2 = fileToExecute.find_last_of(".");
74 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
76 // export input files on cluster
77 exportInputFiles(job);
79 // build batch script for job
80 buildBatchScript(job);
82 // define name of log file (local)
83 string logFile = generateTemporaryFileName("SGE-submitlog");
85 // define command to submit batch
86 string subCommand = string("cd ") + dirForTmpFiles + "; qsub " +
87 fileNameToExecute + "_Batch.sh";
88 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
91 cerr << command.c_str() << endl;
92 status = system(command.c_str());
94 throw EmulationException("Error of connection on remote host");
96 // read id of submitted job in log file
98 FILE *fp = fopen(logFile.c_str(),"r");
99 fgets( line, 128, fp);
103 istringstream iss(line);
104 iss >> strjob >> strjob >> strjob;
106 JobId id(this, strjob);
110 // Methode pour le controle des jobs : retire un job du gestionnaire
111 void BatchManager_eSGE::deleteJob(const JobId & jobid)
115 istringstream iss(jobid.getReference());
118 // define command to delete batch
119 string subCommand = string("qdel ") + iss.str();
120 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
121 cerr << command.c_str() << endl;
122 status = system(command.c_str());
124 throw EmulationException("Error of connection on remote host");
126 cerr << "jobId = " << ref << "killed" << endl;
129 // Methode pour le controle des jobs : suspend un job en file d'attente
130 void BatchManager_eSGE::holdJob(const JobId & jobid)
132 throw EmulationException("Not yet implemented");
135 // Methode pour le controle des jobs : relache un job suspendu
136 void BatchManager_eSGE::releaseJob(const JobId & jobid)
138 throw EmulationException("Not yet implemented");
142 // Methode pour le controle des jobs : modifie un job en file d'attente
143 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
145 throw EmulationException("Not yet implemented");
148 // Methode pour le controle des jobs : modifie un job en file d'attente
149 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param)
151 alterJob(jobid, param, Environnement());
154 // Methode pour le controle des jobs : modifie un job en file d'attente
155 void BatchManager_eSGE::alterJob(const JobId & jobid, const Environnement & env)
157 alterJob(jobid, Parametre(), env);
160 // Methode pour le controle des jobs : renvoie l'etat du job
161 JobInfo BatchManager_eSGE::queryJob(const JobId & jobid)
164 istringstream iss(jobid.getReference());
167 // define name of log file (local)
168 string logFile = generateTemporaryFileName(string("SGE-querylog-id") + jobid.getReference());
170 // define command to query batch
171 string subCommand = string("qstat | grep ") + iss.str();
172 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
175 cerr << command.c_str() << endl;
176 int status = system(command.c_str());
177 if (status && status != 256)
178 throw EmulationException("Error of connection on remote host");
180 JobInfo_eSGE ji = JobInfo_eSGE(id,logFile);
184 // Methode pour le controle des jobs : teste si un job est present en machine
185 bool BatchManager_eSGE::isRunning(const JobId & jobid)
187 throw EmulationException("Not yet implemented");
190 void BatchManager_eSGE::buildBatchScript(const Job & job)
193 //TODO porting on Win32 platform
194 Parametre params = job.getParametre();
195 Environnement env = job.getEnvironnement();
196 const long nbproc = params[NBPROC];
197 const long edt = params[MAXWALLTIME];
198 const long mem = params[MAXRAMSIZE];
199 const string workDir = params[WORKDIR];
200 const std::string dirForTmpFiles = params[TMPDIR];
201 const string fileToExecute = params[EXECUTABLE];
202 const string home = params[HOMEDIR];
203 const std::string queue = params[QUEUE];
204 std::string rootNameToExecute;
205 std::string fileNameToExecute;
206 std::string filelogtemp;
207 if( fileToExecute.size() > 0 ){
208 string::size_type p1 = fileToExecute.find_last_of("/");
209 string::size_type p2 = fileToExecute.find_last_of(".");
210 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
211 fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename((char *) fileToExecute.c_str()));
213 int idx = dirForTmpFiles.find("Batch/");
214 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
217 rootNameToExecute = "command";
220 ofstream tempOutputFile;
221 std::string TmpFileName = createAndOpenTemporaryFile("SGE-script", tempOutputFile);
223 tempOutputFile << "#! /bin/sh -f" << endl;
225 tempOutputFile << "#$ -q " << queue << endl;
226 tempOutputFile << "#$ -pe mpich " << nbproc << endl;
228 tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
230 tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
231 if( fileToExecute.size() > 0 ){
232 tempOutputFile << "#$ -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
233 tempOutputFile << "#$ -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
236 tempOutputFile << "#$ -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
237 tempOutputFile << "#$ -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
239 if( workDir.size() > 0 )
240 tempOutputFile << "cd " << workDir << endl ;
241 if( fileToExecute.size() > 0 ){
242 tempOutputFile << _mpiImpl->boot("",nbproc);
243 tempOutputFile << _mpiImpl->run("${TMPDIR}/machines",nbproc,fileNameToExecute);
244 tempOutputFile << _mpiImpl->halt();
247 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
248 tempOutputFile << env["COMMAND"];
251 tempOutputFile.flush();
252 tempOutputFile.close();
253 chmod(TmpFileName.c_str(), 0x1ED);
254 cerr << TmpFileName.c_str() << endl;
256 int status = _protocol.copyFile(TmpFileName, "", "",
257 dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
258 _hostname, _username);
260 throw EmulationException("Error of connection on remote host");
262 remove(TmpFileName.c_str());
266 std::string BatchManager_eSGE::getWallTime(const long edt)
273 oss << h << ":" << m;
275 oss << h << ":0" << m;