Back to index

salome-kernel  6.5.0
SALOME_ParallelContainerProxyMpi.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME ParallelContainerProxyMpi : Launching the proxy of a MPI PaCO++ object
00024 //  File   : SALOME_ParallelContainerProxyMpi.cxx
00025 //  Author : André Ribes, EDF
00026 //  Module : SALOME PARALLEL
00027 //
00028 #include <iostream>
00029 #include <string>
00030 #include <stdio.h>
00031 
00032 #ifndef WIN32
00033 #include <unistd.h>
00034 #else
00035 #include <process.h>
00036 #endif
00037 
00038 //#include "SALOME_ComponentPaCO_Engines_Container_server.h"
00039 #include "SALOME_ParallelContainerProxy_i.hxx"
00040 #include <paco_omni.h>
00041 #include <paco_dummy.h>
00042 
00043 #include <mpi.h>
00044 
00045 #include "SALOME_NamingService.hxx"
00046 
00047 #include "utilities.h"
00048 #include "Basics_Utils.hxx"
00049 #include "Utils_ORB_INIT.hxx"
00050 #include "Utils_SINGLETON.hxx"
00051 #include "SALOMETraceCollector.hxx"
00052 #include "OpUtil.hxx"
00053 
00054 #include "Container_init_python.hxx"
00055 
00056 #ifdef _DEBUG_
00057 #include <signal.h>
00058 
00059 
00060 typedef void (*sighandler_t)(int);
00061 sighandler_t setsig(int sig, sighandler_t handler)
00062 {
00063   struct sigaction context, ocontext;
00064   context.sa_handler = handler;
00065   sigemptyset(&context.sa_mask);
00066   context.sa_flags = 0;
00067   if (sigaction(sig, &context, &ocontext) == -1)
00068     return SIG_ERR;
00069   return ocontext.sa_handler;
00070 }
00071 
00072 void AttachDebugger()
00073 {
00074   if(getenv ("DEBUGGER"))
00075   {
00076     std::stringstream exec;
00077     exec << "$DEBUGGER SALOME_ParallelContainerProxyMpi " << getpid() << "&";
00078     std::cerr << exec.str() << std::endl;
00079     system(exec.str().c_str());
00080     while(1);
00081   }
00082 }
00083 
00084 void Handler(int theSigId)
00085 {
00086   std::cerr << "SIGSEGV: "  << std::endl;
00087   AttachDebugger();
00088   //to exit or not to exit
00089   exit(1);
00090 }
00091 
00092 void terminateHandler(void)
00093 {
00094   std::cerr << "Terminate: not managed exception !"  << std::endl;
00095   AttachDebugger();
00096 }
00097 
00098 void unexpectedHandler(void)
00099 {
00100   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
00101   AttachDebugger();
00102 }
00103 
00104 void handler(int t) {
00105   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00106   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00107   std::cerr << "SIGSEGV in :" << getpid() << std::endl;
00108   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00109   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00110   while (1) {}
00111 }
00112 #endif
00113 
00114 int main(int argc, char* argv[])
00115 {
00116   INFOS("Launching a parallel Mpi proxy container");
00117 
00118 #ifdef _DEBUG_
00119   signal(SIGSEGV, handler);
00120 #endif
00121 
00122   // MPI Init
00123   int provided;
00124   MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED ,&provided);
00125   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
00126   KERNEL_PYTHON::init_python(argc,argv);
00127 
00128 #ifdef _DEBUG_
00129   if(getenv ("DEBUGGER"))
00130   {
00131     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
00132     setsig(SIGSEGV,&Handler);
00133     //set_terminate(&terminateHandler);
00134     set_terminate(__gnu_cxx::__verbose_terminate_handler);
00135     set_unexpected(&unexpectedHandler);
00136   }
00137 #endif
00138 
00139   std::string containerName("");
00140   containerName = argv[1];
00141   int nb_nodes;
00142   sscanf(argv[2],"%d",&nb_nodes);
00143 
00144   try {  
00145     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00146     ASSERT(!CORBA::is_nil(obj));
00147     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
00148     PortableServer::POAManager_var pman = root_poa->the_POAManager();
00149 
00150 #ifndef WIN32
00151     // add this container to the kill list
00152     char aCommand[100];
00153     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerProxyMpi", getpid());
00154     system(aCommand);
00155 #endif
00156 
00157     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
00158 
00159     // PaCO++ code
00160     paco_fabrique_manager* pfm = paco_getFabriqueManager();
00161     pfm->register_com("dummy", new paco_dummy_fabrique());
00162     pfm->register_thread("omnithread", new paco_omni_fabrique());
00163 
00164     Container_proxy_impl_final * proxy =   new Container_proxy_impl_final(orb,
00165                                                                           pfm->get_thread("omnithread"),
00166                                                                           root_poa,
00167                                                                           containerName);
00168 
00169     // PaCO++ code
00170     proxy->setLibCom("dummy", proxy);
00171     proxy->setLibThread("omnithread");
00172     PaCO::PacoTopology_t serveur_topo;
00173     serveur_topo.total = nb_nodes;
00174     proxy->setTopology(serveur_topo);
00175 
00176     // Activation
00177     //PortableServer::ObjectId_var _id = root_poa->activate_object(proxy);
00178     //obj = root_poa->id_to_reference(_id);
00179     obj = proxy->_this();
00180 
00181     // in the NamingService
00182     std::string hostname = Kernel_Utils::GetHostname();
00183     Engines::Container_var pCont = Engines::Container::_narrow(obj);
00184     std::string _containerName = ns->BuildContainerNameForNS(containerName.c_str(),
00185                                                         hostname.c_str());
00186     std::cerr << "---------" << _containerName << "----------" << std::endl;
00187     ns->Register(pCont, _containerName.c_str());
00188     pman->activate();
00189     orb->run();
00190     PyGILState_Ensure();
00191     //Delete python container that destroy orb from python (pyCont._orb.destroy())
00192     Py_Finalize();
00193     MPI_Finalize();
00194     delete ns;
00195   }
00196   catch (PaCO::PACO_Exception& e)
00197   {
00198     INFOS("Caught PaCO::PACO_Exception");
00199     std::cerr << e << std::endl;
00200   }
00201   catch(CORBA::SystemException&)
00202   {
00203     INFOS("Caught CORBA::SystemException.");
00204   }
00205   catch(PortableServer::POA::ServantAlreadyActive&)
00206   {
00207     INFOS("Caught CORBA::ServantAlreadyActiveException");
00208   }
00209   catch(CORBA::Exception&)
00210   {
00211     INFOS("Caught CORBA::Exception.");
00212   }
00213   catch(std::exception& exc)
00214   {
00215     INFOS("Caught std::exception - "<<exc.what()); 
00216   }
00217   catch(...)
00218   {
00219     INFOS("Caught unknown exception.");
00220   }
00221   return 0 ;
00222 }
00223