Salome HOME
Update copyrights
[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
33 #include <Batch_NotYetImplementedException.hxx>
34 #include <Batch_Constants.hxx>
35
36 #include "Batch_FactBatchManager_eLL.hxx"
37 #include "Batch_BatchManager_eLL.hxx"
38 #include "Batch_JobInfo_eLL.hxx"
39
40 using namespace std;
41
42 namespace Batch {
43
44   BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
45                                      const char * username,
46                                      CommunicationProtocolType protocolType, const char * mpiImpl,
47                                      int nb_proc_per_node)
48     : BatchManager(parent, host),
49       BatchManager_eClient(parent, host, username, protocolType, mpiImpl),
50       _nb_proc_per_node(nb_proc_per_node)
51   {
52     // Nothing to do
53   }
54
55   BatchManager_eLL::~BatchManager_eLL()
56   {
57     // Nothing to do
58   }
59
60   // Method to submit a job to the batch manager
61   const JobId BatchManager_eLL::submitJob(const Job & job)
62   {
63     int status;
64     Parametre params = job.getParametre();
65     const string workDir = params[WORKDIR];
66
67     // export input files on cluster
68     exportInputFiles(job);
69
70     // build command file to submit the job and copy it on the server
71     string cmdFile = buildCommandFile(job);
72
73     // define name of log file (local)
74     string logFile = generateTemporaryFileName("LL-submitlog");
75
76     // define command to submit batch
77     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
78     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
79     command += " > ";
80     command += logFile;
81     cerr << command.c_str() << endl;
82     status = system(command.c_str());
83     if (status)
84     {
85       ifstream error_message(logFile.c_str());
86       string mess;
87       string temp;
88       while(getline(error_message, temp))
89         mess += temp;
90       error_message.close();
91       throw EmulationException("Error of connection on remote host, error was: " + mess);
92     }
93
94     // read id of submitted job in log file
95     string jobref;
96     ifstream idfile(logFile.c_str());
97     string line;
98     while (idfile && line.compare(0, 9, "llsubmit:") != 0)
99       getline(idfile, line);
100     idfile.close();
101     if (line.compare(0, 9, "llsubmit:") == 0)
102     {
103       string::size_type p1 = line.find_first_of("\"");
104       string::size_type p2 = line.find_last_of("\"");
105       if (p1 != p2)
106         jobref = line.substr(p1 + 1, p2 - p1 - 1);
107     }
108     if (jobref.size() == 0)
109       throw EmulationException("Error in the submission of the job on the remote host");
110
111     JobId id(this, jobref);
112     return id;
113   }
114
115   /**
116    * Create LoadLeveler command file and copy it on the server.
117    * Return the name of the remote file.
118    */
119   string BatchManager_eLL::buildCommandFile(const Job & job)
120   {
121     Parametre params = job.getParametre();
122
123     // Job Parameters
124     string workDir = "";
125     string fileToExecute = "";
126     string queue = "";
127
128     // Mandatory parameters
129     if (params.find(WORKDIR) != params.end()) 
130       workDir = params[WORKDIR].str();
131     else 
132       throw EmulationException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
133     if (params.find(EXECUTABLE) != params.end()) 
134       fileToExecute = params[EXECUTABLE].str();
135     else 
136       throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
137
138     string::size_type p1 = fileToExecute.find_last_of("/");
139     string::size_type p2 = fileToExecute.find_last_of(".");
140     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
141     string fileNameToExecute = fileToExecute.substr(p1+1);
142
143     // Create batch submit file
144     ofstream tempOutputFile;
145     string tmpFileName = createAndOpenTemporaryFile("LL-script", tempOutputFile);
146
147     tempOutputFile << "#!/bin/bash" << endl;
148     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
149     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
150
151     if (params.find(NAME) != params.end())
152       tempOutputFile << "# @ job_name = " << params[NAME] << endl;
153
154     // Optional parameters
155     int nbproc = 1;
156     if (params.find(NBPROC) != params.end())
157       nbproc = params[NBPROC];
158
159     if (params.find(EXCLUSIVE) != params.end()) {
160       if (params[EXCLUSIVE])
161         tempOutputFile << "# @ node_usage = not_shared" << endl;
162       else
163         tempOutputFile << "# @ node_usage = shared" << endl;
164     }
165
166     // If job type is not specified, try to guess it from number of procs
167     string job_type;
168     if (params.find(LL_JOBTYPE) != params.end())
169       job_type = params[LL_JOBTYPE].str();
170     else if (nbproc == 1)
171       job_type = "serial";
172     else
173       job_type = "mpich";
174
175     tempOutputFile << "# @ job_type = " << job_type << endl;
176
177     if (job_type == "mpich") {
178       int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
179       tempOutputFile << "# @ node = " << nodes_requested << endl;
180       tempOutputFile << "# @ total_tasks = " << nbproc << endl;
181     }
182
183     if (params.find(MAXWALLTIME) != params.end())
184       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
185     if (params.find(MAXRAMSIZE) != params.end())
186       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
187     if (params.find(QUEUE) != params.end())
188       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
189
190     // Define environment for the job
191     Environnement env = job.getEnvironnement();
192     if (!env.empty()) {
193       tempOutputFile << "# @ environment = ";
194       Environnement::const_iterator iter;
195       for (iter = env.begin() ; iter != env.end() ; ++iter) {
196         tempOutputFile << iter->first << "=" << iter->second << "; ";
197       }
198       tempOutputFile << endl;
199     }
200
201     tempOutputFile << "# @ queue" << endl;
202
203     // generate nodes file
204     tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
205
206     // Launch the executable
207     tempOutputFile << "cd " << workDir << endl;
208     tempOutputFile << "./" + fileNameToExecute << endl;
209
210     tempOutputFile.flush();
211     tempOutputFile.close();
212
213     cerr << "Batch script file generated is: " << tmpFileName << endl;
214
215     string remoteFileName = rootNameToExecute + "_LL.cmd";
216     int status = _protocol.copyFile(tmpFileName, "", "",
217                                     workDir + "/" + remoteFileName,
218                                     _hostname, _username);
219     if (status)
220       throw EmulationException("Cannot copy command file on host " + _hostname);
221
222     return remoteFileName;
223   }
224
225   void BatchManager_eLL::deleteJob(const JobId & jobid)
226   {
227     // define command to delete job
228     string subCommand = "llcancel " + jobid.getReference();
229     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
230     cerr << command.c_str() << endl;
231
232     int status = system(command.c_str());
233     if (status)
234       throw EmulationException("Can't delete job " + jobid.getReference());
235
236     cerr << "job " << jobid.getReference() << " killed" << endl;
237   }
238
239   void BatchManager_eLL::holdJob(const JobId & jobid)
240   {
241     throw NotYetImplementedException("BatchManager_eLL::holdJob");
242   }
243
244   void BatchManager_eLL::releaseJob(const JobId & jobid)
245   {
246     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
247   }
248
249   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
250   {
251     throw NotYetImplementedException("BatchManager_eLL::alterJob");
252   }
253
254   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
255   {
256     throw NotYetImplementedException("BatchManager_eLL::alterJob");
257   }
258
259   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
260   {
261     throw NotYetImplementedException("BatchManager_eLL::alterJob");
262   }
263
264   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
265   {
266     // define name of log file (local)
267     string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
268
269     // define command to query batch
270     string subCommand = "llq -f %st " + jobid.getReference();
271     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
272     command += " > ";
273     command += logFile;
274     cerr << command.c_str() << endl;
275     int status = system(command.c_str());
276     if (status != 0)
277       throw EmulationException("Can't query job " + jobid.getReference());
278
279     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
280     return jobinfo;
281   }
282
283   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
284   {
285     return JobId(this, reference);
286   }
287
288 }