Salome HOME
Merge branch 'omu/multijob'
[tools/ydefx.git] / src / cpp / Launcher.hxx
1 // Copyright (C) 2019  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 #ifndef YDEFX_LAUNCHER_H
20 #define YDEFX_LAUNCHER_H
21
22 #include "TMonoPyJob.hxx"
23 #include "TPyStudyJob.hxx"
24
25 namespace ydefx
26 {
27 class Job;
28 class Launcher
29 {
30 public:
31   Launcher():_lastError(){}
32   const std::string& lastError()const{return _lastError;}
33
34   /*!
35    * Create and submit a new job.
36    * Return nullptr in case of failure. Check the error with lastError().
37    */
38   template <class ...Ts>
39   Job* submitMonoPyJob(const PyStudyFunction& fnScript,
40                        Sample<Ts...>& sample,
41                        const JobParametersProxy& params);
42
43   template <class ...Ts>
44   Job* submitPyStudyJob(py2cpp::PyPtr& pyStudyObj,
45                         const PyStudyFunction& fnScript,
46                         Sample<Ts...>& sample,
47                         const JobParametersProxy& params);
48
49   /*!
50    * Connect to an already created job.
51    * Return nullptr in case of failure. Check the error with lastError().
52    */
53   template <class ...Ts>
54   Job* connectJob(const std::string& jobDump, Sample<Ts...>& sample);
55
56   template <class ...Ts>
57   Job* submitMultiPyJob(const PyStudyFunction& fn,
58                         Sample<Ts...>& sample,
59                         const JobParametersProxy& params);
60
61 private:
62   std::string _lastError;
63 };
64
65 ////////////////////////////////////////////////////////////////////////////////
66 // Template implementations
67 ////////////////////////////////////////////////////////////////////////////////
68
69 template <class ...Ts>
70 Job* Launcher::submitMonoPyJob(const PyStudyFunction& fnScript,
71                     Sample<Ts...>& sample,
72                     const JobParametersProxy& params)
73 {
74   Job* result = nullptr;
75   _lastError = "";
76   try
77   {
78     result = new TMonoPyJob<Ts...>(fnScript, sample, params);
79   }
80   catch(std::exception& e)
81   {
82     if(result != nullptr)
83       delete result;
84     result = nullptr;
85     _lastError = e.what();
86     return result;
87   }
88
89   if(!result->lastError().empty())
90   {
91     _lastError  = result->lastError();
92     delete result;
93     result = nullptr;
94     return result;
95   }
96
97   if(!result->launch())
98   {
99     _lastError = "Failed to submit job.\n";
100     _lastError  += result->lastError();
101     delete result;
102     result = nullptr;
103   }
104   return result;
105 }
106
107 template <class ...Ts>
108 Job* Launcher::submitPyStudyJob(py2cpp::PyPtr& pyStudyObj,
109                                 const PyStudyFunction& fnScript,
110                                 Sample<Ts...>& sample,
111                                 const JobParametersProxy& params)
112 {
113   Job* result = nullptr;
114   _lastError = "";
115   try
116   {
117     result = new TPyStudyJob<Ts...>(pyStudyObj, fnScript, sample, params);
118   }
119   catch(std::exception& e)
120   {
121     if(result != nullptr)
122       delete result;
123     result = nullptr;
124     _lastError = e.what();
125     return result;
126   }
127
128   if(!result->lastError().empty())
129   {
130     _lastError  = result->lastError();
131     delete result;
132     result = nullptr;
133     return result;
134   }
135
136   if(!result->launch())
137   {
138     _lastError = "Failed to submit job.\n";
139     _lastError  += result->lastError();
140     delete result;
141     result = nullptr;
142   }
143   return result;
144 }
145
146 template <class ...Ts>
147 Job* Launcher::connectJob(const std::string& jobDump,
148                       Sample<Ts...>& sample)
149 {
150   Job* result = nullptr;
151   _lastError = "";
152   try
153   {
154     result = new TMonoPyJob<Ts...>(jobDump, sample);
155   }
156   catch(std::exception& e)
157   {
158     if(result != nullptr)
159       delete result;
160     result = nullptr;
161     _lastError = e.what();
162   }
163
164   return result;
165 }
166
167 template <class ...Ts>
168 Job* Launcher::submitMultiPyJob(const PyStudyFunction& fn,
169                       Sample<Ts...>& sample,
170                       const JobParametersProxy& params)
171 {
172   // TODO
173   _lastError = "Not implemented!";
174   return nullptr;
175 }
176
177 }
178 #endif // YDEFX_LAUNCHER_H