]> SALOME platform Git repositories - tools/libbatch.git/blob - src/SGE/Batch_BatchManager_eSGE.cxx
Salome HOME
Added missing includes for newer gcc versions.
[tools/libbatch.git] / src / SGE / 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 <stdlib.h>
33 #include <string.h>
34
35 #include <iostream>
36 #include <fstream>
37 #include <sstream>
38 #include <sys/stat.h>
39 #ifdef WIN32
40 #include <io.h>
41 #else
42 #include <libgen.h>
43 #endif
44
45 #include "Batch_BatchManager_eSGE.hxx"
46 #include "Batch_JobInfo_eSGE.hxx"
47
48 using namespace std;
49
50 namespace Batch {
51
52   BatchManager_eSGE::BatchManager_eSGE(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_eSGE::~BatchManager_eSGE()
61   {
62     // Nothing to do
63   }
64
65   // Methode pour le controle des jobs : soumet un job au gestionnaire
66   const JobId BatchManager_eSGE::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("SGE-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     char line[128];
98     FILE *fp = fopen(logFile.c_str(),"r");
99     fgets( line, 128, fp);
100     fclose(fp);
101
102     string strjob;
103     istringstream iss(line);
104     iss >> strjob >> strjob >> strjob;
105
106     JobId id(this, strjob);
107     return id;
108   }
109
110   // Methode pour le controle des jobs : retire un job du gestionnaire
111   void BatchManager_eSGE::deleteJob(const JobId & jobid)
112   {
113     int status;
114     int ref;
115     istringstream iss(jobid.getReference());
116     iss >> ref;
117
118     // define command to delete batch
119     string subCommand = string("qdel ") + iss.str();
120     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
121     cerr << command.c_str() << endl;
122     status = system(command.c_str());
123     if(status)
124       throw EmulationException("Error of connection on remote host");
125
126     cerr << "jobId = " << ref << "killed" << endl;
127   }
128
129   // Methode pour le controle des jobs : suspend un job en file d'attente
130   void BatchManager_eSGE::holdJob(const JobId & jobid)
131   {
132     throw EmulationException("Not yet implemented");
133   }
134
135   // Methode pour le controle des jobs : relache un job suspendu
136   void BatchManager_eSGE::releaseJob(const JobId & jobid)
137   {
138     throw EmulationException("Not yet implemented");
139   }
140
141
142   // Methode pour le controle des jobs : modifie un job en file d'attente
143   void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
144   {
145     throw EmulationException("Not yet implemented");
146   }
147
148   // Methode pour le controle des jobs : modifie un job en file d'attente
149   void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param)
150   {
151     alterJob(jobid, param, Environnement());
152   }
153
154   // Methode pour le controle des jobs : modifie un job en file d'attente
155   void BatchManager_eSGE::alterJob(const JobId & jobid, const Environnement & env)
156   {
157     alterJob(jobid, Parametre(), env);
158   }
159
160   // Methode pour le controle des jobs : renvoie l'etat du job
161   JobInfo BatchManager_eSGE::queryJob(const JobId & jobid)
162   {
163     int id;
164     istringstream iss(jobid.getReference());
165     iss >> id;
166
167     // define name of log file (local)
168     string logFile = generateTemporaryFileName(string("SGE-querylog-id") + jobid.getReference());
169
170     // define command to query batch
171     string subCommand = string("qstat | grep ") + iss.str();
172     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
173     command += " > ";
174     command += logFile;
175     cerr << command.c_str() << endl;
176     int status = system(command.c_str());
177     if (status && status != 256)
178       throw EmulationException("Error of connection on remote host");
179
180     JobInfo_eSGE ji = JobInfo_eSGE(id,logFile);
181     return ji;
182   }
183
184   // Methode pour le controle des jobs : teste si un job est present en machine
185   bool BatchManager_eSGE::isRunning(const JobId & jobid)
186   {
187     throw EmulationException("Not yet implemented");
188   }
189
190   void BatchManager_eSGE::buildBatchScript(const Job & job)
191   {
192 #ifndef WIN32
193     //TODO porting on Win32 platform
194     Parametre params = job.getParametre();
195     Environnement env = job.getEnvironnement();
196     const long nbproc = params[NBPROC];
197     const long edt = params[MAXWALLTIME];
198     const long mem = params[MAXRAMSIZE];
199     const string workDir = params[WORKDIR];
200     const std::string dirForTmpFiles = params[TMPDIR];
201     const string fileToExecute = params[EXECUTABLE];
202     const string home = params[HOMEDIR];
203     const std::string queue = params[QUEUE];
204     std::string rootNameToExecute;
205     std::string fileNameToExecute;
206     std::string filelogtemp;
207     if( fileToExecute.size() > 0 ){
208       string::size_type p1 = fileToExecute.find_last_of("/");
209       string::size_type p2 = fileToExecute.find_last_of(".");
210       rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
211       fileNameToExecute = "~/" + dirForTmpFiles + "/" + string(basename((char *) fileToExecute.c_str()));
212
213       int idx = dirForTmpFiles.find("Batch/");
214       filelogtemp = dirForTmpFiles.substr(idx+6, dirForTmpFiles.length());
215     }
216     else{
217       rootNameToExecute = "command";
218     }
219
220     ofstream tempOutputFile;
221     std::string TmpFileName = createAndOpenTemporaryFile("SGE-script", tempOutputFile);
222
223     tempOutputFile << "#! /bin/sh -f" << endl;
224     if (queue != "")
225       tempOutputFile << "#$ -q " << queue << endl;
226     tempOutputFile << "#$ -pe mpich " << nbproc << endl;
227     if( edt > 0 )
228       tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
229     if( mem > 0 )
230       tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
231     if( fileToExecute.size() > 0 ){
232       tempOutputFile << "#$ -o " << home << "/" << dirForTmpFiles << "/output.log." << filelogtemp << endl ;
233       tempOutputFile << "#$ -e " << home << "/" << dirForTmpFiles << "/error.log." << filelogtemp << endl ;
234     }
235     else{
236       tempOutputFile << "#$ -o " << dirForTmpFiles << "/" << env["LOGFILE"] << ".output.log" << endl ;
237       tempOutputFile << "#$ -e " << dirForTmpFiles << "/" << env["LOGFILE"] << ".error.log" << endl ;
238     }
239     if( workDir.size() > 0 )
240       tempOutputFile << "cd " << workDir << endl ;
241     if( fileToExecute.size() > 0 ){
242       tempOutputFile << _mpiImpl->boot("",nbproc);
243       tempOutputFile << _mpiImpl->run("${TMPDIR}/machines",nbproc,fileNameToExecute);
244       tempOutputFile << _mpiImpl->halt();
245     }
246     else{
247       tempOutputFile << "source " << env["SOURCEFILE"] << endl ;
248       tempOutputFile << env["COMMAND"];
249     }
250
251     tempOutputFile.flush();
252     tempOutputFile.close();
253     chmod(TmpFileName.c_str(), 0x1ED);
254     cerr << TmpFileName.c_str() << endl;
255
256     int status = _protocol.copyFile(TmpFileName, "", "",
257                                     dirForTmpFiles + "/" + rootNameToExecute + "_Batch.sh",
258                                     _hostname, _username);
259     if (status)
260       throw EmulationException("Error of connection on remote host");
261
262     remove(TmpFileName.c_str());
263 #endif //WIN32
264   }
265
266   std::string BatchManager_eSGE::getWallTime(const long edt)
267   {
268     long h, m;
269     h = edt / 60;
270     m = edt - h*60;
271     ostringstream oss;
272     if( m >= 10 )
273       oss << h << ":" << m;
274     else
275       oss << h << ":0" << m;
276     return oss.str();
277   }
278
279 }