1 // Copyright (C) 2007-2010 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
50 #include "Batch_BatchManager_eLSF.hxx"
51 #include "Batch_JobInfo_eLSF.hxx"
57 BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
58 const char * username,
59 CommunicationProtocolType protocolType, const char * mpiImpl)
60 : BatchManager(parent, host),
61 BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
68 BatchManager_eLSF::~BatchManager_eLSF()
73 // Methode pour le controle des jobs : soumet un job au gestionnaire
74 const JobId BatchManager_eLSF::submitJob(const Job & job)
77 Parametre params = job.getParametre();
78 const std::string workDir = params[WORKDIR];
79 const string fileToExecute = params[EXECUTABLE];
80 string::size_type p1 = fileToExecute.find_last_of("/");
81 string::size_type p2 = fileToExecute.find_last_of(".");
82 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
84 // export input files on cluster
85 cerr << "Export des fichiers en entree" << endl;
86 exportInputFiles(job);
88 // build batch script for job
89 cerr << "Construction du script de batch" << endl;
90 buildBatchScript(job);
91 cerr << "Script envoye" << endl;
93 // define name of log file (local)
94 string logFile = generateTemporaryFileName("LSF-submitlog");
96 // define command to submit batch
97 string subCommand = string("cd ") + workDir + "; bsub < " + fileNameToExecute + "_Batch.sh";
98 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
102 cerr << command.c_str() << endl;
103 status = system(command.c_str());
106 ifstream error_message(logFile.c_str());
109 while(std::getline(error_message, temp))
111 error_message.close();
112 throw EmulationException("Error of connection on remote host, error was: " + mess);
115 // read id of submitted job in log file
117 FILE *fp = fopen(logFile.c_str(),"r");
118 fgets( line, 128, fp);
122 int p10 = sline.find("<");
123 int p20 = sline.find(">");
124 string strjob = sline.substr(p10+1,p20-p10-1);
126 JobId id(this, strjob);
130 // Ce manager permet de faire de la reprise
132 BatchManager_eLSF::addJob(const Batch::Job & job, const std::string reference)
134 return JobId(this, reference);
137 // Methode pour le controle des jobs : retire un job du gestionnaire
138 void BatchManager_eLSF::deleteJob(const JobId & jobid)
142 istringstream iss(jobid.getReference());
145 // define command to delete batch
146 string subCommand = string("bkill ") + iss.str();
147 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
148 cerr << command.c_str() << endl;
149 status = system(command.c_str());
151 throw EmulationException("Error of connection on remote host");
153 cerr << "jobId = " << ref << "killed" << endl;
156 // Methode pour le controle des jobs : suspend un job en file d'attente
157 void BatchManager_eLSF::holdJob(const JobId & jobid)
159 throw EmulationException("Not yet implemented");
162 // Methode pour le controle des jobs : relache un job suspendu
163 void BatchManager_eLSF::releaseJob(const JobId & jobid)
165 throw EmulationException("Not yet implemented");
169 // Methode pour le controle des jobs : modifie un job en file d'attente
170 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
172 throw EmulationException("Not yet implemented");
175 // Methode pour le controle des jobs : modifie un job en file d'attente
176 void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
178 alterJob(jobid, param, Environnement());
181 // Methode pour le controle des jobs : modifie un job en file d'attente
182 void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
184 alterJob(jobid, Parametre(), env);
187 // Methode pour le controle des jobs : renvoie l'etat du job
188 JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
191 istringstream iss(jobid.getReference());
194 // define name of log file (local)
195 string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
197 // define command to query batch
198 string subCommand = string("bjobs ") + iss.str();
199 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
202 cerr << command.c_str() << endl;
203 int status = system(command.c_str());
205 throw EmulationException("Error of connection on remote host");
207 JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
213 // Methode pour le controle des jobs : teste si un job est present en machine
214 bool BatchManager_eLSF::isRunning(const JobId & jobid)
216 throw EmulationException("Not yet implemented");
219 void BatchManager_eLSF::buildBatchScript(const Job & job)
221 #ifndef WIN32 //TODO: need for porting on Windows
222 Parametre params = job.getParametre();
226 string fileToExecute = "";
232 // Mandatory parameters
233 if (params.find(WORKDIR) != params.end())
234 workDir = params[WORKDIR].str();
236 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
237 if (params.find(EXECUTABLE) != params.end())
238 fileToExecute = params[EXECUTABLE].str();
240 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
242 // Optional parameters
243 if (params.find(NBPROC) != params.end())
244 nbproc = params[NBPROC];
245 if (params.find(MAXWALLTIME) != params.end())
246 edt = params[MAXWALLTIME];
247 if (params.find(MAXRAMSIZE) != params.end())
248 mem = params[MAXRAMSIZE];
249 if (params.find(QUEUE) != params.end())
250 queue = params[QUEUE].str();
252 string::size_type p1 = fileToExecute.find_last_of("/");
253 string::size_type p2 = fileToExecute.find_last_of(".");
254 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
255 string fileNameToExecute = fileToExecute.substr(p1+1);
257 // Create batch submit file
258 ofstream tempOutputFile;
259 std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
261 tempOutputFile << "#! /bin/sh -f" << endl ;
263 tempOutputFile << "#BSUB -q " << queue << endl;
265 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
267 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
268 tempOutputFile << "#BSUB -n " << nbproc << endl ;
269 size_t pos = workDir.find("$HOME");
271 if( pos != string::npos )
272 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
274 pos = workDir.find("~");
275 if( pos != string::npos )
276 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
280 tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
281 tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
283 tempOutputFile << "cd " << workDir << endl ;
285 // generate nodes file
286 tempOutputFile << "NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
287 tempOutputFile << "bool=0" << endl;
288 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
289 tempOutputFile << " if test $bool = 0; then" << endl;
290 tempOutputFile << " n=$i" << endl;
291 tempOutputFile << " bool=1" << endl;
292 tempOutputFile << " else" << endl;
293 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
294 tempOutputFile << " echo $n >> $NODEFILE" << endl;
295 tempOutputFile << " done" << endl;
296 tempOutputFile << " bool=0" << endl;
297 tempOutputFile << " fi" << endl;
298 tempOutputFile << "done" << endl;
300 // Abstraction of PBS_NODEFILE - TODO
301 tempOutputFile << "export LIBBATCH_NODEFILE=$NODEFILE" << endl;
303 // Launch the executable
304 tempOutputFile << "./" + fileNameToExecute << endl;
306 // Remove the node file
307 tempOutputFile << "rm $NODEFILE" << endl;
309 tempOutputFile.flush();
310 tempOutputFile.close();
312 BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
313 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
315 int status = _protocol.copyFile(TmpFileName, "", "",
316 workDir + "/" + rootNameToExecute + "_Batch.sh",
317 _hostname, _username);
319 throw EmulationException("Error of connection on remote host");
325 std::string BatchManager_eLSF::getWallTime(const long edt)
332 oss << h << ":" << m;
334 oss << h << ":0" << m;
338 std::string BatchManager_eLSF::getHomeDir(std::string tmpdir)
341 int idx = tmpdir.find("Batch/");
342 std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
343 filelogtemp = "/tmp/logs" + filelogtemp + "_home";
345 string subCommand = string("echo $HOME");
346 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
347 cerr << command.c_str() << endl;
348 int status = system(command.c_str());
350 throw EmulationException("Error of launching home command on remote host");
352 std::ifstream file_home(filelogtemp.c_str());
353 std::getline(file_home, home);