Salome HOME
Refactored submission based on SH, RSH and SSH by grouping related commands in Commun...
[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
37 #include <Batch_config.h>
38
39 #ifdef MSVC
40 #include <io.h>
41 #else
42 #include <libgen.h>
43 #endif
44
45 #include "Batch_BatchManager_ePBS.hxx"
46 #include "Batch_JobInfo_ePBS.hxx"
47
48 using namespace std;
49
50 namespace Batch {
51
52   BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host,
53                                        CommunicationProtocolType protocolType, const char * mpiImpl)
54     : BatchManager_eClient(parent, host, protocolType, mpiImpl)
55   {
56     // Nothing to do
57   }
58
59   // Destructeur
60   BatchManager_ePBS::~BatchManager_ePBS()
61   {
62     // Nothing to do
63   }
64
65   // Methode pour le controle des jobs : soumet un job au gestionnaire
66   const JobId BatchManager_ePBS::submitJob(const Job & job)
67   {
68     int status;
69     Parametre params = job.getParametre();
70     const std::string dirForTmpFiles = params[TMPDIR];
71     const string fileToExecute = params[EXECUTABLE];
72     string::size_type p1 = fileToExecute.find_last_of("/");
73     string::size_type p2 = fileToExecute.find_last_of(".");
74     std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
75
76     // export input files on cluster
77     exportInputFiles(job);
78
79     // build batch script for job
80     buildBatchScript(job);
81
82     // define name of log file (local)
83     string logFile = generateTemporaryFileName("PBS-submitlog");
84
85     // define command to submit batch
86     string subCommand = string("cd ") + dirForTmpFiles + "; qsub " +
87                         fileNameToExecute + "_Batch.sh";
88     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
89     command += " > ";
90     command += logFile;
91     cerr << command.c_str() << endl;
92     status = system(command.c_str());
93     if(status)
94       throw EmulationException("Error of connection on remote host");
95
96     // read id of submitted job in log file
97     ifstream idfile(logFile.c_str());
98     string sline;
99     idfile >> sline;
100     idfile.close();
101     if (sline.size() == 0)
102       throw EmulationException("Error in the submission of the job on the remote host");
103
104     size_t pos = sline.find(".");
105     string strjob;
106     if(pos == string::npos)
107       strjob = sline;
108     else
109       strjob = sline.substr(0,pos);
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_ePBS::deleteJob(const JobId & jobid)
117   {
118     int status;
119     int ref;
120     istringstream iss(jobid.getReference());
121     iss >> ref;
122
123     // define command to delete batch
124     string subCommand = string("qdel ") + iss.str();
125     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
126     cerr << command.c_str() << endl;
127     status = system(command.c_str());
128     if (status)
129       throw EmulationException("Error of connection on remote host");
130
131     cerr << "jobId = " << ref << "killed" << endl;
132   }
133
134   // Methode pour le controle des jobs : suspend un job en file d'attente
135   void BatchManager_ePBS::holdJob(const JobId & jobid)
136   {
137     throw EmulationException("Not yet implemented");
138   }
139
140   // Methode pour le controle des jobs : relache un job suspendu
141   void BatchManager_ePBS::releaseJob(const JobId & jobid)
142   {
143     throw EmulationException("Not yet implemented");
144   }
145
146
147   // Methode pour le controle des jobs : modifie un job en file d'attente
148   void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
149   {
150     throw EmulationException("Not yet implemented");
151   }
152
153   // Methode pour le controle des jobs : modifie un job en file d'attente
154   void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
155   {
156     alterJob(jobid, param, Environnement());
157   }
158
159   // Methode pour le controle des jobs : modifie un job en file d'attente
160   void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
161   {
162     alterJob(jobid, Parametre(), env);
163   }
164
165   // Methode pour le controle des jobs : renvoie l'etat du job
166   JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
167   {
168     int id;
169     istringstream iss(jobid.getReference());
170     iss >> id;
171
172     // define name of log file (local)
173     string logFile = generateTemporaryFileName(string("PBS-querylog-id") + jobid.getReference());
174
175     // define command to query batch
176     string subCommand = string("qstat -f ") + iss.str();
177     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
178     command += " > ";
179     command += logFile;
180     cerr << command.c_str() << endl;
181     int status = system(command.c_str());
182     if(status && status != 153 && status != 256*153)
183       throw EmulationException("Error of connection on remote host");
184
185     JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
186     return ji;
187   }
188
189   // Methode pour le controle des jobs : teste si un job est present en machine
190   bool BatchManager_ePBS::isRunning(const JobId & jobid)
191   {
192     throw EmulationException("Not yet implemented");
193   }
194
195   void BatchManager_ePBS::buildBatchScript(const Job & job)
196   {
197     Parametre params = job.getParametre();
198     Environnement env = job.getEnvironnement();
199     const long nbproc = params[NBPROC];
200     const long edt = params[MAXWALLTIME];
201     const long mem = params[MAXRAMSIZE];
202     const string workDir = params[WORKDIR];
203     const std::string dirForTmpFiles = params[TMPDIR];
204     const string fileToExecute = params[EXECUTABLE];
205     const string home = params[HOMEDIR];
206     const std::string queue = params[QUEUE];
207     std::string rootNameToExecute;
208     std::string fileNameToExecute;
209     std::string filelogtemp;
210     if( fileToExecute.size() > 0 ){
211       string::size_type p1 = fileToExecute.find_last_of("/");
212       string::size_type p2 = fileToExecute.find_last_of(".");
213       rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
214
215 #ifdef MSVC
216       char fname[_MAX_FNAME];
217       char ext[_MAX_EXT];
218       _splitpath_s(fileToExecute.c_str(), NULL, 0, NULL, 0, fname, _MAX_FNAME, ext, _MAX_EXT);
219       string execBaseName = string(fname) + ext;
220 #else
221       char* basec=strdup(fileToExecute.c_str());
222       string execBaseName = string(basename(basec));
223       free(basec);
224 #endif
225
226       fileNameToExecute = "~/" + dirForTmpFiles + "/" + execBaseName;
227
228       int idx = dirForTmpFiles.find("Batch/");
229       filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
230     }
231     else{
232       rootNameToExecute = "command";
233     }
234
235     ofstream tempOutputFile;
236     std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
237
238     tempOutputFile << "#! /bin/sh -f" << endl;
239     if (queue != "")
240       tempOutputFile << "#BSUB -q " << queue << endl;
241     if( edt > 0 )
242       tempOutputFile << "#PBS -l walltime=" << edt*60 << endl ;
243     if( mem > 0 )
244       tempOutputFile << "#PBS -l mem=" << mem << "mb" << endl ;
245     if( fileToExecute.size() > 0 ){
246       tempOutputFile << "#PBS -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
247       tempOutputFile << "#PBS -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
248     }
249     else{
250       tempOutputFile << "#PBS -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
251       tempOutputFile << "#PBS -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
252     }
253     if( workDir.size() > 0 )
254       tempOutputFile << "cd " << workDir << endl ;
255     if( fileToExecute.size() > 0 ){
256       tempOutputFile << _mpiImpl->boot("${PBS_NODEFILE}",nbproc);
257       tempOutputFile << _mpiImpl->run("${PBS_NODEFILE}",nbproc,fileNameToExecute);
258       tempOutputFile << _mpiImpl->halt();
259     }
260     else{
261       tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
262       tempOutputFile << env["COMMAND"];
263     }
264
265     tempOutputFile.flush();
266     tempOutputFile.close();
267 #ifdef WIN32
268     _chmod(
269 #else
270     chmod(
271 #endif
272       TmpFileName.c_str(), 0x1ED);
273     cerr << TmpFileName.c_str() << endl;
274
275     int status = _protocol.copyFile(TmpFileName, "", "",
276                                     dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
277                                     _hostname, _username);
278     if (status)
279       throw EmulationException("Error of connection on remote host");
280
281     remove(TmpFileName.c_str());
282   }
283
284 }