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