1 // Copyright (C) 2012-2020 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, or (at your option) any later version.
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::runJob(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 // build batch script for job
56 string scriptFile = buildBatchScript(job);
58 // define command to submit batch
59 string subCommand = string("oarsub -t allow_classic_ssh -d ") + workDir + " -S " + workDir + "/" + scriptFile;
60 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
66 int status = Utils::getCommandOutput(command, output);
68 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
70 // read id of submitted job in output
71 istringstream logfile(output);
72 string sline, idline, id;
76 while (getline(logfile, sline) && sline != "")
81 vector<string> tokens;
82 JobInfo::Tokenize(idline, tokens, "=");
87 throw RunTimeException("Error in the submission of the job on the remote host");
90 JobId jobid(this, id);
94 // retire un job du gestionnaire
95 void BatchManager_OAR::deleteJob(const JobId & jobid)
97 // define command to delete job
98 string subCommand = "oardel " + jobid.getReference();
99 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
102 int status = system(command.c_str());
104 throw RunTimeException("Can't delete job " + jobid.getReference());
106 LOG("job " << jobid.getReference() << " killed");
109 // Renvoie l'etat du job
110 JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
112 // define command to query batch
113 string subCommand = "oarstat -fj " + jobid.getReference();
114 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
117 int status = Utils::getCommandOutput(command, output);
118 if(status && status != 153 && status != 256*153)
119 throw RunTimeException("Error of connection on remote host");
121 JobInfo_OAR jobinfo = JobInfo_OAR(jobid.getReference(), output);
125 string BatchManager_OAR::buildBatchScript(const Job & job)
127 Parametre params = job.getParametre();
131 string fileToExecute = "";
137 // Mandatory parameters
138 if (params.find(WORKDIR) != params.end())
139 workDir = params[WORKDIR].str();
141 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
142 if (params.find(EXECUTABLE) != params.end())
143 fileToExecute = params[EXECUTABLE].str();
145 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
147 // Optional parameters
148 if (params.find(NBPROC) != params.end())
149 nbproc = params[NBPROC];
150 int nbprocpernode = 1;
151 if (params.find(NBPROCPERNODE) != params.end())
152 nbprocpernode = params[NBPROCPERNODE];
153 long walltimeSecs = 0;
154 if (params.find(MAXWALLTIME) != params.end())
155 walltimeSecs = (long)params[MAXWALLTIME] * 60;
156 if (params.find(MAXRAMSIZE) != params.end())
157 mem = params[MAXRAMSIZE];
158 if (params.find(QUEUE) != params.end())
159 queue = params[QUEUE].str();
161 string::size_type p1 = fileToExecute.find_last_of("/");
162 string::size_type p2 = fileToExecute.find_last_of(".");
163 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
164 string fileNameToExecute = fileToExecute.substr(p1+1);
166 // Create batch submit file
167 ofstream tempOutputFile;
168 string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
170 tempOutputFile << "#!/bin/sh -f" << endl;
172 int nb_full_nodes(0);
173 int nb_proc_on_last_node(0);
177 nb_full_nodes = nbproc / nbprocpernode;
178 nb_proc_on_last_node = nbproc % nbprocpernode;
180 // In exclusive mode, we reserve all procs on the nodes
181 if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0)
184 nb_proc_on_last_node = 0;
188 if (nb_full_nodes > 0)
190 tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
191 if (walltimeSecs > 0)
193 tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
197 tempOutputFile << endl;
202 if (walltimeSecs > 0)
204 tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
210 tempOutputFile << "#OAR -q " << queue << endl;
213 tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
214 tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log." << rootNameToExecute << endl;
216 tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
218 // Launch the executable
219 tempOutputFile << "cd " << tmpDir << endl;
220 tempOutputFile << "./" + fileNameToExecute << endl;
221 tempOutputFile.flush();
222 tempOutputFile.close();
224 Utils::chmod(tmpFileName.c_str(), 0x1ED);
225 LOG("Batch script file generated is: " << tmpFileName);
227 string remoteFileName = rootNameToExecute + "_Batch.sh";
228 int status = _protocol.copyFile(tmpFileName, "", "",
229 workDir + "/" + remoteFileName,
230 _hostname, _username);
232 throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
234 return remoteFileName;
237 string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
239 int h(seconds / 3600);
240 int m((seconds % 3600) / 60);
241 int s((seconds % 3600) % 60);
244 ss << h << ":" << m << ":" << s;