1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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 * BatchManager_LL.cxx :
25 * Created on: 25 nov. 2010
26 * Author : Renaud BARATE - EDF R&D
34 #include <Constants.hxx>
37 #include "FactBatchManager_LL.hxx"
38 #include "BatchManager_LL.hxx"
39 #include "JobInfo_LL.hxx"
46 BatchManager_LL::BatchManager_LL(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_LL::~BatchManager_LL()
59 // Method to submit a job to the batch manager
60 const JobId BatchManager_LL::runJob(const Job & job)
62 Parametre params = job.getParametre();
63 const string workDir = params[WORKDIR];
65 // build command file to submit the job and copy it on the server
66 string cmdFile = buildCommandFile(job);
68 // define command to submit batch
69 string subCommand = string("cd ") + workDir + "; llsubmit " + cmdFile;
70 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
75 int status = Utils::getCommandOutput(command, output);
77 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
79 // find id of submitted job in output
81 istringstream idfile(output);
83 while (idfile && line.compare(0, 9, "llsubmit:") != 0)
84 getline(idfile, line);
85 if (line.compare(0, 9, "llsubmit:") == 0)
87 string::size_type p1 = line.find_first_of("\"");
88 string::size_type p2 = line.find_last_of("\"");
90 jobref = line.substr(p1 + 1, p2 - p1 - 1);
92 if (jobref.size() == 0)
93 throw RunTimeException("Error in the submission of the job on the remote host");
95 JobId id(this, jobref);
100 * Create LoadLeveler command file and copy it on the server.
101 * Return the name of the remote file.
103 string BatchManager_LL::buildCommandFile(const Job & job)
105 Parametre params = job.getParametre();
109 string fileToExecute = "";
112 // Mandatory parameters
113 if (params.find(WORKDIR) != params.end())
114 workDir = params[WORKDIR].str();
116 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
117 if (params.find(EXECUTABLE) != params.end())
118 fileToExecute = params[EXECUTABLE].str();
120 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
122 string::size_type p1 = fileToExecute.find_last_of("/");
123 string::size_type p2 = fileToExecute.find_last_of(".");
124 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
125 string fileNameToExecute = fileToExecute.substr(p1+1);
127 // Create batch submit file
128 ofstream tempOutputFile;
129 string tmpFileName = Utils::createAndOpenTemporaryFile("LL-script", tempOutputFile);
131 tempOutputFile << "#!/bin/bash" << endl;
132 tempOutputFile << "# @ output = " << workDir << "/logs/output.log." << rootNameToExecute << endl;
133 tempOutputFile << "# @ error = " << workDir << "/logs/error.log." << rootNameToExecute << endl;
135 if (params.find(NAME) != params.end())
136 tempOutputFile << "# @ job_name = " << params[NAME] << endl;
138 // Optional parameters
140 if (params.find(NBPROC) != params.end())
141 nbproc = params[NBPROC];
142 int nbprocpernode = 1;
143 if (params.find(NBPROCPERNODE) != params.end())
144 nbprocpernode = params[NBPROCPERNODE];
146 if (params.find(EXCLUSIVE) != params.end()) {
147 if (params[EXCLUSIVE])
148 tempOutputFile << "# @ node_usage = not_shared" << endl;
150 tempOutputFile << "# @ node_usage = shared" << endl;
153 // If job type is not specified, try to guess it from number of procs
155 if (params.find(LL_JOBTYPE) != params.end())
156 job_type = params[LL_JOBTYPE].str();
157 else if (nbproc == 1)
162 tempOutputFile << "# @ job_type = " << job_type << endl;
164 if (job_type == "mpich") {
165 int nodes_requested = (nbproc + nbprocpernode -1) / nbprocpernode;
166 tempOutputFile << "# @ node = " << nodes_requested << endl;
167 tempOutputFile << "# @ total_tasks = " << nbproc << endl;
170 if (params.find(MAXWALLTIME) != params.end())
171 tempOutputFile << "# @ wall_clock_limit = " << params[MAXWALLTIME] << ":00" << endl;
172 if (params.find(MAXRAMSIZE) != params.end())
173 tempOutputFile << "# @ as_limit = " << params[MAXRAMSIZE] << "mb" << endl;
174 if (params.find(QUEUE) != params.end())
175 tempOutputFile << "# @ class = " << params[QUEUE] << endl;
177 // Define environment for the job
178 Environnement env = job.getEnvironnement();
180 tempOutputFile << "# @ environment = ";
181 Environnement::const_iterator iter;
182 for (iter = env.begin() ; iter != env.end() ; ++iter) {
183 tempOutputFile << iter->first << "=" << iter->second << "; ";
185 tempOutputFile << endl;
188 tempOutputFile << "# @ queue" << endl;
190 // generate nodes file
191 tempOutputFile << "export LIBBATCH_NODEFILE=$LOADL_HOSTFILE" << endl;
193 // Launch the executable
194 tempOutputFile << "cd " << workDir << endl;
195 tempOutputFile << "./" + fileNameToExecute << endl;
197 tempOutputFile.flush();
198 tempOutputFile.close();
200 LOG("Batch script file generated is: " << tmpFileName);
202 string remoteFileName = rootNameToExecute + "_LL.cmd";
203 int status = _protocol.copyFile(tmpFileName, "", "",
204 workDir + "/" + remoteFileName,
205 _hostname, _username);
207 throw RunTimeException("Cannot copy command file on host " + _hostname);
209 return remoteFileName;
212 void BatchManager_LL::deleteJob(const JobId & jobid)
214 // define command to delete job
215 string subCommand = "llcancel " + jobid.getReference();
216 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
219 int status = system(command.c_str());
221 throw RunTimeException("Can't delete job " + jobid.getReference());
223 LOG("job " << jobid.getReference() << " killed");
226 JobInfo BatchManager_LL::queryJob(const JobId & jobid)
228 // define command to query batch
229 string subCommand = "llq -f %st " + jobid.getReference();
230 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
233 int status = Utils::getCommandOutput(command, output);
235 throw RunTimeException("Can't query job " + jobid.getReference());
237 JobInfo_LL jobinfo = JobInfo_LL(jobid.getReference(), output);