]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
last version after merge BR_V300_lcrm
authorsecher <secher>
Wed, 20 Jul 2005 11:42:43 +0000 (11:42 +0000)
committersecher <secher>
Wed, 20 Jul 2005 11:42:43 +0000 (11:42 +0000)
idl/SALOME_MPIContainer.idl
src/Container/SALOME_Container_i.hxx
src/MPIContainer/MPIContainer_i.cxx
src/MPIContainer/MPIContainer_i.hxx
src/MPIContainer/Makefile.in
src/NamingService/SALOME_NamingService.cxx
src/ResourcesManager/SALOME_ResourcesManager.cxx

index dfe23ec5ebb6f636d2ffa1472ce06041533a8b60..13e135f6bca997697f1ff56b2a18d73925382f52 100644 (file)
@@ -32,6 +32,8 @@ module Engines
   interface MPIContainer:Container,MPIObject
   {
     // asynchronous version to load and remove parallel component in parallel
+    oneway void Asload_component_Library(in string componentName);
+    oneway void Ascreate_component_instance(in string componentName,in long studyId);
     oneway void Asload_impl(in string nameToRegister, in string componentName);
     oneway void Asremove_impl(in Component component_i);
     oneway void Asfinalize_removal();
index d64dd22a03448a493fa32892cd8ad87d97a05f8d..c6135bb8d181716a08dda7950e245dbfd3e17e16 100644 (file)
@@ -58,9 +58,9 @@ public:
 
   // --- CORBA methods
 
-  bool load_component_Library(const char* componentName);
+  virtual bool load_component_Library(const char* componentName);
 
-  Engines::Component_ptr
+  virtual Engines::Component_ptr
   create_component_instance( const char* componentName,
                             CORBA::Long studyId); // 0 for multiStudy
 
index 97e6bcffaae4a73506ba5d1255d9e97766b629f7..3b97ad7938dd76dcec8cd55aee9c547c61c620f2 100644 (file)
 #include <iostream.h>
 #include <dlfcn.h>
 #include <stdio.h>
+#include "SALOME_Component_i.hxx"
 #include "MPIContainer_i.hxx"
 #include "SALOME_NamingService.hxx"
 #include "Utils_SINGLETON.hxx"
 #include "OpUtil.hxx"
 #include "utilities.h"
+#include <Python.h>
+#include "Container_init_python.hxx"
 using namespace std;
 
 // L'appel au registry SALOME ne se fait que pour le process 0
@@ -79,14 +82,295 @@ Engines_MPIContainer_i::~Engines_MPIContainer_i(void)
 }
 
 // Load component
