]> SALOME platform Git repositories - modules/kernel.git/blob - src/Batch/Batch_BatchManager_eLSF.cxx
Salome HOME
Porting functionality 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 namespace Batch {
41
42   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)
43   {
44     // Nothing to do
45   }
46
47   // Destructeur
48   BatchManager_eLSF::~BatchManager_eLSF()
49   {
50     // Nothing to do
51   }
52
53   // Methode pour le controle des jobs : soumet un job au gestionnaire
54   const JobId BatchManager_eLSF::submitJob(const Job & job)
55   {
56     int status;
57     Parametre params = job.getParametre();
58     const std::string dirForTmpFiles = params[TMPDIR];
59     const string fileToExecute = params[EXECUTABLE];
60     string::size_type p1 = fileToExecute.find_last_of("/");
61     string::size_type p2 = fileToExecute.find_last_of(".");
62     std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
63
64     // export input files on cluster
65     exportInputFiles(job);
66
67     // build batch script for job
68     buildBatchScript(job);
69
70     // define name of log file
71     string logFile="/tmp/logs/";
72     logFile += getenv("USER");
73     logFile += "/batchSalome_";
74     srand ( time(NULL) );
75     int ir = rand();
76     ostringstream oss;
77     oss << ir;
78     logFile += oss.str();
79     logFile += ".log";
80
81     string command;
82
83     // define command to submit batch
84     command = _protocol;
85     command += " ";
86
87     if(_username != ""){
88       command += _username;
89       command += "@";
90     }
91
92     command += _hostname;
93     command += " \"cd " ;
94     command += dirForTmpFiles ;
95     command += "; bsub < " ;
96     command += fileNameToExecute ;
97     command += "_Batch.sh\" > ";
98     command += logFile;
99     cerr << command.c_str() << endl;
100     status = system(command.c_str());
101     if(status)
102       throw EmulationException("Error of connection on remote host");
103
104     // read id of submitted job in log file
105     char line[128];
106     FILE *fp = fopen(logFile.c_str(),"r");
107     fgets( line, 128, fp);
108     fclose(fp);
109     
110     string sline(line);
111     int p10 = sline.find("<");
112     int p20 = sline.find(">");
113     string strjob = sline.substr(p10+1,p20-p10-1);
114
115     JobId id(this, strjob);
116     return id;
117   }
118
119   // Methode pour le controle des jobs : retire un job du gestionnaire
120   void BatchManager_eLSF::deleteJob(const JobId & jobid)
121   {
122     int status;
123     int ref;
124     istringstream iss(jobid.getReference());
125     iss >> ref;
126     
127     // define command to submit batch
128     string command;
129     command = _protocol;
130     command += " ";
131
132     if (_username != ""){
133       command += _username;
134       command += "@";
135     }
136
137     command += _hostname;
138     command += " \"bkill " ;
139     command += iss.str();
140     command += "\"";
141     cerr << command.c_str() << endl;
142     status = system(command.c_str());
143     if(status)
144       throw EmulationException("Error of connection on remote host");
145
146     cerr << "jobId = " << ref << "killed" << endl;
147   }
148    
149   // Methode pour le controle des jobs : suspend un job en file d'attente
150   void BatchManager_eLSF::holdJob(const JobId & jobid)
151   {
152     throw EmulationException("Not yet implemented");
153   }
154
155   // Methode pour le controle des jobs : relache un job suspendu
156   void BatchManager_eLSF::releaseJob(const JobId & jobid)
157   {
158     throw EmulationException("Not yet implemented");
159   }
160
161
162   // Methode pour le controle des jobs : modifie un job en file d'attente
163   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
164   {
165     throw EmulationException("Not yet implemented");
166   }
167
168   // Methode pour le controle des jobs : modifie un job en file d'attente
169   void BatchManager_eLSF::alterJob(const JobId & jobid, const Parametre & param)
170   {
171     alterJob(jobid, param, Environnement());
172   }
173
174   // Methode pour le controle des jobs : modifie un job en file d'attente
175   void BatchManager_eLSF::alterJob(const JobId & jobid, const Environnement & env)
176   {
177     alterJob(jobid, Parametre(), env);
178   }
179
180   // Methode pour le controle des jobs : renvoie l'etat du job
181   JobInfo BatchManager_eLSF::queryJob(const JobId & jobid)
182   {
183     int id;
184     istringstream iss(jobid.getReference());
185     iss >> id;
186
187     // define name of log file
188     string logFile="/tmp/logs/";
189     logFile += getenv("USER");
190     logFile += "/batchSalome_";
191
192     srand ( time(NULL) );
193     int ir = rand();
194     ostringstream oss;
195     oss << ir;
196     logFile += oss.str();
197     logFile += ".log";
198
199     string command;
200     int status;
201
202     // define command to submit batch
203     command = _protocol;
204     command += " ";
205
206     if (_username != ""){
207       command += _username;
208       command += "@";
209     }
210
211     command += _hostname;
212     command += " \"bjobs " ;
213     command += iss.str();
214     command += "\" > ";
215     command += logFile;
216     cerr << command.c_str() << endl;
217     status = system(command.c_str());
218     if(status)
219       throw EmulationException("Error of connection on remote host");
220
221     JobInfo_eLSF ji = JobInfo_eLSF(id,logFile);
222     return ji;
223   }
224
225
226
227   // Methode pour le controle des jobs : teste si un job est present en machine
228   bool BatchManager_eLSF::isRunning(const JobId & jobid)
229   {
230     throw EmulationException("Not yet implemented");
231   }
232
233   void BatchManager_eLSF::buildBatchScript(const Job & job) throw(EmulationException)
234   {
235 #ifndef WIN32 //TODO: need for porting on Windows
236     int status;
237     Parametre params = job.getParametre();
238     const int nbproc = params[NBPROC];
239     const long edt = params[MAXWALLTIME];
240     const long mem = params[MAXRAMSIZE];
241     const string workDir = params[WORKDIR];
242     const std::string dirForTmpFiles = params[TMPDIR];
243     const string fileToExecute = params[EXECUTABLE];
244     string::size_type p1 = fileToExecute.find_last_of("/");
245     string::size_type p2 = fileToExecute.find_last_of(".");
246     std::string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
247     std::string fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename(fileToExecute.c_str()));
248
249     int idx = dirForTmpFiles.find("Batch/");
250     std::string filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
251
252     std::string TmpFileName = BuildTemporaryFileName();
253     ofstream tempOutputFile;
254     tempOutputFile.open(TmpFileName.c_str(), ofstream::out );
255
256     tempOutputFile << "#! /bin/sh -f" << endl ;
257     if( edt > 0 )
258       tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
259     if( mem > 0 )
260       tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
261     tempOutputFile << "#BSUB -n " << nbproc << endl ;
262     tempOutputFile << "#BSUB -o runSalome.output.log." << filelogtemp << endl ;
263     tempOutputFile << "#BSUB -e runSalome.error.log." << filelogtemp << endl ;
264     if( workDir.size() > 0 )
265       tempOutputFile << "cd " << workDir << endl ;
266     tempOutputFile << _mpiImpl->boot("",nbproc);
267     tempOutputFile << _mpiImpl->run("",nbproc,fileNameToExecute);
268     tempOutputFile << _mpiImpl->halt();
269     tempOutputFile.flush();
270     tempOutputFile.close();
271 #ifdef WIN32
272     _chmod(
273 #else
274     chmod(
275 #endif
276       TmpFileName.c_str(), 0x1ED);
277     cerr << TmpFileName.c_str() << endl;
278
279     string command;
280     if( _protocol == "rsh" )
281       command = "rcp ";
282     else if( _protocol == "ssh" )
283       command = "scp ";
284     else
285       throw EmulationException("Unknown protocol");
286     command += TmpFileName;
287     command += " ";
288     if(_username != ""){
289       command +=  _username;
290       command += "@";
291     }
292     command += _hostname;
293     command += ":";
294     command += dirForTmpFiles ;
295     command += "/" ;
296     command += rootNameToExecute ;
297     command += "_Batch.sh" ;
298     cerr << command.c_str() << endl;
299     status = system(command.c_str());
300     if(status)
301       throw EmulationException("Error of connection on remote host");    
302
303     RmTmpFile(TmpFileName);
304 #endif
305     
306   }
307
308   std::string BatchManager_eLSF::getWallTime(const long edt)
309   {
310     long h, m;
311     h = edt / 60;
312     m = edt - h*60;
313     ostringstream oss;
314     if( m >= 10 )
315       oss << h << ":" << m;
316     else
317       oss << h << ":0" << m;
318     return oss.str();
319   }
320
321 }