1 // Copyright (C) 2012-2014 INRIA
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <Constants.hxx>
27 #include "BatchManager_OAR.hxx"
28 #include "JobInfo_OAR.hxx"
34 BatchManager_OAR::BatchManager_OAR(const FactBatchManager * parent, const char * host,
35 const char * username,
36 CommunicationProtocolType protocolType, const char * mpiImpl)
37 : BatchManager(parent, host, username, protocolType, mpiImpl)
41 BatchManager_OAR::~BatchManager_OAR()
45 // Soumet un job au gestionnaire
46 const JobId BatchManager_OAR::submitJob(const Job & job)
48 Parametre params = job.getParametre();
49 const string workDir = params[WORKDIR];
50 const string fileToExecute = params[EXECUTABLE];
51 string::size_type p1 = fileToExecute.find_last_of("/");
52 string::size_type p2 = fileToExecute.find_last_of(".");
53 std::string fileNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
55 // export input files on cluster
56 exportInputFiles(job);
58 // build batch script for job
59 string scriptFile = buildBatchScript(job);
61 // define command to submit batch
62 string subCommand = string("oarsub -t allow_classic_ssh -d ") + workDir + " -S " + workDir + "/" + scriptFile;
63 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
69 int status = Utils::getCommandOutput(command, output);
71 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
73 // read id of submitted job in output
74 istringstream logfile(output);
75 string sline, idline, id;
79 while (getline(logfile, sline) && sline != "")
84 vector<string> tokens;
85 JobInfo::Tokenize(idline, tokens, "=");
90 throw RunTimeException("Error in the submission of the job on the remote host");
93 JobId jobid(this, id);
97 // retire un job du gestionnaire
98 void BatchManager_OAR::deleteJob(const JobId & jobid)
100 // define command to delete job
101 string subCommand = "oardel " + jobid.getReference();
102 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
105 int status = system(command.c_str());
107 throw RunTimeException("Can't delete job " + jobid.getReference());
109 LOG("job " << jobid.getReference() << " killed");
112 // Renvoie l'etat du job
113 JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
115 // define command to query batch
116 string subCommand = "oarstat -fj " + jobid.getReference();
117 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
120 int status = Utils::getCommandOutput(command, output);
121 if(status && status != 153 && status != 256*153)
122 throw RunTimeException("Error of connection on remote host");
124 JobInfo_OAR jobinfo = JobInfo_OAR(jobid.getReference(), output);
128 string BatchManager_OAR::buildBatchScript(const Job & job)
130 Parametre params = job.getParametre();
134 string fileToExecute = "";
140 // Mandatory parameters
141 if (params.find(WORKDIR) != params.end())
142 workDir = params[WORKDIR].str();
144 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
145 if (params.find(EXECUTABLE) != params.end())
146 fileToExecute = params[EXECUTABLE].str();
148 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
150 // Optional parameters
151 if (params.find(NBPROC) != params.end())
152 nbproc = params[NBPROC];
153 int nbprocpernode = 1;
154 if (params.find(NBPROCPERNODE) != params.end())
155 nbprocpernode = params[NBPROCPERNODE];
156 long walltimeSecs = 0;
157 if (params.find(MAXWALLTIME) != params.end())
158 walltimeSecs = (long)params[MAXWALLTIME] * 60;
159 if (params.find(MAXRAMSIZE) != params.end())
160 mem = params[MAXRAMSIZE];
161 if (params.find(QUEUE) != params.end())
162 queue = params[QUEUE].str();
164 string::size_type p1 = fileToExecute.find_last_of("/");
165 string::size_type p2 = fileToExecute.find_last_of(".");
166 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
167 string fileNameToExecute = fileToExecute.substr(p1+1);
169 // Create batch submit file
170 ofstream tempOutputFile;
171 string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
173 tempOutputFile << "#!/bin/sh -f" << endl;
175 int nb_full_nodes(0);
176 int nb_proc_on_last_node(0);
180 nb_full_nodes = nbproc / nbprocpernode;
181 nb_proc_on_last_node = nbproc % nbprocpernode;
183 // In exclusive mode, we reserve all procs on the nodes
184 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0)
187 nb_proc_on_last_node = 0;
191 if (nb_full_nodes > 0)
193 tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
194 if (walltimeSecs > 0)
196 tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
200 tempOutputFile << endl;
205 if (walltimeSecs > 0)
207 tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
213 tempOutputFile << "#OAR -q " << queue << endl;
216 tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
217 tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log." << rootNameToExecute << endl;
219 tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
221 // Launch the executable
222 tempOutputFile << "cd " << tmpDir << endl;
223 tempOutputFile << "./" + fileNameToExecute << endl;
224 tempOutputFile.flush();
225 tempOutputFile.close();
227 Utils::chmod(tmpFileName.c_str(), 0x1ED);
228 LOG("Batch script file generated is: " << tmpFileName);
230 string remoteFileName = rootNameToExecute + "_Batch.sh";
231 int status = _protocol.copyFile(tmpFileName, "", "",
232 workDir + "/" + remoteFileName,
233 _hostname, _username);
235 throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
237 return remoteFileName;
240 string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
242 int h(seconds / 3600);
243 int m((seconds % 3600) / 60);
244 int s((seconds % 3600) % 60);
247 ss << h << ":" << m << ":" << s;