1 // Copyright (C) 2007-2014 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_LSF.cxx : emulation of LSF client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
36 #include <Constants.hxx>
38 #include <NotYetImplementedException.hxx>
39 #include "BatchManager_LSF.hxx"
40 #include "JobInfo_LSF.hxx"
47 BatchManager_LSF::BatchManager_LSF(const FactBatchManager * parent, const char * host,
48 const char * username,
49 CommunicationProtocolType protocolType, const char * mpiImpl)
50 : BatchManager(parent, host, username, protocolType, mpiImpl)
56 BatchManager_LSF::~BatchManager_LSF()
61 // Methode pour le controle des jobs : soumet un job au gestionnaire
62 const JobId BatchManager_LSF::submitJob(const Job & job)
64 Parametre params = job.getParametre();
65 const std::string workDir = params[WORKDIR];
67 // export input files on cluster
68 LOG("Export des fichiers en entree");
69 exportInputFiles(job);
71 // build batch script for job
72 LOG("Construction du script de batch");
73 string scriptFile = buildSubmissionScript(job);
76 // define command to submit batch
77 string subCommand = string("cd ") + workDir + "; bsub < " + scriptFile;
78 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
83 int status = Utils::getCommandOutput(command, output);
85 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
87 // read id of submitted job in output
88 int p10 = output.find("<");
89 int p20 = output.find(">");
90 string strjob = output.substr(p10+1,p20-p10-1);
92 JobId id(this, strjob);
96 // Methode pour le controle des jobs : retire un job du gestionnaire
97 void BatchManager_LSF::deleteJob(const JobId & jobid)
101 istringstream iss(jobid.getReference());
104 // define command to delete batch
105 string subCommand = string("bkill ") + iss.str();
106 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
108 status = system(command.c_str());
110 throw RunTimeException("Error of connection on remote host");
112 LOG("jobId = " << ref << "killed");
115 // Methode pour le controle des jobs : renvoie l'etat du job
116 JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
119 istringstream iss(jobid.getReference());
122 // define command to query batch
123 string subCommand = string("bjobs ") + iss.str();
124 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
128 int status = Utils::getCommandOutput(command, output);
129 if (status) throw RunTimeException("Error of connection on remote host");
131 JobInfo_LSF ji = JobInfo_LSF(id, output);
137 // Methode pour le controle des jobs : teste si un job est present en machine
138 bool BatchManager_LSF::isRunning(const JobId & jobid)
140 throw NotYetImplementedException("BatchManager_LSF::isRunning");
143 std::string BatchManager_LSF::buildSubmissionScript(const Job & job)
145 Parametre params = job.getParametre();
149 string fileToExecute = "";
155 // Mandatory parameters
156 if (params.find(WORKDIR) != params.end())
157 workDir = params[WORKDIR].str();
159 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
160 if (params.find(EXECUTABLE) != params.end())
161 fileToExecute = params[EXECUTABLE].str();
163 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
165 // Optional parameters
166 if (params.find(NBPROC) != params.end())
167 nbproc = params[NBPROC];
168 if (params.find(MAXWALLTIME) != params.end())
169 edt = params[MAXWALLTIME];
170 if (params.find(MAXRAMSIZE) != params.end())
171 mem = params[MAXRAMSIZE];
172 if (params.find(QUEUE) != params.end())
173 queue = params[QUEUE].str();
175 string::size_type p1 = fileToExecute.find_last_of("/");
176 string::size_type p2 = fileToExecute.find_last_of(".");
177 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
178 string fileNameToExecute = fileToExecute.substr(p1+1);
180 // Create batch submit file
181 ofstream tempOutputFile;
182 std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
184 tempOutputFile << "#! /bin/sh -f" << endl ;
185 if (params.find(NAME) != params.end())
186 tempOutputFile << "#BSUB -J " << params[NAME] << endl;
188 tempOutputFile << "#BSUB -q " << queue << endl;
190 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
192 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
193 tempOutputFile << "#BSUB -n " << nbproc << endl ;
195 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE]) {
196 tempOutputFile << "#BSUB -x" << endl ;
199 size_t pos = workDir.find("$HOME");
201 if( pos != string::npos )
202 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
204 pos = workDir.find("~");
205 if( pos != string::npos )
206 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
210 tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
211 tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
213 // Define environment for the job
214 Environnement env = job.getEnvironnement();
215 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
216 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
219 tempOutputFile << "cd " << workDir << endl ;
221 // generate nodes file
222 tempOutputFile << "LIBBATCH_NODEFILE=$(mktemp nodefile-XXXXXXXXXX) || exit 1" << endl;
223 tempOutputFile << "bool=0" << endl;
224 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
225 tempOutputFile << " if test $bool = 0; then" << endl;
226 tempOutputFile << " n=$i" << endl;
227 tempOutputFile << " bool=1" << endl;
228 tempOutputFile << " else" << endl;
229 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
230 tempOutputFile << " echo $n >> $LIBBATCH_NODEFILE" << endl;
231 tempOutputFile << " done" << endl;
232 tempOutputFile << " bool=0" << endl;
233 tempOutputFile << " fi" << endl;
234 tempOutputFile << "done" << endl;
235 tempOutputFile << "export LIBBATCH_NODEFILE" << endl;
237 // Launch the executable
238 tempOutputFile << "./" + fileNameToExecute;
239 if (params.find(ARGUMENTS) != params.end()) {
240 Versatile V = params[ARGUMENTS];
241 for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
242 StringType argt = * static_cast<StringType *>(*it);
244 tempOutputFile << " " << arg;
247 tempOutputFile << endl;
249 // Remove the node file
250 tempOutputFile << "rm $LIBBATCH_NODEFILE" << endl;
252 tempOutputFile.flush();
253 tempOutputFile.close();
255 LOG("Batch script file generated is: " << TmpFileName.c_str());
257 string remoteFileName = rootNameToExecute + "_Batch.sh";
258 int status = _protocol.copyFile(TmpFileName, "", "",
259 workDir + "/" + remoteFileName,
260 _hostname, _username);
262 throw RunTimeException("Error of connection on remote host");
263 return remoteFileName;
266 std::string BatchManager_LSF::getWallTime(const long edt)
273 oss << h << ":" << m;
275 oss << h << ":0" << m;
279 std::string BatchManager_LSF::getHomeDir(std::string tmpdir)
282 int idx = tmpdir.find("Batch/");
283 std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
284 filelogtemp = "/tmp/logs" + filelogtemp + "_home";
286 string subCommand = string("echo $HOME");
287 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
289 int status = system(command.c_str());
291 throw RunTimeException("Error of launching home command on remote host");
293 std::ifstream file_home(filelogtemp.c_str());
294 std::getline(file_home, home);