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