1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // File : MPIContainer_i.cxx
28 #include "Basics_Utils.hxx"
29 #include "SALOME_Component_i.hxx"
30 #include "MPIContainer_i.hxx"
31 #include "SALOME_NamingService.hxx"
32 #include "Utils_SINGLETON.hxx"
34 #include "utilities.h"
37 #include <pthread.h> // must be before Python.h !
39 #include "Container_init_python.hxx"
41 // L'appel au registry SALOME ne se fait que pour le process 0
42 Engines_MPIContainer_i::Engines_MPIContainer_i(CORBA::ORB_ptr orb,
43 PortableServer::POA_ptr poa,
45 int argc, char *argv[])
46 : Engines_Container_i(orb,poa,containerName,argc,argv,false)
49 _id = _poa->activate_object(this);
50 CORBA::Object_var obj=_poa->id_to_reference(*_id);
51 Engines::Container_var pCont = Engines::Container::_narrow(obj);
56 _NS = new SALOME_NamingService();
57 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
59 std::string hostname = Kernel_Utils::GetHostname();
60 _containerName = _NS->BuildContainerNameForNS(containerName,hostname.c_str());
61 SCRUTE(_containerName);
62 _NS->Register(pCont, _containerName.c_str());
66 // Root recupere les ior des container des autre process
67 Engines::MPIObject_var pobj = POA_Engines::MPIContainer::_this();
68 BCastIOR(_orb,pobj,true);
71 Engines_MPIContainer_i::Engines_MPIContainer_i()
72 : Engines_Container_i()
76 Engines_MPIContainer_i::~Engines_MPIContainer_i(void)
78 MESSAGE("[" << _numproc << "] Engines_MPIContainer_i::~Engines_MPIContainer_i()");
82 void Engines_MPIContainer_i::Shutdown()
85 MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
87 _NS->Destroy_FullDirectory(_containerName.c_str());
88 _NS->Destroy_Name(_containerName.c_str());
89 for(ip= 1;ip<_nbproc;ip++)
90 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
93 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
94 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
98 itm->second->destroy();
100 catch(const CORBA::Exception& e)
102 // ignore this entry and continue
106 // ignore this entry and continue
114 // Load a component library
115 bool Engines_MPIContainer_i::load_component_Library(const char* componentName, CORBA::String_out reason)
117 reason=CORBA::string_dup("");
121 th = new pthread_t[_nbproc];
122 for(int ip=1;ip<_nbproc;ip++){
123 thread_st *st = new thread_st;
126 st->compoName = componentName;
127 pthread_create(&(th[ip]),NULL,th_loadcomponentlibrary,(void*)st);
131 bool ret = Lload_component_Library(componentName);
134 for(int ip=1;ip<_nbproc;ip++)
135 pthread_join(th[ip],NULL);
141 bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
143 std::string aCompName = componentName;
145 // --- try dlopen C++ component
147 std::string impl_name = std::string ("lib") + aCompName + std::string("Engine.so");
149 _numInstanceMutex.lock(); // lock to be alone
150 // (see decInstanceCnt, finalize_removal))
151 if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
152 if (_library_map[impl_name])
154 MESSAGE("[" << _numproc << "] Library " << impl_name << " already loaded");
155 _numInstanceMutex.unlock();
160 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
163 _library_map[impl_name] = handle;
164 _numInstanceMutex.unlock();
165 MESSAGE("[" << _numproc << "] Library " << impl_name << " loaded");
170 MESSAGE("[" << _numproc << "] Can't load shared library : " << impl_name);
171 MESSAGE("[" << _numproc << "] error dlopen: " << dlerror());
173 _numInstanceMutex.unlock();
175 // --- try import Python component
177 INFOS("[" << _numproc << "] try import Python component "<<componentName);
178 if (_isSupervContainer)
180 INFOS("[" << _numproc << "] Supervision Container does not support Python Component Engines");
183 if (_library_map[aCompName])
185 return true; // Python Component, already imported
189 Py_ACQUIRE_NEW_THREAD;
190 PyObject *mainmod = PyImport_AddModule((char *)"__main__");
191 PyObject *globals = PyModule_GetDict(mainmod);
192 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
193 PyObject *result = PyObject_CallMethod(pyCont,
194 (char*)"import_component",
195 (char*)"s",componentName);
196 std::string ret= PyString_AsString(result);
198 Py_RELEASE_NEW_THREAD;
200 if (ret=="") // import possible: Python component
202 _library_map[aCompName] = (void *)pyCont; // any non O value OK
203 MESSAGE("[" << _numproc << "] import Python: "<<aCompName<<" OK");
210 // Create an instance of component
211 Engines::EngineComponent_ptr
212 Engines_MPIContainer_i::create_component_instance_env( const char* componentName,
214 const Engines::FieldsDict& env,
215 CORBA::String_out reason)
217 reason=CORBA::string_dup("");
221 th = new pthread_t[_nbproc];
222 for(int ip=1;ip<_nbproc;ip++){
223 thread_st *st = new thread_st;
226 st->compoName = componentName;
227 st->studyId = studyId;
228 pthread_create(&(th[ip]),NULL,th_createcomponentinstance,(void*)st);
232 Engines::EngineComponent_ptr cptr = Lcreate_component_instance(componentName,studyId);
235 for(int ip=1;ip<_nbproc;ip++)
236 pthread_join(th[ip],NULL);
243 Engines::EngineComponent_ptr
244 Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterName, CORBA::Long studyId)
247 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
248 return Engines::EngineComponent::_nil() ;
251 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
252 Engines::MPIObject_var pobj;
254 std::string aCompName = genericRegisterName;
255 if (_library_map[aCompName]) { // Python component
256 if (_isSupervContainer) {
257 INFOS("Supervision Container does not support Python Component Engines");
258 return Engines::EngineComponent::_nil();
260 _numInstanceMutex.lock() ; // lock on the instance number
262 int numInstance = _numInstance ;
263 _numInstanceMutex.unlock() ;
266 sprintf( aNumI , "%d" , numInstance ) ;
267 std::string instanceName = aCompName + "_inst_" + aNumI ;
268 std::string component_registerName =
269 _containerName + "/" + instanceName;
271 Py_ACQUIRE_NEW_THREAD;
272 PyObject *mainmod = PyImport_AddModule((char*)"__main__");
273 PyObject *globals = PyModule_GetDict(mainmod);
274 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
275 PyObject *result = PyObject_CallMethod(pyCont,
276 (char*)"create_component_instance",
279 instanceName.c_str(),
283 PyArg_ParseTuple(result,"ss", &ior, &error);
284 std::string iors = ior;
286 Py_RELEASE_NEW_THREAD;
288 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
289 iobject = Engines::EngineComponent::_narrow( obj ) ;
290 pobj = Engines::MPIObject::_narrow(obj) ;
292 _NS->Register(iobject, component_registerName.c_str()) ;
293 // Root recupere les ior des composants des autre process
294 BCastIOR(_orb,pobj,false);
296 return iobject._retn();
301 std::string impl_name = std::string ("lib") + genericRegisterName +std::string("Engine.so");
302 if (_library_map.count(impl_name) != 0) // C++ component
304 void* handle = _library_map[impl_name];
305 iobject = createMPIInstance(genericRegisterName,
308 return iobject._retn();
311 return Engines::EngineComponent::_nil() ;
314 Engines::EngineComponent_ptr
315 Engines_MPIContainer_i::createMPIInstance(std::string genericRegisterName,
319 Engines::EngineComponent_var iobject;
320 Engines::MPIObject_var pobj;
321 // --- find the factory
323 std::string aGenRegisterName = genericRegisterName;
324 std::string factory_name = aGenRegisterName + std::string("Engine_factory");
326 typedef PortableServer::ObjectId * (*MPIFACTORY_FUNCTION)
328 PortableServer::POA_ptr,
329 PortableServer::ObjectId *,
334 MPIFACTORY_FUNCTION MPIComponent_factory = (MPIFACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
336 if ( !MPIComponent_factory )
338 INFOS( "[" << _numproc << "] Can't resolve symbol: " + factory_name );
340 pobj = Engines::MPIObject::_nil();
341 BCastIOR(_orb,pobj,false);
342 return Engines::EngineComponent::_nil();
345 // --- create instance
347 iobject = Engines::EngineComponent::_nil() ;
351 _numInstanceMutex.lock() ; // lock on the instance number
353 int numInstance = _numInstance ;
354 _numInstanceMutex.unlock() ;
357 sprintf( aNumI , "%d" , numInstance ) ;
358 std::string instanceName = aGenRegisterName + "_inst_" + aNumI ;
359 std::string component_registerName =
360 _containerName + "/" + instanceName;
362 // --- Instanciate required CORBA object
364 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
365 id = (MPIComponent_factory) ( _orb, _poa, _id, instanceName.c_str(), aGenRegisterName.c_str() ) ;
367 // --- get reference & servant from id
369 CORBA::Object_var obj = _poa->id_to_reference(*id);
370 iobject = Engines::EngineComponent::_narrow( obj ) ;
371 pobj = Engines::MPIObject::_narrow(obj) ;
373 Engines_Component_i *servant =
374 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
376 //SCRUTE(servant->pd_refCount);
377 servant->_remove_ref(); // compensate previous id_to_reference
378 //SCRUTE(servant->pd_refCount);
379 _listInstances_map[instanceName] = iobject;
380 _cntInstances_map[aGenRegisterName] += 1;
381 //SCRUTE(servant->pd_refCount);
383 servant->setStudyId(studyId);
385 bool ret_studyId = servant->setStudyId(studyId);
389 // --- register the engine under the name
390 // containerName(.dir)/instanceName(.object)
393 _NS->Register( iobject , component_registerName.c_str() ) ;
394 MESSAGE( component_registerName.c_str() << " bound" ) ;
396 // Root recupere les ior des composants des autre process
397 BCastIOR(_orb,pobj,false);
400 catch(const std::exception &ex){
402 return Engines::EngineComponent::_nil();
404 return iobject._retn();
408 Engines::EngineComponent_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
409 const char* componentName)
413 th = new pthread_t[_nbproc];
414 for(int ip=1;ip<_nbproc;ip++){
415 thread_st *st = new thread_st;
418 st->nameToRegister = nameToRegister;
419 st->compoName = componentName;
420 pthread_create(&(th[ip]),NULL,th_loadimpl,(void*)st);
424 Engines::EngineComponent_ptr cptr = Lload_impl(nameToRegister,componentName);
427 for(int ip=1;ip<_nbproc;ip++)
428 pthread_join(th[ip],NULL);
436 Engines::EngineComponent_ptr Engines_MPIContainer_i::Lload_impl(
437 const char* nameToRegister,
438 const char* componentName)
440 Engines::EngineComponent_var iobject;
441 Engines::MPIObject_var pobj;
444 sprintf(cproc,"_%d",_numproc);
446 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lload_impl");
448 _numInstanceMutex.lock() ; // lock on the instance number
451 sprintf(_aNumI,"%d",_numInstance) ;
453 std::string _impl_name = componentName;
454 std::string _nameToRegister = nameToRegister;
455 std::string instanceName = _nameToRegister + "_inst_" + _aNumI + cproc;
456 MESSAGE("[" << _numproc << "] instanceName=" << instanceName);
458 std::string absolute_impl_name(_impl_name);
459 MESSAGE("[" << _numproc << "] absolute_impl_name=" << absolute_impl_name);
460 void * handle = dlopen(absolute_impl_name.c_str(), RTLD_LAZY);
462 INFOS("[" << _numproc << "] Can't load shared library : " << absolute_impl_name);
463 INFOS("[" << _numproc << "] error dlopen: " << dlerror());
464 return Engines::EngineComponent::_nil() ;
467 std::string factory_name = _nameToRegister + std::string("Engine_factory");
468 MESSAGE("[" << _numproc << "] factory_name=" << factory_name) ;
471 PortableServer::ObjectId * (*MPIComponent_factory) (CORBA::ORB_ptr,
472 PortableServer::POA_ptr,
473 PortableServer::ObjectId *,
476 (PortableServer::ObjectId * (*) (CORBA::ORB_ptr,
477 PortableServer::POA_ptr,
478 PortableServer::ObjectId *,
481 dlsym(handle, factory_name.c_str());
484 if ((error = dlerror()) != NULL){
485 // Try to load a sequential component
486 MESSAGE("[" << _numproc << "] Try to load a sequential component");
487 _numInstanceMutex.unlock() ;
488 iobject = Engines_Container_i::load_impl(nameToRegister,componentName);
489 if( CORBA::is_nil(iobject) ) return Engines::EngineComponent::_duplicate(iobject);
492 // Instanciation du composant parallele
493 MESSAGE("[" << _numproc << "] Try to load a parallel component");
494 PortableServer::ObjectId * id = (MPIComponent_factory)
495 (_orb, _poa, _id, instanceName.c_str(), _nameToRegister.c_str());
496 // get reference from id
497 CORBA::Object_var o = _poa->id_to_reference(*id);
498 pobj = Engines::MPIObject::_narrow(o) ;
499 iobject = Engines::EngineComponent::_narrow(o) ;
503 // utiliser + tard le registry ici :
504 // register the engine under the name containerName.dir/nameToRegister.object
505 std::string component_registerName = _containerName + "/" + _nameToRegister;
506 _NS->Register(iobject, component_registerName.c_str()) ;
509 _numInstanceMutex.unlock() ;
511 // Root recupere les ior des composants des autre process
512 BCastIOR(_orb,pobj,false);
514 END_OF("[" <<_numproc << "] MPIContainer_i::Lload_impl");
515 return Engines::EngineComponent::_duplicate(iobject);
519 void Engines_MPIContainer_i::remove_impl(Engines::EngineComponent_ptr component_i)
521 Engines::MPIObject_ptr pcptr;
522 Engines::MPIObject_ptr spcptr;
526 pcptr = (Engines::MPIObject_ptr)component_i;
527 th = new pthread_t[_nbproc];
528 for(int ip=1;ip<_nbproc;ip++){
529 thread_st *st = new thread_st;
532 spcptr = Engines::MPIObject::_narrow((*(pcptr->tior()))[ip]);
533 st->cptr = (Engines::EngineComponent_ptr)spcptr;
534 pthread_create(&(th[ip]),NULL,th_removeimpl,(void*)st);
538 ASSERT(! CORBA::is_nil(component_i));
539 std::string instanceName = component_i->instanceName() ;
540 MESSAGE("[" << _numproc << "] unload component " << instanceName);
541 _numInstanceMutex.lock() ; // lock on the remove on handle_map
542 _listInstances_map.erase(instanceName);
543 _numInstanceMutex.unlock() ;
544 component_i->destroy() ;
546 _NS->Destroy_Name(instanceName.c_str());
549 for(int ip=1;ip<_nbproc;ip++)
550 pthread_join(th[ip],NULL);
556 void Engines_MPIContainer_i::finalize_removal()
560 th = new pthread_t[_nbproc];
561 for(int ip=1;ip<_nbproc;ip++){
562 thread_st *st = new thread_st;
565 pthread_create(&(th[ip]),NULL,th_finalizeremoval,(void*)st);
569 _numInstanceMutex.lock(); // lock to be alone
570 // (see decInstanceCnt, load_component_Library)
571 std::map<std::string, void *>::iterator ith;
572 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
574 void *handle = (*ith).second;
575 std::string impl_name= (*ith).first;
580 // dlclose(handle); // SALOME unstable after ...
581 // _library_map.erase(impl_name);
584 _toRemove_map.clear();
585 _numInstanceMutex.unlock();
588 for(int ip=1;ip<_nbproc;ip++)
589 pthread_join(th[ip],NULL);
594 void *th_loadcomponentlibrary(void *s)
596 thread_st *st = (thread_st*)s;
598 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_component_Library(st->compoName.c_str(),reason);
599 CORBA::string_free(reason);
603 void *th_createcomponentinstance(void *s)
605 thread_st *st = (thread_st*)s;
606 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->create_component_instance(st->compoName.c_str(),st->studyId);
610 void *th_loadimpl(void *s)
612 thread_st *st = (thread_st*)s;
613 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_impl(st->nameToRegister.c_str(),st->compoName.c_str());
617 void *th_removeimpl(void *s)
619 thread_st *st = (thread_st*)s;
620 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->remove_impl(st->cptr);
624 void *th_finalizeremoval(void *s)
626 thread_st *st = (thread_st*)s;
627 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->finalize_removal();