1 // Copyright (C) 2007-2011 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_eLSF.cxx : emulation of LSF 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_Constants.hxx>
37 #include <Batch_Utils.hxx>
38 #include "Batch_BatchManager_eLSF.hxx"
39 #include "Batch_JobInfo_eLSF.hxx"
45 BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
46 const char * username,
47 CommunicationProtocolType protocolType, const char * mpiImpl)
48 : BatchManager(parent, host),
49 BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
56 BatchManager_eLSF::~BatchManager_eLSF()
61 // Methode pour le controle des jobs : soumet un job au gestionnaire
62 const JobId BatchManager_eLSF::submitJob(const Job & job)
64 Parametre params = job.getParametre();
65 const std::string workDir = params[WORKDIR];
67 // export input files on cluster
68 cerr << "Export des fichiers en entree" << endl;
69 exportInputFiles(job);
71 // build batch script for job
72 cerr << "Construction du script de batch" << endl;
73 string scriptFile = buildSubmissionScript(job);
74 cerr << "Script envoye" << endl;
76 // define command to submit batch
77 string subCommand = string("cd ") + workDir + "; bsub < " + scriptFile;
78 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
80 cerr << command.c_str() << endl;
83 int status = Utils::getCommandOutput(command, output);
85 if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
87 // read id of submitted job in output
88 int p10 = output.find("<");
89 int p20 = output.find(">");
90 string strjob = output.substr(p10+1,p20-p10-1);
92 JobId id(this, strjob);
96 // Ce manager permet de faire de la reprise
98 BatchManager_eLSF::addJob(const Batch::Job & job, const std::string reference)
100 return JobId(this, reference);
103 // Methode pour le controle des jobs : retire un job du gestionnaire
104 void BatchManager_eLSF::deleteJob(const JobId & jobid)
108 istringstream iss(jobid.getReference());
111 // define command to delete batch
112 string subCommand = string("bkill ") + iss.str();
113 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
114 cerr << command.c_str() << endl;
115 status = system(command.c_str());
117 throw EmulationException("Error of connection on remote host");
119 cerr << "jobId = " << ref << "killed" << endl;
122 // Methode pour le controle des jobs : suspend un job en file d'attente
123 void BatchManager_eLSF::holdJob(const JobId & jobid)
125 throw EmulationException("Not yet implemented");
128 // Methode pour le controle des jobs : relache un job suspendu
129 void BatchManager_eLSF::releaseJob(const JobId & jobid)
131 throw EmulationException("Not yet implemented");
135 // Methode pour le controle des jobs : modifie un job en file d'attente
136 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
138 throw EmulationException("Not yet implemented");
141 // Methode pour le controle des jobs : modifie un job en file d'attente
142 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
144 alterJob(jobid, param, Environnement());
147 // Methode pour le controle des jobs : modifie un job en file d'attente
148 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
150 alterJob(jobid, Parametre(), env);
153 // Methode pour le controle des jobs : renvoie l'etat du job
154 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
157 istringstream iss(jobid.getReference());
160 // define command to query batch
161 string subCommand = string("bjobs ") + iss.str();
162 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
163 cerr << command.c_str() << endl;
166 int status = Utils::getCommandOutput(command, output);
167 if (status) throw EmulationException("Error of connection on remote host");
169 JobInfo_eLSF ji = JobInfo_eLSF(id, output);
175 // Methode pour le controle des jobs : teste si un job est present en machine
176 bool BatchManager_eLSF::isRunning(const JobId & jobid)
178 throw EmulationException("Not yet implemented");
181 std::string BatchManager_eLSF::buildSubmissionScript(const Job & job)
183 Parametre params = job.getParametre();
187 string fileToExecute = "";
193 // Mandatory parameters
194 if (params.find(WORKDIR) != params.end())
195 workDir = params[WORKDIR].str();
197 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
198 if (params.find(EXECUTABLE) != params.end())
199 fileToExecute = params[EXECUTABLE].str();
201 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
203 // Optional parameters
204 if (params.find(NBPROC) != params.end())
205 nbproc = params[NBPROC];
206 if (params.find(MAXWALLTIME) != params.end())
207 edt = params[MAXWALLTIME];
208 if (params.find(MAXRAMSIZE) != params.end())
209 mem = params[MAXRAMSIZE];
210 if (params.find(QUEUE) != params.end())
211 queue = params[QUEUE].str();
213 string::size_type p1 = fileToExecute.find_last_of("/");
214 string::size_type p2 = fileToExecute.find_last_of(".");
215 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
216 string fileNameToExecute = fileToExecute.substr(p1+1);
218 // Create batch submit file
219 ofstream tempOutputFile;
220 std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
222 tempOutputFile << "#! /bin/sh -f" << endl ;
223 if (params.find(NAME) != params.end())
224 tempOutputFile << "#BSUB -J " << params[NAME] << endl;
226 tempOutputFile << "#BSUB -q " << queue << endl;
228 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
230 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
231 tempOutputFile << "#BSUB -n " << nbproc << endl ;
233 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE]) {
234 tempOutputFile << "#BSUB -x" << endl ;
237 size_t pos = workDir.find("$HOME");
239 if( pos != string::npos )
240 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
242 pos = workDir.find("~");
243 if( pos != string::npos )
244 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
248 tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
249 tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
251 // Define environment for the job
252 Environnement env = job.getEnvironnement();
253 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
254 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
257 tempOutputFile << "cd " << workDir << endl ;
259 // generate nodes file
260 tempOutputFile << "LIBBATCH_NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
261 tempOutputFile << "bool=0" << endl;
262 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
263 tempOutputFile << " if test $bool = 0; then" << endl;
264 tempOutputFile << " n=$i" << endl;
265 tempOutputFile << " bool=1" << endl;
266 tempOutputFile << " else" << endl;
267 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
268 tempOutputFile << " echo $n >> $LIBBATCH_NODEFILE" << endl;
269 tempOutputFile << " done" << endl;
270 tempOutputFile << " bool=0" << endl;
271 tempOutputFile << " fi" << endl;
272 tempOutputFile << "done" << endl;
273 tempOutputFile << "export LIBBATCH_NODEFILE" << endl;
275 // Launch the executable
276 tempOutputFile << "./" + fileNameToExecute << endl;
278 // Remove the node file
279 tempOutputFile << "rm $LIBBATCH_NODEFILE" << endl;
281 tempOutputFile.flush();
282 tempOutputFile.close();
284 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
286 string remoteFileName = rootNameToExecute + "_Batch.sh";
287 int status = _protocol.copyFile(TmpFileName, "", "",
288 workDir + "/" + remoteFileName,
289 _hostname, _username);
291 throw EmulationException("Error of connection on remote host");
292 return remoteFileName;
295 std::string BatchManager_eLSF::getWallTime(const long edt)
302 oss << h << ":" << m;
304 oss << h << ":0" << m;
308 std::string BatchManager_eLSF::getHomeDir(std::string tmpdir)
311 int idx = tmpdir.find("Batch/");
312 std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
313 filelogtemp = "/tmp/logs" + filelogtemp + "_home";
315 string subCommand = string("echo $HOME");
316 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
317 cerr << command.c_str() << endl;
318 int status = system(command.c_str());
320 throw EmulationException("Error of launching home command on remote host");
322 std::ifstream file_home(filelogtemp.c_str());
323 std::getline(file_home, home);