1 // Copyright (C) 2007-2010 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 // SALOME MPIContainer : implemenation of container based on MPI libraries
23 // File : MPIContainer_i.cxx
29 #include "Basics_Utils.hxx"
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"
42 // L'appel au registry SALOME ne se fait que pour le process 0
43 Engines_MPIContainer_i::Engines_MPIContainer_i(CORBA::ORB_ptr orb,
44 PortableServer::POA_ptr poa,
46 int argc, char *argv[])
47 : Engines_Container_i(orb,poa,containerName,argc,argv,false)
50 _id = _poa->activate_object(this);
51 CORBA::Object_var obj=_poa->id_to_reference(*_id);
52 Engines::Container_var pCont = Engines::Container::_narrow(obj);
57 _NS = new SALOME_NamingService();
58 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
60 std::string hostname = Kernel_Utils::GetHostname();
61 _containerName = _NS->BuildContainerNameForNS(containerName,hostname.c_str());
62 SCRUTE(_containerName);
63 _NS->Register(pCont, _containerName.c_str());
67 // Root recupere les ior des container des autre process
68 Engines::MPIObject_var pobj = POA_Engines::MPIContainer::_this();
69 BCastIOR(_orb,pobj,true);
72 Engines_MPIContainer_i::Engines_MPIContainer_i()
73 : Engines_Container_i()
77 Engines_MPIContainer_i::~Engines_MPIContainer_i(void)
79 MESSAGE("[" << _numproc << "] Engines_MPIContainer_i::~Engines_MPIContainer_i()");
83 void Engines_MPIContainer_i::Shutdown()
86 MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
88 _NS->Destroy_FullDirectory(_containerName.c_str());
89 _NS->Destroy_Name(_containerName.c_str());
90 for(ip= 1;ip<_nbproc;ip++)
91 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
94 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
95 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
99 itm->second->destroy();
101 catch(const CORBA::Exception& e)
103 // ignore this entry and continue
107 // ignore this entry and continue
115 // Load a component library
116 bool Engines_MPIContainer_i::load_component_Library(const char* componentName, CORBA::String_out reason)
118 reason=CORBA::string_dup("");
122 th = new pthread_t[_nbproc];
123 for(int ip=1;ip<_nbproc;ip++){
124 thread_st *st = new thread_st;
127 st->compoName = componentName;
128 pthread_create(&(th[ip]),NULL,th_loadcomponentlibrary,(void*)st);
132 bool ret = Lload_component_Library(componentName);
135 for(int ip=1;ip<_nbproc;ip++)
136 pthread_join(th[ip],NULL);
142 bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
144 std::string aCompName = componentName;
146 // --- try dlopen C++ component
148 std::string impl_name = std::string ("lib") + aCompName + std::string("Engine.so");
150 _numInstanceMutex.lock(); // lock to be alone
151 // (see decInstanceCnt, finalize_removal))
152 if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
153 if (_library_map[impl_name])
155 MESSAGE("[" << _numproc << "] Library " << impl_name << " already loaded");
156 _numInstanceMutex.unlock();
161 handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
164 _library_map[impl_name] = handle;
165 _numInstanceMutex.unlock();
166 MESSAGE("[" << _numproc << "] Library " << impl_name << " loaded");
171 MESSAGE("[" << _numproc << "] Can't load shared library : " << impl_name);
172 MESSAGE("[" << _numproc << "] error dlopen: " << dlerror());
174 _numInstanceMutex.unlock();
176 // --- try import Python component
178 INFOS("[" << _numproc << "] try import Python component "<<componentName);
179 if (_isSupervContainer)
181 INFOS("[" << _numproc << "] Supervision Container does not support Python Component Engines");
184 if (_library_map[aCompName])
186 return true; // Python Component, already imported
190 Py_ACQUIRE_NEW_THREAD;
191 PyObject *mainmod = PyImport_AddModule((char *)"__main__");
192 PyObject *globals = PyModule_GetDict(mainmod);
193 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
194 PyObject *result = PyObject_CallMethod(pyCont,
195 (char*)"import_component",
196 (char*)"s",componentName);
197 std::string ret= PyString_AsString(result);
199 Py_RELEASE_NEW_THREAD;
201 if (ret=="") // import possible: Python component
203 _library_map[aCompName] = (void *)pyCont; // any non O value OK
204 MESSAGE("[" << _numproc << "] import Python: "<<aCompName<<" OK");
211 // Create an instance of component
212 Engines::EngineComponent_ptr
213 Engines_MPIContainer_i::create_component_instance_env( const char* componentName,
215 const Engines::FieldsDict& env,
216 CORBA::String_out reason)
218 reason=CORBA::string_dup("");
222 th = new pthread_t[_nbproc];
223 for(int ip=1;ip<_nbproc;ip++){
224 thread_st *st = new thread_st;
227 st->compoName = componentName;
228 st->studyId = studyId;
229 pthread_create(&(th[ip]),NULL,th_createcomponentinstance,(void*)st);
233 Engines::EngineComponent_ptr cptr = Lcreate_component_instance(componentName,studyId);
236 for(int ip=1;ip<_nbproc;ip++)
237 pthread_join(th[ip],NULL);
244 Engines::EngineComponent_ptr
245 Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterName, CORBA::Long studyId)
248 INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
249 return Engines::EngineComponent::_nil() ;
252 Engines::EngineComponent_var iobject = Engines::EngineComponent::_nil() ;
253 Engines::MPIObject_var pobj;
255 std::string aCompName = genericRegisterName;
256 if (_library_map[aCompName]) { // Python component
257 if (_isSupervContainer) {
258 INFOS("Supervision Container does not support Python Component Engines");
259 return Engines::EngineComponent::_nil();
261 _numInstanceMutex.lock() ; // lock on the instance number
263 int numInstance = _numInstance ;
264 _numInstanceMutex.unlock() ;
267 sprintf( aNumI , "%d" , numInstance ) ;
268 std::string instanceName = aCompName + "_inst_" + aNumI ;
269 std::string component_registerName =
270 _containerName + "/" + instanceName;
272 Py_ACQUIRE_NEW_THREAD;
273 PyObject *mainmod = PyImport_AddModule((char*)"__main__");
274 PyObject *globals = PyModule_GetDict(mainmod);
275 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
276 PyObject *result = PyObject_CallMethod(pyCont,
277 (char*)"create_component_instance",
280 instanceName.c_str(),
284 PyArg_ParseTuple(result,"ss", &ior, &error);
285 std::string iors = ior;
287 Py_RELEASE_NEW_THREAD;
289 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
290 iobject = Engines::EngineComponent::_narrow( obj ) ;
291 pobj = Engines::MPIObject::_narrow(obj) ;
293 _NS->Register(iobject, component_registerName.c_str()) ;
294 // Root recupere les ior des composants des autre process
295 BCastIOR(_orb,pobj,false);
297 return iobject._retn();
302 std::string impl_name = std::string ("lib") + genericRegisterName +std::string("Engine.so");
303 if (_library_map.count(impl_name) != 0) // C++ component
305 void* handle = _library_map[impl_name];
306 iobject = createMPIInstance(genericRegisterName,
309 return iobject._retn();
312 return Engines::EngineComponent::_nil() ;
315 Engines::EngineComponent_ptr
316 Engines_MPIContainer_i::createMPIInstance(std::string genericRegisterName,
320 Engines::EngineComponent_var iobject;
321 Engines::MPIObject_var pobj;
322 // --- find the factory
324 std::string aGenRegisterName = genericRegisterName;
325 std::string factory_name = aGenRegisterName + std::string("Engine_factory");
327 typedef PortableServer::ObjectId * (*MPIFACTORY_FUNCTION)
329 PortableServer::POA_ptr,
330 PortableServer::ObjectId *,
335 MPIFACTORY_FUNCTION MPIComponent_factory = (MPIFACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
337 if ( !MPIComponent_factory )
339 INFOS( "[" << _numproc << "] Can't resolve symbol: " + factory_name );
341 pobj = Engines::MPIObject::_nil();
342 BCastIOR(_orb,pobj,false);
343 return Engines::EngineComponent::_nil();
346 // --- create instance
348 iobject = Engines::EngineComponent::_nil() ;
352 _numInstanceMutex.lock() ; // lock on the instance number
354 int numInstance = _numInstance ;
355 _numInstanceMutex.unlock() ;
358 sprintf( aNumI , "%d" , numInstance ) ;
359 std::string instanceName = aGenRegisterName + "_inst_" + aNumI ;
360 std::string component_registerName =
361 _containerName + "/" + instanceName;
363 // --- Instanciate required CORBA object
365 PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
366 id = (MPIComponent_factory) ( _orb, _poa, _id, instanceName.c_str(), aGenRegisterName.c_str() ) ;
368 // --- get reference & servant from id
370 CORBA::Object_var obj = _poa->id_to_reference(*id);
371 iobject = Engines::EngineComponent::_narrow( obj ) ;
372 pobj = Engines::MPIObject::_narrow(obj) ;
374 Engines_Component_i *servant =
375 dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
377 //SCRUTE(servant->pd_refCount);
378 servant->_remove_ref(); // compensate previous id_to_reference
379 //SCRUTE(servant->pd_refCount);
380 _listInstances_map[instanceName] = iobject;
381 _cntInstances_map[aGenRegisterName] += 1;
382 //SCRUTE(servant->pd_refCount);
384 servant->setStudyId(studyId);
386 bool ret_studyId = servant->setStudyId(studyId);
390 // --- register the engine under the name
391 // containerName(.dir)/instanceName(.object)
394 _NS->Register( iobject , component_registerName.c_str() ) ;
395 MESSAGE( component_registerName.c_str() << " bound" ) ;
397 // Root recupere les ior des composants des autre process
398 BCastIOR(_orb,pobj,false);
401 catch(const std::exception &ex){
403 return Engines::EngineComponent::_nil();
405 return iobject._retn();
409 Engines::EngineComponent_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
410 const char* componentName)
414 th = new pthread_t[_nbproc];
415 for(int ip=1;ip<_nbproc;ip++){
416 thread_st *st = new thread_st;
419 st->nameToRegister = nameToRegister;
420 st->compoName = componentName;
421 pthread_create(&(th[ip]),NULL,th_loadimpl,(void*)st);
425 Engines::EngineComponent_ptr cptr = Lload_impl(nameToRegister,componentName);
428 for(int ip=1;ip<_nbproc;ip++)
429 pthread_join(th[ip],NULL);
437 Engines::EngineComponent_ptr Engines_MPIContainer_i::Lload_impl(
438 const char* nameToRegister,
439 const char* componentName)
441 Engines::EngineComponent_var iobject;
442 Engines::MPIObject_var pobj;
445 sprintf(cproc,"_%d",_numproc);
447 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lload_impl");
449 _numInstanceMutex.lock() ; // lock on the instance number
452 sprintf(_aNumI,"%d",_numInstance) ;
454 std::string _impl_name = componentName;
455 std::string _nameToRegister = nameToRegister;
456 std::string instanceName = _nameToRegister + "_inst_" + _aNumI + cproc;
457 MESSAGE("[" << _numproc << "] instanceName=" << instanceName);
459 std::string absolute_impl_name(_impl_name);
460 MESSAGE("[" << _numproc << "] absolute_impl_name=" << absolute_impl_name);
461 void * handle = dlopen(absolute_impl_name.c_str(), RTLD_LAZY);
463 INFOS("[" << _numproc << "] Can't load shared library : " << absolute_impl_name);
464 INFOS("[" << _numproc << "] error dlopen: " << dlerror());
465 return Engines::EngineComponent::_nil() ;
468 std::string factory_name = _nameToRegister + std::string("Engine_factory");
469 MESSAGE("[" << _numproc << "] factory_name=" << factory_name) ;
472 PortableServer::ObjectId * (*MPIComponent_factory) (CORBA::ORB_ptr,
473 PortableServer::POA_ptr,
474 PortableServer::ObjectId *,
477 (PortableServer::ObjectId * (*) (CORBA::ORB_ptr,
478 PortableServer::POA_ptr,
479 PortableServer::ObjectId *,
482 dlsym(handle, factory_name.c_str());
485 if ((error = dlerror()) != NULL){
486 // Try to load a sequential component
487 MESSAGE("[" << _numproc << "] Try to load a sequential component");
488 _numInstanceMutex.unlock() ;
489 iobject = Engines_Container_i::load_impl(nameToRegister,componentName);
490 if( CORBA::is_nil(iobject) ) return Engines::EngineComponent::_duplicate(iobject);
493 // Instanciation du composant parallele
494 MESSAGE("[" << _numproc << "] Try to load a parallel component");
495 PortableServer::ObjectId * id = (MPIComponent_factory)
496 (_orb, _poa, _id, instanceName.c_str(), _nameToRegister.c_str());
497 // get reference from id
498 CORBA::Object_var o = _poa->id_to_reference(*id);
499 pobj = Engines::MPIObject::_narrow(o) ;
500 iobject = Engines::EngineComponent::_narrow(o) ;
504 // utiliser + tard le registry ici :
505 // register the engine under the name containerName.dir/nameToRegister.object
506 std::string component_registerName = _containerName + "/" + _nameToRegister;
507 _NS->Register(iobject, component_registerName.c_str()) ;
510 _numInstanceMutex.unlock() ;
512 // Root recupere les ior des composants des autre process
513 BCastIOR(_orb,pobj,false);
515 END_OF("[" <<_numproc << "] MPIContainer_i::Lload_impl");
516 return Engines::EngineComponent::_duplicate(iobject);
520 void Engines_MPIContainer_i::remove_impl(Engines::EngineComponent_ptr component_i)
522 Engines::MPIObject_ptr pcptr;
523 Engines::MPIObject_ptr spcptr;
527 pcptr = (Engines::MPIObject_ptr)component_i;
528 th = new pthread_t[_nbproc];
529 for(int ip=1;ip<_nbproc;ip++){
530 thread_st *st = new thread_st;
533 spcptr = Engines::MPIObject::_narrow((*(pcptr->tior()))[ip]);
534 st->cptr = (Engines::EngineComponent_ptr)spcptr;
535 pthread_create(&(th[ip]),NULL,th_removeimpl,(void*)st);
539 ASSERT(! CORBA::is_nil(component_i));
540 std::string instanceName = component_i->instanceName() ;
541 MESSAGE("[" << _numproc << "] unload component " << instanceName);
542 _numInstanceMutex.lock() ; // lock on the remove on handle_map
543 _listInstances_map.erase(instanceName);
544 _numInstanceMutex.unlock() ;
545 component_i->destroy() ;
547 _NS->Destroy_Name(instanceName.c_str());
550 for(int ip=1;ip<_nbproc;ip++)
551 pthread_join(th[ip],NULL);
557 void Engines_MPIContainer_i::finalize_removal()
561 th = new pthread_t[_nbproc];
562 for(int ip=1;ip<_nbproc;ip++){
563 thread_st *st = new thread_st;
566 pthread_create(&(th[ip]),NULL,th_finalizeremoval,(void*)st);
570 _numInstanceMutex.lock(); // lock to be alone
571 // (see decInstanceCnt, load_component_Library)
572 std::map<std::string, void *>::iterator ith;
573 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
575 void *handle = (*ith).second;
576 std::string impl_name= (*ith).first;
581 // dlclose(handle); // SALOME unstable after ...
582 // _library_map.erase(impl_name);
585 _toRemove_map.clear();
586 _numInstanceMutex.unlock();
589 for(int ip=1;ip<_nbproc;ip++)
590 pthread_join(th[ip],NULL);
595 void *th_loadcomponentlibrary(void *s)
597 thread_st *st = (thread_st*)s;
599 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_component_Library(st->compoName.c_str(),reason);
600 CORBA::string_free(reason);
604 void *th_createcomponentinstance(void *s)
606 thread_st *st = (thread_st*)s;
607 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->create_component_instance(st->compoName.c_str(),st->studyId);
611 void *th_loadimpl(void *s)
613 thread_st *st = (thread_st*)s;
614 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_impl(st->nameToRegister.c_str(),st->compoName.c_str());
618 void *th_removeimpl(void *s)
620 thread_st *st = (thread_st*)s;
621 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->remove_impl(st->cptr);
625 void *th_finalizeremoval(void *s)
627 thread_st *st = (thread_st*)s;
628 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->finalize_removal();