Salome HOME
0bb4fbb9320e8a4fe9bf403bd8f4200fcf946213
[tools/libbatch.git] / src / SGE / Batch_BatchManager_eSGE.cxx
1 //  Copyright (C) 2007-2012  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
40 #include <stdlib.h>
41 #include <string.h>
42
43 #ifdef WIN32
44 #include <io.h>
45 #else
46 #include <libgen.h>
47 #endif
48
49 #include <Batch_Constants.hxx>
50 #include <Batch_Utils.hxx>
51 #include <Batch_NotYetImplementedException.hxx>
52
53 #include "Batch_BatchManager_eSGE.hxx"
54 #include "Batch_JobInfo_eSGE.hxx"
55
56 using namespace std;
57
58 namespace Batch {
59
60   BatchManager_eSGE::BatchManager_eSGE(const FactBatchManager * parent, const char * host,
61                                        const char * username,
62                                        CommunicationProtocolType protocolType, const char * mpiImpl)
63   : BatchManager(parent, host, username, protocolType, mpiImpl)
64   {
65     // Nothing to do
66   }
67
68   // Destructeur
69   BatchManager_eSGE::~BatchManager_eSGE()
70   {
71     // Nothing to do
72   }
73
74   // Methode pour le controle des jobs : soumet un job au gestionnaire
75   const JobId BatchManager_eSGE::submitJob(const Job & job)
76   {
77     Parametre params = job.getParametre();
78     const std::string workDir = params[WORKDIR];
79     const string fileToExecute = params[EXECUTABLE];
80     string::size_type p1 = fileToExecute.find_last_of("/");
81     string::size_type p2 = fileToExecute.find_last_of(".");
82     std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
83
84     // export input files on cluster
85     exportInputFiles(job);
86
87     // build batch script for job
88     buildBatchScript(job);
89
90     // define command to submit batch
91     string subCommand = string("bash -l -c \\\"cd ") + workDir + "; qsub " + fileNameToExecute + "_Batch.sh\\\"";
92     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
93     command += " 2>&1";
94     cerr << command.c_str() << endl;
95
96     // submit job
97     string output;
98     int status = Utils::getCommandOutput(command, output);
99     cout << output;
100     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
101
102     // find id of submitted job in output
103     string strjob;
104     istringstream iss(output);
105     iss >> strjob >> strjob >> strjob;
106
107     JobId id(this, strjob);
108     return id;
109   }
110
111   // Ce manager permet de faire de la reprise
112   const Batch::JobId
113   BatchManager_eSGE::addJob(const Batch::Job & job, const std::string reference)
114   {
115     return JobId(this, reference);
116   }
117
118   // Methode pour le controle des jobs : retire un job du gestionnaire
119   void BatchManager_eSGE::deleteJob(const JobId & jobid)
120   {
121     int status;
122     int ref;
123     istringstream iss(jobid.getReference());
124     iss >> ref;
125
126     // define command to delete batch
127     string subCommand = string("bash -l -c \\\"qdel ") + iss.str() + string("\\\"");
128     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
129     cerr << command.c_str() << endl;
130     status = system(command.c_str());
131     if(status)
132       throw RunTimeException("Error of connection on remote host");
133
134     cerr << "jobId = " << ref << "killed" << endl;
135   }
136
137   // Methode pour le controle des jobs : suspend un job en file d'attente
138   void BatchManager_eSGE::holdJob(const JobId & jobid)
139   {
140     throw NotYetImplementedException("BatchManager_eSGE::holdJob");
141   }
142
143   // Methode pour le controle des jobs : relache un job suspendu
144   void BatchManager_eSGE::releaseJob(const JobId & jobid)
145   {
146     throw NotYetImplementedException("BatchManager_eSGE::releaseJob");
147   }
148
149
150   // Methode pour le controle des jobs : modifie un job en file d'attente
151   void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
152   {
153     throw NotYetImplementedException("BatchManager_eSGE::alterJob");
154   }
155
156   // Methode pour le controle des jobs : modifie un job en file d'attente
157   void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param)
158   {
159     alterJob(jobid, param, Environnement());
160   }
161
162   // Methode pour le controle des jobs : modifie un job en file d'attente
163   void BatchManager_eSGE::alterJob(const JobId & jobid, const Environnement & env)
164   {
165     alterJob(jobid, Parametre(), env);
166   }
167
168   // Methode pour le controle des jobs : renvoie l'etat du job
169   JobInfo BatchManager_eSGE::queryJob(const JobId & jobid)
170   {
171     int id;
172     istringstream iss(jobid.getReference());
173     iss >> id;
174
175     // define command to query batch
176     string subCommand = string("bash -l -c \\\"qstat | grep ") + iss.str() + string("\\\"");
177     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
178     cerr << command.c_str() << endl;
179
180     string output;
181     int status = Utils::getCommandOutput(command, output);
182     if (status && status != 256)
183       throw RunTimeException("Error of connection on remote host");
184
185     JobInfo_eSGE ji = JobInfo_eSGE(id, output);
186     return ji;
187   }
188
189   // Methode pour le controle des jobs : teste si un job est present en machine
190   bool BatchManager_eSGE::isRunning(const JobId & jobid)
191   {
192     throw NotYetImplementedException("BatchManager_eSGE::isRunning");
193   }
194
195   void BatchManager_eSGE::buildBatchScript(const Job & job)
196   {
197 #ifndef WIN32
198     //TODO porting on Win32 platform
199     std::cerr << "BuildBatchScript" << std::endl;
200     Parametre params = job.getParametre();
201
202     // Job Parameters
203     string workDir       = "";
204     string fileToExecute = "";
205     int nbproc           = 0;
206     int edt              = 0;
207     int mem              = 0;
208     string queue         = "";
209
210     // Mandatory parameters
211     if (params.find(WORKDIR) != params.end()) 
212       workDir = params[WORKDIR].str();
213     else 
214       throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
215     if (params.find(EXECUTABLE) != params.end()) 
216       fileToExecute = params[EXECUTABLE].str();
217     else 
218       throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
219
220     // Optional parameters
221     if (params.find(NBPROC) != params.end()) 
222       nbproc = params[NBPROC];
223     if (params.find(MAXWALLTIME) != params.end()) 
224       edt = params[MAXWALLTIME];
225     if (params.find(MAXRAMSIZE) != params.end()) 
226       mem = params[MAXRAMSIZE];
227     if (params.find(QUEUE) != params.end()) 
228       queue = params[QUEUE].str();
229
230     string::size_type p1 = fileToExecute.find_last_of("/");
231     string::size_type p2 = fileToExecute.find_last_of(".");
232     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
233     string fileNameToExecute = fileToExecute.substr(p1+1);
234
235     // Create batch submit file
236     ofstream tempOutputFile;
237     std::string TmpFileName = Utils::createAndOpenTemporaryFile("SGE-script", tempOutputFile);
238
239     tempOutputFile << "#! /bin/sh -f" << endl;
240     if (queue != "")
241       tempOutputFile << "#$ -q " << queue << endl;
242     tempOutputFile << "#$ -pe " << _mpiImpl->name() << " " << nbproc << endl;
243     if( edt > 0 )
244       tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
245     if( mem > 0 )
246       tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
247     tempOutputFile << "#$ -o " << workDir << "/logs/output.log." << rootNameToExecute << endl ;
248     tempOutputFile << "#$ -e " << workDir << "/logs/error.log." << rootNameToExecute << endl ;
249
250     // Abstraction of PBS_NODEFILE - TODO
251     tempOutputFile << "export LIBBATCH_NODEFILE=$TMPDIR/machines" << endl;
252
253     // Launch the executable
254     tempOutputFile << "cd " << workDir << endl ;
255     tempOutputFile << "./" + fileNameToExecute << endl;
256     tempOutputFile.flush();
257     tempOutputFile.close();
258
259     BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
260     cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
261
262     int status = _protocol.copyFile(TmpFileName, "", "",
263                                     workDir + "/" + rootNameToExecute + "_Batch.sh",
264                                     _hostname, _username);
265     if (status)
266       throw RunTimeException("Error of connection on remote host");
267
268 #endif //WIN32
269   }
270
271   std::string BatchManager_eSGE::getWallTime(const long edt)
272   {
273     long h, m;
274     h = edt / 60;
275     m = edt - h*60;
276     ostringstream oss;
277     if( m >= 10 )
278       oss << h << ":" << m;
279     else
280       oss << h << ":0" << m;
281     oss << ":00"; // the seconds
282
283     return oss.str();
284   }
285
286 }