1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BatchLight_BatchManager_PBS.hxx"
21 #include "BatchLight_BatchManager_SLURM.hxx"
22 #include "BatchLight_Job.hxx"
23 #include "SALOME_Launcher.hxx"
25 #include <sys/types.h>
30 #include "Utils_CorbaException.hxx"
31 #include "Batch_Date.hxx"
33 #define TIME_OUT_TO_LAUNCH_CONT 21
37 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
39 //=============================================================================
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
46 //=============================================================================
48 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa)
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);
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);
61 _NS->Register(refContMan,_LauncherNameInNS);
62 MESSAGE("constructor end");
65 //=============================================================================
69 //=============================================================================
71 SALOME_Launcher::~SALOME_Launcher()
73 MESSAGE("destructor");
77 std::map < string, BatchLight::BatchManager * >::const_iterator it;
78 for(it=_batchmap.begin();it!=_batchmap.end();it++)
82 //=============================================================================
84 * shutdown all the containers, then the ContainerManager servant
86 //=============================================================================
88 void SALOME_Launcher::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);
97 if(!CORBA::is_nil(_orb))
101 //=============================================================================
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
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)
116 MESSAGE("BEGIN OF SALOME_Launcher::submitSalomeJob");
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);
125 // search batch manager for that cluster in map or instanciate one
126 std::map < string, BatchLight::BatchManager * >::const_iterator it = _batchmap.find(clustername);
128 if(it == _batchmap.end())
129 _batchmap[clustername] = FactoryBatchManager( p );
131 // submit job on cluster
132 BatchLight::Job* job = new BatchLight::Job( fileToExecute, filesToExport, filesToImport, NumberOfProcessors );
133 jobId = _batchmap[clustername]->submitJob(job);
135 catch(const SALOME_Exception &ex){
137 THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::INTERNAL_ERROR);
142 //=============================================================================
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
148 //=============================================================================
149 char* SALOME_Launcher::querySalomeJob( const CORBA::Long jobId,
150 const Engines::MachineParameters& params)
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);
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");
165 status = _batchmap[clustername]->queryJob(jobId);
167 catch(const SALOME_Exception &ex){
168 INFOS("Caught exception.");
169 THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
171 return CORBA::string_dup(status.c_str());
174 //=============================================================================
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
180 //=============================================================================
181 void SALOME_Launcher::deleteSalomeJob( const CORBA::Long jobId,
182 const Engines::MachineParameters& params)
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);
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");
196 _batchmap[clustername]->deleteJob(jobId);
198 catch(const SALOME_Exception &ex){
199 INFOS("Caught exception.");
200 THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
204 //=============================================================================
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
210 //=============================================================================
211 void SALOME_Launcher::getResultSalomeJob( const char *directory,
212 const CORBA::Long jobId,
213 const Engines::MachineParameters& params)
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);
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");
227 _batchmap[clustername]->importOutputFiles( directory, jobId );
229 catch(const SALOME_Exception &ex){
230 INFOS("Caught exception.");
231 THROW_SALOME_CORBA_EXCEPTION(ex.what(),SALOME::BAD_PARAM);
235 //=============================================================================
237 * Factory to instanciate the good batch manager for choosen cluster.
239 //=============================================================================
241 BatchLight::BatchManager *SALOME_Launcher::FactoryBatchManager( const Engines::MachineParameters* params ) throw(SALOME_Exception)
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;
255 string sb = (const char*)params->batch;
257 return new BatchLight::BatchManager_PBS(p);
258 else if(sb == "slurm")
259 return new BatchLight::BatchManager_SLURM(p);
261 MESSAGE("BATCH = " << params->batch);
262 throw SALOME_Exception("no batchmanager for that cluster");