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
37 #include <Batch_config.h>
45 #include "Batch_BatchManager_ePBS.hxx"
46 #include "Batch_JobInfo_ePBS.hxx"
52 BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host,
53 CommunicationProtocolType protocolType, const char * mpiImpl)
54 : BatchManager_eClient(parent, host, protocolType, mpiImpl)
60 BatchManager_ePBS::~BatchManager_ePBS()
65 // Methode pour le controle des jobs : soumet un job au gestionnaire
66 const JobId BatchManager_ePBS::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("PBS-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
97 ifstream idfile(logFile.c_str());
101 if (sline.size() == 0)
102 throw EmulationException("Error in the submission of the job on the remote host");
104 size_t pos = sline.find(".");
106 if(pos == string::npos)
109 strjob = sline.substr(0,pos);
111 JobId id(this, strjob);
115 // Methode pour le controle des jobs : retire un job du gestionnaire
116 void BatchManager_ePBS::deleteJob(const JobId & jobid)
120 istringstream iss(jobid.getReference());
123 // define command to delete batch
124 string subCommand = string("qdel ") + iss.str();
125 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
126 cerr << command.c_str() << endl;
127 status = system(command.c_str());
129 throw EmulationException("Error of connection on remote host");
131 cerr << "jobId = " << ref << "killed" << endl;
134 // Methode pour le controle des jobs : suspend un job en file d'attente
135 void BatchManager_ePBS::holdJob(const JobId & jobid)
137 throw EmulationException("Not yet implemented");
140 // Methode pour le controle des jobs : relache un job suspendu
141 void BatchManager_ePBS::releaseJob(const JobId & jobid)
143 throw EmulationException("Not yet implemented");
147 // Methode pour le controle des jobs : modifie un job en file d'attente
148 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
150 throw EmulationException("Not yet implemented");
153 // Methode pour le controle des jobs : modifie un job en file d'attente
154 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
156 alterJob(jobid, param, Environnement());
159 // Methode pour le controle des jobs : modifie un job en file d'attente
160 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
162 alterJob(jobid, Parametre(), env);
165 // Methode pour le controle des jobs : renvoie l'etat du job
166 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
169 istringstream iss(jobid.getReference());
172 // define name of log file (local)
173 string logFile = generateTemporaryFileName(string("PBS-querylog-id") + jobid.getReference());
175 // define command to query batch
176 string subCommand = string("qstat -f ") + iss.str();
177 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
180 cerr << command.c_str() << endl;
181 int status = system(command.c_str());
182 if(status && status != 153 && status != 256*153)
183 throw EmulationException("Error of connection on remote host");
185 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
189 // Methode pour le controle des jobs : teste si un job est present en machine
190 bool BatchManager_ePBS::isRunning(const JobId & jobid)
192 throw EmulationException("Not yet implemented");
195 void BatchManager_ePBS::buildBatchScript(const Job & job)
197 Parametre params = job.getParametre();
198 Environnement env = job.getEnvironnement();
199 const long nbproc = params[NBPROC];
200 const long edt = params[MAXWALLTIME];
201 const long mem = params[MAXRAMSIZE];
202 const string workDir = params[WORKDIR];
203 const std::string dirForTmpFiles = params[TMPDIR];
204 const string fileToExecute = params[EXECUTABLE];
205 const string home = params[HOMEDIR];
206 const std::string queue = params[QUEUE];
207 std::string rootNameToExecute;
208 std::string fileNameToExecute;
209 std::string filelogtemp;
210 if( fileToExecute.size() > 0 ){
211 string::size_type p1 = fileToExecute.find_last_of("/");
212 string::size_type p2 = fileToExecute.find_last_of(".");
213 rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
216 char fname[_MAX_FNAME];
218 _splitpath_s(fileToExecute.c_str(), NULL, 0, NULL, 0, fname, _MAX_FNAME, ext, _MAX_EXT);
219 string execBaseName = string(fname) + ext;
221 char* basec=strdup(fileToExecute.c_str());
222 string execBaseName = string(basename(basec));
226 fileNameToExecute = "~/" + dirForTmpFiles + "/" + execBaseName;
228 int idx = dirForTmpFiles.find("Batch/");
229 filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
232 rootNameToExecute = "command";
235 ofstream tempOutputFile;
236 std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
238 tempOutputFile << "#! /bin/sh -f" << endl;
240 tempOutputFile << "#BSUB -q " << queue << endl;
242 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl ;
244 tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
245 if( fileToExecute.size() > 0 ){
246 tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
247 tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
250 tempOutputFile << "#PBS -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
251 tempOutputFile << "#PBS -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
253 if( workDir.size() > 0 )
254 tempOutputFile << "cd " << workDir << endl ;
255 if( fileToExecute.size() > 0 ){
256 tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
257 tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
258 tempOutputFile << _mpiImpl->halt();
261 tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
262 tempOutputFile << env["COMMAND"];
265 tempOutputFile.flush();
266 tempOutputFile.close();
272 TmpFileName.c_str(), 0x1ED);
273 cerr << TmpFileName.c_str() << endl;
275 int status = _protocol.copyFile(TmpFileName, "", "",
276 dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
277 _hostname, _username);
279 throw EmulationException("Error of connection on remote host");
281 remove(TmpFileName.c_str());