1 // Copyright (C) 2007-2012 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
34 #include <Batch_NotYetImplementedException.hxx>
35 #include <Batch_Constants.hxx>
36 #include <Batch_Utils.hxx>
38 #include "Batch_FactBatchManager_eLL.hxx"
39 #include "Batch_BatchManager_eLL.hxx"
40 #include "Batch_JobInfo_eLL.hxx"
46 BatchManager_eLL::BatchManager_eLL(const FactBatchManager * parent, const char * host,
47 const char * username,
48 CommunicationProtocolType protocolType, const char * mpiImpl)
49 : BatchManager(parent, host, username, protocolType, mpiImpl)
54 BatchManager_eLL::~BatchManager_eLL()
59 // Method to submit a job to the batch manager
60 const JobId BatchManager_eLL::submitJob(const Job & job)
62 Parametre params = job.getParametre();
63 const string workDir = params[WORKDIR];
65 // export input files on cluster
66 exportInputFiles(job);
68 // build command file to submit the job and copy it on the server
69 string cmdFile = buildCommandFile(job);
71 // define command to submit batch
72 string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
73 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
74 cerr << command.c_str() << endl;
78 int status = Utils::getCommandOutput(command, output);
80 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
82 // find id of submitted job in output
84 istringstream idfile(output);
86 while (idfile && line.compare(0, 9, "llsubmit:") != 0)
87 getline(idfile, line);
88 if (line.compare(0, 9, "llsubmit:") == 0)
90 string::size_type p1 = line.find_first_of("\"");
91 string::size_type p2 = line.find_last_of("\"");
93 jobref = line.substr(p1 + 1, p2 - p1 - 1);
95 if (jobref.size() == 0)
96 throw RunTimeException("Error in the submission of the job on the remote host");
98 JobId id(this, jobref);
103 * Create LoadLeveler command file and copy it on the server.
104 * Return the name of the remote file.
106 string BatchManager_eLL::buildCommandFile(const Job & job)
108 Parametre params = job.getParametre();
112 string fileToExecute = "";
115 // Mandatory parameters
116 if (params.find(WORKDIR) != params.end())
117 workDir = params[WORKDIR].str();
119 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
120 if (params.find(EXECUTABLE) != params.end())
121 fileToExecute = params[EXECUTABLE].str();
123 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
125 string::size_type p1 = fileToExecute.find_last_of("/");
126 string::size_type p2 = fileToExecute.find_last_of(".");
127 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
128 string fileNameToExecute = fileToExecute.substr(p1+1);
130 // Create batch submit file
131 ofstream tempOutputFile;
132 string tmpFileName = Utils::createAndOpenTemporaryFile("LL-script", tempOutputFile);
134 tempOutputFile << "#!/bin/bash" << endl;
135 tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
136 tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
138 if (params.find(NAME) != params.end())
139 tempOutputFile << "# @ job_name = " << params[NAME] << endl;
141 // Optional parameters
143 if (params.find(NBPROC) != params.end())
144 nbproc = params[NBPROC];
145 int nbprocpernode = 1;
146 if (params.find(NBPROCPERNODE) != params.end())
147 nbprocpernode = params[NBPROCPERNODE];
149 if (params.find(EXCLUSIVE) != params.end()) {
150 if (params[EXCLUSIVE])
151 tempOutputFile << "# @ node_usage = not_shared" << endl;
153 tempOutputFile << "# @ node_usage = shared" << endl;
156 // If job type is not specified, try to guess it from number of procs
158 if (params.find(LL_JOBTYPE) != params.end())
159 job_type = params[LL_JOBTYPE].str();
160 else if (nbproc == 1)
165 tempOutputFile << "# @ job_type = " << job_type << endl;
167 if (job_type == "mpich") {
168 int nodes_requested = (nbproc + nbprocpernode -1) / nbprocpernode;
169 tempOutputFile << "# @ node = " << nodes_requested << endl;
170 tempOutputFile << "# @ total_tasks = " << nbproc << endl;
173 if (params.find(MAXWALLTIME) != params.end())
174 tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
175 if (params.find(MAXRAMSIZE) != params.end())
176 tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
177 if (params.find(QUEUE) != params.end())
178 tempOutputFile << "# @ class = " << params[QUEUE] << endl;
180 // Define environment for the job
181 Environnement env = job.getEnvironnement();
183 tempOutputFile << "# @ environment = ";
184 Environnement::const_iterator iter;
185 for (iter = env.begin() ; iter != env.end() ; ++iter) {
186 tempOutputFile << iter->first << "=" << iter->second << "; ";
188 tempOutputFile << endl;
191 tempOutputFile << "# @ queue" << endl;
193 // generate nodes file
194 tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
196 // Launch the executable
197 tempOutputFile << "cd " << workDir << endl;
198 tempOutputFile << "./" + fileNameToExecute << endl;
200 tempOutputFile.flush();
201 tempOutputFile.close();
203 cerr << "Batch script file generated is: " << tmpFileName << endl;
205 string remoteFileName = rootNameToExecute + "_LL.cmd";
206 int status = _protocol.copyFile(tmpFileName, "", "",
207 workDir + "/" + remoteFileName,
208 _hostname, _username);
210 throw RunTimeException("Cannot copy command file on host " + _hostname);
212 return remoteFileName;
215 void BatchManager_eLL::deleteJob(const JobId & jobid)
217 // define command to delete job
218 string subCommand = "llcancel " + jobid.getReference();
219 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
220 cerr << command.c_str() << endl;
222 int status = system(command.c_str());
224 throw RunTimeException("Can't delete job " + jobid.getReference());
226 cerr << "job " << jobid.getReference() << " killed" << endl;
229 void BatchManager_eLL::holdJob(const JobId & jobid)
231 throw NotYetImplementedException("BatchManager_eLL::holdJob");
234 void BatchManager_eLL::releaseJob(const JobId & jobid)
236 throw NotYetImplementedException("BatchManager_eLL::releaseJob");
239 void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
241 throw NotYetImplementedException("BatchManager_eLL::alterJob");
244 void BatchManager_eLL::alterJob(const JobId & jobid, const Parametre & param)
246 throw NotYetImplementedException("BatchManager_eLL::alterJob");
249 void BatchManager_eLL::alterJob(const JobId & jobid, const Environnement & env)
251 throw NotYetImplementedException("BatchManager_eLL::alterJob");
254 JobInfo BatchManager_eLL::queryJob(const JobId & jobid)
256 // define command to query batch
257 string subCommand = "llq -f %st " + jobid.getReference();
258 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
259 cerr << command.c_str() << endl;
261 int status = Utils::getCommandOutput(command, output);
263 throw RunTimeException("Can't query job " + jobid.getReference());
265 JobInfo_eLL jobinfo = JobInfo_eLL(jobid.getReference(), output);
269 const JobId BatchManager_eLL::addJob(const Job & job, const string reference)
271 return JobId(this, reference);