-Engines::Component_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
-                                                const char* componentName)
+void Engines_MPIContainer_i::Shutdown()
 {
   int ip;
+  MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
+  if( _numproc == 0 ){
+    _NS->Destroy_FullDirectory(_containerName.c_str());
+    for(ip= 1;ip<_nbproc;ip++)
+      (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
+  }
+  _orb->shutdown(0);
 
+}
+
+// Load a component library
+bool Engines_MPIContainer_i::load_component_Library(const char* componentName)
+{
   if( _numproc == 0 ){
     // Invocation du chargement du composant dans les autres process
-    for(ip= 1;ip<_nbproc;ip++)
+    for(int ip= 1;ip<_nbproc;ip++)
+      (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asload_component_Library(componentName);
+  }
+
+  return Lload_component_Library(componentName);
+}
+
+void Engines_MPIContainer_i::Asload_component_Library(const char* componentName)
+{
+  Lload_component_Library(componentName);
+}
+
+bool Engines_MPIContainer_i::Lload_component_Library(const char* componentName)
+{
+  string aCompName = componentName;
+
+  // --- try dlopen C++ component
+
+  string impl_name = string ("lib") + aCompName + string("Engine.so");
+  SCRUTE(impl_name);
+  
+  _numInstanceMutex.lock(); // lock to be alone 
+  // (see decInstanceCnt, finalize_removal))
+  if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name);
+  if (_library_map[impl_name])
+    {
+      MESSAGE("[" << _numproc << "] Library " << impl_name << " already loaded");
+      _numInstanceMutex.unlock();
+      return true;
+    }
+  
+  void* handle;
+  handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
+  if ( handle )
+    {
+      _library_map[impl_name] = handle;
+      _numInstanceMutex.unlock();
+      return true;
+    }
+  else
+    {
+      INFOS("[" << _numproc << "] Can't load shared library : " << impl_name);
+      INFOS("[" << _numproc << "] error dlopen: " << dlerror());
+    }
+  _numInstanceMutex.unlock();
+
+  // --- try import Python component
+
+  INFOS("[" << _numproc << "] try import Python component "<<componentName);
+  if (_isSupervContainer)
+    {
+      INFOS("[" << _numproc << "] Supervision Container does not support Python Component Engines");
+      return false;
+    }
+  if (_library_map[aCompName])
+    {
+      return true; // Python Component, already imported
+    }
+  else
+    {
+      Py_ACQUIRE_NEW_THREAD;
+      PyObject *mainmod = PyImport_AddModule("__main__");
+      PyObject *globals = PyModule_GetDict(mainmod);
+      PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+      PyObject *result = PyObject_CallMethod(pyCont,
+                                            "import_component",
+                                            "s",componentName);
+      int ret= PyInt_AsLong(result);
+      SCRUTE(ret);
+      Py_RELEASE_NEW_THREAD;
+  
+      if (ret) // import possible: Python component
+       {
+         _library_map[aCompName] = (void *)pyCont; // any non O value OK
+         MESSAGE("[" << _numproc << "] import Python: "<<aCompName<<" OK");
+         return true;
+       }
+    }
+  return false;
+}
+
+// Create an instance of component
+Engines::Component_ptr
+Engines_MPIContainer_i::create_component_instance( const char* componentName,
+                                                  CORBA::Long studyId)
+{
+  if( _numproc == 0 ){
+    // Invocation du chargement du composant dans les autres process
+    for(int ip= 1;ip<_nbproc;ip++)
+      (Engines::MPIContainer::_narrow((*_tior)[ip]))->Ascreate_component_instance(componentName,studyId);
+  }
+
+  return Lcreate_component_instance(componentName,studyId);
+}
+
+void Engines_MPIContainer_i::Ascreate_component_instance( const char* componentName,
+                                                         CORBA::Long studyId)
+{
+  Lcreate_component_instance(componentName,studyId);
+}
+
+Engines::Component_ptr
+Engines_MPIContainer_i::Lcreate_component_instance( const char* genericRegisterName, CORBA::Long studyId)
+{
+  if (studyId < 0) {
+    INFOS("studyId must be > 0 for mono study instance, =0 for multiStudy");
+    return Engines::Component::_nil() ;
+  }
+
+  Engines::Component_var iobject = Engines::Component::_nil() ;
+  Engines::MPIObject_var pobj;
+
+  string aCompName = genericRegisterName;
+  if (_library_map[aCompName]) { // Python component
+    if (_isSupervContainer) {
+      INFOS("Supervision Container does not support Python Component Engines");
+      return Engines::Component::_nil();
+    }
+    _numInstanceMutex.lock() ; // lock on the instance number
+    _numInstance++ ;
+    int numInstance = _numInstance ;
+    _numInstanceMutex.unlock() ;
+
+    char aNumI[12];
+    sprintf( aNumI , "%d" , numInstance ) ;
+    string instanceName = aCompName + "_inst_" + aNumI ;
+    string component_registerName =
+      _containerName + "/" + instanceName;
+
+    Py_ACQUIRE_NEW_THREAD;
+    PyObject *mainmod = PyImport_AddModule("__main__");
+    PyObject *globals = PyModule_GetDict(mainmod);
+    PyObject *pyCont = PyDict_GetItemString(globals, "pyCont");
+    PyObject *result = PyObject_CallMethod(pyCont,
+                                          "create_component_instance",
+                                          "ssl",
+                                          aCompName.c_str(),
+                                          instanceName.c_str(),
+                                          studyId);
+    string iors = PyString_AsString(result);
+    SCRUTE(iors);
+    Py_RELEASE_NEW_THREAD;
+  
+    CORBA::Object_var obj = _orb->string_to_object(iors.c_str());
+    iobject = Engines::Component::_narrow( obj ) ;
+    pobj = Engines::MPIObject::_narrow(obj) ;
+    if( _numproc == 0 )
+      _NS->Register(iobject, component_registerName.c_str()) ;
+    // Root recupere les ior des composants des autre process
+    BCastIOR(_orb,pobj,false);
+
+    return iobject._retn();
+  }
+  
+  //--- try C++
+
+  string impl_name = string ("lib") + genericRegisterName +string("Engine.so");
+  void* handle = _library_map[impl_name];
+  if ( !handle ) {
+    INFOS("shared library " << impl_name <<"must be loaded before instance");
+    return Engines::Component::_nil() ;
+  }
+  else {
+    iobject = createMPIInstance(genericRegisterName,
+                               handle,
+                               studyId);
+    return iobject._retn();
+  }
+}
+
+Engines::Component_ptr
+Engines_MPIContainer_i::createMPIInstance(string genericRegisterName,
+                                         void *handle,
+                                         int studyId)
+{
+  Engines::Component_var iobject;
+  Engines::MPIObject_var pobj;
+  // --- find the factory
+
+  string aGenRegisterName = genericRegisterName;
+  string factory_name = aGenRegisterName + string("Engine_factory");
+  SCRUTE(factory_name) ;
+
+  typedef  PortableServer::ObjectId * (*MPIFACTORY_FUNCTION)
+    (int,int,
+     CORBA::ORB_ptr,
+     PortableServer::POA_ptr, 
+     PortableServer::ObjectId *, 
+     const char *, 
+     const char *) ;
+
+  MPIFACTORY_FUNCTION MPIComponent_factory
+    = (MPIFACTORY_FUNCTION) dlsym(handle, factory_name.c_str());
+
+  char *error ;
+  if ( (error = dlerror() ) != NULL) {
+    // Try to load a sequential component
+    MESSAGE("[" << _numproc << "] Try to load a sequential component");
+    _numInstanceMutex.unlock() ;
+    iobject = Engines_Container_i::createInstance(genericRegisterName,handle,studyId);
+    if( CORBA::is_nil(iobject) ) return Engines::Component::_duplicate(iobject);
+  }
+
+  // --- create instance
+
+  iobject = Engines::Component::_nil() ;
+
+  try
+    {
+      _numInstanceMutex.lock() ; // lock on the instance number
+      _numInstance++ ;
+      int numInstance = _numInstance ;
+      _numInstanceMutex.unlock() ;
+
+      char aNumI[12];
+      sprintf( aNumI , "%d" , numInstance ) ;
+      string instanceName = aGenRegisterName + "_inst_" + aNumI ;
+      string component_registerName =
+       _containerName + "/" + instanceName;
+
+      // --- Instanciate required CORBA object
+
+      PortableServer::ObjectId *id ; //not owner, do not delete (nore use var)
+      id = (MPIComponent_factory) ( _nbproc,_numproc,_orb, _poa, _id, instanceName.c_str(),
+                                aGenRegisterName.c_str() ) ;
+
+      // --- get reference & servant from id
+
+      CORBA::Object_var obj = _poa->id_to_reference(*id);
+      iobject = Engines::Component::_narrow( obj ) ;
+      pobj = Engines::MPIObject::_narrow(obj) ;
+
+      Engines_Component_i *servant =
+       dynamic_cast<Engines_Component_i*>(_poa->reference_to_servant(iobject));
+      ASSERT(servant);
+      //SCRUTE(servant->pd_refCount);
+      servant->_remove_ref(); // compensate previous id_to_reference 
+      //SCRUTE(servant->pd_refCount);
+      _listInstances_map[instanceName] = iobject;
+      _cntInstances_map[aGenRegisterName] += 1;
+      SCRUTE(aGenRegisterName);
+      SCRUTE(_cntInstances_map[aGenRegisterName]);
+      //SCRUTE(servant->pd_refCount);
+      bool ret_studyId = servant->setStudyId(studyId);
+      ASSERT(ret_studyId);
+
+      // --- register the engine under the name
+      //     containerName(.dir)/instanceName(.object)
+
+      if( _numproc == 0 ){
+       _NS->Register( iobject , component_registerName.c_str() ) ;
+       MESSAGE( component_registerName.c_str() << " bound" ) ;
+      }
+      // Root recupere les ior des composants des autre process
+      BCastIOR(_orb,pobj,false);
+
+    }
+  catch (...)
+    {
+      INFOS( "Container_i::createInstance exception catched" ) ;
+    }
+  return iobject._retn();
+}
+
+// Load component
+Engines::Component_ptr Engines_MPIContainer_i::load_impl(const char* nameToRegister,
+                                                const char* componentName)
+{
+  if( _numproc == 0 ){
+    // Invocation du chargement du composant dans les autres process
+    for(int ip= 1;ip<_nbproc;ip++)
       (Engines::MPIContainer::_narrow((*_tior)[ip]))->Asload_impl(nameToRegister,
                                                                componentName);
   }
@@ -268,18 +552,3 @@ void Engines_MPIContainer_i::Lfinalize_removal()
 
   END_OF("[" << _numproc << "] MPIContainer_i::Lfinalize_removal");
 }
-
-// Load component
-void Engines_MPIContainer_i::Shutdown()
-{
-  int ip;
-  MESSAGE("[" << _numproc << "] shutdown of MPI Corba Server");
-  if( _numproc == 0 ){
-    _NS->Destroy_FullDirectory(_containerName.c_str());
-    for(ip= 1;ip<_nbproc;ip++)
-      (Engines::MPIContainer::_narrow((*_tior)[ip]))->Shutdown();
-  }
-  _orb->shutdown(0);
-
-}
-
index a805639a11c82d04b9c251ac9bd784e423479c39..a73af8b38abca7da9993b83df6d704993a11ec59 100644 (file)
@@ -48,13 +48,28 @@ class Engines_MPIContainer_i : public POA_Engines::MPIContainer,
   // Destructor
   ~Engines_MPIContainer_i();
 
+  // shutdown corba server
+  void Shutdown();
+
+  // Load a component library
+  // synchronous version for process 0
+  bool load_component_Library(const char* componentName);
+  // asynchronous version for other process
+  void Asload_component_Library(const char* componentName);
+
+  // Create an instance of component
+  // synchronous version for process 0
+  Engines::Component_ptr
+  create_component_instance( const char* componentName,
+                            CORBA::Long studyId); // 0 for multiStudy
+  // asynchronous version for other process
+  void Ascreate_component_instance( const char* componentName,
+                                 CORBA::Long studyId); // 0 for multiStudy
+
   // Load a component in current MPI container
   // synchronous version for process 0
   Engines::Component_ptr load_impl(const char* nameToRegister,
                                   const char* componentName);
-  // shutdown corba server
-  void Shutdown();
-
   // asynchronous version for other process
   void Asload_impl(const char* nameToRegister, const char* componentName);
 
@@ -72,10 +87,19 @@ class Engines_MPIContainer_i : public POA_Engines::MPIContainer,
  private:
   // local version to not duplicate code 
   // called by synchronous and asynchronous version
+  bool Lload_component_Library(const char* componentName);
+  Engines::Component_ptr
+  Lcreate_component_instance( const char* componentName,
+                             CORBA::Long studyId); // 0 for multiStudy
   Engines::Component_ptr Lload_impl(const char* nameToRegister,
                                    const char* componentName);
   void Lremove_impl(Engines::Component_ptr component_i);
   void Lfinalize_removal();
 
+  Engines::Component_ptr
+  createMPIInstance(std::string genericRegisterName,
+                   void *handle,
+                   int studyId);
+
 };
 #endif
