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