Salome HOME
Upgrade version to 2.4.3
[tools/libbatch.git] / src / OAR / BatchManager_OAR.cxx
1 //  Copyright (C) 2012-2015  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.
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
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);
165
166     // Create batch submit file
167     ofstream tempOutputFile;
168     string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
169
170     tempOutputFile << "#!/bin/sh -f" << endl;
171
172     int nb_full_nodes(0);
173     int nb_proc_on_last_node(0);
174
175     if (nbproc > 0)
176     {
177       nb_full_nodes = nbproc / nbprocpernode;
178       nb_proc_on_last_node = nbproc % nbprocpernode;
179
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)
182       {
183         nb_full_nodes += 1;
184         nb_proc_on_last_node = 0;
185       }
186     }
187
188     if (nb_full_nodes > 0)
189     {
190       tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
191       if (walltimeSecs > 0)
192       {
193         tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
194       }
195       else
196       {
197         tempOutputFile << endl;
198       }
199     }
200     else
201     {
202       if (walltimeSecs > 0)
203       {
204         tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
205       }
206     }
207
208     if (queue != "")
209     {
210       tempOutputFile << "#OAR -q " << queue << endl;
211     }
212
213     tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
214     tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log."  << rootNameToExecute << endl;
215
216     tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
217
218     // Launch the executable
219     tempOutputFile << "cd " << tmpDir << endl;
220     tempOutputFile << "./" + fileNameToExecute << endl;
221     tempOutputFile.flush();
222     tempOutputFile.close();
223
224     Utils::chmod(tmpFileName.c_str(), 0x1ED);
225     LOG("Batch script file generated is: " << tmpFileName);
226
227     string remoteFileName = rootNameToExecute + "_Batch.sh";
228     int status = _protocol.copyFile(tmpFileName, "", "",
229                                     workDir + "/" + remoteFileName,
230                                     _hostname, _username);
231     if (status)
232       throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
233
234     return remoteFileName;
235   }
236
237   string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
238   {
239     int h(seconds / 3600);
240     int m((seconds % 3600) / 60);
241     int s((seconds % 3600) % 60);
242
243     stringstream ss;
244     ss << h << ":" << m << ":" << s;
245
246     return ss.str();
247   }
248 }