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_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::runJob(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 // build batch script for job
86 LOG("Construction du script de batch");
87 buildBatchScript(job);
90 // define command to submit batch
91 string subCommand = string("bash -l -c \\\"cd ") + workDir + "; ccc_msub " + fileNameToExecute + "_Batch.sh\\\"";
92 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
98 int status = Utils::getCommandOutput(command, output);
100 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
102 // find id of submitted job in output
103 istringstream idfile(output);
109 if (sidj.size() == 0)
110 throw RunTimeException("Error in the submission of the job on the remote host");
112 JobId id(this, sidj);
116 // Methode pour le controle des jobs : retire un job du gestionnaire
117 void BatchManager_CCC::deleteJob(const JobId & jobid)
121 istringstream iss(jobid.getReference());
124 // define command to delete batch
125 string subCommand = string("bash -l -c \\\"ccc_mdel ") + iss.str() + string("\\\"");
126 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
128 status = system(command.c_str());
130 throw RunTimeException("Error of connection on remote host");
132 LOG("jobId = " << ref << "killed");
135 // Methode pour le controle des jobs : renvoie l'etat du job
136 JobInfo BatchManager_CCC::queryJob(const JobId & jobid)
139 istringstream iss(jobid.getReference());
142 // define command to query batch
143 string subCommand = string("bash -l -c \\\"bjobs ") + iss.str() + string("\\\"");
144 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
148 int status = Utils::getCommandOutput(command, output);
150 throw RunTimeException("Error of connection on remote host");
152 JobInfo_CCC ji = JobInfo_CCC(id, output);
158 // Methode pour le controle des jobs : teste si un job est present en machine
159 bool BatchManager_CCC::isRunning(const JobId & /*jobid*/)
161 throw NotYetImplementedException("BatchManager_CCC::isRunning");
164 void BatchManager_CCC::buildBatchScript(const Job & job)
166 #ifndef WIN32 //TODO: need for porting on Windows
167 Parametre params = job.getParametre();
171 string fileToExecute = "";
177 // Mandatory parameters
178 if (params.find(WORKDIR) != params.end())
179 workDir = params[WORKDIR].str();
181 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
182 if (params.find(EXECUTABLE) != params.end())
183 fileToExecute = params[EXECUTABLE].str();
185 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
187 // Optional parameters
188 if (params.find(NBPROC) != params.end())
189 nbproc = params[NBPROC];
190 if (params.find(MAXWALLTIME) != params.end())
191 edt = (long)params[MAXWALLTIME] * 60;
192 if (params.find(MAXRAMSIZE) != params.end())
193 mem = params[MAXRAMSIZE];
194 if (params.find(QUEUE) != params.end())
195 queue = params[QUEUE].str();
197 string::size_type p1 = fileToExecute.find_last_of("/");
198 string::size_type p2 = fileToExecute.find_last_of(".");
199 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
200 string fileNameToExecute = fileToExecute.substr(p1+1);
202 // Create batch submit file
203 ofstream tempOutputFile;
204 std::string TmpFileName = Utils::createAndOpenTemporaryFile("LSF-script", tempOutputFile);
206 tempOutputFile << "#!/bin/bash" << endl ;
208 tempOutputFile << "#MSUB -q " << queue << endl;
210 tempOutputFile << "#MSUB -T " << edt << endl ;
212 tempOutputFile << "#MSUB -M " << mem << endl ;
213 tempOutputFile << "#MSUB -n " << nbproc << endl ;
214 size_t pos = workDir.find("$HOME");
216 if( pos != string::npos )
217 baseDir = getHomeDir(workDir) + workDir.substr(pos+5,workDir.length()-5);
219 pos = workDir.find("~");
220 if( pos != string::npos )
221 baseDir = getHomeDir(workDir) + workDir.substr(pos+1,workDir.length()-1);
225 tempOutputFile << "#MSUB -o " << baseDir << "/logs/output.log." << rootNameToExecute << endl ;
226 tempOutputFile << "#MSUB -e " << baseDir << "/logs/error.log." << rootNameToExecute << endl ;
228 if (params.find(EXTRAPARAMS) != params.end())
229 tempOutputFile << params[EXTRAPARAMS] << endl;
231 tempOutputFile << "cd " << workDir << endl ;
233 // generate nodes file
234 tempOutputFile << "bool=0" << endl;
235 tempOutputFile << "for i in $LSB_MCPU_HOSTS; do" << endl;
236 tempOutputFile << " if test $bool = 0; then" << endl;
237 tempOutputFile << " n=$i" << endl;
238 tempOutputFile << " bool=1" << endl;
239 tempOutputFile << " else" << endl;
240 tempOutputFile << " for ((j=0;j<$i;j++)); do" << endl;
241 tempOutputFile << " echo $n >> nodesFile." << rootNameToExecute << endl;
242 tempOutputFile << " done" << endl;
243 tempOutputFile << " bool=0" << endl;
244 tempOutputFile << " fi" << endl;
245 tempOutputFile << "done" << endl;
247 // Abstraction of PBS_NODEFILE - TODO
248 tempOutputFile << "export LIBBATCH_NODEFILE=nodesFile." << rootNameToExecute << endl;
250 // Allow resource sharing in CCRT nodes
251 tempOutputFile << "export OMPI_MCA_orte_process_binding=none" << endl;
253 // Launch the executable
254 tempOutputFile << "./" + fileNameToExecute << endl;
255 tempOutputFile.flush();
256 tempOutputFile.close();
258 Utils::chmod(TmpFileName.c_str(), 0x1ED);
259 LOG("Batch script file generated is: " << TmpFileName.c_str());
261 int status = _protocol.copyFile(TmpFileName, "", "",
262 workDir + "/" + rootNameToExecute + "_Batch.sh",
263 _hostname, _username);
265 throw RunTimeException("Error of connection on remote host");
271 std::string BatchManager_CCC::getHomeDir(std::string tmpdir)
275 string subCommand = string("echo ");
276 subCommand += tmpdir;
277 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
281 int status = Utils::getCommandOutput(command, output);
284 throw RunTimeException("Error of launching home command on remote host");
286 std::istringstream file_home(output);
287 std::getline(file_home, home);