Salome HOME
00ac0bd4fd800f82f6ec304cd7425eb562d9b16d
[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 #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     tempOutputFile << "# @ node_usage = not_shared" << endl;
151
152     if (params.find(NAME) != params.end())
153       tempOutputFile << "# @ job_name = " << params[NAME] << endl;
154
155     // Optional parameters
156     int nbproc = 1;
157     if (params.find(NBPROC) != params.end())
158       nbproc = params[NBPROC];
159
160     // If job type is not specified, try to guess it from number of procs
161     string job_type;
162     if (params.find(LL_JOBTYPE) != params.end())
163       job_type = params[LL_JOBTYPE].str();
164     else if (nbproc == 1)
165       job_type = "serial";
166     else
167       job_type = "mpich";
168
169     tempOutputFile << "# @ job_type = " << job_type << endl;
170
171     if (job_type != "serial") {
172       int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
173       tempOutputFile << "# @ node = " << nodes_requested << endl;
174       tempOutputFile << "# @ tasks_per_node = " << _nb_proc_per_node << endl;
175     }
176
177     if (params.find(MAXWALLTIME) != params.end())
178       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
179     if (params.find(MAXRAMSIZE) != params.end())
180       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
181     if (params.find(QUEUE) != params.end())
182       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
183
184     // Define environment for the job
185     Environnement env = job.getEnvironnement();
186     if (!env.empty()) {
187       tempOutputFile << "# @ environment = ";
188       Environnement::const_iterator iter;
189       for (iter = env.begin() ; iter != env.end() ; ++iter) {
190         tempOutputFile << iter->first << "=" << iter->second << "; ";
191       }
192       tempOutputFile << endl;
193     }
194
195     tempOutputFile << "# @ queue" << endl;
196
197     // generate nodes file
198     tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
199
200     // Launch the executable
201     tempOutputFile << "cd " << workDir << endl;
202     tempOutputFile << "./" + fileNameToExecute << endl;
203
204     tempOutputFile.flush();
205     tempOutputFile.close();
206
207     cerr << "Batch script file generated is: " << tmpFileName << endl;
208
209     string remoteFileName = rootNameToExecute + "_LL.cmd";
210     int status = _protocol.copyFile(tmpFileName, "", "",
211                                     workDir + "/" + remoteFileName,
212                                     _hostname, _username);
213     if (status)
214       throw EmulationException("Cannot copy command file on host " + _hostname);
215
216     return remoteFileName;
217   }
218
219   void BatchManager_eLL::deleteJob(const JobId & jobid)
220   {
221     // define command to delete job
222     string subCommand = "llcancel " + jobid.getReference();
223     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
224     cerr << command.c_str() << endl;
225
226     int status = system(command.c_str());
227     if (status)
228       throw EmulationException("Can't delete job " + jobid.getReference());
229
230     cerr << "job " << jobid.getReference() << " killed" << endl;
231   }
232
233   void BatchManager_eLL::holdJob(const JobId & jobid)
234   {
235     throw NotYetImplementedException("BatchManager_eLL::holdJob");
236   }
237
238   void BatchManager_eLL::releaseJob(const JobId & jobid)
239   {
240     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
241   }
242
243   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
244   {
245     throw NotYetImplementedException("BatchManager_eLL::alterJob");
246   }
247
248   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
249   {
250     throw NotYetImplementedException("BatchManager_eLL::alterJob");
251   }
252
253   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
254   {
255     throw NotYetImplementedException("BatchManager_eLL::alterJob");
256   }
257
258   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
259   {
260     // define name of log file (local)
261     string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
262
263     // define command to query batch
264     string subCommand = "llq -f %st " + jobid.getReference();
265     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
266     command += " > ";
267     command += logFile;
268     cerr << command.c_str() << endl;
269     int status = system(command.c_str());
270     if (status != 0)
271       throw EmulationException("Can't query job " + jobid.getReference());
272
273     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
274     return jobinfo;
275   }
276
277   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
278   {
279     return JobId(this, reference);
280   }
281
282 }