1 // Copyright (C) 2007-2020 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 = 0;
158 if (params.find(NBPROCPERNODE) != params.end())
159 nbprocpernode = params[NBPROCPERNODE];
161 if (params.find(NBNODE) != params.end())
163 nbnode = params[NBNODE];
164 if(nbnode > 0 && nbprocpernode == 0)
165 nbprocpernode = nbproc / nbnode;
167 if(nbprocpernode == 0) // if not defined
170 if (params.find(MAXWALLTIME) != params.end())
171 edt = params[MAXWALLTIME];
173 if (params.find(MAXRAMSIZE) != params.end())
174 mem = params[MAXRAMSIZE];
176 if (params.find(QUEUE) != params.end())
177 queue = params[QUEUE].str();
179 string::size_type p1 = fileToExecute.find_last_of("/");
180 string::size_type p2 = fileToExecute.find_last_of(".");
181 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
182 string fileNameToExecute = fileToExecute.substr(p1+1);
184 // Create batch submit file
185 ofstream tempOutputFile;
186 std::string TmpFileName = Utils::createAndOpenTemporaryFile("PBS-script", tempOutputFile);
188 tempOutputFile << "#! /bin/sh -f" << endl;
189 if (params.find(NAME) != params.end()) {
190 tempOutputFile << "#PBS -N " << params[NAME] << endl;
195 int nb_full_nodes = nbproc / nbprocpernode;
196 int nb_proc_on_last_node = nbproc % nbprocpernode;
198 // In exclusive mode, we reserve all procs on the nodes
199 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0) {
201 nb_proc_on_last_node = 0;
204 tempOutputFile << "#PBS -l nodes=";
207 if (nb_full_nodes > 0) {
208 tempOutputFile << nb_full_nodes << ":ppn=" << nbprocpernode;
209 if (nb_proc_on_last_node > 0) {
210 tempOutputFile << "+";
214 // Partly reserved node
215 if (nb_proc_on_last_node > 0) {
216 tempOutputFile << "1:ppn=" << nb_proc_on_last_node;
219 tempOutputFile << endl;
222 tempOutputFile << "#PBS -q " << queue << endl;
224 tempOutputFile << "#PBS -l walltime=" << edt*60 << endl;
226 tempOutputFile << "#PBS -l mem=" << mem << "MB" << endl;
227 tempOutputFile << "#PBS -o " << workDir << "/logs/output.log." << rootNameToExecute << endl;
228 tempOutputFile << "#PBS -e " << workDir << "/logs/error.log." << rootNameToExecute << endl;
230 // Define environment for the job
232 tempOutputFile << "#PBS -v ";
233 Environnement::const_iterator iter;
234 for (iter = env.begin() ; iter != env.end() ; ++iter) {
235 tempOutputFile << iter->first << "=" << iter->second << ",";
237 tempOutputFile << endl;
241 tempOutputFile << "export LIBBATCH_NODEFILE=$PBS_NODEFILE" << endl;
243 // Launch the executable
244 tempOutputFile << "cd " << workDir << endl;
245 tempOutputFile << "./" + fileNameToExecute << endl;
246 tempOutputFile.flush();
247 tempOutputFile.close();
249 LOG("Batch script file generated is: " << TmpFileName.c_str());
251 string remoteFileName = rootNameToExecute + "_Batch.sh";
252 int status = _protocol.copyFile(TmpFileName, "", "",
253 workDir + "/" + remoteFileName,
254 _hostname, _username);
256 throw RunTimeException("Error of connection on remote host, cannot copy batch submission file");
257 return remoteFileName;