1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 * Batch_BatchManager_eLL.cxx :
25 * Created on: 25 nov. 2010
26 * Author : Renaud BARATE - EDF R&D
33 #include <Batch_NotYetImplementedException.hxx>
34 #include <Batch_Constants.hxx>
36 #include "Batch_FactBatchManager_eLL.hxx"
37 #include "Batch_BatchManager_eLL.hxx"
38 #include "Batch_JobInfo_eLL.hxx"
44 BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
45 const char * username,
46 CommunicationProtocolType protocolType, const char * mpiImpl,
48 : BatchManager(parent, host),
49 BatchManager_eClient(parent, host, username, protocolType, mpiImpl),
50 _nb_proc_per_node(nb_proc_per_node)
55 BatchManager_eLL::~BatchManager_eLL()
60 // Method to submit a job to the batch manager
61 const JobId BatchManager_eLL::submitJob(const Job & job)
64 Parametre params = job.getParametre();
65 const string workDir = params[WORKDIR];
67 // export input files on cluster
68 exportInputFiles(job);
70 // build command file to submit the job and copy it on the server
71 string cmdFile = buildCommandFile(job);
73 // define name of log file (local)
74 string logFile = generateTemporaryFileName("LL-submitlog");
76 // define command to submit batch
77 string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
78 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
81 cerr << command.c_str() << endl;
82 status = system(command.c_str());
85 ifstream error_message(logFile.c_str());
88 while(getline(error_message, temp))
90 error_message.close();
91 throw EmulationException("Error of connection on remote host, error was: " + mess);
94 // read id of submitted job in log file
96 ifstream idfile(logFile.c_str());
98 while (idfile && line.compare(0, 9, "llsubmit:") != 0)
99 getline(idfile, line);
101 if (line.compare(0, 9, "llsubmit:") == 0)
103 string::size_type p1 = line.find_first_of("\"");
104 string::size_type p2 = line.find_last_of("\"");
106 jobref = line.substr(p1 + 1, p2 - p1 - 1);
108 if (jobref.size() == 0)
109 throw EmulationException("Error in the submission of the job on the remote host");
111 JobId id(this, jobref);
116 * Create LoadLeveler command file and copy it on the server.
117 * Return the name of the remote file.
119 string BatchManager_eLL::buildCommandFile(const Job & job)
121 Parametre params = job.getParametre();
125 string fileToExecute = "";
128 // Mandatory parameters
129 if (params.find(WORKDIR) != params.end())
130 workDir = params[WORKDIR].str();
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();
136 throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
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);
143 // Create batch submit file
144 ofstream tempOutputFile;
145 string tmpFileName = createAndOpenTemporaryFile("LL-script", tempOutputFile);
147 tempOutputFile << "#!/bin/bash" << endl;
148 tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
149 tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
151 if (params.find(NAME) != params.end())
152 tempOutputFile << "# @ job_name = " << params[NAME] << endl;
154 // Optional parameters
156 if (params.find(NBPROC) != params.end())
157 nbproc = params[NBPROC];
159 if (params.find(EXCLUSIVE) != params.end()) {
160 if (params[EXCLUSIVE])
161 tempOutputFile << "# @ node_usage = not_shared" << endl;
163 tempOutputFile << "# @ node_usage = shared" << endl;
166 // If job type is not specified, try to guess it from number of procs
168 if (params.find(LL_JOBTYPE) != params.end())
169 job_type = params[LL_JOBTYPE].str();
170 else if (nbproc == 1)
175 tempOutputFile << "# @ job_type = " << job_type << endl;
177 if (job_type == "mpich") {
178 int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
179 tempOutputFile << "# @ node = " << nodes_requested << endl;
180 tempOutputFile << "# @ total_tasks = " << nbproc << endl;
183 if (params.find(MAXWALLTIME) != params.end())
184 tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
185 if (params.find(MAXRAMSIZE) != params.end())
186 tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
187 if (params.find(QUEUE) != params.end())
188 tempOutputFile << "# @ class = " << params[QUEUE] << endl;
190 // Define environment for the job
191 Environnement env = job.getEnvironnement();
193 tempOutputFile << "# @ environment = ";
194 Environnement::const_iterator iter;
195 for (iter = env.begin() ; iter != env.end() ; ++iter) {
196 tempOutputFile << iter->first << "=" << iter->second << "; ";
198 tempOutputFile << endl;
201 tempOutputFile << "# @ queue" << endl;
203 // generate nodes file
204 tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
206 // Launch the executable
207 tempOutputFile << "cd " << workDir << endl;
208 tempOutputFile << "./" + fileNameToExecute << endl;
210 tempOutputFile.flush();
211 tempOutputFile.close();
213 cerr << "Batch script file generated is: " << tmpFileName << endl;
215 string remoteFileName = rootNameToExecute + "_LL.cmd";
216 int status = _protocol.copyFile(tmpFileName, "", "",
217 workDir + "/" + remoteFileName,
218 _hostname, _username);
220 throw EmulationException("Cannot copy command file on host " + _hostname);
222 return remoteFileName;
225 void BatchManager_eLL::deleteJob(const JobId & jobid)
227 // define command to delete job
228 string subCommand = "llcancel " + jobid.getReference();
229 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
230 cerr << command.c_str() << endl;
232 int status = system(command.c_str());
234 throw EmulationException("Can't delete job " + jobid.getReference());
236 cerr << "job " << jobid.getReference() << " killed" << endl;
239 void BatchManager_eLL::holdJob(const JobId & jobid)
241 throw NotYetImplementedException("BatchManager_eLL::holdJob");
244 void BatchManager_eLL::releaseJob(const JobId & jobid)
246 throw NotYetImplementedException("BatchManager_eLL::releaseJob");
249 void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
251 throw NotYetImplementedException("BatchManager_eLL::alterJob");
254 void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
256 throw NotYetImplementedException("BatchManager_eLL::alterJob");
259 void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
261 throw NotYetImplementedException("BatchManager_eLL::alterJob");
264 JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
266 // define name of log file (local)
267 string logFile = generateTemporaryFileName("LL-querylog-" + jobid.getReference());
269 // define command to query batch
270 string subCommand = "llq -f %st " + jobid.getReference();
271 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
274 cerr << command.c_str() << endl;
275 int status = system(command.c_str());
277 throw EmulationException("Can't query job " + jobid.getReference());
279 JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), logFile);
283 const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
285 return JobId(this, reference);