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_CCC.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
49 #include <Constants.hxx>
50 #include <NotYetImplementedException.hxx>
53 #include "BatchManager_CCC.hxx"
54 #include "JobInfo_CCC.hxx"
61 BatchManager_CCC::BatchManager_CCC(const FactBatchManager * parent, const char * host,
62 const char * username,
63 CommunicationProtocolType protocolType, const char * mpiImpl)
64 : BatchManager(parent, host, username, protocolType, mpiImpl)
70 BatchManager_CCC::~BatchManager_CCC()
75 // Methode pour le controle des jobs : soumet un job au gestionnaire
76 const JobId BatchManager_CCC::submitJob(const Job & job)
78 Parametre params = job.getParametre();
79 const std::string workDir = params[WORKDIR];
80 const string fileToExecute = params[EXECUTABLE];
81 string::size_type p1 = fileToExecute.find_last_of("/");
82 string::size_type p2 = fileToExecute.find_last_of(".");
83 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
85 // export input files on cluster
86 LOG("Export des fichiers en entree");
87 exportInputFiles(job);
89 // build batch script for job
90 LOG("Construction du script de batch");
91 buildBatchScript(job);
94 // define command to submit batch
95 string subCommand = string("bash -l -c \\\"cd ") + workDir + "; ccc_msub " + fileNameToExecute + "_Batch.sh\\\"";
96 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
102 int status = Utils::getCommandOutput(command, output);
104 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
106 // find id of submitted job in output
107 istringstream idfile(output);
113 if (sidj.size() == 0)
114 throw RunTimeException("Error in the submission of the job on the remote host");
116 JobId id(this, sidj);
120 // Methode pour le controle des jobs : retire un job du gestionnaire
121 void BatchManager_CCC::deleteJob(const JobId & jobid)
125 istringstream iss(jobid.getReference());
128 // define command to delete batch
129 string subCommand = string("bash -l -c \\\"ccc_mdel ") + iss.str() + string("\\\"");
130 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
132 status = system(command.c_str());
134 throw RunTimeException("Error of connection on remote host");
136 LOG("jobId = " << ref << "killed");
139 // Methode pour le controle des jobs : renvoie l'etat du job
140 JobInfo BatchManager_CCC::queryJob(const JobId & jobid)
143 istringstream iss(jobid.getReference());
146 // define command to query batch
147 string subCommand = string("bash -l -c \\\"bjobs ") + iss.str() + string("\\\"");
148 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
152 int status = Utils::getCommandOutput(command, output);
154 throw RunTimeException("Error of connection on remote host");
156 JobInfo_CCC ji = JobInfo_CCC(id, output);
162 // Methode pour le controle des jobs : teste si un job est present en machine
163 bool BatchManager_CCC::isRunning(const JobId & jobid)
165 throw NotYetImplementedException("BatchManager_CCC::isRunning");
168 void BatchManager_CCC::buildBatchScript(const Job & job)
170 #ifndef WIN32 //TODO: need for porting on Windows
171 Parametre params = job.getParametre();
175 string fileToExecute = "";
181 // Mandatory parameters
182 if (params.find(WORKDIR) != params.end())
183 workDir = params[WORKDIR].str();
185 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
186 if (params.find(EXECUTABLE) != params.end())
187 fileToExecute = params[EXECUTABLE].str();
189 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
191 // Optional parameters
192 if (params.find(NBPROC) != params.end())
193 nbproc = params[NBPROC];
194 if (params.find(MAXWALLTIME) != params.end())
195 edt = (long)params[MAXWALLTIME] * 60;
196 if (params.find(MAXRAMSIZE) != params.end())
197 mem = params[MAXRAMSIZE];
198 if (params.find(QUEUE) != params.end())
199 queue = params[QUEUE].str();
201 string::size_type p1 = fileToExecute.find_last_of("/");
202 string::size_type p2 = fileToExecute.find_last_of(".");
203 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
204 string fileNameToExecute = fileToExecute.substr(p1+1);
206 // Create batch submit file
207 ofstream tempOutputFile;
208 std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
210 tempOutputFile << "#!/bin/bash" << endl ;
212 tempOutputFile << "#MSUB -q " << queue << endl;
214 tempOutputFile << "#MSUB -T " << edt << endl ;
216 tempOutputFile << "#MSUB -M " << mem << endl ;
217 tempOutputFile << "#MSUB -n " << nbproc << endl ;
218 size_t pos = workDir.find("$HOME");
220 if( pos != string::npos )
221 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
223 pos = workDir.find("~");
224 if( pos != string::npos )
225 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
229 tempOutputFile << "#MSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
230 tempOutputFile << "#MSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
232 tempOutputFile << "cd " << workDir << endl ;
234 // generate nodes file
235 tempOutputFile << "bool=0" << endl;
236 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
237 tempOutputFile << " if test $bool = 0; then" << endl;
238 tempOutputFile << " n=$i" << endl;
239 tempOutputFile << " bool=1" << endl;
240 tempOutputFile << " else" << endl;
241 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
242 tempOutputFile << " echo $n >> nodesFile." << rootNameToExecute << endl;
243 tempOutputFile << " done" << endl;
244 tempOutputFile << " bool=0" << endl;
245 tempOutputFile << " fi" << endl;
246 tempOutputFile << "done" << endl;
248 // Abstraction of PBS_NODEFILE - TODO
249 tempOutputFile << "export LIBBATCH_NODEFILE=nodesFile." << rootNameToExecute << endl;
251 // Allow resource sharing in CCRT nodes
252 tempOutputFile << "export OMPI_MCA_orte_process_binding=none" << endl;
254 // Launch the executable
255 tempOutputFile << "./" + fileNameToExecute << endl;
256 tempOutputFile.flush();
257 tempOutputFile.close();
259 Utils::chmod(TmpFileName.c_str(), 0x1ED);
260 LOG("Batch script file generated is: " << TmpFileName.c_str());
262 int status = _protocol.copyFile(TmpFileName, "", "",
263 workDir + "/" + rootNameToExecute + "_Batch.sh",
264 _hostname, _username);
266 throw RunTimeException("Error of connection on remote host");
272 std::string BatchManager_CCC::getHomeDir(std::string tmpdir)
276 string subCommand = string("echo ");
277 subCommand += tmpdir;
278 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
282 int status = Utils::getCommandOutput(command, output);
285 throw RunTimeException("Error of launching home command on remote host");
287 std::istringstream file_home(output);
288 std::getline(file_home, home);