1 // SALOME MPIContainer : implemenation of container based on MPI libraries
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : MPIContainer_i.cxx
30 #include "SALOME_Component_i.hxx"
31 #include "MPIContainer_i.hxx"
32 #include "SALOME_NamingService.hxx"
33 #include "Utils_SINGLETON.hxx"
35 #include "utilities.h"
38 #include <pthread.h> // must be before Python.h !
40 #include "Container_init_python.hxx"
43 // L'appel au registry SALOME ne se fait que pour le process 0
44 Engines_MPIContainer_i::Engines_MPIContainer_i(int nbproc, int numproc,
46 PortableServer::POA_ptr poa,
48 int argc, char *argv[])
49 : Engines_Container_i(orb,poa,containerName,argc,argv,false), MPIObject_i(nbproc,numproc)
52 string IdContainerinNS;
53 char idc[3*sizeof(long)];
55 MESSAGE("[" << numproc << "] activate object");
56 _id = _poa->activate_object(this);
60 for(int i=0;i<argc;i++)
62 if(strcmp(argv[i],"-id")==NULL)
73 _NS = new SALOME_NamingService();
74 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
76 CORBA::Object_var obj=_poa->id_to_reference(*_id);
77 Engines::Container_var pCont = Engines::Container::_narrow(obj);
79 string hostname = GetHostname();
80 _containerName = _NS->BuildContainerNameForNS(containerName,hostname.c_str());
81 SCRUTE(_containerName);
82 _NS->Register(pCont, _containerName.c_str());
84 // A parallel container registers in Naming Service
85 // on the machine where is process 0. ContainerManager does'nt know the name
86 // of this machine before the launch of the parallel container. So to get
87 // the IOR of the parallel container in Naming Service, ContainerManager
88 // gives a unique Id. The parallel container registers his name under
89 // /ContainerManager/Id directory in NamingService
91 IdContainerinNS = "/ContainerManager/id";
92 sprintf(idc,"%ld",id);
93 IdContainerinNS += idc;
94 SCRUTE(IdContainerinNS);
95 _NS->Register(pCont, IdContainerinNS.c_str());
99 // Root recupere les ior des container des autre process
100 Engines::MPIObject_var pobj = POA_Engines::MPIContainer::_this();
101 BCastIOR(_orb,pobj,true);
104 Engines_MPIContainer_i::Engines_MPIContainer_i(int nbproc, int numproc)
105 : Engines_Container_i(), MPIObject_i(nbproc,numproc)
109 Engines_MPIContainer_i::~Engines_MPIContainer_i(void)
111 MESSAGE("[" << _numproc << "] Engines_MPIContainer_i::~Engines_MPIContainer_i()");
115 void Engines_MPIContainer_i::Shutdown()
118 MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
120 _NS->Destroy_FullDirectory(_containerName.c_str());
121 for(ip= 1;ip<_nbproc;ip++)
122 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
128 // Load a component library
129 bool Engines_MPIContainer_i::load_component_Library(const char* componentName)
132 // Invocation du chargement du composant dans les autres process
133 for(int ip= 1;ip<_nbproc;ip++)
134 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asload_component_Library(componentName);
137 return Lload_component_Library(componentName);
140 void Engines_MPIContainer_i::Asload_component_Library(const char* componentName)
142 Lload_component_Library(componentName);
145 bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
147 string aCompName = componentName;
149 // --- try dlopen C++ component
151 string impl_name = string ("lib") + aCompName + string("Engine.so");
154 _numInstanceMutex.lock(); // lock to be alone
155 // (see decInstanceCnt, finalize_removal))
156 if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
157 if (_library_map[impl_name])
159 MESSAGE("[" << _numproc << "] Library " << impl_name << " already loaded");
160 _numInstanceMutex.unlock();
165 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
168 _library_map[impl_name] = handle;
169 _numInstanceMutex.unlock();
174 INFOS("[" << _numproc << "] Can't load shared library : " << impl_name);
175 INFOS("[" << _numproc << "] error dlopen: " << dlerror());
177 _numInstanceMutex.unlock();
179 // --- try import Python component
181 INFOS("[" << _numproc << "] try import Python component "<<componentName);
182 if (_isSupervContainer)
184 INFOS("[" << _numproc << "] Supervision Container does not support Python Component Engines");
187 if (_library_map[aCompName])
189 return true; // Python Component, already imported
193 Py_ACQUIRE_NEW_THREAD;
194 PyObject *mainmod = PyImport_AddModule("__main__");
195 PyObject *globals = PyModule_GetDict(mainmod);
196 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
197 PyObject *result = PyObject_CallMethod(pyCont,
200 int ret= PyInt_AsLong(result);
202 Py_RELEASE_NEW_THREAD;
204 if (ret) // import possible: Python component
206 _library_map[aCompName] = (void *)pyCont; // any non O value OK
207 MESSAGE("[" << _numproc << "] import Python: "<<aCompName<<" OK");
214 // Create an instance of component
215 Engines::Component_ptr
216 Engines_MPIContainer_i::create_component_instance( const char* componentName,
220 // Invocation du chargement du composant dans les autres process
221 for(int ip= 1;ip<_nbproc;ip++)
222 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Ascreate_component_instance(componentName,studyId);
225 return Lcreate_component_instance(componentName,studyId);
228 void Engines_MPIContainer_i::Ascreate_component_instance( const char* componentName,
231 Lcreate_component_instance(componentName,studyId);
234 Engines::Component_ptr
235 Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterName, CORBA::Long studyId)
238 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
239 return Engines::Component::_nil() ;
242 Engines::Component_var iobject = Engines::Component::_nil() ;
243 Engines::MPIObject_var pobj;
245 string aCompName = genericRegisterName;
246 if (_library_map[aCompName]) { // Python component
247 if (_isSupervContainer) {
248 INFOS("Supervision Container does not support Python Component Engines");
249 return Engines::Component::_nil();
251 _numInstanceMutex.lock() ; // lock on the instance number
253 int numInstance = _numInstance ;
254 _numInstanceMutex.unlock() ;
257 sprintf( aNumI , "%d" , numInstance ) ;
258 string instanceName = aCompName + "_inst_" + aNumI ;
259 string component_registerName =
260 _containerName + "/" + instanceName;
262 Py_ACQUIRE_NEW_THREAD;
263 PyObject *mainmod = PyImport_AddModule("__main__");
264 PyObject *globals = PyModule_GetDict(mainmod);
265 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
266 PyObject *result = PyObject_CallMethod(pyCont,
267 "create_component_instance",
270 instanceName.c_str(),
272 string iors = PyString_AsString(result);
274 Py_RELEASE_NEW_THREAD;
276 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
277 iobject = Engines::Component::_narrow( obj ) ;
278 pobj = Engines::MPIObject::_narrow(obj) ;
280 _NS->Register(iobject, component_registerName.c_str()) ;
281 // Root recupere les ior des composants des autre process
282 BCastIOR(_orb,pobj,false);
284 return iobject._retn();
289 string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
290 void* handle = _library_map[impl_name];
292 INFOS("shared library " << impl_name <<"must be loaded before instance");
293 return Engines::Component::_nil() ;
296 iobject = createMPIInstance(genericRegisterName,
299 return iobject._retn();
303 Engines::Component_ptr
304 Engines_MPIContainer_i::createMPIInstance(string genericRegisterName,
308 Engines::Component_var iobject;
309 Engines::MPIObject_var pobj;
310 // --- find the factory
312 string aGenRegisterName = genericRegisterName;
313 string factory_name = aGenRegisterName + string("Engine_factory");
314 SCRUTE(factory_name) ;
316 typedef PortableServer::ObjectId * (*MPIFACTORY_FUNCTION)
319 PortableServer::POA_ptr,
320 PortableServer::ObjectId *,
324 MPIFACTORY_FUNCTION MPIComponent_factory
325 = (MPIFACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
328 if ( (error = dlerror() ) != NULL) {
329 // Try to load a sequential component
330 MESSAGE("[" << _numproc << "] Try to load a sequential component");
331 _numInstanceMutex.unlock() ;
332 iobject = Engines_Container_i::createInstance(genericRegisterName,handle,studyId);
333 if( CORBA::is_nil(iobject) ) return Engines::Component::_duplicate(iobject);
336 // --- create instance
338 iobject = Engines::Component::_nil() ;
342 _numInstanceMutex.lock() ; // lock on the instance number
344 int numInstance = _numInstance ;
345 _numInstanceMutex.unlock() ;
348 sprintf( aNumI , "%d" , numInstance ) ;
349 string instanceName = aGenRegisterName + "_inst_" + aNumI ;
350 string component_registerName =
351 _containerName + "/" + instanceName;
353 // --- Instanciate required CORBA object
355 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
356 id = (MPIComponent_factory) ( _nbproc,_numproc,_orb, _poa, _id, instanceName.c_str(),
357 aGenRegisterName.c_str() ) ;
359 // --- get reference & servant from id
361 CORBA::Object_var obj = _poa->id_to_reference(*id);
362 iobject = Engines::Component::_narrow( obj ) ;
363 pobj = Engines::MPIObject::_narrow(obj) ;
365 Engines_Component_i *servant =
366 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
368 //SCRUTE(servant->pd_refCount);
369 servant->_remove_ref(); // compensate previous id_to_reference
370 //SCRUTE(servant->pd_refCount);
371 _listInstances_map[instanceName] = iobject;
372 _cntInstances_map[aGenRegisterName] += 1;
373 SCRUTE(aGenRegisterName);
374 SCRUTE(_cntInstances_map[aGenRegisterName]);
375 //SCRUTE(servant->pd_refCount);
376 bool ret_studyId = servant->setStudyId(studyId);
379 // --- register the engine under the name
380 // containerName(.dir)/instanceName(.object)
383 _NS->Register( iobject , component_registerName.c_str() ) ;
384 MESSAGE( component_registerName.c_str() << " bound" ) ;
386 // Root recupere les ior des composants des autre process
387 BCastIOR(_orb,pobj,false);
392 INFOS( "Container_i::createInstance exception catched" ) ;
394 return iobject._retn();
398 Engines::Component_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
399 const char* componentName)
402 // Invocation du chargement du composant dans les autres process
403 for(int ip= 1;ip<_nbproc;ip++)
404 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asload_impl(nameToRegister,
408 return Lload_impl(nameToRegister,componentName);
413 void Engines_MPIContainer_i::Asload_impl(const char* nameToRegister,
414 const char* componentName)
416 Lload_impl(nameToRegister,componentName);
419 Engines::Component_ptr Engines_MPIContainer_i::Lload_impl(
420 const char* nameToRegister,
421 const char* componentName)
423 Engines::Component_var iobject;
424 Engines::MPIObject_var pobj;
427 sprintf(cproc,"_%d",_numproc);
429 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lload_impl");
431 _numInstanceMutex.lock() ; // lock on the instance number
434 sprintf(_aNumI,"%d",_numInstance) ;
436 string _impl_name = componentName;
437 string _nameToRegister = nameToRegister;
438 string instanceName = _nameToRegister + "_inst_" + _aNumI + cproc;
439 MESSAGE("[" << _numproc << "] instanceName=" << instanceName);
441 string absolute_impl_name(_impl_name);
442 MESSAGE("[" << _numproc << "] absolute_impl_name=" << absolute_impl_name);
443 void * handle = dlopen(absolute_impl_name.c_str(), RTLD_LAZY);
445 INFOS("[" << _numproc << "] Can't load shared library : " << absolute_impl_name);
446 INFOS("[" << _numproc << "] error dlopen: " << dlerror());
447 return Engines::Component::_nil() ;
450 string factory_name = _nameToRegister + string("Engine_factory");
451 MESSAGE("[" << _numproc << "] factory_name=" << factory_name) ;
453 PortableServer::ObjectId * (*MPIComponent_factory) (int,int,
455 PortableServer::POA_ptr,
456 PortableServer::ObjectId *,
459 (PortableServer::ObjectId * (*) (int,int,
461 PortableServer::POA_ptr,
462 PortableServer::ObjectId *,
465 dlsym(handle, factory_name.c_str());
468 if ((error = dlerror()) != NULL){
469 // Try to load a sequential component
470 MESSAGE("[" << _numproc << "] Try to load a sequential component");
471 _numInstanceMutex.unlock() ;
472 iobject = Engines_Container_i::load_impl(nameToRegister,componentName);
473 if( CORBA::is_nil(iobject) ) return Engines::Component::_duplicate(iobject);
476 // Instanciation du composant parallele
477 MESSAGE("[" << _numproc << "] Try to load a parallel component");
478 PortableServer::ObjectId * id = (MPIComponent_factory)
479 (_nbproc,_numproc,_orb, _poa, _id, instanceName.c_str(), _nameToRegister.c_str());
480 // get reference from id
481 CORBA::Object_var o = _poa->id_to_reference(*id);
482 pobj = Engines::MPIObject::_narrow(o) ;
483 iobject = Engines::Component::_narrow(o) ;
487 // utiliser + tard le registry ici :
488 // register the engine under the name containerName.dir/nameToRegister.object
489 string component_registerName = _containerName + "/" + _nameToRegister;
490 _NS->Register(iobject, component_registerName.c_str()) ;
493 _numInstanceMutex.unlock() ;
495 // Root recupere les ior des composants des autre process
496 BCastIOR(_orb,pobj,false);
498 END_OF("[" <<_numproc << "] MPIContainer_i::Lload_impl");
499 return Engines::Component::_duplicate(iobject);
503 void Engines_MPIContainer_i::remove_impl(Engines::Component_ptr component_i)
506 Engines::Component_ptr cptr;
507 Engines::MPIObject_ptr pcptr;
508 Engines::MPIObject_ptr spcptr;
510 ASSERT(! CORBA::is_nil(component_i));
513 // Invocation de la destruction du composant dans les autres process
514 pcptr = (Engines::MPIObject_ptr)component_i;
515 for(ip= 1;ip<_nbproc;ip++){
516 spcptr = Engines::MPIObject::_narrow((*(pcptr->tior()))[ip]);
517 cptr = (Engines::Component_ptr)spcptr;
518 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asremove_impl(cptr);
522 Lremove_impl(component_i);
525 void Engines_MPIContainer_i::Asremove_impl(Engines::Component_ptr component_i)
527 Lremove_impl(component_i);
530 void Engines_MPIContainer_i::Lremove_impl(Engines::Component_ptr component_i)
532 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lremove_impl");
534 ASSERT(! CORBA::is_nil(component_i));
536 string instanceName = component_i->instanceName() ;
537 MESSAGE("[" << _numproc << "] unload component " << instanceName);
538 component_i->destroy() ;
539 MESSAGE("[" << _numproc << "] test key handle_map");
540 _numInstanceMutex.lock() ; // lock on the remove on handle_map
541 _numInstanceMutex.unlock() ;
542 MESSAGE("[" << _numproc << "] list handle_map");
544 END_OF("[" << _numproc << "] MPIContainer_i::Lremove_impl");
548 void Engines_MPIContainer_i::finalize_removal()
553 // Invocation de la destruction du composant dans les autres process
554 for(ip= 1;ip<_nbproc;ip++)
555 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asfinalize_removal();
561 void Engines_MPIContainer_i::Asfinalize_removal()
566 void Engines_MPIContainer_i::Lfinalize_removal()
568 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lfinalize_removal");
570 // map<string, void *>::iterator im ;
571 // // lock on the explore remove_map & dlclose
572 // _numInstanceMutex.lock() ;
573 // for (im = remove_map.begin() ; im != remove_map.end() ; im ++)
575 // void * handle = (*im).second ;
576 // MESSAGE("[" << _numproc << "] dlclose " << (*im).first);
579 // MESSAGE("[" << _numproc << "] remove_map.clear()");
580 // remove_map.clear() ;
581 // _numInstanceMutex.unlock() ;
583 END_OF("[" << _numproc << "] MPIContainer_i::Lfinalize_removal");