1 // Copyright (C) 2007-2008 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_ePBS.cxx : emulation of PBS client
25 * Auteur : Bernard SECHER - CEA DEN, André RIBES - EDF R&D
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
42 #include <Batch_config.h>
50 #include "Batch_BatchManager_ePBS.hxx"
51 #include "Batch_JobInfo_ePBS.hxx"
57 BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host,
58 CommunicationProtocolType protocolType, const char * mpiImpl,
60 : BatchManager_eClient(parent, host, protocolType, mpiImpl),
61 BatchManager(parent, host)
64 _nb_proc_per_node = nb_proc_per_node;
68 BatchManager_ePBS::~BatchManager_ePBS()
73 // Methode pour le controle des jobs : soumet un job au gestionnaire
74 const JobId BatchManager_ePBS::submitJob(const Job & job)
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);
84 // export input files on cluster
85 exportInputFiles(job);
87 // build batch script for job
88 buildBatchScript(job);
90 // define name of log file (local)
91 string logFile = generateTemporaryFileName("PBS-submitlog");
93 // define command to submit batch
94 string subCommand = string("cd ") + workDir + "; qsub " + fileNameToExecute + "_Batch.sh";
95 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
99 cerr << command.c_str() << endl;
100 status = system(command.c_str());
103 ifstream error_message(logFile.c_str());
106 while(std::getline(error_message, temp))
108 error_message.close();
109 throw EmulationException("Error of connection on remote host, error was: " + mess);
112 // read id of submitted job in log file
113 ifstream idfile(logFile.c_str());
117 if (sline.size() == 0)
118 throw EmulationException("Error in the submission of the job on the remote host");
120 size_t pos = sline.find(".");
122 if(pos == string::npos)
125 strjob = sline.substr(0,pos);
127 JobId id(this, strjob);
131 // Methode pour le controle des jobs : retire un job du gestionnaire
132 void BatchManager_ePBS::deleteJob(const JobId & jobid)
136 istringstream iss(jobid.getReference());
139 // define command to delete batch
140 string subCommand = string("qdel ") + iss.str();
141 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
142 cerr << command.c_str() << endl;
143 status = system(command.c_str());
145 throw EmulationException("Error of connection on remote host");
147 cerr << "jobId = " << ref << "killed" << endl;
150 // Methode pour le controle des jobs : suspend un job en file d'attente
151 void BatchManager_ePBS::holdJob(const JobId & jobid)
153 throw EmulationException("Not yet implemented");
156 // Methode pour le controle des jobs : relache un job suspendu
157 void BatchManager_ePBS::releaseJob(const JobId & jobid)
159 throw EmulationException("Not yet implemented");
163 // Methode pour le controle des jobs : modifie un job en file d'attente
164 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
166 throw EmulationException("Not yet implemented");
169 // Methode pour le controle des jobs : modifie un job en file d'attente
170 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
172 alterJob(jobid, param, Environnement());
175 // Methode pour le controle des jobs : modifie un job en file d'attente
176 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
178 alterJob(jobid, Parametre(), env);
181 // Methode pour le controle des jobs : renvoie l'etat du job
182 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
185 istringstream iss(jobid.getReference());
188 // define name of log file (local)
189 string logFile = generateTemporaryFileName(string("PBS-querylog-id") + jobid.getReference());
191 // define command to query batch
192 string subCommand = string("qstat -f ") + iss.str();
193 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
196 cerr << command.c_str() << endl;
197 int status = system(command.c_str());
198 if(status && status != 153 && status != 256*153)
199 throw EmulationException("Error of connection on remote host");
201 JobInfo_ePBS ji = JobInfo_ePBS(id,logFile);
205 // Methode pour le controle des jobs : teste si un job est present en machine
206 bool BatchManager_ePBS::isRunning(const JobId & jobid)
208 throw EmulationException("Not yet implemented");
211 void BatchManager_ePBS::buildBatchScript(const Job & job)
213 std::cerr << "BuildBatchScript" << std::endl;
214 Parametre params = job.getParametre();
215 Environnement env = job.getEnvironnement();
219 string fileToExecute = "";
225 // Mandatory parameters
226 if (params.find(WORKDIR) != params.end())
227 workDir = params[WORKDIR].str();
229 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
230 if (params.find(EXECUTABLE) != params.end())
231 fileToExecute = params[EXECUTABLE].str();
233 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
235 // Optional parameters
236 if (params.find(NBPROC) != params.end())
237 nbproc = params[NBPROC];
238 if (params.find(MAXWALLTIME) != params.end())
239 edt = params[MAXWALLTIME];
240 if (params.find(MAXRAMSIZE) != params.end())
241 mem = params[MAXRAMSIZE];
242 if (params.find(QUEUE) != params.end())
243 queue = params[QUEUE].str();
245 string::size_type p1 = fileToExecute.find_last_of("/");
246 string::size_type p2 = fileToExecute.find_last_of(".");
247 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
248 string fileNameToExecute = fileToExecute.substr(p1+1);
250 // Create batch submit file
251 ofstream tempOutputFile;
252 std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
254 tempOutputFile << "#! /bin/sh -f" << endl;
257 // Division - arrondi supérieur
258 int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
259 tempOutputFile << "#PBS -l nodes=" << nodes_requested << ":ppn=" << _nb_proc_per_node << endl;
262 tempOutputFile << "#PBS -q " << queue << endl;
264 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl;
266 tempOutputFile << "#PBS -l mem=" << mem << "kb" << endl;
267 tempOutputFile << "#PBS -o " << workDir << "/logs/output.log." << rootNameToExecute << endl;
268 tempOutputFile << "#PBS -e " << workDir << "/logs/error.log." << rootNameToExecute << endl;
270 // Define environment for the job
272 tempOutputFile << "#PBS -v ";
273 Environnement::const_iterator iter;
274 for (iter = env.begin() ; iter != env.end() ; ++iter) {
275 tempOutputFile << iter->first << "=" << iter->second << ",";
277 tempOutputFile << endl;
280 // Abstraction of PBS_NODEFILE - TODO
281 tempOutputFile << "export LIBBATCH_NODEFILE=$PBS_NODEFILE" << endl;
283 // Launch the executable
284 tempOutputFile << "cd " << workDir << endl;
285 tempOutputFile << "./" + fileNameToExecute << endl;
286 tempOutputFile.flush();
287 tempOutputFile.close();
289 BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
290 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
292 int status = _protocol.copyFile(TmpFileName, "", "",
293 workDir + "/" + rootNameToExecute + "_Batch.sh",
294 _hostname, _username);
296 throw EmulationException("Error of connection on remote host, cannot copy batch submission file");