Salome HOME
Added environment for LoadLeveler jobs
[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 <iostream>
30 #include <fstream>
31
32 #include <Batch_NotYetImplementedException.hxx>
33
34 #include "Batch_BatchManager_eLL.hxx"
35 #include "Batch_JobInfo_eLL.hxx"
36
37 using namespace std;
38
39 namespace Batch {
40
41   BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
42                                      const char * username,
43                                      CommunicationProtocolType protocolType, const char * mpiImpl,
44                                      int nb_proc_per_node)
45     : BatchManager(parent, host),
46       BatchManager_eClient(parent, host, username, protocolType, mpiImpl)
47   {
48     // Nothing to do
49   }
50
51   BatchManager_eLL::~BatchManager_eLL()
52   {
53     // Nothing to do
54   }
55
56   // Method to submit a job to the batch manager
57   const JobId BatchManager_eLL::submitJob(const Job & job)
58   {
59     int status;
60     Parametre params = job.getParametre();
61     const string workDir = params[WORKDIR];
62
63     // export input files on cluster
64     exportInputFiles(job);
65
66     // build command file to submit the job and copy it on the server
67     string cmdFile = buildCommandFile(job);
68
69     // define name of log file (local)
70     string logFile = generateTemporaryFileName("LL-submitlog");
71
72     // define command to submit batch
73     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
74     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
75     command += " > ";
76     command += logFile;
77     cerr << command.c_str() << endl;
78     status = system(command.c_str());
79     if (status)
80     {
81       ifstream error_message(logFile.c_str());
82       string mess;
83       string temp;
84       while(getline(error_message, temp))
85         mess += temp;
86       error_message.close();
87       throw EmulationException("Error of connection on remote host, error was: " + mess);
88     }
89
90     // read id of submitted job in log file
91     string jobref;
92     ifstream idfile(logFile.c_str());
93     string line;
94     while (idfile && line.compare(0, 9, "llsubmit:") != 0)
95       getline(idfile, line);
96     idfile.close();
97     if (line.compare(0, 9, "llsubmit:") == 0)
98     {
99       string::size_type p1 = line.find_first_of("\"");
100       string::size_type p2 = line.find_last_of("\"");
101       if (p1 != p2)
102         jobref = line.substr(p1 + 1, p2 - p1 - 1);
103     }
104     if (jobref.size() == 0)
105       throw EmulationException("Error in the submission of the job on the remote host");
106
107     JobId id(this, jobref);
108     return id;
109   }
110
111   /**
112    * Create LoadLeveler command file and copy it on the server.
113    * Return the name of the remote file.
114    */
115   string BatchManager_eLL::buildCommandFile(const Job & job)
116   {
117     Parametre params = job.getParametre();
118
119     // Job Parameters
120     string workDir = "";
121     string fileToExecute = "";
122     string queue = "";
123
124     // Mandatory parameters
125     if (params.find(WORKDIR) != params.end()) 
126       workDir = params[WORKDIR].str();
127     else 
128       throw EmulationException("params[WORKDIR] is not defined ! Please defined it, cannot submit this job");
129     if (params.find(EXECUTABLE) != params.end()) 
130       fileToExecute = params[EXECUTABLE].str();
131     else 
132       throw EmulationException("params[EXECUTABLE] is not defined ! Please defined it, cannot submit this job");
133
134     // Optional parameters
135     if (params.find(QUEUE) != params.end()) 
136       queue = params[QUEUE].str();
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 << "# @ executable = " << fileNameToExecute << endl;
148     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
149     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
150     if (queue != "")
151       tempOutputFile << "# @ class = " << queue << endl;
152
153     // Define environment for the job
154     Environnement env = job.getEnvironnement();
155     if (!env.empty()) {
156       tempOutputFile << "# @ environment = ";
157       Environnement::const_iterator iter;
158       for (iter = env.begin() ; iter != env.end() ; ++iter) {
159         tempOutputFile << iter->first << "=" << iter->second << "; ";
160       }
161       tempOutputFile << endl;
162     }
163
164     tempOutputFile << "# @ job_type = bluegene" << endl;
165     tempOutputFile << "# @ queue" << endl;
166
167     tempOutputFile.flush();
168     tempOutputFile.close();
169
170     cerr << "Batch script file generated is: " << tmpFileName << endl;
171
172     string remoteFileName = rootNameToExecute + "_LL.cmd";
173     int status = _protocol.copyFile(tmpFileName, "", "",
174                                     workDir + "/" + remoteFileName,
175                                     _hostname, _username);
176     if (status)
177       throw EmulationException("Cannot copy command file on host " + _hostname);
178
179     return remoteFileName;
180   }
181
182   void BatchManager_eLL::deleteJob(const JobId & jobid)
183   {
184     throw NotYetImplementedException("BatchManager_eLL::deleteJob");
185   }
186
187   void BatchManager_eLL::holdJob(const JobId & jobid)
188   {
189     throw NotYetImplementedException("BatchManager_eLL::holdJob");
190   }
191
192   void BatchManager_eLL::releaseJob(const JobId & jobid)
193   {
194     throw NotYetImplementedException("BatchManager_eLL::releaseJob");
195   }
196
197   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
198   {
199     throw NotYetImplementedException("BatchManager_eLL::alterJob");
200   }
201
202   void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
203   {
204     throw NotYetImplementedException("BatchManager_eLL::alterJob");
205   }
206
207   void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
208   {
209     throw NotYetImplementedException("BatchManager_eLL::alterJob");
210   }
211
212   JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
213   {
214     // define name of log file (local)
215     string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
216
217     // define command to query batch
218     string subCommand = "llq -f %st " + jobid.getReference();
219     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
220     command += " > ";
221     command += logFile;
222     cerr << command.c_str() << endl;
223     int status = system(command.c_str());
224     if (status != 0)
225       throw EmulationException("Can't query job " + jobid.getReference());
226
227     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
228     return jobinfo;
229   }
230
231   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
232   {
233     throw NotYetImplementedException("BatchManager_eLL::addJob");
234   }
235
236 }