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