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