Salome HOME
Fixed nb_proc_per_node 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 <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       _nb_proc_per_node(nb_proc_per_node)
49   {
50     // Nothing to do
51   }
52
53   BatchManager_eLL::~BatchManager_eLL()
54   {
55     // Nothing to do
56   }
57
58   // Method to submit a job to the batch manager
59   const JobId BatchManager_eLL::submitJob(const Job & job)
60   {
61     int status;
62     Parametre params = job.getParametre();
63     const string workDir = params[WORKDIR];
64
65     // export input files on cluster
66     exportInputFiles(job);
67
68     // build command file to submit the job and copy it on the server
69     string cmdFile = buildCommandFile(job);
70
71     // define name of log file (local)
72     string logFile = generateTemporaryFileName("LL-submitlog");
73
74     // define command to submit batch
75     string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
76     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
77     command += " > ";
78     command += logFile;
79     cerr << command.c_str() << endl;
80     status = system(command.c_str());
81     if (status)
82     {
83       ifstream error_message(logFile.c_str());
84       string mess;
85       string temp;
86       while(getline(error_message, temp))
87         mess += temp;
88       error_message.close();
89       throw EmulationException("Error of connection on remote host, error was: " + mess);
90     }
91
92     // read id of submitted job in log file
93     string jobref;
94     ifstream idfile(logFile.c_str());
95     string line;
96     while (idfile && line.compare(0, 9, "llsubmit:") != 0)
97       getline(idfile, line);
98     idfile.close();
99     if (line.compare(0, 9, "llsubmit:") == 0)
100     {
101       string::size_type p1 = line.find_first_of("\"");
102       string::size_type p2 = line.find_last_of("\"");
103       if (p1 != p2)
104         jobref = line.substr(p1 + 1, p2 - p1 - 1);
105     }
106     if (jobref.size() == 0)
107       throw EmulationException("Error in the submission of the job on the remote host");
108
109     JobId id(this, jobref);
110     return id;
111   }
112
113   /**
114    * Create LoadLeveler command file and copy it on the server.
115    * Return the name of the remote file.
116    */
117   string BatchManager_eLL::buildCommandFile(const Job & job)
118   {
119     Parametre params = job.getParametre();
120
121     // Job Parameters
122     string workDir = "";
123     string fileToExecute = "";
124     string queue = "";
125
126     // Mandatory parameters
127     if (params.find(WORKDIR) != params.end()) 
128       workDir = params[WORKDIR].str();
129     else 
130       throw EmulationException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
131     if (params.find(EXECUTABLE) != params.end()) 
132       fileToExecute = params[EXECUTABLE].str();
133     else 
134       throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
135
136     string::size_type p1 = fileToExecute.find_last_of("/");
137     string::size_type p2 = fileToExecute.find_last_of(".");
138     string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
139     string fileNameToExecute = fileToExecute.substr(p1+1);
140
141     // Create batch submit file
142     ofstream tempOutputFile;
143     string tmpFileName = createAndOpenTemporaryFile("LL-script", tempOutputFile);
144
145     tempOutputFile << "#!/bin/bash" << endl;
146     tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
147     tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
148     tempOutputFile << "# @ node_usage = not_shared" << endl;
149
150     if (params.find(NAME) != params.end())
151       tempOutputFile << "# @ job_name = " << params[NAME] << endl;
152
153     // Optional parameters
154     int nbproc = 1;
155     if (params.find(NBPROC) != params.end())
156       nbproc = params[NBPROC];
157     //if (nbproc == 1)
158     //  tempOutputFile << "# @ job_type = serial" << endl;
159     //else {
160       //  tempOutputFile << "# @ job_type = parallel" << endl;
161       int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
162       tempOutputFile << "# @ job_type = mpich" << endl;
163       tempOutputFile << "# @ node = " << nodes_requested << endl;
164       tempOutputFile << "# @ tasks_per_node = " << _nb_proc_per_node << endl;
165     //}
166     //tempOutputFile << "# @ job_type = bluegene" << endl;
167
168     if (params.find(MAXWALLTIME) != params.end())
169       tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
170     if (params.find(MAXRAMSIZE) != params.end())
171       tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
172     if (params.find(QUEUE) != params.end())
173       tempOutputFile << "# @ class = " << params[QUEUE] << endl;
174
175     // Define environment for the job
176     Environnement env = job.getEnvironnement();
177     if (!env.empty()) {
178       tempOutputFile << "# @ environment = ";
179       Environnement::const_iterator iter;
180       for (iter = env.begin() ; iter != env.end() ; ++iter) {
181         tempOutputFile << iter->first << "=" << iter->second << "; ";
182       }
183       tempOutputFile << endl;
184     }
185
186     tempOutputFile << "# @ queue" << endl;
187
188     // generate nodes file
189     tempOutputFile << "NODEFILE=`mktemp nodefile-XXXXXXXXXX` || exit 1" << endl;
190     tempOutputFile << "for node in $LOADL_PROCESSOR_LIST; do" << endl;
191     tempOutputFile << "  echo $node >> $NODEFILE" << endl;
192     tempOutputFile << "done" << endl;
193     tempOutputFile << "export LIBBATCH_NODEFILE=$NODEFILE" << 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 EmulationException("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 EmulationException("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 name of log file (local)
256     string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
257
258     // define command to query batch
259     string subCommand = "llq -f %st " + jobid.getReference();
260     string command = _protocol.getExecCommand(subCommand, _hostname, _username);
261     command += " > ";
262     command += logFile;
263     cerr << command.c_str() << endl;
264     int status = system(command.c_str());
265     if (status != 0)
266       throw EmulationException("Can't query job " + jobid.getReference());
267
268     JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
269     return jobinfo;
270   }
271
272   const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
273   {
274     throw NotYetImplementedException("BatchManager_eLL::addJob");
275   }
276
277 }