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_SGE.cxx : emulation of SGE client
25 * Auteur : Bernard SECHER - CEA DEN
26 * Mail : mailto:bernard.secher@cea.fr
27 * Date : Thu Apr 24 10:17:22 2008
48 #include <Constants.hxx>
50 #include <NotYetImplementedException.hxx>
52 #include "BatchManager_SGE.hxx"
53 #include "JobInfo_SGE.hxx"
59 BatchManager_SGE::BatchManager_SGE(const FactBatchManager * parent, const char * host,
60 const char * username,
61 CommunicationProtocolType protocolType, const char * mpiImpl)
62 : BatchManager(parent, host, username, protocolType, mpiImpl)
68 BatchManager_SGE::~BatchManager_SGE()
73 // Methode pour le controle des jobs : soumet un job au gestionnaire
74 const JobId BatchManager_SGE::submitJob(const Job & job)
76 Parametre params = job.getParametre();
77 const std::string workDir = params[WORKDIR];
78 const string fileToExecute = params[EXECUTABLE];
79 string::size_type p1 = fileToExecute.find_last_of("/");
80 string::size_type p2 = fileToExecute.find_last_of(".");
81 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
83 // export input files on cluster
84 exportInputFiles(job);
86 // build batch script for job
87 buildBatchScript(job);
89 // define command to submit batch
90 string subCommand = string("bash -l -c \\\"cd ") + workDir + "; qsub " + fileNameToExecute + "_Batch.sh\\\"";
91 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
93 cerr << command.c_str() << endl;
97 int status = Utils::getCommandOutput(command, output);
99 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
101 // find id of submitted job in output
103 istringstream iss(output);
104 iss >> strjob >> strjob >> strjob;
106 JobId id(this, strjob);
111 // Methode pour le controle des jobs : retire un job du gestionnaire
112 void BatchManager_SGE::deleteJob(const JobId & jobid)
116 istringstream iss(jobid.getReference());
119 // define command to delete batch
120 string subCommand = string("bash -l -c \\\"qdel ") + iss.str() + string("\\\"");
121 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
122 cerr << command.c_str() << endl;
123 status = system(command.c_str());
125 throw RunTimeException("Error of connection on remote host");
127 cerr << "jobId = " << ref << "killed" << endl;
130 // Methode pour le controle des jobs : renvoie l'etat du job
131 JobInfo BatchManager_SGE::queryJob(const JobId & jobid)
134 istringstream iss(jobid.getReference());
137 // define command to query batch
138 string subCommand = string("bash -l -c \\\"qstat | grep ") + iss.str() + string("\\\"");
139 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
140 cerr << command.c_str() << endl;
143 int status = Utils::getCommandOutput(command, output);
144 if (status && status != 256)
145 throw RunTimeException("Error of connection on remote host");
147 JobInfo_SGE ji = JobInfo_SGE(id, output);
151 // Methode pour le controle des jobs : teste si un job est present en machine
152 bool BatchManager_SGE::isRunning(const JobId & jobid)
154 throw NotYetImplementedException("BatchManager_SGE::isRunning");
157 void BatchManager_SGE::buildBatchScript(const Job & job)
160 //TODO porting on Win32 platform
161 std::cerr << "BuildBatchScript" << std::endl;
162 Parametre params = job.getParametre();
166 string fileToExecute = "";
172 // Mandatory parameters
173 if (params.find(WORKDIR) != params.end())
174 workDir = params[WORKDIR].str();
176 throw RunTimeException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
177 if (params.find(EXECUTABLE) != params.end())
178 fileToExecute = params[EXECUTABLE].str();
180 throw RunTimeException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
182 // Optional parameters
183 if (params.find(NBPROC) != params.end())
184 nbproc = params[NBPROC];
185 if (params.find(MAXWALLTIME) != params.end())
186 edt = params[MAXWALLTIME];
187 if (params.find(MAXRAMSIZE) != params.end())
188 mem = params[MAXRAMSIZE];
189 if (params.find(QUEUE) != params.end())
190 queue = params[QUEUE].str();
192 string::size_type p1 = fileToExecute.find_last_of("/");
193 string::size_type p2 = fileToExecute.find_last_of(".");
194 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
195 string fileNameToExecute = fileToExecute.substr(p1+1);
197 // Create batch submit file
198 ofstream tempOutputFile;
199 std::string TmpFileName = Utils::createAndOpenTemporaryFile("SGE-script", tempOutputFile);
201 tempOutputFile << "#! /bin/sh -f" << endl;
203 tempOutputFile << "#$ -q " << queue << endl;
204 tempOutputFile << "#$ -pe " << _mpiImpl->name() << " " << nbproc << endl;
206 tempOutputFile << "#$ -l h_rt=" << getWallTime(edt) << endl ;
208 tempOutputFile << "#$ -l h_vmem=" << mem << "M" << endl ;
209 tempOutputFile << "#$ -o " << workDir << "/logs/output.log." << rootNameToExecute << endl ;
210 tempOutputFile << "#$ -e " << workDir << "/logs/error.log." << rootNameToExecute << endl ;
212 // Abstraction of PBS_NODEFILE - TODO
213 tempOutputFile << "export LIBBATCH_NODEFILE=$TMPDIR/machines" << endl;
215 // Launch the executable
216 tempOutputFile << "cd " << workDir << endl ;
217 tempOutputFile << "./" + fileNameToExecute << endl;
218 tempOutputFile.flush();
219 tempOutputFile.close();
221 Utils::chmod(TmpFileName.c_str(), 0x1ED);
222 cerr << "Batch script file generated is: " << TmpFileName.c_str() << endl;
224 int status = _protocol.copyFile(TmpFileName, "", "",
225 workDir + "/" + rootNameToExecute + "_Batch.sh",
226 _hostname, _username);
228 throw RunTimeException("Error of connection on remote host");
233 std::string BatchManager_SGE::getWallTime(const long edt)
240 oss << h << ":" << m;
242 oss << h << ":0" << m;
243 oss << ":00"; // the seconds