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