]> SALOME platform Git repositories - tools/libbatch.git/blob - src/Vishnu/Batch_BatchManager_eVishnu.cxx
Salome HOME
Add parameters nbproc and memory for Vishnu jobs
[tools/libbatch.git] / src / Vishnu / Batch_BatchManager_eVishnu.cxx
1 //  Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  *  Batch_BatchManager_eVishnu.cxx :
24  *
25  *  Created on: 24 june 2011
26  *  Author : Renaud BARATE - EDF R&D
27  */
28
29 #include <cstdlib>
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33
34 #include <Batch_NotYetImplementedException.hxx>
35 #include <Batch_Constants.hxx>
36 #include <Batch_Utils.hxx>
37
38 #include "Batch_FactBatchManager_eVishnu.hxx"
39 #include "Batch_BatchManager_eVishnu.hxx"
40 #include "Batch_JobInfo_eVishnu.hxx"
41
42 using namespace std;
43
44 namespace Batch {
45
46   BatchManager_eVishnu::BatchManager_eVishnu(const FactBatchManager * parent,
47                                              const char * host,
48                                              const char * username,
49                                              CommunicationProtocolType protocolType,
50                                              const char * mpiImpl,
51                                              int nb_proc_per_node)
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)
56   {
57   }
58
59   BatchManager_eVishnu::~BatchManager_eVishnu()
60   {
61   }
62
63   // Method to submit a job to the batch manager
64   const JobId BatchManager_eVishnu::submitJob(const Job & job)
65   {
66     int status;
67     Parametre params = job.getParametre();
68     const string workDir = params[WORKDIR];
69
70     // export input files on cluster
71     exportInputFiles(job);
72
73     // build command file to submit the job
74     string cmdFile = buildCommandFile(job);
75
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] << " ";
82
83     // define name of log file (local)
84     string logFile = generateTemporaryFileName("vishnu-submitlog");
85
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,
91                                               _hostname,
92                                               _username);
93     command += " > ";
94     command += logFile;
95     cerr << command.c_str() << endl;
96     status = system(command.c_str());
97     if (status)
98     {
99       ifstream error_message(logFile.c_str());
100       string mess;
101       string temp;
102       while(getline(error_message, temp))
103         mess += temp;
104       error_message.close();
105       throw EmulationException("Error of connection on remote host, error was: " + mess);
106     }
107
108     // read id of submitted job in log file
109     string jobref;
110     ifstream idfile(logFile.c_str());
111     string line;
112     while (idfile && line.compare(0, 13, "Job Id     : ") != 0)
113       getline(idfile, line);
114     idfile.close();
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");
119
120     JobId id(this, jobref);
121     return id;
122   }
123
124
125   void BatchManager_eVishnu::exportInputFiles(const Job& job)
126   {
127     int status;
128     Parametre params = job.getParametre();
129     const Versatile & V = params[INFILE];
130     Versatile::const_iterator Vit;
131
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,
137                                               _hostname,
138                                               _username);
139     cerr << command.c_str() << endl;
140     status = system(command.c_str());
141     if (status != 0)
142       throw EmulationException("Can't create remote directories");
143
144     // copy executable
145     string executeFile = params[EXECUTABLE];
146     if (executeFile.size() != 0) {
147
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,
152                                                 _hostname,
153                                                 _username);
154       cerr << command.c_str() << endl;
155       status = system(command.c_str());
156       if (status != 0)
157         throw EmulationException("Can't copy executable");
158     }
159
160     // copy filesToExportList
161     for (Vit=V.begin(); Vit!=V.end(); Vit++) {
162       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
163       Couple inputFile = cpt;
164
165       // Get absolute paths
166       char * buf = getcwd(NULL, 0);
167       string cwd = buf;
168       free(buf);
169
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();
176
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,
181                                                 _hostname,
182                                                 _username);
183       cerr << command.c_str() << endl;
184       status = system(command.c_str());
185       if (status != 0)
186         throw EmulationException("Can't copy file");
187     }
188   }
189
190   /**
191    * Create Vishnu command file and copy it on the server.
192    * Return the name of the remote file.
193    */
194   string BatchManager_eVishnu::buildCommandFile(const Job & job)
195   {
196     Parametre params = job.getParametre();
197
198     // Job Parameters
199     string workDir = "";
200     string fileToExecute = "";
201     string queue = "";
202
203     // Mandatory parameters
204     if (params.find(WORKDIR) != params.end()) 
205       workDir = params[WORKDIR].str();
206     else 
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();
210     else 
211       throw EmulationException("params[EXECUTABLE] is not defined. Please define it, cannot submit this job.");
212
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);
217
218     // Create batch submit file
219     ofstream tempOutputFile;
220     string tmpFileName = createAndOpenTemporaryFile("vishnu-script", tempOutputFile);
221
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;
225
226     if (params.find(NAME) != params.end())
227       tempOutputFile << "#% vishnu_job_name=\"" << params[NAME] << "\"" << endl;
228
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 << ":";
235       if (m < 10)
236         tempOutputFile << "0";
237       tempOutputFile << m << ":00" << endl;
238     }
239     if (params.find(QUEUE) != params.end())
240       tempOutputFile << "#% vishnu_queue=" << params[QUEUE] << endl;
241
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;
246     }
247
248     // Node file
249     tempOutputFile << "export LIBBATCH_NODEFILE=$VISHNU_BATCHJOB_NODEFILE" << endl;
250
251     // Launch the executable
252     tempOutputFile << "cd " << workDir << endl;
253     tempOutputFile << "./" + fileNameToExecute << endl;
254
255     tempOutputFile.flush();
256     tempOutputFile.close();
257
258     cerr << "Batch script file generated is: " << tmpFileName << endl;
259     return tmpFileName;
260   }
261
262   void BatchManager_eVishnu::deleteJob(const JobId & jobid)
263   {
264     string vishnuJobId;
265     istringstream iss(jobid.getReference());
266     getline(iss, vishnuJobId, ':');
267
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;
274
275     int status = system(command.c_str());
276     if (status)
277       throw EmulationException("Can't delete job " + jobid.getReference());
278
279     cerr << "job " << jobid.getReference() << " killed" << endl;
280   }
281
282   void BatchManager_eVishnu::holdJob(const JobId & jobid)
283   {
284     throw NotYetImplementedException("BatchManager_eVishnu::holdJob");
285   }
286
287   void BatchManager_eVishnu::releaseJob(const JobId & jobid)
288   {
289     throw NotYetImplementedException("BatchManager_eVishnu::releaseJob");
290   }
291
292   void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
293   {
294     throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
295   }
296
297   void BatchManager_eVishnu::alterJob(const JobId & jobid, const Parametre & param)
298   {
299     throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
300   }
301
302   void BatchManager_eVishnu::alterJob(const JobId & jobid, const Environnement & env)
303   {
304     throw NotYetImplementedException("BatchManager_eVishnu::alterJob");
305   }
306
307   JobInfo BatchManager_eVishnu::queryJob(const JobId & jobid)
308   {
309     // define name of log file (local)
310     string logFile = generateTemporaryFileName("vishnu-querylog-" + jobid.getReference());
311
312     string vishnuJobId;
313     istringstream iss(jobid.getReference());
314     getline(iss, vishnuJobId, ':');
315
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);
321     command += " > ";
322     command += logFile;
323     cerr << command.c_str() << endl;
324     int status = system(command.c_str());
325     if (status != 0)
326       throw EmulationException("Can't query job " + jobid.getReference());
327
328     JobInfo_eVishnu jobinfo = JobInfo_eVishnu(jobid.getReference(), logFile);
329     return jobinfo;
330   }
331
332   const JobId BatchManager_eVishnu::addJob(const Job & job, const string reference)
333   {
334     return JobId(this, reference);
335   }
336
337   void BatchManager_eVishnu::importOutputFiles(const Job & job, const std::string directory)
338   {
339     Parametre params = job.getParametre();
340     const Versatile & V = params[OUTFILE];
341     Versatile::const_iterator Vit;
342
343     // Create local result directory
344     char * buf = getcwd(NULL, 0);
345     string cwd = buf;
346     free(buf);
347     string absdir = (Utils::isAbsolutePath(directory))? directory : cwd + "/" + directory;
348     int status = CommunicationProtocol::getInstance(SH).makeDirectory(absdir, "", "");
349     if (status) {
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;
355     }
356
357     for (Vit=V.begin(); Vit!=V.end(); Vit++) {
358       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
359       Couple outputFile = cpt;
360
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();
368
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,
373                                                 _hostname,
374                                                 _username);
375       cerr << command.c_str() << endl;
376       status = system(command.c_str());
377       if (status != 0)
378         throw EmulationException("Can't copy file");
379     }
380
381     // Copy logs
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,
386                                               _hostname,
387                                               _username);
388     cerr << command.c_str() << endl;
389     status = system(command.c_str());
390     if (status != 0)
391       throw EmulationException("Can't copy logs");
392   }
393
394 }