]> SALOME platform Git repositories - modules/kernel.git/blob - src/Batch/Batch_BatchManager_eLSF.cxx
Salome HOME
Merging from V4_1_0_maintainance for porting on Win32 Platform
[modules/kernel.git] / src / Batch / Batch_BatchManager_eLSF.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 /*
21  * BatchManager_eLSF.cxx : emulation of LSF client
22  *
23  * Auteur : Bernard SECHER - CEA DEN
24  * Mail   : mailto:bernard.secher@cea.fr
25  * Date   : Thu Apr 24 10:17:22 2008
26  * Projet : PAL Salome 
27  *
28  */
29
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <sys/stat.h>
34 #include "Batch_BatchManager_eLSF.hxx"
35 #ifdef WIN32
36 # include <time.h>
37 # include <io.h>
38 #endif
39
40 using namespace std;
41
42 namespace Batch {
43
44   BatchManager_eLSF::BatchManager_eLSF(const FactBatchManager * parent, const char * host, const char * protocol, const char * mpiImpl) throw(InvalidArgumentException,ConnexionFailureException) : BatchManager_eClient(parent,host,protocol,mpiImpl)
45   {
46     // Nothing to do
47   }
48
49   // Destructeur
50   BatchManager_eLSF::~BatchManager_eLSF()
51   {
52     // Nothing to do
53   }
54
55   // Methode pour le controle des jobs : soumet un job au gestionnaire
56   const JobId BatchManager_eLSF::submitJob(const Job & job)
57   {
58     int status;
59     Parametre params = job.getParametre();
60     const std::string dirForTmpFiles = params[TMPDIR];
61     const string fileToExecute = params[EXECUTABLE];
62     string::size_type p1 = fileToExecute.find_last_of("/");
63     string::size_type p2 = fileToExecute.find_last_of(".");
64     std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
65
66     // export input files on cluster
67     exportInputFiles(job);
68
69     // build batch script for job
70     buildBatchScript(job);
71
72     // define name of log file
73     string logFile="/tmp/logs/";
74     logFile += getenv("USER");
75     logFile += "/batchSalome_";
76     srand ( time(NULL) );
77     int ir = rand();
78     ostringstream oss;
79     oss << ir;
80     logFile += oss.str();
81     logFile += ".log";
82
83     string command;
84
85     // define command to submit batch
86     command = _protocol;
87     command += " ";
88
89     if(_username != ""){
90       command += _username;
91       command += "@";
92     }
93
94     command += _hostname;
95     command += " \"cd " ;
96     command += dirForTmpFiles ;
97     command += "; bsub < " ;
98     command += fileNameToExecute ;
99     command += "_Batch.sh\" > ";
100     command += logFile;
101     cerr << command.c_str() << endl;
102     status = system(command.c_str());
103     if(status)
104       throw EmulationException("Error of connection on remote host");
105
106     // read id of submitted job in log file
107     char line[128];
108     FILE *fp = fopen(logFile.c_str(),"r");
109     fgets( line, 128, fp);
110     fclose(fp);
111     
112     string sline(line);
113     int p10 = sline.find("<");
114     int p20 = sline.find(">");
115     string strjob = sline.substr(p10+1,p20-p10-1);
116
117     JobId id(this, strjob);
118     return id;
119   }
120
121   // Methode pour le controle des jobs : retire un job du gestionnaire
122   void BatchManager_eLSF::deleteJob(const JobId & jobid)
123   {
124     int status;
125     int ref;
126     istringstream iss(jobid.getReference());
127     iss >> ref;
128     
129     // define command to submit batch
130     string command;
131     command = _protocol;
132     command += " ";
133
134     if (_username != ""){
135       command += _username;
136       command += "@";
137     }
138
139     command += _hostname;
140     command += " \"bkill " ;
141     command += iss.str();
142     command += "\"";
143     cerr << command.c_str() << endl;
144     status = system(command.c_str());
145     if(status)
146       throw EmulationException("Error of connection on remote host");
147
148     cerr << "jobId = " << ref << "killed" << endl;
149   }
150    
151   // Methode pour le controle des jobs : suspend un job en file d'attente
152   void BatchManager_eLSF::holdJob(const JobId & jobid)
153   {
154     throw EmulationException("Not yet implemented");
155   }
156
157   // Methode pour le controle des jobs : relache un job suspendu
158   void BatchManager_eLSF::releaseJob(const JobId & jobid)
159   {
160     throw EmulationException("Not yet implemented");
161   }
162
163
164   // Methode pour le controle des jobs : modifie un job en file d'attente
165   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
166   {
167     throw EmulationException("Not yet implemented");
168   }
169
170   // Methode pour le controle des jobs : modifie un job en file d'attente
171   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
172   {
173     alterJob(jobid, param, Environnement());
174   }
175
176   // Methode pour le controle des jobs : modifie un job en file d'attente
177   void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
178   {
179     alterJob(jobid, Parametre(), env);
180   }
181
182   // Methode pour le controle des jobs : renvoie l'etat du job
183   JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
184   {
185     int id;
186     istringstream iss(jobid.getReference());
187     iss >> id;
188
189     // define name of log file
190     string logFile="/tmp/logs/";
191     logFile += getenv("USER");
192     logFile += "/batchSalome_";
193
194     srand ( time(NULL) );
195     int ir = rand();
196     ostringstream oss;
197     oss << ir;
198     logFile += oss.str();
199     logFile += ".log";
200
201     string command;
202     int status;
203
204     // define command to submit batch
205     command = _protocol;
206     command += " ";
207
208     if (_username != ""){
209       command += _username;
210       command += "@";
211     }
212
213     command += _hostname;
214     command += " \"bjobs " ;
215     command += iss.str();
216     command += "\" > ";
217     command += logFile;
218     cerr << command.c_str() << endl;
219     status = system(command.c_str());
220     if(status)
221       throw EmulationException("Error of connection on remote host");
222
223     JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
224     return ji;
225   }
226
227
228
229   // Methode pour le controle des jobs : teste si un job est present en machine
230   bool BatchManager_eLSF::isRunning(const JobId & jobid)
231   {
232     throw EmulationException("Not yet implemented");
233   }
234
235   void BatchManager_eLSF::buildBatchScript(const Job & job) throw(EmulationException)
236   {
237 #ifndef WIN32 //TODO: need for porting on Windows
238     int status;
239     Parametre params = job.getParametre();
240     const int nbproc = params[NBPROC];
241     const long edt = params[MAXWALLTIME];
242     const long mem = params[MAXRAMSIZE];
243     const string workDir = params[WORKDIR];
244     const std::string dirForTmpFiles = params[TMPDIR];
245     const string fileToExecute = params[EXECUTABLE];
246     string::size_type p1 = fileToExecute.find_last_of("/");
247     string::size_type p2 = fileToExecute.find_last_of(".");
248     std::string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
249     std::string fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(fileToExecute.c_str()));
250
251     int idx = dirForTmpFiles.find("Batch/");
252     std::string filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
253
254     std::string TmpFileName = BuildTemporaryFileName();
255     ofstream tempOutputFile;
256     tempOutputFile.open(TmpFileName.c_str(), ofstream::out );
257
258     tempOutputFile << "#! /bin/sh -f" << endl ;
259     if( edt > 0 )
260       tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
261     if( mem > 0 )
262       tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
263     tempOutputFile << "#BSUB -n " << nbproc << endl ;
264     tempOutputFile << "#BSUB -o runSalome.output.log." << filelogtemp << endl ;
265     tempOutputFile << "#BSUB -e runSalome.error.log." << filelogtemp << endl ;
266     if( workDir.size() > 0 )
267       tempOutputFile << "cd " << workDir << endl ;
268     tempOutputFile << _mpiImpl->boot("",nbproc);
269     tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
270     tempOutputFile << _mpiImpl->halt();
271     tempOutputFile.flush();
272     tempOutputFile.close();
273 #ifdef WIN32
274     _chmod(
275 #else
276     chmod(
277 #endif
278       TmpFileName.c_str(), 0x1ED);
279     cerr << TmpFileName.c_str() << endl;
280
281     string command;
282     if( _protocol == "rsh" )
283       command = "rcp ";
284     else if( _protocol == "ssh" )
285       command = "scp ";
286     else
287       throw EmulationException("Unknown protocol");
288     command += TmpFileName;
289     command += " ";
290     if(_username != ""){
291       command +=  _username;
292       command += "@";
293     }
294     command += _hostname;
295     command += ":";
296     command += dirForTmpFiles ;
297     command += "/" ;
298     command += rootNameToExecute ;
299     command += "_Batch.sh" ;
300     cerr << command.c_str() << endl;
301     status = system(command.c_str());
302     if(status)
303       throw EmulationException("Error of connection on remote host");    
304
305     RmTmpFile(TmpFileName);
306 #endif
307     
308   }
309
310   std::string BatchManager_eLSF::getWallTime(const long edt)
311   {
312     long h, m;
313     h = edt / 60;
314     m = edt - h*60;
315     ostringstream oss;
316     if( m >= 10 )
317       oss << h << ":" << m;
318     else
319       oss << h << ":0" << m;
320     return oss.str();
321   }
322
323 }