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