1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * BatchManager_eSGE.cxx : emulation of SGE client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
49 #include "Batch_BatchManager_eSGE.hxx"
50 #include "Batch_JobInfo_eSGE.hxx"
56 BatchManager_eSGE::BatchManager_eSGE(const FactBatchManager * parent, const char * host,
57 CommunicationProtocolType protocolType, const char * mpiImpl)
58 : BatchManager_eClient(parent, host, protocolType, mpiImpl),
59 BatchManager(parent, host)
65 BatchManager_eSGE::~BatchManager_eSGE()
70 // Methode pour le controle des jobs : soumet un job au gestionnaire
71 const JobId BatchManager_eSGE::submitJob(const Job & job)
74 Parametre params = job.getParametre();
75 const std::string workDir = params[WORKDIR];
76 const string fileToExecute = params[EXECUTABLE];
77 string::size_type p1 = fileToExecute.find_last_of("/");
78 string::size_type p2 = fileToExecute.find_last_of(".");
79 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
81 // export input files on cluster
82 exportInputFiles(job);
84 // build batch script for job
85 buildBatchScript(job);
87 // define name of log file (local)
88 string logFile = generateTemporaryFileName("SGE-submitlog");
90 // define command to submit batch
91 string subCommand = string("cd ") + workDir + "; qsub " + fileNameToExecute + "_Batch.sh";
92 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
96 cerr << command.c_str() << endl;
97 status = system(command.c_str());
100 ifstream error_message(logFile.c_str());
103 while(std::getline(error_message, temp))
105 error_message.close();
106 throw EmulationException("Error of connection on remote host, error was: " + mess);
109 // read id of submitted job in log file
111 FILE *fp = fopen(logFile.c_str(),"r");
112 fgets( line, 128, fp);
116 istringstream iss(line);
117 iss >> strjob >> strjob >> strjob;
119 JobId id(this, strjob);
123 // Methode pour le controle des jobs : retire un job du gestionnaire
124 void BatchManager_eSGE::deleteJob(const JobId & jobid)
128 istringstream iss(jobid.getReference());
131 // define command to delete batch
132 string subCommand = string("qdel ") + iss.str();
133 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
134 cerr << command.c_str() << endl;
135 status = system(command.c_str());
137 throw EmulationException("Error of connection on remote host");
139 cerr << "jobId = " << ref << "killed" << endl;
142 // Methode pour le controle des jobs : suspend un job en file d'attente
143 void BatchManager_eSGE::holdJob(const JobId & jobid)
145 throw EmulationException("Not yet implemented");
148 // Methode pour le controle des jobs : relache un job suspendu
149 void BatchManager_eSGE::releaseJob(const JobId & jobid)
151 throw EmulationException("Not yet implemented");
155 // Methode pour le controle des jobs : modifie un job en file d'attente
156 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
158 throw EmulationException("Not yet implemented");
161 // Methode pour le controle des jobs : modifie un job en file d'attente
162 void BatchManager_eSGE::alterJob(const JobId & jobid, const Parametre & param)
164 alterJob(jobid, param, Environnement());
167 // Methode pour le controle des jobs : modifie un job en file d'attente
168 void BatchManager_eSGE::alterJob(const JobId & jobid, const Environnement & env)
170 alterJob(jobid, Parametre(), env);
173 // Methode pour le controle des jobs : renvoie l'etat du job
174 JobInfo BatchManager_eSGE::queryJob(const JobId & jobid)
177 istringstream iss(jobid.getReference());
180 // define name of log file (local)
181 string logFile = generateTemporaryFileName(string("SGE-querylog-id") + jobid.getReference());
183 // define command to query batch
184 string subCommand = string("qstat | grep ") + iss.str();
185 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
188 cerr << command.c_str() << endl;
189 int status = system(command.c_str());
190 if (status && status != 256)
191 throw EmulationException("Error of connection on remote host");
193 JobInfo_eSGE ji = JobInfo_eSGE(id,logFile);
197 // Methode pour le controle des jobs : teste si un job est present en machine
198 bool BatchManager_eSGE::isRunning(const JobId & jobid)
200 throw EmulationException("Not yet implemented");
203 void BatchManager_eSGE::buildBatchScript(const Job & job)
206 //TODO porting on Win32 platform
207 std::cerr << "BuildBatchScript" << std::endl;
208 Parametre params = job.getParametre();
212 string fileToExecute = "";
218 // Mandatory parameters
219 if (params.find(WORKDIR) != params.end())
220 workDir = params[WORKDIR].str();
222 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
223 if (params.find(EXECUTABLE) != params.end())
224 fileToExecute = params[EXECUTABLE].str();
226 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
228 // Optional parameters
229 if (params.find(NBPROC) != params.end())
230 nbproc = params[NBPROC];
231 if (params.find(MAXWALLTIME) != params.end())
232 edt = params[MAXWALLTIME];
233 if (params.find(MAXRAMSIZE) != params.end())
234 mem = params[MAXRAMSIZE];
235 if (params.find(QUEUE) != params.end())
236 queue = params[QUEUE].str();
238 string::size_type p1 = fileToExecute.find_last_of("/");
239 string::size_type p2 = fileToExecute.find_last_of(".");
240 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
241 string fileNameToExecute = fileToExecute.substr(p1+1);
243 // Create batch submit file
244 ofstream tempOutputFile;
245 std::string TmpFileName = createAndOpenTemporaryFile("SGE-script", tempOutputFile);
247 tempOutputFile << "#! /bin/sh -f" << endl;
249 tempOutputFile << "#$ -q " << queue << endl;
250 tempOutputFile << "#$ -pe mpich " << nbproc << endl;
252 tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
254 tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
255 tempOutputFile << "#$ -o " << workDir << "/logs/output.log." << rootNameToExecute << endl ;
256 tempOutputFile << "#$ -e " << workDir << "/logs/error.log." << rootNameToExecute << endl ;
258 // Abstraction of PBS_NODEFILE - TODO
259 tempOutputFile << "export LIBBATCH_NODEFILE=$TMPDIR/machines" << endl;
261 // Launch the executable
262 tempOutputFile << "cd " << workDir << endl ;
263 tempOutputFile << "./" + fileNameToExecute << endl;
264 tempOutputFile.flush();
265 tempOutputFile.close();
267 BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
268 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
270 int status = _protocol.copyFile(TmpFileName, "", "",
271 workDir + "/" + rootNameToExecute + "_Batch.sh",
272 _hostname, _username);
274 throw EmulationException("Error of connection on remote host");
279 std::string BatchManager_eSGE::getWallTime(const long edt)
286 oss << h << ":" << m;
288 oss << h << ":0" << m;