]> SALOME platform Git repositories - modules/kernel.git/blob - src/Launcher_SWIG/Launcher.i
Salome HOME
20da573b229259cffa3f50572c91ea254cfe7238
[modules/kernel.git] / src / Launcher_SWIG / Launcher.i
1 // Copyright (C) 2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 %module pylauncher
21
22 %{
23 #include "SALOME_ResourcesCatalog_Parser.hxx"
24 #include "Launcher_Job.hxx"
25 #include "Launcher_Job_SALOME.hxx"
26 #include "Launcher_Job_YACSFile.hxx"
27 #include "Launcher.hxx"
28 #include "ResourcesManager.hxx"
29
30 static PyObject *convertJob(Launcher::Job *job, int owner)
31 {
32   PyObject *ret(nullptr);
33   if(!job)
34     {
35       Py_XINCREF(Py_None);
36       return Py_None;
37     }
38   if(dynamic_cast<Launcher::Job_YACSFile *>(job))
39     return SWIG_NewPointerObj((void*)dynamic_cast<Launcher::Job_YACSFile *>(job),SWIGTYPE_p_Launcher__Job_YACSFile,owner);
40   throw LauncherException("Not recognized type of job on downcast !");
41 }
42
43 %}
44
45 %include std_string.i
46
47 %typemap(out) const std::list<std::string>&
48 {
49   std::size_t i;
50   std::list<std::string>::const_iterator iL;
51   $result = PyList_New($1->size());
52   for (i=0, iL=$1->cbegin(); iL!=$1->cend(); i++, iL++)
53     PyList_SetItem($result,i,PyUnicode_FromString((*iL).c_str())); 
54 }
55
56 %typemap(out) const std::map<std::string, std::string> &
57 {
58   $result = PyDict_New();
59   for(std::map<std::string, std::string>::const_iterator iL=$1->cbegin();iL!=$1->cend();iL++)
60     {
61       PyObject *a(PyUnicode_FromString((*iL).first.c_str()));
62       PyObject *b(PyUnicode_FromString((*iL).second.c_str()));
63       PyDict_SetItem($result,a,b);
64       Py_DECREF(a); Py_DECREF(b);
65     }
66 }
67
68 %typemap(out) std::vector<std::string> *
69 {
70   std::size_t i;
71   std::vector<std::string>::const_iterator iL;
72   $result = PyList_New($1->size());
73   for (i=0, iL=$1->cbegin(); iL!=$1->cend(); i++, iL++)
74     PyList_SetItem($result,i,PyUnicode_FromString((*iL).c_str())); 
75 }
76
77 %typemap(out) Launcher::Job *
78 {
79   $result=convertJob($1,$owner);
80 }
81    
82 %exception
83 {
84   try {
85     $function
86       }
87   catch(LauncherException& e)
88     {
89       SWIG_exception_fail(SWIG_RuntimeError,e.msg.c_str());
90     }
91   catch (...)
92     {
93       SWIG_exception_fail(SWIG_UnknownError, "Unknown");
94     }
95 }
96
97 struct resourceParams
98 {
99   resourceParams();
100
101   std::string name;
102   std::string hostname;
103   bool can_launch_batch_jobs;
104   bool can_run_containers;
105   std::string OS;
106   long nb_proc;
107   long nb_node;
108   long nb_proc_per_node;
109   long cpu_clock;
110   long mem_mb;
111   std::vector<std::string> componentList;
112   std::vector<std::string> resourceList;
113 };
114
115 class ResourcesManager_cpp
116 {
117  public:
118   ResourcesManager_cpp(const char *xmlFilePath);
119 };
120
121 class ParserResourcesType
122 {
123 public:
124   ParserResourcesType();
125   ~ParserResourcesType();
126   std::string getAccessProtocolTypeStr() const;
127   std::string getResourceTypeStr() const;
128   std::string getBatchTypeStr() const;
129   std::string getMpiImplTypeStr() const;
130   std::string getClusterInternalProtocolStr() const;
131   std::string getCanLaunchBatchJobsStr() const;
132   std::string getCanRunContainersStr() const;
133
134   void setAccessProtocolTypeStr(const std::string & protocolTypeStr);
135   void setResourceTypeStr(const std::string & resourceTypeStr);
136   void setBatchTypeStr(const std::string & batchTypeStr);
137   void setMpiImplTypeStr(const std::string & mpiImplTypeStr);
138   void setClusterInternalProtocolStr(const std::string & internalProtocolTypeStr);
139   void setCanLaunchBatchJobsStr(const std::string & canLaunchBatchJobsStr);
140   void setCanRunContainersStr(const std::string & canRunContainersStr);
141 };
142
143 %feature("unref") Launcher::Job "$this->decrRef();"
144 %feature("unref") Launcher::Job_SALOME "$this->decrRef();"
145 %feature("unref") Launcher::Job_YACSFile "$this->decrRef();"
146
147 namespace Launcher
148 {
149   class Job
150   {
151     public:
152       Job();
153       virtual ~Job();
154       void setState(const std::string & state);
155       std::string getState() const;
156       std::string getAssignedHostnames();
157       void setNumber(const int & number);
158       int getNumber();
159       virtual void setResourceDefinition(const ParserResourcesType & resource_definition);
160       ParserResourcesType getResourceDefinition() const;
161       // Common parameters
162       void setJobName(const std::string & job_name);
163       virtual void setJobFile(const std::string & job_file);
164       void setPreCommand(const std::string & preCommand);
165       void setWorkDirectory(const std::string & work_directory);
166       void setLocalDirectory(const std::string & local_directory);
167       void setResultDirectory(const std::string & result_directory);
168       void add_in_file(const std::string & file);
169       void add_out_file(const std::string & file);
170       void setMaximumDuration(const std::string & maximum_duration);
171       void setResourceRequiredParams(const resourceParams & resource_required_params);
172       void setQueue(const std::string & queue);
173       void setPartition(const std::string & partition);
174       void setEnvFile(const std::string & env_file);
175       void setExclusive(bool exclusive);
176       void setExclusiveStr(const std::string & exclusiveStr);
177       void setMemPerCpu(unsigned long mem_per_cpu);
178       void setWCKey(const std::string & wckey);
179       void setExtraParams(const std::string & extra_params);
180       void setReference(const std::string & reference);
181       // For COORM
182       void setLauncherFile(const std::string & launcher_file);
183       void setLauncherArgs(const std::string & launcher_args);
184
185       std::string getJobName() const;
186       std::string getJobFile() const;
187       std::string getPreCommand() const;
188       std::string getWorkDirectory() const;
189       std::string getLocalDirectory() const;
190       std::string getResultDirectory() const;
191       const std::list<std::string> & get_in_files() const;
192       const std::list<std::string> & get_out_files() const;
193       std::string getMaximumDuration() const;
194       resourceParams getResourceRequiredParams() const;
195       std::string getQueue() const;
196       std::string getPartition() const;
197       std::string getEnvFile() const;
198       std::string getJobType() const;
199       bool getExclusive() const;
200       std::string getExclusiveStr() const;
201       unsigned long getMemPerCpu() const;
202       std::string getWCKey() const;
203       std::string getExtraParams() const;
204       std::string getReference() const;
205       std::string getLauncherFile() const;
206       std::string getLauncherArgs() const;
207       std::string updateJobState();
208
209       void addSpecificParameter(const std::string & name, const std::string & value);
210       const std::map<std::string, std::string> & getSpecificParameters() const;
211       virtual void checkSpecificParameters();
212
213       // Checks
214       void checkMaximumDuration(const std::string & maximum_duration);
215       void checkResourceRequiredParams(const resourceParams & resource_required_params);
216
217       // Helps
218       long convertMaximumDuration(const std::string & maximum_duration);
219       std::string getLaunchDate() const;
220
221       void stopJob();
222       void removeJob();
223       virtual void update_job() = 0;
224   };
225
226   class Job_SALOME : public Job
227   {
228   private:
229     Job_SALOME();
230   public:
231     virtual ~Job_SALOME();
232     virtual void setResourceDefinition(const ParserResourcesType & resource_definition);
233     virtual void update_job();
234 #ifdef WITH_LIBBATCH
235   public:
236     std::string buildSalomeScript(Batch::Parametre params);
237 #endif
238   };
239
240   class Job_YACSFile : public Job_SALOME
241   {
242   public:
243     Job_YACSFile();
244     virtual ~Job_YACSFile();
245     virtual void setJobFile(const std::string & job_file);
246     virtual void checkSpecificParameters();
247   };
248 }
249
250 class Launcher_cpp
251 {
252 public:
253   Launcher_cpp();
254   virtual ~Launcher_cpp();
255   void         createJob(Launcher::Job * new_job);
256   void         launchJob(int job_id);
257   const char * getJobState(int job_id);
258   const char * getAssignedHostnames(int job_id); // Get names or ids of hosts assigned to the job
259   void         getJobResults(int job_id, std::string directory);
260   void         clearJobWorkingDir(int job_id);
261   bool         getJobDumpState(int job_id, std::string directory);
262   bool         getJobWorkFile(int job_id, std::string work_file, std::string directory);
263   void         stopJob(int job_id);
264   void         removeJob(int job_id);
265   std::string  dumpJob(int job_id);
266   int restoreJob(const std::string& dumpedJob);
267   std::list<int> loadJobs(const char* jobs_file);
268   void saveJobs(const char* jobs_file);
269   long createJobWithFile(std::string xmlExecuteFile, std::string clusterName);
270   std::map<int, Launcher::Job *> getJobs();
271   void addJobDirectlyToMap(Launcher::Job * new_job);
272   void SetResourcesManager( ResourcesManager_cpp *rm );
273   Launcher::Job * findJob(int job_id);
274 };
275
276 %pythoncode %{
277 def sendJobToSession(self, job_id, sessionId=None):
278     """Send job specified by its job_id in self to a remote SALOME session.
279     Doing so, it's possible to follow the job created locally into the JobManager module of the target SALOME session.
280     SALOME session is specified by the file pointed by the content of OMNIORB_CONFIG environement var. The content of this var is called sessionId.
281     If sessionId is let untouched, the current OMNIORB_CONFIG environement var is used.
282     If this method fails to connect to the target SALOME session a RuntimeError exception will be thrown.
283     """
284     def job_type_traducer(jyf):
285         dico = {'Job_YACSFile' : 'yacs_file'}
286         st = type(jyf).__name__
287         if st not in dico:
288             raise RuntimeError("Not recognized type %s !"%st)
289         return dico[st]
290     #
291     def resource_required_func(jyf):
292         import Engines
293         rp =jyf.getResourceRequiredParams()
294         l12 = [('name', None), ('hostname', None), ('can_launch_batch_jobs', None), ('can_run_containers', None), ('OS', None), ('componentList', None), ('nb_proc', None), ('mem_mb', None), ('cpu_clock', None), ('nb_node', None), ('nb_proc_per_node', None), ('policy', lambda x: 'cycl'), ('resList', lambda x: x.resourceList)]
295         kw2={}
296         for a,b in l12:
297             if a and not b:
298                 kw2[a]=getattr(rp,a)
299             else:
300                 if a and b:
301                     kw2[a]=b(rp)
302         return Engines.ResourceParameters(**kw2)
303     #
304     filest = self.dumpJob(job_id);
305     # Connect to SALOME session a retrieve its SalomeLauncher object
306     import os
307     if sessionId is not None:
308       os.environ["OMNIORB_CONFIG"]=sessionId
309     import Engines
310     import orbmodule
311     try:
312       clt=orbmodule.client()
313       sl = clt.Resolve("SalomeLauncher")
314     except:
315       raise RuntimeError("Fail to connect to the remote SALOME session.")
316     # swig to CORBA translation
317     # Job_YACSFile -> Engines.JobParameters and resourceParams -> Engines.ResourceParameters()
318     l21= [('job_name', None), ('job_type', job_type_traducer), ('job_file', None), ('pre_command', None), ('env_file', None), ('in_files', lambda x: x.get_in_files()), ('out_files', lambda x: x.get_out_files()), ('work_directory', None), ('local_directory', None), ('result_directory', None), ('maximum_duration', None), ('resource_required',resource_required_func) , ('queue', None), ('partition', None), ('exclusive', None), ('mem_per_cpu', None), ('wckey', lambda x: x.getWCKey() ), ('extra_params', None), ('specific_parameters', lambda x: list(x.getSpecificParameters().items())), ('launcher_file', None), ('launcher_args', None)]
319     kw={}
320     jyf = self.findJob(job_id)
321     for a,b in l21:
322         if not b and a:
323             kw[a]=eval("jyf.get%s()"%"".join(["%s%s"%(elt2[0].upper(),elt2[1:]) for elt2 in a.split("_")]),{"jyf" : jyf})
324         else:
325             if a and b:
326                 kw[a]=b(jyf)
327     jyc = Engines.JobParameters(**kw)
328     ########################
329     bpc = sl.createJob(jyc)
330     sl.restoreJob(filest)
331
332 Launcher_cpp.sendJobToSession = sendJobToSession
333 del sendJobToSession
334 %}