]> SALOME platform Git repositories - tools/libbatch.git/blob - src/LoadLeveler/Batch_BatchManager_eLL.cxx
Salome HOME
Fixed compilation problem with gcc 4.4.5
[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
148     // Optional parameters
149     if (params.find(MAXWALLTIME) != params.end())
150       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
151     if (params.find(MAXRAMSIZE) != params.end())
152       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
153     if (params.find(QUEUE) != params.end())
154       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
155
156     // Define environment for the job
157     Environnement env = job.getEnvironnement();
158     if (!env.empty()) {
159       tempOutputFile << "# @ environment = ";
160       Environnement::const_iterator iter;
161       for (iter = env.begin() ; iter != env.end() ; ++iter) {
162         tempOutputFile << iter->first << "=" << iter->second << "; ";
163       }
164       tempOutputFile << endl;
165     }
166
167     tempOutputFile << "# @ job_type = bluegene" << endl;
168     tempOutputFile << "# @ queue" << endl;
169
170     // generate nodes file
171     tempOutputFile << "NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
172     tempOutputFile << "for node in $LOADL_PROCESSOR_LIST; do" << endl;
173     tempOutputFile << "  echo $node >> $NODEFILE" << endl;
174     tempOutputFile << "done" << endl;
175     tempOutputFile << "export LIBBATCH_NODEFILE=$NODEFILE" << endl;
176
177     // Launch the executable
178     tempOutputFile << "cd " << workDir << endl;
179     tempOutputFile << "./" + fileNameToExecute << endl;
180
181     tempOutputFile.flush();
182     tempOutputFile.close();
183
184     cerr << "Batch script file generated is: " << tmpFileName << endl;
185
186     string remoteFileName = rootNameToExecute + "_LL.cmd";
187     int status = _protocol.copyFile(tmpFileName, "", "",
188                                     workDir + "/" + remoteFileName,
189                                     _hostname, _username);
190     if (status)
191       throw EmulationException("Cannot copy command file on host " + _hostname);
192
193     return remoteFileName;
194   }
195
196   void BatchManager_eLL::deleteJob(const JobId & jobid)
197   {
198     // define command to delete job
199     string subCommand = "llcancel " + jobid.getReference();
200     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
201     cerr << command.c_str() << endl;
202
203     int status = system(command.c_str());
204     if (status)
205       throw EmulationException("Can't delete job " + jobid.getReference());
206
207     cerr << "job " << jobid.getReference() << " killed" << endl;
208   }
209
210   void BatchManager_eLL::holdJob(const JobId & jobid)
211   {
212     throw NotYetImplementedException("BatchManager_eLL::holdJob");
213   }
214
215   void BatchManager_eLL::releaseJob(const JobId & jobid)
216   {
217     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
218   }
219
220   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
221   {
222     throw NotYetImplementedException("BatchManager_eLL::alterJob");
223   }
224
225   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
226   {
227     throw NotYetImplementedException("BatchManager_eLL::alterJob");
228   }
229
230   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
231   {
232     throw NotYetImplementedException("BatchManager_eLL::alterJob");
233   }
234
235   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
236   {
237     // define name of log file (local)
238     string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
239
240     // define command to query batch
241     string subCommand = "llq -f %st " + jobid.getReference();
242     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
243     command += " > ";
244     command += logFile;
245     cerr << command.c_str() << endl;
246     int status = system(command.c_str());
247     if (status != 0)
248       throw EmulationException("Can't query job " + jobid.getReference());
249
250     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
251     return jobinfo;
252   }
253
254   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
255   {
256     throw NotYetImplementedException("BatchManager_eLL::addJob");
257   }
258
259 }