index ad437b00526b0e5df4efb1dda2fb8a212f8fd820..af110328fecc1500d119c504fd0f515b485ca993 100644 (file)
@@ -52,12 +52,13 @@ BIN = SALOME_MPIContainer
 BIN_SRC = 
 BIN_SERVER_IDL = SALOME_MPIObject.idl SALOME_MPIContainer.idl
 
+CPPFLAGS+= $(PYTHON_INCLUDES) 
 LDFLAGS+= -lSalomeContainer -lSalomeNS -lRegistry -lOpUtil -lSalomeNotification -lSALOMELocalTrace -lSalomeResourcesManager
 
 ifeq (@WITHMPI@,yes)
+  LIBS += $(PYTHON_LIBS) $(MPI_LIBS)
   CXXFLAGS+=${MPI_INCLUDES}
   CXX_DEPEND_FLAG+=${MPI_INCLUDES}
-  LDFLAGS+= ${MPI_LIBS}
   LDFLAGSFORBIN= $(LDFLAGS) -lSALOMETraceCollector
   LIBSFORBIN= $(LIBS)
 endif
index 4cae11af8429ea151b2adfcf7623869cd6144eeb..bca86ef097853505b1e3aed37e1c3ea887a7d8ea 100644 (file)
@@ -452,7 +452,7 @@ CORBA::Object_ptr SALOME_NamingService::ResolveComponent(const char* hostname, c
       name += containerName;
     name += "/";
     name += componentName;
-    return Resolve(name.c_str());
+    return ResolveFirst(name.c_str());
   }
   else {
     Change_Directory(name.c_str());
@@ -461,7 +461,7 @@ CORBA::Object_ptr SALOME_NamingService::ResolveComponent(const char* hostname, c
       name = contList[ind].c_str();
       name += "/";
       name += componentName;
-      CORBA::Object_ptr obj = Resolve(name.c_str());
+      CORBA::Object_ptr obj = ResolveFirst(name.c_str());
       if( !CORBA::is_nil(obj) )
        return obj;
     }
index 8edf5a264beacbb4a26ca634b34e3f8e4aef0a00..c3a339e1820792e5c953d945984e1ba6f39ff493 100644 (file)
@@ -220,14 +220,12 @@ string SALOME_ResourcesManager::BuildTempFileToLaunchRemoteContainer(const strin
   }
   tempOutputFile << (*(resInfo.ModulesPath.find("KERNEL"))).second << "/bin/salome/";
   if(params.isMPI){
-    cout << "PARALLELE" << endl;
     if(Engines_Container_i::isPythonContainer(params.container_name))
       tempOutputFile << "pyMPI SALOME_ContainerPy.py ";
     else
       tempOutputFile << "SALOME_MPIContainer ";
   }
   else{
-    cout << "SEQUENTIEL" << endl;
     if(Engines_Container_i::isPythonContainer(params.container_name))
       tempOutputFile << "SALOME_ContainerPy.py ";
     else