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 * BatchManager_Vishnu.cxx :
25 * Created on: 24 june 2011
26 * Author : Renaud BARATE - EDF R&D
38 #include <Constants.hxx>
41 #include "BatchManager_Vishnu.hxx"
42 #include "JobInfo_Vishnu.hxx"
49 BatchManager_Vishnu::BatchManager_Vishnu(const FactBatchManager * parent,
51 const char * username,
52 CommunicationProtocolType protocolType,
54 : // Force SH protocol for Vishnu
55 BatchManager(parent, host, username, SH, mpiImpl)
59 BatchManager_Vishnu::~BatchManager_Vishnu()
63 // Method to submit a job to the batch manager
64 const JobId BatchManager_Vishnu::submitJob(const Job & job)
66 // export input files on cluster
67 exportInputFiles(job);
69 // build command file to submit the job
70 string cmdFile = buildCommandFile(job);
72 // define extra parameters (that can not be defined in the command file)
73 Parametre params = job.getParametre();
74 ostringstream extraParams;
75 if (params.find(NBPROC) != params.end())
76 extraParams << "-P " << params[NBPROC] << " ";
77 if (params.find(MAXRAMSIZE) != params.end())
78 extraParams << "-m " << params[MAXRAMSIZE] << " ";
80 // define command to submit batch
81 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
82 subCommand += "vishnu_connect && ";
83 subCommand += "vishnu_submit_job " + extraParams.str() + _hostname + " " + cmdFile + " && ";
84 subCommand += "vishnu_close";
85 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
91 int status = Utils::getCommandOutput(command, output);
93 if (status != 0) throw RunTimeException("Can't submit job, error was: " + output);
95 // find id of submitted job in output
96 string search = "Job Id : ";
97 string::size_type pos = output.find(search);
98 if (pos == string::npos)
99 throw RunTimeException("Error in the submission of the job on the remote host");
100 pos += search.size();
101 string::size_type endl_pos = output.find('\n', pos);
102 string::size_type count = (endl_pos == string::npos)? string::npos : endl_pos - pos;
103 string jobref = output.substr(pos, count);
104 if (jobref.size() == 0)
105 throw RunTimeException("Error in the submission of the job on the remote host");
107 JobId id(this, jobref);
112 void BatchManager_Vishnu::exportInputFiles(const Job& job)
114 Parametre params = job.getParametre();
115 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
116 subCommand += "vishnu_connect && ";
118 // create remote directories
119 subCommand += "vishnu_create_dir -p " + _hostname + ":" + params[WORKDIR].str() + "/logs && ";
122 string executeFile = params[EXECUTABLE];
123 if (executeFile.size() != 0) {
124 subCommand += "vishnu_copy_file " + executeFile + " " +
125 _hostname + ":" + params[WORKDIR].str() + "/ && ";
128 // copy filesToExportList
129 const Versatile & V = params[INFILE];
130 Versatile::const_iterator Vit;
131 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
132 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
133 Couple inputFile = cpt;
135 // Get absolute paths
136 char * buf = getcwd(NULL, 0);
140 string absremote = (Utils::isAbsolutePath(inputFile.getRemote()))?
141 inputFile.getRemote() :
142 params[WORKDIR].str() + "/" + inputFile.getRemote();
143 string abslocal = (Utils::isAbsolutePath(inputFile.getLocal()))?
144 inputFile.getLocal() :
145 cwd + "/" + inputFile.getLocal();
147 if (Vit != V.begin())
148 subCommand += " && ";
149 subCommand += "vishnu_copy_file " + abslocal + " " + _hostname + ":" + absremote;
151 subCommand += " && vishnu_close";
154 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
158 int status = Utils::getCommandOutput(command, output);
161 throw RunTimeException("Can't copy input files, error was: " + output);
165 * Create Vishnu command file and copy it on the server.
166 * Return the name of the remote file.
168 string BatchManager_Vishnu::buildCommandFile(const Job & job)
170 Parametre params = job.getParametre();
174 string fileToExecute = "";
177 // Mandatory parameters
178 if (params.find(WORKDIR) != params.end())
179 workDir = params[WORKDIR].str();
181 throw RunTimeException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
182 if (params.find(EXECUTABLE) != params.end())
183 fileToExecute = params[EXECUTABLE].str();
185 throw RunTimeException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
187 string::size_type p1 = fileToExecute.find_last_of("/");
188 string::size_type p2 = fileToExecute.find_last_of(".");
189 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
190 string fileNameToExecute = fileToExecute.substr(p1+1);
192 // Create batch submit file
193 ofstream tempOutputFile;
194 string tmpFileName = Utils::createAndOpenTemporaryFile("vishnu-script", tempOutputFile);
196 tempOutputFile << "#!/bin/sh" << endl;
197 tempOutputFile << "#% vishnu_output=" << workDir << "/logs/output.log." << rootNameToExecute << endl;
198 tempOutputFile << "#% vishnu_rror=" << workDir << "/logs/error.log." << rootNameToExecute << endl;
200 if (params.find(NAME) != params.end())
201 tempOutputFile << "#% vishnu_job_name=\"" << params[NAME] << "\"" << endl;
203 // Optional parameters
204 if (params.find(MAXWALLTIME) != params.end()) {
205 long totalMinutes = params[MAXWALLTIME];
206 long h = totalMinutes / 60;
207 long m = totalMinutes - h * 60;
208 tempOutputFile << "#% vishnu_wallclocklimit=" << h << ":";
210 tempOutputFile << "0";
211 tempOutputFile << m << ":00" << endl;
213 if (params.find(QUEUE) != params.end())
214 tempOutputFile << "#% vishnu_queue=" << params[QUEUE] << endl;
216 // Define environment for the job
217 Environnement env = job.getEnvironnement();
218 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
219 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
223 tempOutputFile << "export LIBBATCH_NODEFILE=$VISHNU_BATCHJOB_NODEFILE" << endl;
225 // Launch the executable
226 tempOutputFile << "cd " << workDir << endl;
227 tempOutputFile << "./" + fileNameToExecute;
228 if (params.find(ARGUMENTS) != params.end()) {
229 Versatile V = params[ARGUMENTS];
230 for(Versatile::const_iterator it=V.begin(); it!=V.end(); it++) {
231 StringType argt = * static_cast<StringType *>(*it);
233 tempOutputFile << " " << arg;
236 tempOutputFile << endl;
238 tempOutputFile.flush();
239 tempOutputFile.close();
241 LOG("Batch script file generated is: " << tmpFileName);
245 void BatchManager_Vishnu::deleteJob(const JobId & jobid)
247 // define command to delete job
248 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
249 subCommand += "vishnu_connect && ";
250 subCommand += "vishnu_cancel_job " + _hostname + " " + jobid.getReference() + " && ";
251 subCommand += "vishnu_close";
252 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
255 int status = system(command.c_str());
257 throw RunTimeException("Can't delete job " + jobid.getReference());
259 LOG("job " << jobid.getReference() << " killed");
262 JobInfo BatchManager_Vishnu::queryJob(const JobId & jobid)
264 // define command to query batch
265 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
266 subCommand += "vishnu_connect && ";
267 subCommand += "vishnu_get_job_info " + _hostname + " " + jobid.getReference() + " && ";
268 subCommand += "vishnu_close";
269 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
273 int status = Utils::getCommandOutput(command, output);
275 throw RunTimeException("Can't query job " + jobid.getReference());
276 JobInfo_Vishnu jobinfo = JobInfo_Vishnu(jobid.getReference(), output);
280 void BatchManager_Vishnu::importOutputFiles(const Job & job, const std::string directory)
282 // Create local result directory
283 char * buf = getcwd(NULL, 0);
286 string absdir = (Utils::isAbsolutePath(directory))? directory : cwd + "/" + directory;
287 int status = CommunicationProtocol::getInstance(SH).makeDirectory(absdir, "", "");
289 throw RunTimeException("Can't create result directory");
292 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
293 subCommand += "vishnu_connect && ";
296 Parametre params = job.getParametre();
297 const Versatile & V = params[OUTFILE];
298 Versatile::const_iterator Vit;
299 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
300 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
301 Couple outputFile = cpt;
303 // Get absolute paths
304 string absremote = (Utils::isAbsolutePath(outputFile.getRemote()))?
305 outputFile.getRemote() :
306 params[WORKDIR].str() + "/" + outputFile.getRemote();
307 string abslocal = (Utils::isAbsolutePath(outputFile.getLocal()))?
308 outputFile.getLocal() :
309 absdir + "/" + outputFile.getLocal();
311 subCommand += "vishnu_copy_file " + _hostname + ":" + absremote + " " + abslocal + " && ";
315 subCommand += "vishnu_copy_file -r " +_hostname + ":" + params[WORKDIR].str() + "/logs" + " " + absdir + " && ";
316 subCommand += "vishnu_close";
319 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
323 status = Utils::getCommandOutput(command, output);
326 throw RunTimeException("Can't import output files, error was: " + output);