Salome HOME
Removed includes and libraries of OCC
[modules/kernel.git] / src / Launcher / SALOME_Launcher.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "BatchLight_BatchManager_PBS.hxx"
21 #include "BatchLight_BatchManager_SLURM.hxx"
22 #include "BatchLight_Job.hxx"
23 #include "SALOME_Launcher.hxx"
24 #include "OpUtil.hxx"
25 #include <sys/types.h>
26 #ifndef WNT
27 #include <unistd.h>
28 #endif
29 #include <vector>
30 #include "Utils_CorbaException.hxx"
31 #include "Batch_Date.hxx"
32
33 #define TIME_OUT_TO_LAUNCH_CONT 21
34
35 using namespace std;
36
37 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
38
39 //=============================================================================
40 /*! 
41  *  Constructor
42  *  \param orb
43  *  Define a CORBA single thread policy for the server, which avoid to deal
44  *  with non thread-safe usage like Change_Directory in SALOME naming service
45  */
46 //=============================================================================
47
48 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa)
49 {
50   MESSAGE("constructor");
51   _NS = new SALOME_NamingService(orb);
52   _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
53   _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
54
55   _orb = CORBA::ORB::_duplicate(orb) ;
56   _poa = PortableServer::POA::_duplicate(poa) ;
57   PortableServer::ObjectId_var id = _poa->activate_object(this);
58   CORBA::Object_var obj = _poa->id_to_reference(id);
59   Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
60
61   _NS->Register(refContMan,_LauncherNameInNS);
62   MESSAGE("constructor end");
63 }
64
65 //=============================================================================
66 /*! 
67  * destructor
68  */
69 //=============================================================================
70
71 SALOME_Launcher::~SALOME_Launcher()
72 {
73   MESSAGE("destructor");
74   delete _NS;
75   delete _ResManager;
76   delete _ContManager;
77   std::map < string, BatchLight::BatchManager * >::const_iterator it;
78   for(it=_batchmap.begin();it!=_batchmap.end();it++)
79     delete it->second;
80 }
81
82 //=============================================================================
83 /*! CORBA method:
84  *  shutdown all the containers, then the ContainerManager servant
85  */
86 //=============================================================================
87
88 void SALOME_Launcher::Shutdown()
89 {
90   MESSAGE("Shutdown");
91   _NS->Destroy_Name(_LauncherNameInNS);
92   _ContManager->Shutdown();
93   _ResManager->Shutdown();
94   PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
95   _poa->deactivate_object(oid);
96   _remove_ref();
97   if(!CORBA::is_nil(_orb))
98     _orb->shutdown(0);
99 }
100
101 //=============================================================================
102 /*! CORBA Method:
103  *  Submit a batch job on a cluster and returns the JobId
104  *  \param fileToExecute      : .py/.exe/.sh/... to execute on the batch cluster
105  *  \param filesToExport      : to export on the batch cluster
106  *  \param NumberOfProcessors : Number of processors needed on the batch cluster
107  *  \param params             : Constraints for the choice of the batch cluster
108  */
109 //=============================================================================
110 CORBA::Long SALOME_Launcher::submitSalomeJob( const char * fileToExecute ,
111                                               const Engines::FilesList& filesToExport ,
112                                               const Engines::FilesList& filesToImport ,
113                                               const CORBA::Long NumberOfProcessors ,
114                                               const Engines::MachineParameters& params)
115 {
116   MESSAGE("BEGIN OF SALOME_Launcher::submitSalomeJob");
117   CORBA::Long jobId;
118   try{
119     // find a cluster matching the structure params
120     Engines::CompoList aCompoList ;
121     Engines::MachineList *aMachineList = _ResManager->GetFittingResources( params , aCompoList ) ;
122     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
123     string clustername(p->alias);
124     
125     // search batch manager for that cluster in map or instanciate one
126     std::map < string, BatchLight::BatchManager * >::const_iterator it = _batchmap.find(clustername);
127     SCRUTE(clustername);
128     if(it == _batchmap.end())
129       _batchmap[clustername] = FactoryBatchManager( p );
130     
131     // submit job on cluster
132     BatchLight::Job* job = new BatchLight::Job( fileToExecute, filesToExport, filesToImport, NumberOfProcessors );
133     jobId = _batchmap[clustername]->submitJob(job);
134   }
135   catch(const SALOME_Exception &ex){
136     MESSAGE(ex.what());
137     THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::INTERNAL_ERROR);
138   }
139   return jobId;
140 }
141
142 //=============================================================================
143 /*! CORBA Method:
144  *  Query a batch job on a cluster and returns the status of job
145  *  \param jobId              : identification of Salome job
146  *  \param params             : Constraints for the choice of the batch cluster
147  */
148 //=============================================================================
149 char* SALOME_Launcher::querySalomeJob( const CORBA::Long jobId, 
150                                        const Engines::MachineParameters& params)
151 {
152   string status;
153   try{
154     // find a cluster matching params structure
155     Engines::CompoList aCompoList ;
156     Engines::MachineList * aMachineList = _ResManager->GetFittingResources( params , aCompoList ) ;
157     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
158     string clustername(p->alias);
159     
160     // search batch manager for that cluster in map
161     std::map < string, BatchLight::BatchManager * >::const_iterator it = _batchmap.find(clustername);
162     if(it == _batchmap.end())
163       throw SALOME_Exception("no batchmanager for that cluster");
164     
165     status = _batchmap[clustername]->queryJob(jobId);
166   }
167   catch(const SALOME_Exception &ex){
168     INFOS("Caught exception.");
169     THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
170   }
171   return CORBA::string_dup(status.c_str());
172 }
173
174 //=============================================================================
175 /*! CORBA Method:
176  *  Delete a batch job on a cluster 
177  *  \param jobId              : identification of Salome job
178  *  \param params             : Constraints for the choice of the batch cluster
179  */
180 //=============================================================================
181 void SALOME_Launcher::deleteSalomeJob( const CORBA::Long jobId, 
182                                        const Engines::MachineParameters& params)
183 {
184   try{
185     // find a cluster matching params structure
186     Engines::CompoList aCompoList ;
187     Engines::MachineList *aMachineList = _ResManager->GetFittingResources( params , aCompoList ) ;
188     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
189     string clustername(p->alias);
190     
191     // search batch manager for that cluster in map
192     std::map < string, BatchLight::BatchManager * >::const_iterator it = _batchmap.find(clustername);
193     if(it == _batchmap.end())
194       throw SALOME_Exception("no batchmanager for that cluster");
195     
196     _batchmap[clustername]->deleteJob(jobId);
197   }
198   catch(const SALOME_Exception &ex){
199     INFOS("Caught exception.");
200     THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
201   }
202 }
203
204 //=============================================================================
205 /*! CORBA Method:
206  *  Get result files of job on a cluster
207  *  \param jobId              : identification of Salome job
208  *  \param params             : Constraints for the choice of the batch cluster
209  */
210 //=============================================================================
211 void SALOME_Launcher::getResultSalomeJob( const char *directory,
212                                           const CORBA::Long jobId, 
213                                           const Engines::MachineParameters& params)
214 {
215   try{
216     // find a cluster matching params structure
217     Engines::CompoList aCompoList ;
218     Engines::MachineList *aMachineList = _ResManager->GetFittingResources( params , aCompoList ) ;
219     const Engines::MachineParameters* p = _ResManager->GetMachineParameters((*aMachineList)[0]);
220     string clustername(p->alias);
221     
222     // search batch manager for that cluster in map
223     std::map < string, BatchLight::BatchManager * >::const_iterator it = _batchmap.find(clustername);
224     if(it == _batchmap.end())
225       throw SALOME_Exception("no batchmanager for that cluster");
226     
227     _batchmap[clustername]->importOutputFiles( directory, jobId );
228   }
229   catch(const SALOME_Exception &ex){
230     INFOS("Caught exception.");
231     THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
232   }
233 }
234
235 //=============================================================================
236 /*!
237  *  Factory to instanciate the good batch manager for choosen cluster.
238  */ 
239 //=============================================================================
240
241 BatchLight::BatchManager *SALOME_Launcher::FactoryBatchManager( const Engines::MachineParameters* params ) throw(SALOME_Exception)
242 {
243   // Fill structure for batch manager
244   BatchLight::batchParams p;
245   p.hostname = params->alias;
246   p.protocol = params->protocol;
247   p.username = params->username;
248   p.applipath = params->applipath;
249   for(int i=0;i<params->modList.length();i++)
250     p.modulesList.push_back((const char*)params->modList[i]);
251   p.nbnodes = params->nb_node;
252   p.nbprocpernode = params->nb_proc_per_node;
253   p.mpiImpl = params->mpiImpl;
254
255   string sb = (const char*)params->batch;
256   if(sb == "pbs")
257     return new BatchLight::BatchManager_PBS(p);
258   else if(sb == "slurm")
259     return new BatchLight::BatchManager_SLURM(p);
260   else{
261     MESSAGE("BATCH = " << params->batch);
262     throw SALOME_Exception("no batchmanager for that cluster");
263   }
264 }
265