Salome HOME
ac4bea76747fb68561005c447fed3d63af8bcb49
[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 #include "Log.hxx"
55
56 using namespace std;
57
58 namespace Batch {
59
60   BatchManager_SGE::BatchManager_SGE(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_SGE::~BatchManager_SGE()
70   {
71     // Nothing to do
72   }
73
74   // Methode pour le controle des jobs : soumet un job au gestionnaire
75   const JobId BatchManager_SGE::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     LOG(command);
95
96     // submit job
97     string output;
98     int status = Utils::getCommandOutput(command, output);
99     LOG(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
112   // Methode pour le controle des jobs : retire un job du gestionnaire
113   void BatchManager_SGE::deleteJob(const JobId & jobid)
114   {
115     int status;
116     int ref;
117     istringstream iss(jobid.getReference());
118     iss >> ref;
119
120     // define command to delete batch
121     string subCommand = string("bash -l -c \\\"qdel ") + iss.str() + string("\\\"");
122     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
123     LOG(command);
124     status = system(command.c_str());
125     if(status)
126       throw RunTimeException("Error of connection on remote host");
127
128     LOG("jobId = " << ref << "killed");
129   }
130
131   // Methode pour le controle des jobs : renvoie l'etat du job
132   JobInfo BatchManager_SGE::queryJob(const JobId & jobid)
133   {
134     int id;
135     istringstream iss(jobid.getReference());
136     iss >> id;
137
138     // define command to query batch
139     string subCommand = string("bash -l -c \\\"qstat | grep ") + iss.str() + string("\\\"");
140     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
141     LOG(command);
142
143     string output;
144     int status = Utils::getCommandOutput(command, output);
145     if (status && status != 256)
146       throw RunTimeException("Error of connection on remote host");
147
148     JobInfo_SGE ji = JobInfo_SGE(id, output);
149     return ji;
150   }
151
152   // Methode pour le controle des jobs : teste si un job est present en machine
153   bool BatchManager_SGE::isRunning(const JobId & jobid)
154   {
155     throw NotYetImplementedException("BatchManager_SGE::isRunning");
156   }
157
158   void BatchManager_SGE::buildBatchScript(const Job & job)
159   {
160 #ifndef WIN32
161     //TODO porting on Win32 platform
162     LOG("BuildBatchScript");
163     Parametre params = job.getParametre();
164
165     // Job Parameters
166     string workDir       = "";
167     string fileToExecute = "";
168     int nbproc           = 0;
169     int edt              = 0;
170     int mem              = 0;
171     string queue         = "";
172
173     // Mandatory parameters
174     if (params.find(WORKDIR) != params.end()) 
175       workDir = params[WORKDIR].str();
176     else 
177       throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
178     if (params.find(EXECUTABLE) != params.end()) 
179       fileToExecute = params[EXECUTABLE].str();
180     else 
181       throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
182
183     // Optional parameters
184     if (params.find(NBPROC) != params.end()) 
185       nbproc = params[NBPROC];
186     if (params.find(MAXWALLTIME) != params.end()) 
187       edt = params[MAXWALLTIME];
188     if (params.find(MAXRAMSIZE) != params.end()) 
189       mem = params[MAXRAMSIZE];
190     if (params.find(QUEUE) != params.end()) 
191       queue = params[QUEUE].str();
192
193     string::size_type p1 = fileToExecute.find_last_of("/");
194     string::size_type p2 = fileToExecute.find_last_of(".");
195     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
196     string fileNameToExecute = fileToExecute.substr(p1+1);
197
198     // Create batch submit file
199     ofstream tempOutputFile;
200     std::string TmpFileName = Utils::createAndOpenTemporaryFile("SGE-script", tempOutputFile);
201
202     tempOutputFile << "#! /bin/sh -f" << endl;
203     if (queue != "")
204       tempOutputFile << "#$ -q " << queue << endl;
205     tempOutputFile << "#$ -pe " << _mpiImpl->name() << " " << nbproc << endl;
206     if( edt > 0 )
207       tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
208     if( mem > 0 )
209       tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
210     tempOutputFile << "#$ -o " << workDir << "/logs/output.log." << rootNameToExecute << endl ;
211     tempOutputFile << "#$ -e " << workDir << "/logs/error.log." << rootNameToExecute << endl ;
212
213     // Abstraction of PBS_NODEFILE - TODO
214     tempOutputFile << "export LIBBATCH_NODEFILE=$TMPDIR/machines" << endl;
215
216     // Launch the executable
217     tempOutputFile << "cd " << workDir << endl ;
218     tempOutputFile << "./" + fileNameToExecute << endl;
219     tempOutputFile.flush();
220     tempOutputFile.close();
221
222     Utils::chmod(TmpFileName.c_str(), 0x1ED);
223     LOG("Batch script file generated is: " << TmpFileName.c_str());
224
225     int status = _protocol.copyFile(TmpFileName, "", "",
226                                     workDir + "/" + rootNameToExecute + "_Batch.sh",
227                                     _hostname, _username);
228     if (status)
229       throw RunTimeException("Error of connection on remote host");
230
231 #endif //WIN32
232   }
233
234   std::string BatchManager_SGE::getWallTime(const long edt)
235   {
236     long h, m;
237     h = edt / 60;
238     m = edt - h*60;
239     ostringstream oss;
240     if( m >= 10 )
241       oss << h << ":" << m;
242     else
243       oss << h << ":0" << m;
244     oss << ":00"; // the seconds
245
246     return oss.str();
247   }
248
249 }