1 // Copyright (C) 2007-2012 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_LSF.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 <Constants.hxx>
38 #include <NotYetImplementedException.hxx>
39 #include "BatchManager_LSF.hxx"
40 #include "JobInfo_LSF.hxx"
46 BatchManager_LSF::BatchManager_LSF(const FactBatchManager * parent, const char * host,
47 const char * username,
48 CommunicationProtocolType protocolType, const char * mpiImpl)
49 : BatchManager(parent, host, username, protocolType, mpiImpl)
55 BatchManager_LSF::~BatchManager_LSF()
60 // Methode pour le controle des jobs : soumet un job au gestionnaire
61 const JobId BatchManager_LSF::submitJob(const Job & job)
63 Parametre params = job.getParametre();
64 const std::string workDir = params[WORKDIR];
66 // export input files on cluster
67 cerr << "Export des fichiers en entree" << endl;
68 exportInputFiles(job);
70 // build batch script for job
71 cerr << "Construction du script de batch" << endl;
72 string scriptFile = buildSubmissionScript(job);
73 cerr << "Script envoye" << endl;
75 // define command to submit batch
76 string subCommand = string("cd ") + workDir + "; bsub < " + scriptFile;
77 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
79 cerr << command.c_str() << endl;
82 int status = Utils::getCommandOutput(command, output);
84 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
86 // read id of submitted job in output
87 int p10 = output.find("<");
88 int p20 = output.find(">");
89 string strjob = output.substr(p10+1,p20-p10-1);
91 JobId id(this, strjob);
95 // Ce manager permet de faire de la reprise
97 BatchManager_LSF::addJob(const Batch::Job & job, const std::string reference)
99 return JobId(this, reference);
102 // Methode pour le controle des jobs : retire un job du gestionnaire
103 void BatchManager_LSF::deleteJob(const JobId & jobid)
107 istringstream iss(jobid.getReference());
110 // define command to delete batch
111 string subCommand = string("bkill ") + iss.str();
112 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
113 cerr << command.c_str() << endl;
114 status = system(command.c_str());
116 throw RunTimeException("Error of connection on remote host");
118 cerr << "jobId = " << ref << "killed" << endl;
121 // Methode pour le controle des jobs : suspend un job en file d'attente
122 void BatchManager_LSF::holdJob(const JobId & jobid)
124 throw NotYetImplementedException("BatchManager_LSF::holdJob");
127 // Methode pour le controle des jobs : relache un job suspendu
128 void BatchManager_LSF::releaseJob(const JobId & jobid)
130 throw NotYetImplementedException("BatchManager_LSF::releaseJob");
134 // Methode pour le controle des jobs : modifie un job en file d'attente
135 void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
137 throw NotYetImplementedException("BatchManager_LSF::alterJob");
140 // Methode pour le controle des jobs : modifie un job en file d'attente
141 void BatchManager_LSF::alterJob(const JobId & jobid, const Parametre & param)
143 alterJob(jobid, param, Environnement());
146 // Methode pour le controle des jobs : modifie un job en file d'attente
147 void BatchManager_LSF::alterJob(const JobId & jobid, const Environnement & env)
149 alterJob(jobid, Parametre(), env);
152 // Methode pour le controle des jobs : renvoie l'etat du job
153 JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
156 istringstream iss(jobid.getReference());
159 // define command to query batch
160 string subCommand = string("bjobs ") + iss.str();
161 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
162 cerr << command.c_str() << endl;
165 int status = Utils::getCommandOutput(command, output);
166 if (status) throw RunTimeException("Error of connection on remote host");
168 JobInfo_LSF ji = JobInfo_LSF(id, output);
174 // Methode pour le controle des jobs : teste si un job est present en machine
175 bool BatchManager_LSF::isRunning(const JobId & jobid)
177 throw NotYetImplementedException("BatchManager_LSF::isRunning");
180 std::string BatchManager_LSF::buildSubmissionScript(const Job & job)
182 Parametre params = job.getParametre();
186 string fileToExecute = "";
192 // Mandatory parameters
193 if (params.find(WORKDIR) != params.end())
194 workDir = params[WORKDIR].str();
196 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
197 if (params.find(EXECUTABLE) != params.end())
198 fileToExecute = params[EXECUTABLE].str();
200 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
202 // Optional parameters
203 if (params.find(NBPROC) != params.end())
204 nbproc = params[NBPROC];
205 if (params.find(MAXWALLTIME) != params.end())
206 edt = params[MAXWALLTIME];
207 if (params.find(MAXRAMSIZE) != params.end())
208 mem = params[MAXRAMSIZE];
209 if (params.find(QUEUE) != params.end())
210 queue = params[QUEUE].str();
212 string::size_type p1 = fileToExecute.find_last_of("/");
213 string::size_type p2 = fileToExecute.find_last_of(".");
214 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
215 string fileNameToExecute = fileToExecute.substr(p1+1);
217 // Create batch submit file
218 ofstream tempOutputFile;
219 std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
221 tempOutputFile << "#! /bin/sh -f" << endl ;
222 if (params.find(NAME) != params.end())
223 tempOutputFile << "#BSUB -J " << params[NAME] << endl;
225 tempOutputFile << "#BSUB -q " << queue << endl;
227 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
229 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
230 tempOutputFile << "#BSUB -n " << nbproc << endl ;
232 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE]) {
233 tempOutputFile << "#BSUB -x" << endl ;
236 size_t pos = workDir.find("$HOME");
238 if( pos != string::npos )
239 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
241 pos = workDir.find("~");
242 if( pos != string::npos )
243 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
247 tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
248 tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
250 // Define environment for the job
251 Environnement env = job.getEnvironnement();
252 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
253 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
256 tempOutputFile << "cd " << workDir << endl ;
258 // generate nodes file
259 tempOutputFile << "LIBBATCH_NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
260 tempOutputFile << "bool=0" << endl;
261 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
262 tempOutputFile << " if test $bool = 0; then" << endl;
263 tempOutputFile << " n=$i" << endl;
264 tempOutputFile << " bool=1" << endl;
265 tempOutputFile << " else" << endl;
266 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
267 tempOutputFile << " echo $n >> $LIBBATCH_NODEFILE" << endl;
268 tempOutputFile << " done" << endl;
269 tempOutputFile << " bool=0" << endl;
270 tempOutputFile << " fi" << endl;
271 tempOutputFile << "done" << endl;
272 tempOutputFile << "export LIBBATCH_NODEFILE" << endl;
274 // Launch the executable
275 tempOutputFile << "./" + fileNameToExecute << endl;
277 // Remove the node file
278 tempOutputFile << "rm $LIBBATCH_NODEFILE" << endl;
280 tempOutputFile.flush();
281 tempOutputFile.close();
283 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
285 string remoteFileName = rootNameToExecute + "_Batch.sh";
286 int status = _protocol.copyFile(TmpFileName, "", "",
287 workDir + "/" + remoteFileName,
288 _hostname, _username);
290 throw RunTimeException("Error of connection on remote host");
291 return remoteFileName;
294 std::string BatchManager_LSF::getWallTime(const long edt)
301 oss << h << ":" << m;
303 oss << h << ":0" << m;
307 std::string BatchManager_LSF::getHomeDir(std::string tmpdir)
310 int idx = tmpdir.find("Batch/");
311 std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
312 filelogtemp = "/tmp/logs" + filelogtemp + "_home";
314 string subCommand = string("echo $HOME");
315 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
316 cerr << command.c_str() << endl;
317 int status = system(command.c_str());
319 throw RunTimeException("Error of launching home command on remote host");
321 std::ifstream file_home(filelogtemp.c_str());
322 std::getline(file_home, home);