Salome HOME
fc729bf3bb121ffd699bb0f60a4c32c40a86d9ff
[tools/libbatch.git] / src / LoadLeveler / Batch_BatchManager_eLL.cxx
1 //  Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  *  Batch_BatchManager_eLL.cxx :
24  *
25  *  Created on: 25 nov. 2010
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <cstdlib>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33
34 #include <Batch_NotYetImplementedException.hxx>
35 #include <Batch_Constants.hxx>
36 #include <Batch_Utils.hxx>
37
38 #include "Batch_FactBatchManager_eLL.hxx"
39 #include "Batch_BatchManager_eLL.hxx"
40 #include "Batch_JobInfo_eLL.hxx"
41
42 using namespace std;
43
44 namespace Batch {
45
46   BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
47                                      const char * username,
48                                      CommunicationProtocolType protocolType, const char * mpiImpl)
49     : BatchManager(parent, host, username, protocolType, mpiImpl)
50   {
51     // Nothing to do
52   }
53
54   BatchManager_eLL::~BatchManager_eLL()
55   {
56     // Nothing to do
57   }
58
59   // Method to submit a job to the batch manager
60   const JobId BatchManager_eLL::submitJob(const Job & job)
61   {
62     Parametre params = job.getParametre();
63     const string workDir = params[WORKDIR];
64
65     // export input files on cluster
66     exportInputFiles(job);
67
68     // build command file to submit the job and copy it on the server
69     string cmdFile = buildCommandFile(job);
70
71     // define command to submit batch
72     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
73     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
74     cerr << command.c_str() << endl;
75
76     // submit job
77     string output;
78     int status = Utils::getCommandOutput(command, output);
79     cout << output;
80     if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
81
82     // find id of submitted job in output
83     string jobref;
84     istringstream idfile(output);
85     string line;
86     while (idfile && line.compare(0, 9, "llsubmit:") != 0)
87       getline(idfile, line);
88     if (line.compare(0, 9, "llsubmit:") == 0)
89     {
90       string::size_type p1 = line.find_first_of("\"");
91       string::size_type p2 = line.find_last_of("\"");
92       if (p1 != p2)
93         jobref = line.substr(p1 + 1, p2 - p1 - 1);
94     }
95     if (jobref.size() == 0)
96       throw RunTimeException("Error in the submission of the job on the remote host");
97
98     JobId id(this, jobref);
99     return id;
100   }
101
102   /**
103    * Create LoadLeveler command file and copy it on the server.
104    * Return the name of the remote file.
105    */
106   string BatchManager_eLL::buildCommandFile(const Job & job)
107   {
108     Parametre params = job.getParametre();
109
110     // Job Parameters
111     string workDir = "";
112     string fileToExecute = "";
113     string queue = "";
114
115     // Mandatory parameters
116     if (params.find(WORKDIR) != params.end()) 
117       workDir = params[WORKDIR].str();
118     else 
119       throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
120     if (params.find(EXECUTABLE) != params.end()) 
121       fileToExecute = params[EXECUTABLE].str();
122     else 
123       throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
124
125     string::size_type p1 = fileToExecute.find_last_of("/");
126     string::size_type p2 = fileToExecute.find_last_of(".");
127     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
128     string fileNameToExecute = fileToExecute.substr(p1+1);
129
130     // Create batch submit file
131     ofstream tempOutputFile;
132     string tmpFileName = Utils::createAndOpenTemporaryFile("LL-script", tempOutputFile);
133
134     tempOutputFile << "#!/bin/bash" << endl;
135     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
136     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
137
138     if (params.find(NAME) != params.end())
139       tempOutputFile << "# @ job_name = " << params[NAME] << endl;
140
141     // Optional parameters
142     int nbproc = 1;
143     if (params.find(NBPROC) != params.end())
144       nbproc = params[NBPROC];
145     int nbprocpernode = 1;
146     if (params.find(NBPROCPERNODE) != params.end())
147       nbprocpernode = params[NBPROCPERNODE];
148
149     if (params.find(EXCLUSIVE) != params.end()) {
150       if (params[EXCLUSIVE])
151         tempOutputFile << "# @ node_usage = not_shared" << endl;
152       else
153         tempOutputFile << "# @ node_usage = shared" << endl;
154     }
155
156     // If job type is not specified, try to guess it from number of procs
157     string job_type;
158     if (params.find(LL_JOBTYPE) != params.end())
159       job_type = params[LL_JOBTYPE].str();
160     else if (nbproc == 1)
161       job_type = "serial";
162     else
163       job_type = "mpich";
164
165     tempOutputFile << "# @ job_type = " << job_type << endl;
166
167     if (job_type == "mpich") {
168       int nodes_requested = (nbproc + nbprocpernode -1) / nbprocpernode;
169       tempOutputFile << "# @ node = " << nodes_requested << endl;
170       tempOutputFile << "# @ total_tasks = " << nbproc << endl;
171     }
172
173     if (params.find(MAXWALLTIME) != params.end())
174       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
175     if (params.find(MAXRAMSIZE) != params.end())
176       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
177     if (params.find(QUEUE) != params.end())
178       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
179
180     // Define environment for the job
181     Environnement env = job.getEnvironnement();
182     if (!env.empty()) {
183       tempOutputFile << "# @ environment = ";
184       Environnement::const_iterator iter;
185       for (iter = env.begin() ; iter != env.end() ; ++iter) {
186         tempOutputFile << iter->first << "=" << iter->second << "; ";
187       }
188       tempOutputFile << endl;
189     }
190
191     tempOutputFile << "# @ queue" << endl;
192
193     // generate nodes file
194     tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
195
196     // Launch the executable
197     tempOutputFile << "cd " << workDir << endl;
198     tempOutputFile << "./" + fileNameToExecute << endl;
199
200     tempOutputFile.flush();
201     tempOutputFile.close();
202
203     cerr << "Batch script file generated is: " << tmpFileName << endl;
204
205     string remoteFileName = rootNameToExecute + "_LL.cmd";
206     int status = _protocol.copyFile(tmpFileName, "", "",
207                                     workDir + "/" + remoteFileName,
208                                     _hostname, _username);
209     if (status)
210       throw RunTimeException("Cannot copy command file on host " + _hostname);
211
212     return remoteFileName;
213   }
214
215   void BatchManager_eLL::deleteJob(const JobId & jobid)
216   {
217     // define command to delete job
218     string subCommand = "llcancel " + jobid.getReference();
219     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
220     cerr << command.c_str() << endl;
221
222     int status = system(command.c_str());
223     if (status)
224       throw RunTimeException("Can't delete job " + jobid.getReference());
225
226     cerr << "job " << jobid.getReference() << " killed" << endl;
227   }
228
229   void BatchManager_eLL::holdJob(const JobId & jobid)
230   {
231     throw NotYetImplementedException("BatchManager_eLL::holdJob");
232   }
233
234   void BatchManager_eLL::releaseJob(const JobId & jobid)
235   {
236     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
237   }
238
239   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
240   {
241     throw NotYetImplementedException("BatchManager_eLL::alterJob");
242   }
243
244   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
245   {
246     throw NotYetImplementedException("BatchManager_eLL::alterJob");
247   }
248
249   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
250   {
251     throw NotYetImplementedException("BatchManager_eLL::alterJob");
252   }
253
254   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
255   {
256     // define command to query batch
257     string subCommand = "llq -f %st " + jobid.getReference();
258     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
259     cerr << command.c_str() << endl;
260     string output;
261     int status = Utils::getCommandOutput(command, output);
262     if (status != 0)
263       throw RunTimeException("Can't query job " + jobid.getReference());
264
265     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), output);
266     return jobinfo;
267   }
268
269   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
270   {
271     return JobId(this, reference);
272   }
273
274 }