Back to index

salome-kernel  6.5.0
SALOME_ParallelContainerNodeMpi.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 ParallelContainerNodeMpi : Launch mpi PaCO++ object nodes
00024 //  File   : SALOME_ParallelContainerNodeMpi.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_ParallelContainer_i.hxx"
00039 
00040 // PaCO++ include
00041 #include <paco_omni.h>
00042 #include <paco_mpi.h>
00043 
00044 #include <mpi.h>
00045 
00046 #include "SALOME_NamingService.hxx"
00047 
00048 #include "utilities.h"
00049 #include "Basics_Utils.hxx"
00050 #include "Utils_ORB_INIT.hxx"
00051 #include "Utils_SINGLETON.hxx"
00052 #include "SALOMETraceCollector.hxx"
00053 #include "OpUtil.hxx"
00054 
00055 #include "Container_init_python.hxx"
00056 
00057 
00058 #ifdef _DEBUG_
00059 #include <signal.h>
00060 
00061 void handler(int t) {
00062   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00063   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00064   std::cerr << "SIGSEGV in :" << getpid() << std::endl;
00065   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00066   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
00067   while (1) {}
00068 }
00069 #endif
00070 
00071 typedef void (*sighandler_t)(int);
00072 sighandler_t setsig(int sig, sighandler_t handler)
00073 {
00074   struct sigaction context, ocontext;
00075   context.sa_handler = handler;
00076   sigemptyset(&context.sa_mask);
00077   context.sa_flags = 0;
00078   if (sigaction(sig, &context, &ocontext) == -1)
00079     return SIG_ERR;
00080   return ocontext.sa_handler;
00081 }
00082 
00083 void AttachDebugger()
00084 {
00085   if(getenv ("DEBUGGER"))
00086   {
00087     std::stringstream exec;
00088     exec << "$DEBUGGER SALOME_ParallelContainerNodeMpi " << getpid() << "&";
00089     std::cerr << exec.str() << std::endl;
00090     system(exec.str().c_str());
00091     while(1);
00092   }
00093 }
00094 
00095 void Handler(int theSigId)
00096 {
00097   std::cerr << "SIGSEGV: "  << std::endl;
00098   AttachDebugger();
00099   //to exit or not to exit
00100   exit(1);
00101 }
00102 
00103 void terminateHandler(void)
00104 {
00105   std::cerr << "Terminate: not managed exception !"  << std::endl;
00106   AttachDebugger();
00107 }
00108 
00109 void unexpectedHandler(void)
00110 {
00111   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
00112   AttachDebugger();
00113 }
00114 
00115 int main(int argc, char* argv[])
00116 {
00117   INFOS("Launching a parallel Mpi container node");
00118 
00119   // MPI Init
00120   int provided;
00121   int myid;
00122   MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
00123   MPI_Comm_rank(MPI_COMM_WORLD,&myid);
00124 
00125 #ifdef _DEBUG_
00126   if(getenv ("DEBUGGER"))
00127   {
00128     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
00129     setsig(SIGSEGV,&Handler);
00130     set_terminate(&terminateHandler);
00131     //set_terminate(__gnu_cxx::__verbose_terminate_handler);
00132     set_unexpected(&unexpectedHandler);
00133   }
00134 #endif
00135 
00136   std::cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << std::endl;
00137   std::cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << std::endl;
00138   std::cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << std::endl;
00139   std::cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << std::endl;
00140   std::cerr << "Level provided : " << provided << std::endl;
00141   // Initialise the ORB.
00142   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
00143   KERNEL_PYTHON::init_python(argc,argv);
00144 
00145   // Code pour choisir le reseau infiniband .....
00146   /*    std::string hostname_temp = GetHostname();
00147         hostent * t = gethostbyname(hostname_temp.c_str());
00148         std::cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << t->h_addr << " " << hostname_temp << std::endl;
00149         std::cerr << t->h_addr << std::endl;
00150         in_addr * address=(in_addr * ) t->h_addr;
00151         std::cerr << inet_ntoa(* address) << std::endl;
00152         std::string ip = inet_ntoa(* address);
00153         std::cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << std::endl;
00154         std::string com = "giop:tcp:" + ip + ":";
00155         const char* options[][2] = { { "endPoint", com.c_str() }, { 0, 0 } };
00156         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
00157         */
00158   std::string containerName("");
00159   containerName = argv[1];
00160 
00161   std::string proxy_hostname("");
00162   if(argc > 3) {
00163     proxy_hostname = argv[3];
00164   }
00165 
00166   try {  
00167     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
00168     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
00169     PortableServer::POAManager_var pman = root_poa->the_POAManager();
00170 
00171 #ifndef WIN32
00172     // add this container to the kill list
00173     char aCommand[100];
00174     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNodeMpi", getpid());
00175     system(aCommand);
00176 #endif
00177 
00178     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
00179     // On récupère le proxy 
00180     std::string proxyNameInNS = ns->BuildContainerNameForNS(containerName.c_str(), 
00181                                                        proxy_hostname.c_str());
00182     obj = ns->Resolve(proxyNameInNS.c_str());
00183     char * proxy_ior = orb->object_to_string(obj);
00184 
00185     // Node creation
00186     std::string node_name = containerName + "Node";
00187     Engines_Parallel_Container_i * servant =  new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), 
00188                                                                                proxy_ior,
00189                                                                                myid,
00190                                                                                root_poa,
00191                                                                                node_name);
00192     // PaCO++ init
00193     paco_fabrique_manager * pfm = paco_getFabriqueManager();
00194     pfm->register_com("mpi", new paco_mpi_fabrique());
00195     pfm->register_thread("omni", new paco_omni_fabrique());
00196     servant->setLibCom("mpi", MPI_COMM_WORLD);
00197     servant->setLibThread("omni");
00198 
00199     // Activation
00200     obj = servant->_this();
00201 
00202     // In the NamingService
00203     std::string hostname = Kernel_Utils::GetHostname();
00204 
00205     int myid;
00206     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
00207     char buffer [5];
00208     snprintf(buffer, 5, "%d", myid);
00209     node_name = node_name + buffer;
00210 
00211     // We register nodes in two different parts
00212     // In the real machine name and in the proxy machine
00213     std::string _containerName = ns->BuildContainerNameForNS(node_name.c_str(),
00214                                                         hostname.c_str());
00215     std::string _proxymachine_containerName = ns->BuildContainerNameForNS(node_name.c_str(),
00216                                                                      proxy_hostname.c_str());
00217     std::cerr << "Register container node : " << _containerName << std::endl;
00218     std::cerr << "Register container node : " << _proxymachine_containerName << std::endl;
00219     ns->Register(obj, _containerName.c_str());
00220     ns->Register(obj, _proxymachine_containerName.c_str());
00221     pman->activate();
00222     orb->run();
00223     PyGILState_Ensure();
00224     //Delete python container that destroy orb from python (pyCont._orb.destroy())
00225     Py_Finalize();
00226     MPI_Finalize();
00227     CORBA::string_free(proxy_ior);
00228     delete ns;
00229   }
00230   catch (PaCO::PACO_Exception& e)
00231   {
00232     INFOS("Caught PaCO::PACO_Exception");
00233     std::cerr << e << std::endl;
00234   }
00235   catch(CORBA::SystemException&)
00236   {
00237     INFOS("Caught CORBA::SystemException.");
00238   }
00239   catch(PortableServer::POA::ServantAlreadyActive&)
00240   {
00241     INFOS("Caught CORBA::ServantAlreadyActiveException");
00242   }
00243   catch(CORBA::Exception&)
00244   {
00245     INFOS("Caught CORBA::Exception.");
00246   }
00247   catch(std::exception& exc)
00248   {
00249     INFOS("Caught std::exception - "<<exc.what()); 
00250   }
00251   catch(...)
00252   {
00253     INFOS("Caught unknown exception.");
00254   }
00255   return 0 ;
00256 }
00257