]> SALOME platform Git repositories - tools/libbatch.git/blob - src/LSF/Batch_BatchManager_eLSF.cxx
Salome HOME
2b7ea3c595167e558a549ae8a9afbaf84cf5a06f
[tools/libbatch.git] / src / LSF / Batch_BatchManager_eLSF.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * BatchManager_eLSF.cxx : emulation of LSF client
24  *
25  * Auteur : Bernard SECHER - CEA DEN
26  * Mail   : mailto:bernard.secher@cea.fr
27  * Date   : Thu Apr 24 10:17:22 2008
28  * Projet : PAL Salome
29  *
30  */
31
32 #include <iostream>
33 #include <fstream>
34 #include <sstream>
35 #include <string>
36 #include <sys/stat.h>
37
38 #ifdef WIN32
39 #include <io.h>
40 #else
41 #include <libgen.h>
42 #endif
43
44 #include "Batch_BatchManager_eLSF.hxx"
45 #include "Batch_JobInfo_eLSF.hxx"
46
47 using namespace std;
48
49 namespace Batch {
50
51   BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host,
52                                        CommunicationProtocolType protocolType, const char * mpiImpl)
53   : BatchManager_eClient(parent, host, protocolType, mpiImpl)
54   {
55     // Nothing to do
56   }
57
58   // Destructeur
59   BatchManager_eLSF::~BatchManager_eLSF()
60   {
61     // Nothing to do
62   }
63
64   // Methode pour le controle des jobs : soumet un job au gestionnaire
65   const JobId BatchManager_eLSF::submitJob(const Job & job)
66   {
67     int status;
68     Parametre params = job.getParametre();
69     const std::string dirForTmpFiles = params[TMPDIR];
70     const string fileToExecute = params[EXECUTABLE];
71     std::string fileNameToExecute;
72     if( fileToExecute.size() > 0 ){
73       string::size_type p1 = fileToExecute.find_last_of("/");
74       string::size_type p2 = fileToExecute.find_last_of(".");
75       fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
76     }
77     else
78       fileNameToExecute = "command";
79
80     // export input files on cluster
81     exportInputFiles(job);
82
83     // build batch script for job
84     buildBatchScript(job);
85
86     // define name of log file (local)
87     string logFile = generateTemporaryFileName("LSF-submitlog");
88
89     // define command to submit batch
90     string subCommand = string("cd ") + dirForTmpFiles + "; bsub < " +
91                         fileNameToExecute + "_Batch.sh";
92     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
93     command += " > ";
94     command += logFile;
95     cerr << command.c_str() << endl;
96     status = system(command.c_str());
97     if(status)
98       throw EmulationException("Error of connection on remote host");
99
100     // read id of submitted job in log file
101     char line[128];
102     FILE *fp = fopen(logFile.c_str(),"r");
103     fgets( line, 128, fp);
104     fclose(fp);
105
106     string sline(line);
107     int p10 = sline.find("<");
108     int p20 = sline.find(">");
109     string strjob = sline.substr(p10+1,p20-p10-1);
110
111     JobId id(this, strjob);
112     return id;
113   }
114
115   // Methode pour le controle des jobs : retire un job du gestionnaire
116   void BatchManager_eLSF::deleteJob(const JobId & jobid)
117   {
118     int status;
119     int ref;
120     istringstream iss(jobid.getReference());
121     iss >> ref;
122
123     // define command to delete batch
124     string subCommand = string("bkill ") + iss.str();
125     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
126     cerr << command.c_str() << endl;
127     status = system(command.c_str());
128     if (status)
129       throw EmulationException("Error of connection on remote host");
130
131     cerr << "jobId = " << ref << "killed" << endl;
132   }
133
134   // Methode pour le controle des jobs : suspend un job en file d'attente
135   void BatchManager_eLSF::holdJob(const JobId & jobid)
136   {
137     throw EmulationException("Not yet implemented");
138   }
139
140   // Methode pour le controle des jobs : relache un job suspendu
141   void BatchManager_eLSF::releaseJob(const JobId & jobid)
142   {
143     throw EmulationException("Not yet implemented");
144   }
145
146
147   // Methode pour le controle des jobs : modifie un job en file d'attente
148   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
149   {
150     throw EmulationException("Not yet implemented");
151   }
152
153   // Methode pour le controle des jobs : modifie un job en file d'attente
154   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
155   {
156     alterJob(jobid, param, Environnement());
157   }
158
159   // Methode pour le controle des jobs : modifie un job en file d'attente
160   void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
161   {
162     alterJob(jobid, Parametre(), env);
163   }
164
165   // Methode pour le controle des jobs : renvoie l'etat du job
166   JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
167   {
168     int id;
169     istringstream iss(jobid.getReference());
170     iss >> id;
171
172     // define name of log file (local)
173     string logFile = generateTemporaryFileName(string("LSF-querylog-id") + jobid.getReference());
174
175     // define command to query batch
176     string subCommand = string("bjobs ") + iss.str();
177     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
178     command += " > ";
179     command += logFile;
180     cerr << command.c_str() << endl;
181     int status = system(command.c_str());
182     if (status)
183       throw EmulationException("Error of connection on remote host");
184
185     JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
186     return ji;
187   }
188
189
190
191   // Methode pour le controle des jobs : teste si un job est present en machine
192   bool BatchManager_eLSF::isRunning(const JobId & jobid)
193   {
194     throw EmulationException("Not yet implemented");
195   }
196
197   void BatchManager_eLSF::buildBatchScript(const Job & job)
198   {
199 #ifndef WIN32 //TODO: need for porting on Windows
200     Parametre params = job.getParametre();
201     Environnement env = job.getEnvironnement();
202     const int nbproc = params[NBPROC];
203     const long edt = params[MAXWALLTIME];
204     const long mem = params[MAXRAMSIZE];
205     const string workDir = params[WORKDIR];
206     const std::string dirForTmpFiles = params[TMPDIR];
207     const string fileToExecute = params[EXECUTABLE];
208     const string home = params[HOMEDIR];
209     const std::string queue = params[QUEUE];
210     std::string rootNameToExecute;
211     std::string fileNameToExecute;
212     std::string filelogtemp;
213     if( fileToExecute.size() > 0 ){
214       string::size_type p1 = fileToExecute.find_last_of("/");
215       string::size_type p2 = fileToExecute.find_last_of(".");
216       rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
217       char* basec=strdup(fileToExecute.c_str());
218       fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(basec));
219       free(basec);
220
221       int idx = dirForTmpFiles.find("Batch/");
222       filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
223     }
224     else{
225       rootNameToExecute = "command";
226     }
227
228     ofstream tempOutputFile;
229     std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
230
231     tempOutputFile << "#! /bin/sh -f" << endl ;
232     if (queue != "")
233       tempOutputFile << "#BSUB -q " << queue << endl;
234     if( edt > 0 )
235       tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
236     if( mem > 0 )
237       tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
238     tempOutputFile << "#BSUB -n " << nbproc << endl ;
239     if( fileToExecute.size() > 0 ){
240       tempOutputFile << "#BSUB -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
241       tempOutputFile << "#BSUB -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
242     }
243     else{
244       tempOutputFile << "#BSUB -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
245       tempOutputFile << "#BSUB -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
246     }
247     if( workDir.size() > 0 )
248       tempOutputFile << "cd " << workDir << endl ;
249     if( fileToExecute.size() > 0 ){
250       tempOutputFile << _mpiImpl->boot("",nbproc);
251       tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
252       tempOutputFile << _mpiImpl->halt();
253     }
254     else{
255       tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
256       tempOutputFile << env["COMMAND"];
257     }
258
259     tempOutputFile.flush();
260     tempOutputFile.close();
261 #ifdef WIN32
262     _chmod(
263 #else
264     chmod(
265 #endif
266       TmpFileName.c_str(), 0x1ED);
267     cerr << TmpFileName.c_str() << endl;
268
269     int status = _protocol.copyFile(TmpFileName, "", "",
270                                     dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
271                                     _hostname, _username);
272     if (status)
273       throw EmulationException("Error of connection on remote host");
274
275     remove(TmpFileName.c_str());
276 #endif
277
278   }
279
280   std::string BatchManager_eLSF::getWallTime(const long edt)
281   {
282     long h, m;
283     h = edt / 60;
284     m = edt - h*60;
285     ostringstream oss;
286     if( m >= 10 )
287       oss << h << ":" << m;
288     else
289       oss << h << ":0" << m;
290     return oss.str();
291   }
292
293 }