Salome HOME
Update copyrights
[tools/libbatch.git] / src / OAR / BatchManager_OAR.cxx
1 //  Copyright (C) 2012-2014  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::submitJob(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                 // export input files on cluster
56                 exportInputFiles(job);
57
58                 // build batch script for job
59                 string scriptFile = buildBatchScript(job);
60
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);
64     command += " 2>&1";
65     LOG(command);
66
67     // submit job
68     string output;
69     int status = Utils::getCommandOutput(command, output);
70     LOG(output);
71     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
72
73                 // read id of submitted job in output
74                 istringstream logfile(output);
75                 string sline, idline, id;
76
77                 if (logfile)
78                 {
79                         while (getline(logfile, sline) && sline != "")
80                         {
81                                 idline = sline;
82                         }
83
84                         vector<string> tokens;
85                         JobInfo::Tokenize(idline, tokens, "=");
86                         id = tokens[1];
87                 }
88                 else
89                 {
90                         throw RunTimeException("Error in the submission of the job on the remote host");
91                 }
92
93                 JobId jobid(this, id);
94                 return jobid;
95         }
96
97         // retire un job du gestionnaire
98         void BatchManager_OAR::deleteJob(const JobId & jobid)
99         {
100     // define command to delete job
101     string subCommand = "oardel " + jobid.getReference();
102     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
103     LOG(command);
104
105     int status = system(command.c_str());
106     if (status)
107       throw RunTimeException("Can't delete job " + jobid.getReference());
108
109     LOG("job " << jobid.getReference() << " killed");
110         }
111
112         // Renvoie l'etat du job
113         JobInfo BatchManager_OAR::queryJob(const JobId & jobid)
114         {
115     // define command to query batch
116     string subCommand = "oarstat -fj " + jobid.getReference();
117     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
118     LOG(command);
119     string output;
120     int status = Utils::getCommandOutput(command, output);
121     if(status && status != 153 && status != 256*153)
122       throw RunTimeException("Error of connection on remote host");
123
124     JobInfo_OAR jobinfo = JobInfo_OAR(jobid.getReference(), output);
125     return jobinfo;
126         }
127
128         string BatchManager_OAR::buildBatchScript(const Job & job)
129         {
130                 Parametre params = job.getParametre();
131
132                 // Job Parameters
133                 string workDir       = "";
134                 string fileToExecute = "";
135                 string tmpDir = "";
136                 int nbproc               = 0;
137                 int mem              = 0;
138                 string queue         = "";
139
140     // Mandatory parameters
141     if (params.find(WORKDIR) != params.end())
142       workDir = params[WORKDIR].str();
143     else
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();
147     else
148       throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
149
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();
163
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);
168
169                 // Create batch submit file
170     ofstream tempOutputFile;
171     string tmpFileName = Utils::createAndOpenTemporaryFile("OAR-script", tempOutputFile);
172
173                 tempOutputFile << "#!/bin/sh -f" << endl;
174
175                 int nb_full_nodes(0);
176                 int nb_proc_on_last_node(0);
177
178                 if (nbproc > 0)
179                 {
180                         nb_full_nodes = nbproc / nbprocpernode;
181                         nb_proc_on_last_node = nbproc % nbprocpernode;
182
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)
185                         {
186                                 nb_full_nodes += 1;
187                                 nb_proc_on_last_node = 0;
188                         }
189                 }
190
191                 if (nb_full_nodes > 0)
192                 {
193                         tempOutputFile << "#OAR -l nodes=" << nb_full_nodes;
194                         if (walltimeSecs > 0)
195                         {
196                                 tempOutputFile << ",walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
197                         }
198                         else
199                         {
200                                 tempOutputFile << endl;
201                         }
202                 }
203                 else
204                 {
205                         if (walltimeSecs > 0)
206                         {
207                                 tempOutputFile << "#OAR -l walltime=" << convertSecTo_H_M_S(walltimeSecs) << endl;
208                         }
209                 }
210
211                 if (queue != "")
212                 {
213                         tempOutputFile << "#OAR -q " << queue << endl;
214                 }
215
216                 tempOutputFile << "#OAR -O " << tmpDir << "/logs/output.log." << rootNameToExecute << endl;
217                 tempOutputFile << "#OAR -E " << tmpDir << "/logs/error.log."  << rootNameToExecute << endl;
218
219                 tempOutputFile << "export LIBBATCH_NODEFILE=$OAR_NODEFILE" << endl;
220
221                 // Launch the executable
222                 tempOutputFile << "cd " << tmpDir << endl;
223                 tempOutputFile << "./" + fileNameToExecute << endl;
224                 tempOutputFile.flush();
225                 tempOutputFile.close();
226
227                 Utils::chmod(tmpFileName.c_str(), 0x1ED);
228                 LOG("Batch script file generated is: " << tmpFileName);
229
230     string remoteFileName = rootNameToExecute + "_Batch.sh";
231     int status = _protocol.copyFile(tmpFileName, "", "",
232                                     workDir + "/" + remoteFileName,
233                                     _hostname, _username);
234     if (status)
235       throw RunTimeException("Cannot copy batch submission file on host " + _hostname);
236
237     return remoteFileName;
238         }
239
240         string BatchManager_OAR::convertSecTo_H_M_S(long seconds) const
241         {
242                 int h(seconds / 3600);
243                 int m((seconds % 3600) / 60);
244                 int s((seconds % 3600) % 60);
245
246                 stringstream ss;
247                 ss << h << ":" << m << ":" << s;
248                 
249                 return ss.str();
250         }
251 }