1 // Copyright (C) 2007-2021 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_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::runJob(const Job & job)
64 Parametre params = job.getParametre();
65 const std::string workDir = params[WORKDIR];
67 // build batch script for job
68 LOG("Construction du script de batch");
69 string scriptFile = buildSubmissionScript(job);
72 // define command to submit batch
73 string subCommand = string("cd ") + workDir + "; bsub < " + scriptFile;
74 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 // read id of submitted job in output
84 int p10 = output.find("<");
85 int p20 = output.find(">");
86 string strjob = output.substr(p10+1,p20-p10-1);
88 JobId id(this, strjob);
92 // Methode pour le controle des jobs : retire un job du gestionnaire
93 void BatchManager_LSF::deleteJob(const JobId & jobid)
97 istringstream iss(jobid.getReference());
100 // define command to delete batch
101 string subCommand = string("bkill ") + iss.str();
102 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
104 status = system(command.c_str());
106 throw RunTimeException("Error of connection on remote host");
108 LOG("jobId = " << ref << "killed");
111 // Methode pour le controle des jobs : renvoie l'etat du job
112 JobInfo BatchManager_LSF::queryJob(const JobId & jobid)
115 istringstream iss(jobid.getReference());
118 // define command to query batch
119 string subCommand = string("bjobs ") + iss.str();
120 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
124 int status = Utils::getCommandOutput(command, output);
125 if (status) throw RunTimeException("Error of connection on remote host");
127 JobInfo_LSF ji = JobInfo_LSF(id, output);
133 // Methode pour le controle des jobs : teste si un job est present en machine
134 bool BatchManager_LSF::isRunning(const JobId & /*jobid*/)
136 throw NotYetImplementedException("BatchManager_LSF::isRunning");
139 std::string BatchManager_LSF::buildSubmissionScript(const Job & job)
141 Parametre params = job.getParametre();
145 string fileToExecute = "";
151 // Mandatory parameters
152 if (params.find(WORKDIR) != params.end())
153 workDir = params[WORKDIR].str();
155 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
156 if (params.find(EXECUTABLE) != params.end())
157 fileToExecute = params[EXECUTABLE].str();
159 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
161 // Optional parameters
162 if (params.find(NBPROC) != params.end())
163 nbproc = params[NBPROC];
164 if (params.find(MAXWALLTIME) != params.end())
165 edt = params[MAXWALLTIME];
166 if (params.find(MAXRAMSIZE) != params.end())
167 mem = params[MAXRAMSIZE];
168 if (params.find(QUEUE) != params.end())
169 queue = params[QUEUE].str();
171 string::size_type p1 = fileToExecute.find_last_of("/");
172 string::size_type p2 = fileToExecute.find_last_of(".");
173 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
174 string fileNameToExecute = fileToExecute.substr(p1+1);
176 // Create batch submit file
177 ofstream tempOutputFile;
178 std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
180 tempOutputFile << "#! /bin/sh -f" << endl ;
181 if (params.find(NAME) != params.end())
182 tempOutputFile << "#BSUB -J " << params[NAME] << endl;
184 tempOutputFile << "#BSUB -q " << queue << endl;
186 tempOutputFile << "#BSUB -W " << getWallTime(edt) << endl ;
188 tempOutputFile << "#BSUB -M " << mem*1024 << endl ;
189 tempOutputFile << "#BSUB -n " << nbproc << endl ;
191 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE]) {
192 tempOutputFile << "#BSUB -x" << endl ;
195 size_t pos = workDir.find("$HOME");
197 if( pos != string::npos )
198 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
200 pos = workDir.find("~");
201 if( pos != string::npos )
202 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
206 tempOutputFile << "#BSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
207 tempOutputFile << "#BSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
209 // Define environment for the job
210 Environnement env = job.getEnvironnement();
211 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
212 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
215 tempOutputFile << "cd " << workDir << endl ;
217 // generate nodes file
218 tempOutputFile << "LIBBATCH_NODEFILE=$(mktemp nodefile-XXXXXXXXXX) || exit 1" << endl;
219 tempOutputFile << "bool=0" << endl;
220 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
221 tempOutputFile << " if test $bool = 0; then" << endl;
222 tempOutputFile << " n=$i" << endl;
223 tempOutputFile << " bool=1" << endl;
224 tempOutputFile << " else" << endl;
225 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
226 tempOutputFile << " echo $n >> $LIBBATCH_NODEFILE" << endl;
227 tempOutputFile << " done" << endl;
228 tempOutputFile << " bool=0" << endl;
229 tempOutputFile << " fi" << endl;
230 tempOutputFile << "done" << endl;
231 tempOutputFile << "export LIBBATCH_NODEFILE" << endl;
233 // Launch the executable
234 tempOutputFile << "./" + fileNameToExecute;
235 if (params.find(ARGUMENTS) != params.end()) {
236 Versatile V = params[ARGUMENTS];
237 for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
238 StringType argt = * static_cast<StringType *>(*it);
240 tempOutputFile << " " << arg;
243 tempOutputFile << endl;
245 // Remove the node file
246 tempOutputFile << "rm $LIBBATCH_NODEFILE" << endl;
248 tempOutputFile.flush();
249 tempOutputFile.close();
251 LOG("Batch script file generated is: " << TmpFileName.c_str());
253 string remoteFileName = rootNameToExecute + "_Batch.sh";
254 int status = _protocol.copyFile(TmpFileName, "", "",
255 workDir + "/" + remoteFileName,
256 _hostname, _username);
258 throw RunTimeException("Error of connection on remote host");
259 return remoteFileName;
262 std::string BatchManager_LSF::getWallTime(const long edt)
269 oss << h << ":" << m;
271 oss << h << ":0" << m;
275 std::string BatchManager_LSF::getHomeDir(std::string tmpdir)
278 int idx = tmpdir.find("Batch/");
279 std::string filelogtemp = tmpdir.substr(idx+6, tmpdir.length());
280 filelogtemp = "/tmp/logs" + filelogtemp + "_home";
282 string subCommand = string("echo $HOME");
283 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
285 int status = system(command.c_str());
287 throw RunTimeException("Error of launching home command on remote host");
289 std::ifstream file_home(filelogtemp.c_str());
290 std::getline(file_home, home);