Salome HOME
just a little modification
[modules/kernel.git] / src / Batch / Batch_Job_LSF.cxx
1 //  Copyright (C) 2007-2008  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  * Job_LSF.cxx : 
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Fri Nov 14 11:00:39 2003
28  * Projet : Salome 2
29  *
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string>
36 #include <vector>
37 #include "Batch_Job_LSF.hxx"
38
39 using namespace std;
40
41 namespace Batch {
42
43
44   // Constructeur
45   Job_LSF::Job_LSF(const Job & job) : _p_submit(0)
46   {
47     Parametre P = job.getParametre();
48     _p_submit = ParametreToSubmitStruct(P);
49   }
50
51
52   // Destructeur
53   Job_LSF::~Job_LSF()
54   {
55     if (_p_submit) {
56       if (_p_submit->jobName)     delete [] _p_submit->jobName;
57       if (_p_submit->queue)       delete [] _p_submit->queue;
58       if (_p_submit->askedHosts) {
59         delete [] *(_p_submit->askedHosts);
60         delete [] _p_submit->askedHosts;
61       }
62       if (_p_submit->resReq)      delete [] _p_submit->resReq;
63       if (_p_submit->hostSpec)    delete [] _p_submit->hostSpec;
64       if (_p_submit->dependCond)  delete [] _p_submit->dependCond;
65       if (_p_submit->timeEvent)   delete [] _p_submit->timeEvent;
66       if (_p_submit->inFile)      delete [] _p_submit->inFile;
67       if (_p_submit->outFile)     delete [] _p_submit->outFile;
68       if (_p_submit->errFile)     delete [] _p_submit->errFile;
69       if (_p_submit->command)     delete [] _p_submit->command;
70       if (_p_submit->newCommand)  delete [] _p_submit->newCommand;
71       if (_p_submit->chkpntDir)   delete [] _p_submit->chkpntDir;
72       if (_p_submit->xf)          delete [] _p_submit->xf;
73       if (_p_submit->preExecCmd)  delete [] _p_submit->preExecCmd;
74       if (_p_submit->mailUser)    delete [] _p_submit->mailUser;
75       if (_p_submit->projectName) delete [] _p_submit->projectName;
76       if (_p_submit->loginShell)  delete [] _p_submit->loginShell;
77       if (_p_submit->exceptList)  delete [] _p_submit->exceptList;
78       delete _p_submit;
79     }
80   }
81
82
83   // Accesseur
84   struct submit * Job_LSF::getSubmitStruct()
85   {
86     return _p_submit;
87   }
88
89
90   char * Job_LSF::string2char(const string & s)
91   {
92     char * ch = new char [s.size() + 1];
93     memset(ch, 0, s.size() + 1);
94     strncat(ch, s.c_str(), s.size());
95     return ch;
96   }
97
98
99   struct submit * Job_LSF::ParametreToSubmitStruct(const Parametre & P)
100   {
101     if (! _p_submit) _p_submit = new struct submit;
102
103     memset( (void *) _p_submit, 0, sizeof(struct submit));
104
105     struct submit & sub = * _p_submit;
106     sub.options  = 0;
107     sub.options2 = 0;
108
109     sub.beginTime = 0; // job can run as soon as possible (default)
110     sub.termTime  = 0; // job can run as long as it wishes (default)
111
112     sub.numProcessors    = 1; // job can run on one single processor (default)
113     sub.maxNumProcessors = 1; // job can run on one single processor (default)
114
115     for(int i = 0; i< LSF_RLIM_NLIMITS; i++) sub.rLimits[i] = DEFAULT_RLIMIT;
116
117     typedef std::vector< struct xFile > XFTAB;
118     XFTAB xf_tab;
119
120     string st_second;
121     for(Parametre::const_iterator it = P.begin(); it != P.end(); it++) {
122       if ( (*it).first == ACCOUNT ) {
123         sub.options |= SUB_PROJECT_NAME;
124         st_second = (*it).second.str();
125         sub.projectName = string2char(st_second);
126
127       } else if ( (*it).first == CHECKPOINT ) {
128         if (static_cast< long >((*it).second))
129           sub.options |= SUB_CHKPNT_PERIOD;
130         else
131           sub.options &= ~ SUB_CHKPNT_PERIOD;
132
133       } else if ( (*it).first == CKPTINTERVAL ) {
134         sub.chkpntPeriod = static_cast< long >((*it).second);
135
136       } else if ( (*it).first == EXECUTABLE ) {
137         st_second = (*it).second.str();
138         sub.command = string2char(st_second);
139
140       } else if ( (*it).first == EXECUTIONHOST ) {
141         sub.options |= SUB_HOST;
142         if (! sub.numAskedHosts) {
143           sub.numAskedHosts = 1;
144           sub.askedHosts = new char* [1];
145         }
146         st_second = (*it).second.str();
147         sub.askedHosts[0] = string2char(st_second);
148
149       } else if ( (*it).first == HOLD ) {
150         if (static_cast< long >((*it).second))
151           sub.options2 |= SUB2_HOLD;
152         else
153           sub.options2 &= ~ SUB2_HOLD;
154
155       } else if ( (*it).first == INFILE ) {
156         Versatile V = (*it).second;
157         Versatile::iterator Vit;
158
159         for(Vit=V.begin(); Vit!=V.end(); Vit++) {
160           CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
161           Couple cp       = cpt;
162           string local    = cp.getLocal();
163           string remote   = cp.getRemote();
164                                         
165           // ATTENTION : les notions de fichier "local" ou "remote" sont inverses de celle de PBS qui a un point de vue serveur et non pas utilisateur
166           if (remote == "stdin"){
167             sub.options |= SUB_IN_FILE;
168             sub.inFile = string2char(local);
169
170           } else {
171             struct xFile xf;
172             strncpy(xf.subFn,  local.c_str(),  MAXFILENAMELEN - 1); xf.subFn[MAXFILENAMELEN - 1]  = 0;
173             strncpy(xf.execFn, remote.c_str(), MAXFILENAMELEN - 1); xf.execFn[MAXFILENAMELEN - 1] = 0;
174             xf.options = XF_OP_SUB2EXEC;
175             xf_tab.push_back(xf);
176           }
177         }
178
179       } else if ( (*it).first == MAIL ) {
180         sub.options |= SUB_MAIL_USER;
181         st_second = (*it).second.str();
182         sub.mailUser = string2char(st_second);
183
184       } else if ( (*it).first == MAXCPUTIME ) {
185         sub.rLimits[LSF_RLIMIT_CPU] = static_cast< long >((*it).second);
186
187       } else if ( (*it).first == MAXDISKSIZE ) {
188         sub.rLimits[LSF_RLIMIT_FSIZE] = static_cast< long >((*it).second);
189
190       } else if ( (*it).first == MAXRAMSIZE ) {
191         sub.rLimits[LSF_RLIMIT_SWAP] = static_cast< long >((*it).second);
192
193       } else if ( (*it).first == MAXWALLTIME ) {
194         sub.rLimits[LSF_RLIMIT_RUN] = static_cast< long >((*it).second);
195
196       } else if ( (*it).first == NAME ) {
197         sub.options |= SUB_JOB_NAME;
198         st_second = (*it).second.str();
199         sub.jobName = string2char(st_second);
200
201       } else if ( (*it).first == NBPROC ) {
202         sub.numProcessors    = static_cast< long >((*it).second);
203         sub.maxNumProcessors = static_cast< long >((*it).second);
204
205       } else if ( (*it).first == OUTFILE ) {
206         Versatile V = (*it).second;
207         Versatile::iterator Vit;
208
209         for(Vit=V.begin(); Vit!=V.end(); Vit++) {
210           CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
211           Couple cp       = cpt;
212           string local    = cp.getLocal();
213           string remote   = cp.getRemote();
214                                         
215           // ATTENTION : les notions de fichier "local" ou "remote" sont inverses de celle de PBS qui a un point de vue serveur et non pas utilisateur
216           if (remote == "stdout"){
217             sub.options |= SUB_OUT_FILE;
218             sub.outFile = string2char(local);
219
220           } else if (remote == "stderr"){
221             sub.options |= SUB_ERR_FILE;
222             sub.errFile = string2char(local);
223
224           } else {
225             struct xFile xf;
226             strncpy(xf.subFn,  local.c_str(),  MAXFILENAMELEN - 1); xf.subFn[MAXFILENAMELEN - 1]  = 0;
227             strncpy(xf.execFn, remote.c_str(), MAXFILENAMELEN - 1); xf.execFn[MAXFILENAMELEN - 1] = 0;
228             xf.options = XF_OP_EXEC2SUB;
229             xf_tab.push_back(xf);
230           }
231         }
232
233
234       } else if ( (*it).first == QUEUE ) {
235         sub.options |= SUB_QUEUE;
236         st_second = (*it).second.str();
237         sub.queue = string2char(st_second);
238
239       } else if ( (*it).first == STARTTIME ) {
240         sub.beginTime = static_cast< long >((*it).second);
241
242       } else if ( (*it).first == TMPDIR ) {
243         // TODO
244
245       } else if ( (*it).first == USER ) {
246         // TODO
247
248       }
249     }
250
251
252     // Transfert de fichiers en entree et sortie
253     sub.options |= SUB_OTHER_FILES;
254     sub.nxf = xf_tab.size();
255     sub.xf = new struct xFile [sub.nxf];
256     int ixf = 0;
257     for(XFTAB::const_iterator it_xf=xf_tab.begin(); it_xf != xf_tab.end(); it_xf++, ixf++)
258       sub.xf[ixf] = *it_xf; // *it_xf == xf_tab[ixf]
259         
260
261     return _p_submit;
262   }
263
264 }