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_eCCC.cxx : emulation of CCC client for CCRT machines
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2010
50 #include "Batch_Constants.hxx"
51 #include "Batch_BatchManager_eCCC.hxx"
52 #include "Batch_JobInfo_eCCC.hxx"
58 BatchManager_eCCC::BatchManager_eCCC(const FactBatchManager * parent, const char * host,
59 const char * username,
60 CommunicationProtocolType protocolType, const char * mpiImpl)
61 : BatchManager(parent, host),
62 BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
68 BatchManager_eCCC::~BatchManager_eCCC()
73 // Methode pour le controle des jobs : soumet un job au gestionnaire
74 const JobId BatchManager_eCCC::submitJob(const Job & job)
77 Parametre params = job.getParametre();
78 const std::string workDir = params[WORKDIR];
79 const string fileToExecute = params[EXECUTABLE];
80 string::size_type p1 = fileToExecute.find_last_of("/");
81 string::size_type p2 = fileToExecute.find_last_of(".");
82 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
84 // export input files on cluster
85 cerr << "Export des fichiers en entree" << endl;
86 exportInputFiles(job);
88 // build batch script for job
89 cerr << "Construction du script de batch" << endl;
90 buildBatchScript(job);
91 cerr << "Script envoye" << endl;
93 // define name of log file (local)
94 string logFile = generateTemporaryFileName("CCC-submitlog");
96 // define command to submit batch
97 string subCommand = string("bash -l -c \"cd ") + workDir + "; ccc_msub " + fileNameToExecute + "_Batch.sh\"";
98 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
102 cerr << command.c_str() << endl;
103 status = system(command.c_str());
106 ifstream error_message(logFile.c_str());
109 while(std::getline(error_message, temp))
111 error_message.close();
112 throw EmulationException("Error of connection on remote host, error was: " + mess);
115 // read id of submitted job in log file
116 ifstream idfile(logFile.c_str());
123 if (sidj.size() == 0)
124 throw EmulationException("Error in the submission of the job on the remote host");
126 JobId id(this, sidj);
130 // Ce manager permet de faire de la reprise
132 BatchManager_eCCC::addJob(const Batch::Job & job, const std::string reference)
134 return JobId(this, reference);
137 // Methode pour le controle des jobs : retire un job du gestionnaire
138 void BatchManager_eCCC::deleteJob(const JobId & jobid)
142 istringstream iss(jobid.getReference());
145 // define command to delete batch
146 string subCommand = string("bash -l -c \"ccc_mdel ") + iss.str() + string("\"");
147 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
148 cerr << command.c_str() << endl;
149 status = system(command.c_str());
151 throw EmulationException("Error of connection on remote host");
153 cerr << "jobId = " << ref << "killed" << endl;
156 // Methode pour le controle des jobs : suspend un job en file d'attente
157 void BatchManager_eCCC::holdJob(const JobId & jobid)
159 throw EmulationException("Not yet implemented");
162 // Methode pour le controle des jobs : relache un job suspendu
163 void BatchManager_eCCC::releaseJob(const JobId & jobid)
165 throw EmulationException("Not yet implemented");
169 // Methode pour le controle des jobs : modifie un job en file d'attente
170 void BatchManager_eCCC::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
172 throw EmulationException("Not yet implemented");
175 // Methode pour le controle des jobs : modifie un job en file d'attente
176 void BatchManager_eCCC::alterJob(const JobId & jobid, const Parametre & param)
178 alterJob(jobid, param, Environnement());
181 // Methode pour le controle des jobs : modifie un job en file d'attente
182 void BatchManager_eCCC::alterJob(const JobId & jobid, const Environnement & env)
184 alterJob(jobid, Parametre(), env);
187 // Methode pour le controle des jobs : renvoie l'etat du job
188 JobInfo BatchManager_eCCC::queryJob(const JobId & jobid)
191 istringstream iss(jobid.getReference());
194 // define name of log file (local)
195 string logFile = generateTemporaryFileName(string("CCC-querylog-id") + jobid.getReference());
197 // define command to query batch
198 string subCommand = string("bash -l -c \"bjobs ") + iss.str() + string("\"");
199 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
202 cerr << command.c_str() << endl;
203 int status = system(command.c_str());
205 throw EmulationException("Error of connection on remote host");
207 JobInfo_eCCC ji = JobInfo_eCCC(id,logFile);
213 // Methode pour le controle des jobs : teste si un job est present en machine
214 bool BatchManager_eCCC::isRunning(const JobId & jobid)
216 throw EmulationException("Not yet implemented");
219 void BatchManager_eCCC::buildBatchScript(const Job & job)
221 #ifndef WIN32 //TODO: need for porting on Windows
222 Parametre params = job.getParametre();
226 string fileToExecute = "";
232 // Mandatory parameters
233 if (params.find(WORKDIR) != params.end())
234 workDir = params[WORKDIR].str();
236 throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
237 if (params.find(EXECUTABLE) != params.end())
238 fileToExecute = params[EXECUTABLE].str();
240 throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
242 // Optional parameters
243 if (params.find(NBPROC) != params.end())
244 nbproc = params[NBPROC];
245 if (params.find(MAXWALLTIME) != params.end())
246 edt = (long)params[MAXWALLTIME] * 60;
247 if (params.find(MAXRAMSIZE) != params.end())
248 mem = params[MAXRAMSIZE];
249 if (params.find(QUEUE) != params.end())
250 queue = params[QUEUE].str();
252 string::size_type p1 = fileToExecute.find_last_of("/");
253 string::size_type p2 = fileToExecute.find_last_of(".");
254 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
255 string fileNameToExecute = fileToExecute.substr(p1+1);
257 // Create batch submit file
258 ofstream tempOutputFile;
259 std::string TmpFileName = createAndOpenTemporaryFile("LSF-script", tempOutputFile);
261 tempOutputFile << "#!/bin/bash" << endl ;
263 tempOutputFile << "#MSUB -q " << queue << endl;
265 tempOutputFile << "#MSUB -T " << edt << endl ;
267 tempOutputFile << "#MSUB -M " << mem << endl ;
268 tempOutputFile << "#MSUB -n " << nbproc << endl ;
269 size_t pos = workDir.find("$HOME");
271 if( pos != string::npos )
272 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
274 pos = workDir.find("~");
275 if( pos != string::npos )
276 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
280 tempOutputFile << "#MSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
281 tempOutputFile << "#MSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
283 tempOutputFile << "cd " << workDir << endl ;
285 // generate nodes file
286 tempOutputFile << "bool=0" << endl;
287 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
288 tempOutputFile << " if test $bool = 0; then" << endl;
289 tempOutputFile << " n=$i" << endl;
290 tempOutputFile << " bool=1" << endl;
291 tempOutputFile << " else" << endl;
292 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
293 tempOutputFile << " echo $n >> nodesFile." << rootNameToExecute << endl;
294 tempOutputFile << " done" << endl;
295 tempOutputFile << " bool=0" << endl;
296 tempOutputFile << " fi" << endl;
297 tempOutputFile << "done" << endl;
299 // Abstraction of PBS_NODEFILE - TODO
300 tempOutputFile << "export LIBBATCH_NODEFILE=nodesFile." << rootNameToExecute << endl;
302 // Allow resource sharing in CCRT nodes
303 tempOutputFile << "export OMPI_MCA_orte_process_binding=none" << endl;
305 // Launch the executable
306 tempOutputFile << "./" + fileNameToExecute << endl;
307 tempOutputFile.flush();
308 tempOutputFile.close();
310 BATCH_CHMOD(TmpFileName.c_str(), 0x1ED);
311 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
313 int status = _protocol.copyFile(TmpFileName, "", "",
314 workDir + "/" + rootNameToExecute + "_Batch.sh",
315 _hostname, _username);
317 throw EmulationException("Error of connection on remote host");
323 std::string BatchManager_eCCC::getHomeDir(std::string tmpdir)
326 std::string filelogtemp = generateTemporaryFileName("gethomedir");
328 string subCommand = string("echo ");
329 subCommand += tmpdir;
330 string command = _protocol.getExecCommand(subCommand, _hostname, _username) + " > " + filelogtemp;
331 cerr << command.c_str() << endl;
332 int status = system(command.c_str());
334 throw EmulationException("Error of launching home command on remote host");
336 std::ifstream file_home(filelogtemp.c_str());
337 std::getline(file_home, home);