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 name of log file (local)
77 string logFile = generateTemporaryFileName("vishnu-submitlog");
79 // define command to submit batch
80 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
81 subCommand += "vishnu_connect -p 2; ";
82 subCommand += "vishnu_submit_job " + _hostname + " " + cmdFile;
83 string command = _protocol.getExecCommand(subCommand,
88 cerr << command.c_str() << endl;
89 status = system(command.c_str());
92 ifstream error_message(logFile.c_str());
95 while(getline(error_message, temp))
97 error_message.close();
98 throw EmulationException("Error of connection on remote host, error was: " + mess);
101 // read id of submitted job in log file
103 ifstream idfile(logFile.c_str());
105 while (idfile && line.compare(0, 13, "Job Id : ") != 0)
106 getline(idfile, line);
108 if (line.compare(0, 13, "Job Id : ") == 0)
109 jobref = line.substr(13);
110 if (jobref.size() == 0)
111 throw EmulationException("Error in the submission of the job on the remote host");
113 JobId id(this, jobref);
118 void BatchManager_eVishnu::exportInputFiles(const Job& job)
121 Parametre params = job.getParametre();
122 const Versatile & V = params[INFILE];
123 Versatile::const_iterator Vit;
125 // create remote directories
126 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
127 subCommand += "vishnu_connect -p 2; ";
128 subCommand += "vishnu_create_dir -p " + _hostname + ":" + params[WORKDIR].str() + "/logs";
129 string command = _protocol.getExecCommand(subCommand,
132 cerr << command.c_str() << endl;
133 status = system(command.c_str());
135 throw EmulationException("Can't create remote directories");
138 string executeFile = params[EXECUTABLE];
139 if (executeFile.size() != 0) {
141 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
142 subCommand += "vishnu_connect -p 2; ";
143 subCommand += "vishnu_copy_file " + executeFile + " " + _hostname + ":" + params[WORKDIR].str() + "/";
144 string command = _protocol.getExecCommand(subCommand,
147 cerr << command.c_str() << endl;
148 status = system(command.c_str());
150 throw EmulationException("Can't copy executable");
153 // copy filesToExportList
154 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
155 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
156 Couple inputFile = cpt;
158 // Get absolute paths
159 char * buf = getcwd(NULL, 0);
163 string absremote = (Utils::isAbsolutePath(inputFile.getRemote()))?
164 inputFile.getRemote() :
165 params[WORKDIR].str() + "/" + inputFile.getRemote();
166 string abslocal = (Utils::isAbsolutePath(inputFile.getLocal()))?
167 inputFile.getLocal() :
168 cwd + "/" + inputFile.getLocal();
170 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
171 subCommand += "vishnu_connect -p 2; ";
172 subCommand += "vishnu_copy_file " + abslocal + " " + _hostname + ":" + absremote;
173 string command = _protocol.getExecCommand(subCommand,
176 cerr << command.c_str() << endl;
177 status = system(command.c_str());
179 throw EmulationException("Can't copy file");
184 * Create Vishnu command file and copy it on the server.
185 * Return the name of the remote file.
187 string BatchManager_eVishnu::buildCommandFile(const Job & job)
189 Parametre params = job.getParametre();
193 string fileToExecute = "";
196 // Mandatory parameters
197 if (params.find(WORKDIR) != params.end())
198 workDir = params[WORKDIR].str();
200 throw EmulationException("params[WORKDIR] is not defined. Please define it, cannot submit this job.");
201 if (params.find(EXECUTABLE) != params.end())
202 fileToExecute = params[EXECUTABLE].str();
204 throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
206 string::size_type p1 = fileToExecute.find_last_of("/");
207 string::size_type p2 = fileToExecute.find_last_of(".");
208 string rootNameToExecute = fileToExecute.substr(p1+1,p2-p1-1);
209 string fileNameToExecute = fileToExecute.substr(p1+1);
211 // Create batch submit file
212 ofstream tempOutputFile;
213 string tmpFileName = createAndOpenTemporaryFile("vishnu-script", tempOutputFile);
215 tempOutputFile << "#!/bin/sh" << endl;
216 tempOutputFile << "#% vishnu_output=" << workDir << "/logs/output.log." << rootNameToExecute << endl;
217 tempOutputFile << "#% vishnu_error=" << workDir << "/logs/error.log." << rootNameToExecute << endl;
219 if (params.find(NAME) != params.end())
220 tempOutputFile << "#% vishnu_job_name=\"" << params[NAME] << "\"" << endl;
222 // Optional parameters
224 if (params.find(NBPROC) != params.end())
225 nbproc = params[NBPROC];
227 //int nodes_requested = (nbproc + _nb_proc_per_node -1) / _nb_proc_per_node;
228 //tempOutputFile << "#SBATCH --nodes=" << nodes_requested << endl;
229 //tempOutputFile << "#SBATCH --ntasks-per-node=" << _nb_proc_per_node << endl;
231 if (params.find(MAXWALLTIME) != params.end()) {
232 long totalMinutes = params[MAXWALLTIME];
233 long h = totalMinutes / 60;
234 long m = totalMinutes - h * 60;
235 tempOutputFile << "#% vishnu_wallclocklimit=" << h << ":";
237 tempOutputFile << "0";
238 tempOutputFile << m << ":00" << endl;
240 //if (params.find(MAXRAMSIZE) != params.end())
241 // tempOutputFile << "#SBATCH --mem=" << params[MAXRAMSIZE] << endl;
242 if (params.find(QUEUE) != params.end())
243 tempOutputFile << "#% vishnu_queue=" << params[QUEUE] << endl;
245 // Define environment for the job
246 Environnement env = job.getEnvironnement();
247 for (Environnement::const_iterator iter = env.begin() ; iter != env.end() ; ++iter) {
248 tempOutputFile << "export " << iter->first << "=" << iter->second << endl;
251 // generate nodes file
252 //tempOutputFile << "LIBBATCH_NODEFILE=`mktemp nodefile-XXXXXXXXXX`" << endl;
253 //tempOutputFile << "srun hostname > $LIBBATCH_NODEFILE" << endl;
254 //tempOutputFile << "export LIBBATCH_NODEFILE" << endl;
256 // Launch the executable
257 tempOutputFile << "cd " << workDir << endl;
258 tempOutputFile << "./" + fileNameToExecute << endl;
260 tempOutputFile.flush();
261 tempOutputFile.close();
263 cerr << "Batch script file generated is: " << tmpFileName << endl;
267 void BatchManager_eVishnu::deleteJob(const JobId & jobid)
270 istringstream iss(jobid.getReference());
271 getline(iss, vishnuJobId, ':');
273 // define command to delete job
274 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
275 subCommand += "vishnu_connect -p 2; ";
276 subCommand += "vishnu_cancel_job " + _hostname + " " + vishnuJobId;
277 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
278 cerr << command.c_str() << endl;
280 int status = system(command.c_str());
282 throw EmulationException("Can't delete job " + jobid.getReference());
284 cerr << "job " << jobid.getReference() << " killed" << endl;
287 void BatchManager_eVishnu::holdJob(const JobId & jobid)
289 throw NotYetImplementedException("BatchManager_eVishnu::holdJob");
292 void BatchManager_eVishnu::releaseJob(const JobId & jobid)
294 throw NotYetImplementedException("BatchManager_eVishnu::releaseJob");
297 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
299 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
302 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param)
304 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
307 void BatchManager_eVishnu::alterJob(const JobId & jobid, const Environnement & env)
309 throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
312 JobInfo BatchManager_eVishnu::queryJob(const JobId & jobid)
314 // define name of log file (local)
315 string logFile = generateTemporaryFileName("vishnu-querylog-" + jobid.getReference());
318 istringstream iss(jobid.getReference());
319 getline(iss, vishnuJobId, ':');
321 // define command to query batch
322 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
323 subCommand += "vishnu_connect -p 2; ";
324 subCommand += "vishnu_get_job_info " + _hostname + " " + vishnuJobId;
325 string command = _protocol.getExecCommand(subCommand, _hostname, _username);
328 cerr << command.c_str() << endl;
329 int status = system(command.c_str());
331 throw EmulationException("Can't query job " + jobid.getReference());
333 JobInfo_eVishnu jobinfo = JobInfo_eVishnu(jobid.getReference(), logFile);
337 const JobId BatchManager_eVishnu::addJob(const Job & job, const string reference)
339 return JobId(this, reference);
342 void BatchManager_eVishnu::importOutputFiles(const Job & job, const std::string directory)
344 Parametre params = job.getParametre();
345 const Versatile & V = params[OUTFILE];
346 Versatile::const_iterator Vit;
348 // Create local result directory
349 char * buf = getcwd(NULL, 0);
352 string absdir = (Utils::isAbsolutePath(directory))? directory : cwd + "/" + directory;
353 int status = CommunicationProtocol::getInstance(SH).makeDirectory(absdir, "", "");
355 string mess("Directory creation failed. Status is :");
356 ostringstream status_str;
357 status_str << status;
358 mess += status_str.str();
359 cerr << mess << endl;
362 for (Vit=V.begin(); Vit!=V.end(); Vit++) {
363 CoupleType cpt = *static_cast< CoupleType * >(*Vit);
364 Couple outputFile = cpt;
366 // Get absolute paths
367 string absremote = (Utils::isAbsolutePath(outputFile.getRemote()))?
368 outputFile.getRemote() :
369 params[WORKDIR].str() + "/" + outputFile.getRemote();
370 string abslocal = (Utils::isAbsolutePath(outputFile.getLocal()))?
371 outputFile.getLocal() :
372 absdir + "/" + outputFile.getLocal();
374 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
375 subCommand += "vishnu_connect -p 2; ";
376 subCommand += "vishnu_copy_file " + _hostname + ":" + absremote + " " + abslocal;
377 string command = _protocol.getExecCommand(subCommand,
380 cerr << command.c_str() << endl;
381 status = system(command.c_str());
383 throw EmulationException("Can't copy file");
387 string subCommand = string("export OMNIORB_CONFIG=$VISHNU_CONFIG_FILE; ");
388 subCommand += "vishnu_connect -p 2; ";
389 subCommand += "vishnu_copy_file -r " +_hostname + ":" + params[WORKDIR].str() + "/logs" + " " + absdir;
390 string command = _protocol.getExecCommand(subCommand,
393 cerr << command.c_str() << endl;
394 status = system(command.c_str());
396 throw EmulationException("Can't copy logs");