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