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