Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / OAR / BatchManager_OAR.cxx
1 // Copyright (C) 2012-2021  INRIA
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <cstdlib>
21 #include <fstream>
22
23 #include <Constants.hxx>
24 #include <Utils.hxx>
25 #include <Log.hxx>
26
27 #include "BatchManager_OAR.hxx"
28 #include "JobInfo_OAR.hxx"
29
30 using namespace std;
31
32 namespace Batch
33 {
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)
38   {
39   }
40
41   BatchManager_OAR::~BatchManager_OAR()
42   {
43   }
44
45   // Soumet un job au gestionnaire
46   const JobId BatchManager_OAR::runJob(const Job & job)
47   {
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);
54
55     // build batch script for job
56     string scriptFile = buildBatchScript(job);
57
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);
61     command += " 2>&1";
62     LOG(command);
63
64     // submit job
65     string output;
66     int status = Utils::getCommandOutput(command, output);
67     LOG(output);
68     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
69
70     // read id of submitted job in output
71     istringstream logfile(output);
72     string sline, idline, id;
73
74     if (logfile)
75     {
76       while (getline(logfile, sline) && sline != "")
77       {
78         idline = sline;
79       }
80
81       vector<string> tokens;
82       JobInfo::Tokenize(idline, tokens, "=");
83       id = tokens[1];
84     }
85     else
86     {
87       throw RunTimeException("Error in the submission of the job on the remote host");
88     }
89
90     JobId jobid(this, id);
91     return jobid;
92   }
93
94   // retire un job du gestionnaire
95   void BatchManager_OAR::deleteJob(const JobId & jobid)
96   {
97     // define command to delete job
98     string subCommand = "oardel " + jobid.getReference();
99     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
100     LOG(command);
101
102     int status = system(command.c_str());
103     if (status)
104       throw RunTimeException("Can't delete job " + jobid.getReference());
105
106     LOG("job " << jobid.getReference() << " killed");
107   }
108
109   // Renvoie l'etat du job
110   JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
111   {
112     // define command to query batch
113     string subCommand = "oarstat -fj " + jobid.getReference();
114     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
115     LOG(command);
116     string output;
117     int status = Utils::getCommandOutput(command, output);
118     if(status && status != 153 && status != 256*153)
119       throw RunTimeException("Error of connection on remote host");
120
121     JobInfo_OAR jobinfo = JobInfo_OAR(jobid.getReference(), output);
122     return jobinfo;
123   }
124
125   string BatchManager_OAR::buildBatchScript(const Job & job)
126   {
127     Parametre params = job.getParametre();
128
129     // Job Parameters
130     string workDir       = "";
131     string fileToExecute = "";
132     string tmpDir = "";
133     int nbproc           = 0;
134     int mem              = 0;
135     string queue         = "";
136
137     // Mandatory parameters
138     if (params.find(WORKDIR) != params.end())
139       workDir = params[WORKDIR].str();
140     else
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();
144     else
145       throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
146
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();
160     LIBBATCH_UNUSED(mem);
161
162     string::size_type p1 = fileToExecute.find_last_of("/");
163     string::size_type p2 = fileToExecute.find_last_of(".");
164     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
165     string fileNameToExecute = fileToExecute.substr(p1+1);
166
167     // Create batch submit file
168     ofstream tempOutputFile;
169     string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
170
171     tempOutputFile << "#!/bin/sh -f" << endl;
172
173     int nb_full_nodes(0);
174     int nb_proc_on_last_node(0);
175
176     if (nbproc > 0)
177     {
178       nb_full_nodes = nbproc / nbprocpernode;
179       nb_proc_on_last_node = nbproc % nbprocpernode;
180
181       // In exclusive mode, we reserve all procs on the nodes
182       if (params.find(EXCLUSIVE) != params.end() && params[EXCLUSIVE] && nb_proc_on_last_node > 0)
183       {
184         nb_full_nodes += 1;
185         nb_proc_on_last_node = 0;
186       }
187     }
188
189     if (nb_full_nodes > 0)
190     {
191       tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
192       if (walltimeSecs > 0)
193       {
194         tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
195       }
196       else
197       {
198         tempOutputFile << endl;
199       }
200     }
201     else
202     {
203       if (walltimeSecs > 0)
204       {
205         tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
206       }
207     }
208
209     if (queue != "")
210     {
211       tempOutputFile << "#OAR -q " << queue << endl;
212     }
213
214     tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
215     tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log."  << rootNameToExecute << endl;
216
217     tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
218
219     // Launch the executable
220     tempOutputFile << "cd " << tmpDir << endl;
221     tempOutputFile << "./" + fileNameToExecute << endl;
222     tempOutputFile.flush();
223     tempOutputFile.close();
224
225     Utils::chmod(tmpFileName.c_str(), 0x1ED);
226     LOG("Batch script file generated is: " << tmpFileName);
227
228     string remoteFileName = rootNameToExecute + "_Batch.sh";
229     int status = _protocol.copyFile(tmpFileName, "", "",
230                                     workDir + "/" + remoteFileName,
231                                     _hostname, _username);
232     if (status)
233       throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
234
235     return remoteFileName;
236   }
237
238   string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
239   {
240     int h(seconds / 3600);
241     int m((seconds % 3600) / 60);
242     int s((seconds % 3600) % 60);
243
244     stringstream ss;
245     ss << h << ":" << m << ":" << s;
246
247     return ss.str();
248   }
249 }