]> SALOME platform Git repositories - modules/kernel.git/blob - src/Batch/Batch_BatchManager_ePBS.cxx
Salome HOME
merge from branch BR_For40_DSC tag mergeto_V4_1_0_maintainance_29may08
[modules/kernel.git] / src / Batch / Batch_BatchManager_ePBS.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_ePBS.cxx : emulation of PBS 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_ePBS.hxx"
35
36 namespace Batch {
37
38   BatchManager_ePBS::BatchManager_ePBS(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_ePBS::~BatchManager_ePBS()
45   {
46     // Nothing to do
47   }
48
49   // Methode pour le controle des jobs : soumet un job au gestionnaire
50   const JobId BatchManager_ePBS::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 += "; qsub " ;
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 pos = sline.find(".");
108     string strjob;
109     if(pos == string::npos)
110       strjob = sline;
111     else
112       strjob = sline.substr(0,pos);
113
114     JobId id(this, strjob);
115     return id;
116   }
117
118   // Methode pour le controle des jobs : retire un job du gestionnaire
119   void BatchManager_ePBS::deleteJob(const JobId & jobid)
120   {
121     int status;
122     int ref;
123     istringstream iss(jobid.getReference());
124     iss >> ref;
125     
126     // define command to submit batch
127     string command;
128     command = _protocol;
129     command += " ";
130
131     if (_username != ""){
132       command += _username;
133       command += "@";
134     }
135
136     command += _hostname;
137     command += " \"qdel " ;
138     command += iss.str();
139     command += "\"";
140     cerr << command.c_str() << endl;
141     status = system(command.c_str());
142     if(status)
143       throw EmulationException("Error of connection on remote host");
144
145     cerr << "jobId = " << ref << "killed" << endl;
146   }
147    
148   // Methode pour le controle des jobs : suspend un job en file d'attente
149   void BatchManager_ePBS::holdJob(const JobId & jobid)
150   {
151     throw EmulationException("Not yet implemented");
152   }
153
154   // Methode pour le controle des jobs : relache un job suspendu
155   void BatchManager_ePBS::releaseJob(const JobId & jobid)
156   {
157     throw EmulationException("Not yet implemented");
158   }
159
160
161   // Methode pour le controle des jobs : modifie un job en file d'attente
162   void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
163   {
164     throw EmulationException("Not yet implemented");
165   }
166
167   // Methode pour le controle des jobs : modifie un job en file d'attente
168   void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
169   {
170     alterJob(jobid, param, Environnement());
171   }
172
173   // Methode pour le controle des jobs : modifie un job en file d'attente
174   void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
175   {
176     alterJob(jobid, Parametre(), env);
177   }
178
179   // Methode pour le controle des jobs : renvoie l'etat du job
180   JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
181   {
182     int id;
183     istringstream iss(jobid.getReference());
184     iss >> id;
185
186     // define name of log file
187     string logFile="/tmp/logs/";
188     logFile += getenv("USER");
189     logFile += "/batchSalome_";
190
191     ostringstream oss;
192     oss << this << "_" << id;
193     logFile += oss.str();
194     logFile += ".log";
195
196     string command;
197     int status;
198
199     // define command to submit batch
200     command = _protocol;
201     command += " ";
202
203     if (_username != ""){
204       command += _username;
205       command += "@";
206     }
207
208     command += _hostname;
209     command += " \"qstat -f " ;
210     command += iss.str();
211     command += "\" > ";
212     command += logFile;
213     cerr << command.c_str() << endl;
214     status = system(command.c_str());
215     if(status && status != 153 && status != 256*153)
216       throw EmulationException("Error of connection on remote host");
217
218     JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
219     return ji;
220   }
221
222   // Methode pour le controle des jobs : teste si un job est present en machine
223   bool BatchManager_ePBS::isRunning(const JobId & jobid)
224   {
225     throw EmulationException("Not yet implemented");
226   }
227
228   void BatchManager_ePBS::buildBatchScript(const Job & job) throw(EmulationException)
229   {
230     int status;
231     Parametre params = job.getParametre();
232     const long nbproc = params[NBPROC];
233     const long edt = params[MAXWALLTIME];
234     const long mem = params[MAXRAMSIZE];
235     const string workDir = params[WORKDIR];
236     const std::string dirForTmpFiles = params[TMPDIR];
237     const string fileToExecute = params[EXECUTABLE];
238     const string home = params[HOMEDIR];
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 << "#PBS -l walltime=" << edt*60 << endl ;
254     if( mem > 0 )
255       tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
256     tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/runSalome.output.log." << filelogtemp << endl ;
257     tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/runSalome.error.log." << filelogtemp << endl ;
258     if( workDir.size() > 0 )
259       tempOutputFile << "cd " << workDir << endl ;
260     tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
261     tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
262     tempOutputFile << _mpiImpl->halt();
263     tempOutputFile.flush();
264     tempOutputFile.close();
265     chmod(TmpFileName.c_str(), 0x1ED);
266     cerr << TmpFileName.c_str() << endl;
267
268     string command;
269     if( _protocol == "rsh" )
270       command = "rcp ";
271     else if( _protocol == "ssh" )
272       command = "scp ";
273     else
274       throw EmulationException("Unknown protocol");
275     command += TmpFileName;
276     command += " ";
277     if(_username != ""){
278       command +=  _username;
279       command += "@";
280     }
281     command += _hostname;
282     command += ":";
283     command += dirForTmpFiles ;
284     command += "/" ;
285     command += rootNameToExecute ;
286     command += "_Batch.sh" ;
287     cerr << command.c_str() << endl;
288     status = system(command.c_str());
289     if(status)
290       throw EmulationException("Error of connection on remote host");    
291
292     RmTmpFile(TmpFileName);
293     
294   }
295
296 }