1 // Copyright (C) 2007-2012 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
36 #include <Batch_Constants.hxx>
37 #include <Batch_Utils.hxx>
39 #include "Batch_BatchManager_ePBS.hxx"
40 #include "Batch_JobInfo_ePBS.hxx"
46 BatchManager_ePBS::BatchManager_ePBS(const FactBatchManager * parent, const char * host,
47 const char * username,
48 CommunicationProtocolType protocolType, const char * mpiImpl,
50 : BatchManager(parent, host),
51 BatchManager_eClient(parent, host, username, protocolType, mpiImpl),
52 _nb_proc_per_node(nb_proc_per_node)
58 BatchManager_ePBS::~BatchManager_ePBS()
63 // Methode pour le controle des jobs : soumet un job au gestionnaire
64 const JobId BatchManager_ePBS::submitJob(const Job & job)
66 Parametre params = job.getParametre();
67 const std::string workDir = params[WORKDIR];
69 // export input files on cluster
70 exportInputFiles(job);
72 // build batch script for job
73 string scriptFile = buildSubmissionScript(job);
75 // define command to submit batch
76 string subCommand = string("cd ") + workDir + "; qsub " + scriptFile;
77 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
79 cerr << command.c_str() << endl;
83 int status = Utils::getCommandOutput(command, output);
85 if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
87 // normally output contains only id of submitted job, we just need to remove the final \n
88 string jobref = output.substr(0, output.size() - 1);
89 JobId id(this, jobref);
94 // Ce manager permet de faire de la reprise
96 BatchManager_ePBS::addJob(const Batch::Job & job, const std::string reference)
98 return JobId(this, reference);
101 // Methode pour le controle des jobs : retire un job du gestionnaire
102 void BatchManager_ePBS::deleteJob(const JobId & jobid)
106 istringstream iss(jobid.getReference());
109 // define command to delete batch
110 string subCommand = string("qdel ") + iss.str();
111 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
112 cerr << command.c_str() << endl;
113 status = system(command.c_str());
115 throw EmulationException("Error of connection on remote host");
117 cerr << "jobId = " << ref << "killed" << endl;
120 // Methode pour le controle des jobs : suspend un job en file d'attente
121 void BatchManager_ePBS::holdJob(const JobId & jobid)
123 throw EmulationException("Not yet implemented");
126 // Methode pour le controle des jobs : relache un job suspendu
127 void BatchManager_ePBS::releaseJob(const JobId & jobid)
129 throw EmulationException("Not yet implemented");
133 // Methode pour le controle des jobs : modifie un job en file d'attente
134 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
136 throw EmulationException("Not yet implemented");
139 // Methode pour le controle des jobs : modifie un job en file d'attente
140 void BatchManager_ePBS::alterJob(const JobId & jobid, const Parametre & param)
142 alterJob(jobid, param, Environnement());
145 // Methode pour le controle des jobs : modifie un job en file d'attente
146 void BatchManager_ePBS::alterJob(const JobId & jobid, const Environnement & env)
148 alterJob(jobid, Parametre(), env);
151 // Methode pour le controle des jobs : renvoie l'etat du job
152 JobInfo BatchManager_ePBS::queryJob(const JobId & jobid)
155 istringstream iss(jobid.getReference());
158 // define command to query batch
159 string subCommand = string("qstat -f ") + iss.str();
160 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
161 cerr << command.c_str() << endl;
164 int status = Utils::getCommandOutput(command, output);
165 if(status && status != 153 && status != 256*153)
166 throw EmulationException("Error of connection on remote host");
168 JobInfo_ePBS ji = JobInfo_ePBS(id, output);
172 // Methode pour le controle des jobs : teste si un job est present en machine
173 bool BatchManager_ePBS::isRunning(const JobId & jobid)
175 throw EmulationException("Not yet implemented");
178 std::string BatchManager_ePBS::buildSubmissionScript(const Job & job)
180 Parametre params = job.getParametre();
181 Environnement env = job.getEnvironnement();
185 string fileToExecute = "";
191 // Mandatory parameters
192 if (params.find(WORKDIR) != params.end())
193 workDir = params[WORKDIR].str();
195 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
196 if (params.find(EXECUTABLE) != params.end())
197 fileToExecute = params[EXECUTABLE].str();
199 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
201 // Optional parameters
202 if (params.find(NBPROC) != params.end())
203 nbproc = params[NBPROC];
204 if (params.find(MAXWALLTIME) != params.end())
205 edt = params[MAXWALLTIME];
206 if (params.find(MAXRAMSIZE) != params.end())
207 mem = params[MAXRAMSIZE];
208 if (params.find(QUEUE) != params.end())
209 queue = params[QUEUE].str();
211 string::size_type p1 = fileToExecute.find_last_of("/");
212 string::size_type p2 = fileToExecute.find_last_of(".");
213 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
214 string fileNameToExecute = fileToExecute.substr(p1+1);
216 // Create batch submit file
217 ofstream tempOutputFile;
218 std::string TmpFileName = createAndOpenTemporaryFile("PBS-script", tempOutputFile);
220 tempOutputFile << "#! /bin/sh -f" << endl;
221 if (params.find(NAME) != params.end()) {
222 tempOutputFile << "#PBS -N " << params[NAME] << endl;
227 int nb_full_nodes = nbproc / _nb_proc_per_node;
228 int nb_proc_on_last_node = nbproc % _nb_proc_per_node;
230 // In exclusive mode, we reserve all procs on the nodes
231 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0) {
233 nb_proc_on_last_node = 0;
236 tempOutputFile << "#PBS -l nodes=";
239 if (nb_full_nodes > 0) {
240 tempOutputFile << nb_full_nodes << ":ppn=" << _nb_proc_per_node;
241 if (nb_proc_on_last_node > 0) {
242 tempOutputFile << "+";
246 // Partly reserved node
247 if (nb_proc_on_last_node > 0) {
248 tempOutputFile << "1:ppn=" << nb_proc_on_last_node;
251 tempOutputFile << endl;
254 tempOutputFile << "#PBS -q " << queue << endl;
256 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl;
258 tempOutputFile << "#PBS -l mem=" << mem << "MB" << endl;
259 tempOutputFile << "#PBS -o " << workDir << "/logs/output.log." << rootNameToExecute << endl;
260 tempOutputFile << "#PBS -e " << workDir << "/logs/error.log." << rootNameToExecute << endl;
262 // Define environment for the job
264 tempOutputFile << "#PBS -v ";
265 Environnement::const_iterator iter;
266 for (iter = env.begin() ; iter != env.end() ; ++iter) {
267 tempOutputFile << iter->first << "=" << iter->second << ",";
269 tempOutputFile << endl;
272 // Abstraction of PBS_NODEFILE - TODO
273 tempOutputFile << "export LIBBATCH_NODEFILE=$PBS_NODEFILE" << endl;
275 // Launch the executable
276 tempOutputFile << "cd " << workDir << endl;
277 tempOutputFile << "./" + fileNameToExecute << endl;
278 tempOutputFile.flush();
279 tempOutputFile.close();
281 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
283 string remoteFileName = rootNameToExecute + "_Batch.sh";
284 int status = _protocol.copyFile(TmpFileName, "", "",
285 workDir + "/" + remoteFileName,
286 _hostname, _username);
288 throw EmulationException("Error of connection on remote host, cannot copy batch submission file");
289 return remoteFileName;