1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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_PBS.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 <Constants.hxx>
38 #include <NotYetImplementedException.hxx>
40 #include "BatchManager_PBS.hxx"
41 #include "JobInfo_PBS.hxx"
48 BatchManager_PBS::BatchManager_PBS(const FactBatchManager * parent, const char * host,
49 const char * username,
50 CommunicationProtocolType protocolType, const char * mpiImpl)
51 : BatchManager(parent, host, username, protocolType, mpiImpl)
57 BatchManager_PBS::~BatchManager_PBS()
62 // Methode pour le controle des jobs : soumet un job au gestionnaire
63 const JobId BatchManager_PBS::runJob(const Job & job)
65 Parametre params = job.getParametre();
66 const std::string workDir = params[WORKDIR];
68 // build batch script for job
69 string scriptFile = buildSubmissionScript(job);
71 // define command to submit batch
72 string subCommand = string("cd ") + workDir + "; qsub " + scriptFile;
73 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
79 int status = Utils::getCommandOutput(command, output);
81 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
83 // normally output contains only id of submitted job, we just need to remove the final \n
84 string jobref = output.substr(0, output.size() - 1);
85 JobId id(this, jobref);
90 // Methode pour le controle des jobs : retire un job du gestionnaire
91 void BatchManager_PBS::deleteJob(const JobId & jobid)
95 istringstream iss(jobid.getReference());
98 // define command to delete batch
99 string subCommand = string("qdel ") + iss.str();
100 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
102 status = system(command.c_str());
104 throw RunTimeException("Error of connection on remote host");
106 LOG("jobId = " << ref << "killed");
109 // Methode pour le controle des jobs : renvoie l'etat du job
110 JobInfo BatchManager_PBS::queryJob(const JobId & jobid)
113 istringstream iss(jobid.getReference());
116 // define command to query batch
117 string subCommand = string("qstat -f ") + iss.str();
118 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
122 int status = Utils::getCommandOutput(command, output);
123 if(status && status != 153 && status != 256*153)
124 throw RunTimeException("Error of connection on remote host");
126 JobInfo_PBS ji = JobInfo_PBS(id, output);
130 // Methode pour le controle des jobs : teste si un job est present en machine
131 bool BatchManager_PBS::isRunning(const JobId & jobid)
133 throw NotYetImplementedException("BatchManager_PBS::isRunning");
136 std::string BatchManager_PBS::buildSubmissionScript(const Job & job)
138 Parametre params = job.getParametre();
139 Environnement env = job.getEnvironnement();
141 // Mandatory parameters
143 if (params.find(WORKDIR) != params.end())
144 workDir = params[WORKDIR].str();
146 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
147 string fileToExecute;
148 if (params.find(EXECUTABLE) != params.end())
149 fileToExecute = params[EXECUTABLE].str();
151 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
153 // Optional parameters
155 if (params.find(NBPROC) != params.end())
156 nbproc = params[NBPROC];
157 int nbprocpernode = 1;
158 if (params.find(NBPROCPERNODE) != params.end())
159 nbprocpernode = params[NBPROCPERNODE];
161 if (params.find(MAXWALLTIME) != params.end())
162 edt = params[MAXWALLTIME];
164 if (params.find(MAXRAMSIZE) != params.end())
165 mem = params[MAXRAMSIZE];
167 if (params.find(QUEUE) != params.end())
168 queue = params[QUEUE].str();
170 string::size_type p1 = fileToExecute.find_last_of("/");
171 string::size_type p2 = fileToExecute.find_last_of(".");
172 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
173 string fileNameToExecute = fileToExecute.substr(p1+1);
175 // Create batch submit file
176 ofstream tempOutputFile;
177 std::string TmpFileName = Utils::createAndOpenTemporaryFile("PBS-script", tempOutputFile);
179 tempOutputFile << "#! /bin/sh -f" << endl;
180 if (params.find(NAME) != params.end()) {
181 tempOutputFile << "#PBS -N " << params[NAME] << endl;
186 int nb_full_nodes = nbproc / nbprocpernode;
187 int nb_proc_on_last_node = nbproc % nbprocpernode;
189 // In exclusive mode, we reserve all procs on the nodes
190 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0) {
192 nb_proc_on_last_node = 0;
195 tempOutputFile << "#PBS -l nodes=";
198 if (nb_full_nodes > 0) {
199 tempOutputFile << nb_full_nodes << ":ppn=" << nbprocpernode;
200 if (nb_proc_on_last_node > 0) {
201 tempOutputFile << "+";
205 // Partly reserved node
206 if (nb_proc_on_last_node > 0) {
207 tempOutputFile << "1:ppn=" << nb_proc_on_last_node;
210 tempOutputFile << endl;
213 tempOutputFile << "#PBS -q " << queue << endl;
215 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl;
217 tempOutputFile << "#PBS -l mem=" << mem << "MB" << endl;
218 tempOutputFile << "#PBS -o " << workDir << "/logs/output.log." << rootNameToExecute << endl;
219 tempOutputFile << "#PBS -e " << workDir << "/logs/error.log." << rootNameToExecute << endl;
221 // Define environment for the job
223 tempOutputFile << "#PBS -v ";
224 Environnement::const_iterator iter;
225 for (iter = env.begin() ; iter != env.end() ; ++iter) {
226 tempOutputFile << iter->first << "=" << iter->second << ",";
228 tempOutputFile << endl;
232 tempOutputFile << "export LIBBATCH_NODEFILE=$PBS_NODEFILE" << endl;
234 // Launch the executable
235 tempOutputFile << "cd " << workDir << endl;
236 tempOutputFile << "./" + fileNameToExecute << endl;
237 tempOutputFile.flush();
238 tempOutputFile.close();
240 LOG("Batch script file generated is: " << TmpFileName.c_str());
242 string remoteFileName = rootNameToExecute + "_Batch.sh";
243 int status = _protocol.copyFile(TmpFileName, "", "",
244 workDir + "/" + remoteFileName,
245 _hostname, _username);
247 throw RunTimeException("Error of connection on remote host, cannot copy batch submission file");
248 return remoteFileName;