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_eVishnu.cxx :
25 * Created on: 24 june 2011
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_eVishnu.hxx"
39 #include "Batch_BatchManager_eVishnu.hxx"
40 #include "Batch_JobInfo_eVishnu.hxx"
46 BatchManager_eVishnu::BatchManager_eVishnu(const FactBatchManager * parent,
48 const char * username,
49 CommunicationProtocolType protocolType,
52 : BatchManager(parent, host),
53 // Force SH protocol for Vishnu
54 BatchManager_eClient(parent, host, username, SH, mpiImpl),
55 _nb_proc_per_node(nb_proc_per_node)
59 BatchManager_eVishnu::~BatchManager_eVishnu()
63 // Method to submit a job to the batch manager
64 const JobId BatchManager_eVishnu::submitJob(const Job & job)
67 Parametre params = job.getParametre();
68 const string workDir = params[WORKDIR];
70 // export input files on cluster
71 exportInputFiles(job);
73 // build command file to submit the job
74 string cmdFile = buildCommandFile(job);
76 // define extra parameters (that can not be defined in the command file)
77 ostringstream extraParams;
78 if (params.find(NBPROC) != params.end())
79 extraParams << "-P " << params[NBPROC] << " ";
80 if (params.find(MAXRAMSIZE) != params.end())
81 extraParams << "-m " << params[MAXRAMSIZE] << " ";
83 // define name of log file (local)
84 string logFile = generateTemporaryFileName("vishnu-submitlog");
86 // define command to submit batch
87 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
88 subCommand += "vishnu_connect -p 2; ";
89 subCommand += "vishnu_submit_job " + extraParams.str() + _hostname + " " + cmdFile;
90 string command = _protocol.getExecCommand(subCommand,
95 cerr << command.c_str() << endl;
96 status = system(command.c_str());
99 ifstream error_message(logFile.c_str());
102 while(getline(error_message, temp))
104 error_message.close();
105 throw EmulationException("Error of connection on remote host, error was: " + mess);
108 // read id of submitted job in log file
110 ifstream idfile(logFile.c_str());
112 while (idfile && line.compare(0, 13, "Job Id : ") != 0)
113 getline(idfile, line);
115 if (line.compare(0, 13, "Job Id : ") == 0)
116 jobref = line.substr(13);
117 if (jobref.size() == 0)
118 throw EmulationException("Error in the submission of the job on the remote host");
120 JobId id(this, jobref);
125 void BatchManager_eVishnu::exportInputFiles(const Job& job)
128 Parametre params = job.getParametre();
129 const Versatile & V = params[INFILE];
130 Versatile::const_iterator Vit;
132 // create remote directories
133 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
134 subCommand += "vishnu_connect -p 2; ";
135 subCommand += "vishnu_create_dir -p " + _hostname + ":" + params[WORKDIR].str() + "/logs";
136 string command = _protocol.getExecCommand(subCommand,
139 cerr << command.c_str() << endl;
140 status = system(command.c_str());
142 throw EmulationException("Can't create remote directories");
145 string executeFile = params[EXECUTABLE];
146 if (executeFile.size() != 0) {
148 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
149 subCommand += "vishnu_connect -p 2; ";
150 subCommand += "vishnu_copy_file " + executeFile + " " + _hostname + ":" + params[WORKDIR].str() + "/";
151 string command = _protocol.getExecCommand(subCommand,
154 cerr << command.c_str() << endl;
155 status = system(command.c_str());
157 throw EmulationException("Can't copy executable");
160 // copy filesToExportList
161 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
162 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
163 Couple inputFile = cpt;
165 // Get absolute paths
166 char * buf = getcwd(NULL, 0);
170 string absremote = (Utils::isAbsolutePath(inputFile.getRemote()))?
171 inputFile.getRemote() :
172 params[WORKDIR].str() + "/" + inputFile.getRemote();
173 string abslocal = (Utils::isAbsolutePath(inputFile.getLocal()))?
174 inputFile.getLocal() :
175 cwd + "/" + inputFile.getLocal();
177 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
178 subCommand += "vishnu_connect -p 2; ";
179 subCommand += "vishnu_copy_file " + abslocal + " " + _hostname + ":" + absremote;
180 string command = _protocol.getExecCommand(subCommand,
183 cerr << command.c_str() << endl;
184 status = system(command.c_str());
186 throw EmulationException("Can't copy file");
191 * Create Vishnu command file and copy it on the server.
192 * Return the name of the remote file.
194 string BatchManager_eVishnu::buildCommandFile(const Job & job)
196 Parametre params = job.getParametre();
200 string fileToExecute = "";
203 // Mandatory parameters
204 if (params.find(WORKDIR) != params.end())
205 workDir = params[WORKDIR].str();
207 throw EmulationException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
208 if (params.find(EXECUTABLE) != params.end())
209 fileToExecute = params[EXECUTABLE].str();
211 throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
213 string::size_type p1 = fileToExecute.find_last_of("/");
214 string::size_type p2 = fileToExecute.find_last_of(".");
215 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
216 string fileNameToExecute = fileToExecute.substr(p1+1);
218 // Create batch submit file
219 ofstream tempOutputFile;
220 string tmpFileName = createAndOpenTemporaryFile("vishnu-script", tempOutputFile);
222 tempOutputFile << "#!/bin/sh" << endl;
223 tempOutputFile << "#% vishnu_output=" << workDir << "/logs/output.log." << rootNameToExecute << endl;
224 tempOutputFile << "#% vishnu_error=" << workDir << "/logs/error.log." << rootNameToExecute << endl;
226 if (params.find(NAME) != params.end())
227 tempOutputFile << "#% vishnu_job_name=\"" << params[NAME] << "\"" << endl;
229 // Optional parameters
230 if (params.find(MAXWALLTIME) != params.end()) {
231 long totalMinutes = params[MAXWALLTIME];
232 long h = totalMinutes / 60;
233 long m = totalMinutes - h * 60;
234 tempOutputFile << "#% vishnu_wallclocklimit=" << h << ":";
236 tempOutputFile << "0";
237 tempOutputFile << m << ":00" << endl;
239 if (params.find(QUEUE) != params.end())
240 tempOutputFile << "#% vishnu_queue=" << params[QUEUE] << endl;
242 // Define environment for the job
243 Environnement env = job.getEnvironnement();
244 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
245 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
249 tempOutputFile << "export LIBBATCH_NODEFILE=$VISHNU_BATCHJOB_NODEFILE" << endl;
251 // Launch the executable
252 tempOutputFile << "cd " << workDir << endl;
253 tempOutputFile << "./" + fileNameToExecute << endl;
255 tempOutputFile.flush();
256 tempOutputFile.close();
258 cerr << "Batch script file generated is: " << tmpFileName << endl;
262 void BatchManager_eVishnu::deleteJob(const JobId & jobid)
265 istringstream iss(jobid.getReference());
266 getline(iss, vishnuJobId, ':');
268 // define command to delete job
269 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
270 subCommand += "vishnu_connect -p 2; ";
271 subCommand += "vishnu_cancel_job " + _hostname + " " + vishnuJobId;
272 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
273 cerr << command.c_str() << endl;
275 int status = system(command.c_str());
277 throw EmulationException("Can't delete job " + jobid.getReference());
279 cerr << "job " << jobid.getReference() << " killed" << endl;
282 void BatchManager_eVishnu::holdJob(const JobId & jobid)
284 throw NotYetImplementedException("BatchManager_eVishnu::holdJob");
287 void BatchManager_eVishnu::releaseJob(const JobId & jobid)
289 throw NotYetImplementedException("BatchManager_eVishnu::releaseJob");
292 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
294 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
297 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param)
299 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
302 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Environnement & env)
304 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
307 JobInfo BatchManager_eVishnu::queryJob(const JobId & jobid)
309 // define name of log file (local)
310 string logFile = generateTemporaryFileName("vishnu-querylog-" + jobid.getReference());
313 istringstream iss(jobid.getReference());
314 getline(iss, vishnuJobId, ':');
316 // define command to query batch
317 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
318 subCommand += "vishnu_connect -p 2; ";
319 subCommand += "vishnu_get_job_info " + _hostname + " " + vishnuJobId;
320 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
323 cerr << command.c_str() << endl;
324 int status = system(command.c_str());
326 throw EmulationException("Can't query job " + jobid.getReference());
328 JobInfo_eVishnu jobinfo = JobInfo_eVishnu(jobid.getReference(), logFile);
332 const JobId BatchManager_eVishnu::addJob(const Job & job, const string reference)
334 return JobId(this, reference);
337 void BatchManager_eVishnu::importOutputFiles(const Job & job, const std::string directory)
339 Parametre params = job.getParametre();
340 const Versatile & V = params[OUTFILE];
341 Versatile::const_iterator Vit;
343 // Create local result directory
344 char * buf = getcwd(NULL, 0);
347 string absdir = (Utils::isAbsolutePath(directory))? directory : cwd + "/" + directory;
348 int status = CommunicationProtocol::getInstance(SH).makeDirectory(absdir, "", "");
350 string mess("Directory creation failed. Status is :");
351 ostringstream status_str;
352 status_str << status;
353 mess += status_str.str();
354 cerr << mess << endl;
357 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
358 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
359 Couple outputFile = cpt;
361 // Get absolute paths
362 string absremote = (Utils::isAbsolutePath(outputFile.getRemote()))?
363 outputFile.getRemote() :
364 params[WORKDIR].str() + "/" + outputFile.getRemote();
365 string abslocal = (Utils::isAbsolutePath(outputFile.getLocal()))?
366 outputFile.getLocal() :
367 absdir + "/" + outputFile.getLocal();
369 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
370 subCommand += "vishnu_connect -p 2; ";
371 subCommand += "vishnu_copy_file " + _hostname + ":" + absremote + " " + abslocal;
372 string command = _protocol.getExecCommand(subCommand,
375 cerr << command.c_str() << endl;
376 status = system(command.c_str());
378 throw EmulationException("Can't copy file");
382 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
383 subCommand += "vishnu_connect -p 2; ";
384 subCommand += "vishnu_copy_file -r " +_hostname + ":" + params[WORKDIR].str() + "/logs" + " " + absdir;
385 string command = _protocol.getExecCommand(subCommand,
388 cerr << command.c_str() << endl;
389 status = system(command.c_str());
391 throw EmulationException("Can't copy logs");