1 // Copyright (C) 2007-2020 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, or (at your option) any later version.
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
26 #include "Basics_Utils.hxx"
27 #include "SALOME_Component_i.hxx"
28 #include "MPIContainer_i.hxx"
29 #include "SALOME_NamingService.hxx"
30 #include "Utils_SINGLETON.hxx"
32 #include "utilities.h"
35 #include <pthread.h> // must be before Python.h !
42 #include "Container_init_python.hxx"
44 // L'appel au registry SALOME ne se fait que pour le process 0
45 Engines_MPIContainer_i::Engines_MPIContainer_i(CORBA::ORB_ptr orb,
46 PortableServer::POA_ptr poa,
48 int argc, char *argv[])
49 : Engines_Container_i(orb,poa,containerName,argc,argv,false)
52 _id = _poa->activate_object(this);
53 CORBA::Object_var obj=_poa->id_to_reference(*_id);
54 Engines::Container_var pCont = Engines::Container::_narrow(obj);
59 _NS = new SALOME_NamingService();
60 _NS->init_orb( CORBA::ORB::_duplicate(_orb) ) ;
62 std::string hostname = Kernel_Utils::GetHostname();
63 _containerName = _NS->BuildContainerNameForNS(containerName,hostname.c_str());
64 SCRUTE(_containerName);
65 _NS->Register(pCont, _containerName.c_str());
69 // Root recupere les ior des container des autre process
70 Engines::MPIObject_var pobj = POA_Engines::MPIContainer::_this();
71 BCastIOR(_orb,pobj,true);
74 Engines_MPIContainer_i::Engines_MPIContainer_i()
75 : Engines_Container_i()
79 Engines_MPIContainer_i::~Engines_MPIContainer_i(void)
81 MESSAGE("[" << _numproc << "] Engines_MPIContainer_i::~Engines_MPIContainer_i()");
85 void Engines_MPIContainer_i::Shutdown()
88 MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
90 _NS->Destroy_FullDirectory(_containerName.c_str());
91 _NS->Destroy_Name(_containerName.c_str());
92 for(ip= 1;ip<_nbproc;ip++)
93 (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
96 std::map<std::string, Engines::EngineComponent_var>::iterator itm;
97 for (itm = _listInstances_map.begin(); itm != _listInstances_map.end(); itm++)
101 itm->second->destroy();
103 catch(const CORBA::Exception& e)
105 // ignore this entry and continue
109 // ignore this entry and continue
117 // Load a component library
118 bool Engines_MPIContainer_i::load_component_Library(const char* componentName, CORBA::String_out reason)
120 reason=CORBA::string_dup("");
124 th = new pthread_t[_nbproc];
125 for(int ip=1;ip<_nbproc;ip++){
126 thread_st *st = new thread_st;
129 st->compoName = componentName;
130 pthread_create(&(th[ip]),NULL,th_loadcomponentlibrary,(void*)st);
134 bool ret = Lload_component_Library(componentName);
137 for(int ip=1;ip<_nbproc;ip++)
138 pthread_join(th[ip],NULL);
144 bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
146 std::string aCompName = componentName;
148 // --- try dlopen C++ component
151 std::string impl_name = std::string ("lib") + aCompName + std::string("Engine.dylib");
153 std::string impl_name = std::string ("lib") + aCompName + std::string("Engine.so");
156 _numInstanceMutex.lock(); // lock to be alone
157 // (see decInstanceCnt, finalize_removal))
158 if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
159 if (_library_map[impl_name])
161 MESSAGE("[" << _numproc << "] Library " << impl_name << " already loaded");
162 _numInstanceMutex.unlock();
167 handle = dlopen( impl_name.c_str() , RTLD_LAZY | RTLD_GLOBAL ) ;
170 _library_map[impl_name] = handle;
171 _numInstanceMutex.unlock();
172 MESSAGE("[" << _numproc << "] Library " << impl_name << " loaded");
177 MESSAGE("[" << _numproc << "] Can't load shared library : " << impl_name);
178 MESSAGE("[" << _numproc << "] error dlopen: " << dlerror());
180 _numInstanceMutex.unlock();
182 // --- try import Python component
184 INFOS("[" << _numproc << "] try import Python component "<<componentName);
185 if (_isSupervContainer)
187 INFOS("[" << _numproc << "] Supervision Container does not support Python Component Engines");
190 if (_library_map[aCompName])
192 return true; // Python Component, already imported
196 Py_ACQUIRE_NEW_THREAD;
197 PyObject *mainmod = PyImport_AddModule((char *)"__main__");
198 PyObject *globals = PyModule_GetDict(mainmod);
199 PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
200 PyObject *result = PyObject_CallMethod(pyCont,
201 (char*)"import_component",
202 (char*)"s",componentName);
203 std::string ret= PyUnicode_AsUTF8(result);
205 Py_RELEASE_NEW_THREAD;
207 if (ret=="") // import possible: Python component
209 _library_map[aCompName] = (void *)pyCont; // any non O value OK
210 MESSAGE("[" << _numproc << "] import Python: "<<aCompName<<" OK");
217 // Create an instance of component
218 Engines::EngineComponent_ptr
219 Engines_MPIContainer_i::create_component_instance_env( const char* componentName,
220 const Engines::FieldsDict& /*env*/,
221 CORBA::String_out reason)
223 reason=CORBA::string_dup("");
227 th = new pthread_t[_nbproc];
228 for(int ip=1;ip<_nbproc;ip++){
229 thread_st *st = new thread_st;
232 st->compoName = componentName;
233 pthread_create(&(th[ip]),NULL,th_createcomponentinstance,(void*)st);
237 Engines::EngineComponent_ptr cptr = Lcreate_component_instance(componentName);
240 for(int ip=1;ip<_nbproc;ip++)
241 pthread_join(th[ip],NULL);
248 Engines::EngineComponent_ptr
249 Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterName )
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());
282 PyArg_ParseTuple(result,"ss", &ior, &error);
283 std::string iors = ior;
285 Py_RELEASE_NEW_THREAD;
287 CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
288 iobject = Engines::EngineComponent::_narrow( obj ) ;
289 pobj = Engines::MPIObject::_narrow(obj) ;
291 _NS->Register(iobject, component_registerName.c_str()) ;
292 // Root recupere les ior des composants des autre process
293 BCastIOR(_orb,pobj,false);
295 return iobject._retn();
301 std::string impl_name = std::string ("lib") + genericRegisterName +std::string("Engine.dylib");
303 std::string impl_name = std::string ("lib") + genericRegisterName +std::string("Engine.so");
305 if (_library_map.count(impl_name) != 0) // C++ component
307 void* handle = _library_map[impl_name];
308 iobject = createMPIInstance(genericRegisterName,
310 return iobject._retn();
313 return Engines::EngineComponent::_nil() ;
316 Engines::EngineComponent_ptr
317 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 // --- Instantiate 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 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 _listInstances_map[instanceName] = iobject;
375 _cntInstances_map[aGenRegisterName] += 1;
377 // --- register the engine under the name
378 // containerName(.dir)/instanceName(.object)
381 _NS->Register( iobject , component_registerName.c_str() ) ;
382 MESSAGE( component_registerName.c_str() << " bound" ) ;
384 // Root recupere les ior des composants des autre process
385 BCastIOR(_orb,pobj,false);
388 catch(const std::exception &ex){
390 return Engines::EngineComponent::_nil();
392 return iobject._retn();
396 Engines::EngineComponent_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
397 const char* componentName)
401 th = new pthread_t[_nbproc];
402 for(int ip=1;ip<_nbproc;ip++){
403 thread_st *st = new thread_st;
406 st->nameToRegister = nameToRegister;
407 st->compoName = componentName;
408 pthread_create(&(th[ip]),NULL,th_loadimpl,(void*)st);
412 Engines::EngineComponent_ptr cptr = Lload_impl(nameToRegister,componentName);
415 for(int ip=1;ip<_nbproc;ip++)
416 pthread_join(th[ip],NULL);
424 Engines::EngineComponent_ptr Engines_MPIContainer_i::Lload_impl(
425 const char* nameToRegister,
426 const char* componentName)
428 Engines::EngineComponent_var iobject;
429 Engines::MPIObject_var pobj;
432 sprintf(cproc,"_%d",_numproc);
434 BEGIN_OF("[" << _numproc << "] MPIContainer_i::Lload_impl");
436 _numInstanceMutex.lock() ; // lock on the instance number
439 sprintf(_aNumI,"%d",_numInstance) ;
441 std::string _impl_name = componentName;
442 std::string _nameToRegister = nameToRegister;
443 std::string instanceName = _nameToRegister + "_inst_" + _aNumI + cproc;
444 MESSAGE("[" << _numproc << "] instanceName=" << instanceName);
446 std::string absolute_impl_name(_impl_name);
447 MESSAGE("[" << _numproc << "] absolute_impl_name=" << absolute_impl_name);
448 void * handle = dlopen(absolute_impl_name.c_str(), RTLD_LAZY | RTLD_GLOBAL);
450 INFOS("[" << _numproc << "] Can't load shared library : " << absolute_impl_name);
451 INFOS("[" << _numproc << "] error dlopen: " << dlerror());
452 return Engines::EngineComponent::_nil() ;
455 std::string factory_name = _nameToRegister + std::string("Engine_factory");
456 MESSAGE("[" << _numproc << "] factory_name=" << factory_name) ;
459 PortableServer::ObjectId * (*MPIComponent_factory) (CORBA::ORB_ptr,
460 PortableServer::POA_ptr,
461 PortableServer::ObjectId *,
464 (PortableServer::ObjectId * (*) (CORBA::ORB_ptr,
465 PortableServer::POA_ptr,
466 PortableServer::ObjectId *,
469 dlsym(handle, factory_name.c_str());
472 if ((error = dlerror()) != NULL){
473 // Try to load a sequential component
474 MESSAGE("[" << _numproc << "] Try to load a sequential component");
475 _numInstanceMutex.unlock() ;
476 iobject = Engines_Container_i::load_impl(nameToRegister,componentName);
477 if( CORBA::is_nil(iobject) ) return Engines::EngineComponent::_duplicate(iobject);
480 // Instanciation du composant parallele
481 MESSAGE("[" << _numproc << "] Try to load a parallel component");
482 PortableServer::ObjectId * id = (MPIComponent_factory)
483 (_orb, _poa, _id, instanceName.c_str(), _nameToRegister.c_str());
484 // get reference from id
485 CORBA::Object_var o = _poa->id_to_reference(*id);
486 pobj = Engines::MPIObject::_narrow(o) ;
487 iobject = Engines::EngineComponent::_narrow(o) ;
491 // utiliser + tard le registry ici :
492 // register the engine under the name containerName.dir/nameToRegister.object
493 std::string component_registerName = _containerName + "/" + _nameToRegister;
494 _NS->Register(iobject, component_registerName.c_str()) ;
497 _numInstanceMutex.unlock() ;
499 // Root recupere les ior des composants des autre process
500 BCastIOR(_orb,pobj,false);
502 END_OF("[" <<_numproc << "] MPIContainer_i::Lload_impl");
503 return Engines::EngineComponent::_duplicate(iobject);
507 void Engines_MPIContainer_i::remove_impl(Engines::EngineComponent_ptr component_i)
509 Engines::MPIObject_ptr pcptr;
510 Engines::MPIObject_ptr spcptr;
514 pcptr = (Engines::MPIObject_ptr)component_i;
515 th = new pthread_t[_nbproc];
516 for(int ip=1;ip<_nbproc;ip++){
517 thread_st *st = new thread_st;
520 spcptr = Engines::MPIObject::_narrow((*(pcptr->tior()))[ip]);
521 st->cptr = (Engines::EngineComponent_ptr)spcptr;
522 pthread_create(&(th[ip]),NULL,th_removeimpl,(void*)st);
526 ASSERT(! CORBA::is_nil(component_i));
527 std::string instanceName = component_i->instanceName() ;
528 MESSAGE("[" << _numproc << "] unload component " << instanceName);
529 _numInstanceMutex.lock() ; // lock on the remove on handle_map
530 _listInstances_map.erase(instanceName);
531 _numInstanceMutex.unlock() ;
532 component_i->destroy() ;
534 _NS->Destroy_Name(instanceName.c_str());
537 for(int ip=1;ip<_nbproc;ip++)
538 pthread_join(th[ip],NULL);
544 void Engines_MPIContainer_i::finalize_removal()
548 th = new pthread_t[_nbproc];
549 for(int ip=1;ip<_nbproc;ip++){
550 thread_st *st = new thread_st;
553 pthread_create(&(th[ip]),NULL,th_finalizeremoval,(void*)st);
557 _numInstanceMutex.lock(); // lock to be alone
558 // (see decInstanceCnt, load_component_Library)
559 std::map<std::string, void *>::iterator ith;
560 for (ith = _toRemove_map.begin(); ith != _toRemove_map.end(); ith++)
562 void *handle = (*ith).second;
563 std::string impl_name= (*ith).first;
568 // dlclose(handle); // SALOME unstable after ...
569 // _library_map.erase(impl_name);
572 _toRemove_map.clear();
573 _numInstanceMutex.unlock();
576 for(int ip=1;ip<_nbproc;ip++)
577 pthread_join(th[ip],NULL);
582 void *th_loadcomponentlibrary(void *s)
584 thread_st *st = (thread_st*)s;
586 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_component_Library(st->compoName.c_str(),reason);
587 CORBA::string_free(reason);
591 void *th_createcomponentinstance(void *s)
593 thread_st *st = (thread_st*)s;
594 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->create_component_instance(st->compoName.c_str());
598 void *th_loadimpl(void *s)
600 thread_st *st = (thread_st*)s;
601 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->load_impl(st->nameToRegister.c_str(),st->compoName.c_str());
605 void *th_removeimpl(void *s)
607 thread_st *st = (thread_st*)s;
608 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->remove_impl(st->cptr);
612 void *th_finalizeremoval(void *s)
614 thread_st *st = (thread_st*)s;
615 (Engines::MPIContainer::_narrow((*(st->tior))[st->ip]))->finalize_removal();