Salome HOME
Issue 0020194: EDF 977 ALL: Get rid of warnings PACKAGE_VERSION already defined
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerProxyMpi.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME ParallelContainerProxyMpi : Launching the proxy of a MPI PaCO++ object
23 //  File   : SALOME_ParallelContainerProxyMpi.cxx
24 //  Author : AndrĂ© Ribes, EDF
25 //  Module : SALOME PARALLEL
26 //
27 #include <iostream>
28 #include <string>
29 #include <stdio.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #else
34 #include <process.h>
35 #endif
36
37 //#include "SALOME_ComponentPaCO_Engines_Container_server.h"
38 #include "SALOME_ParallelContainerProxy_i.hxx"
39 #include <paco_omni.h>
40 #include <paco_dummy.h>
41
42 #include <mpi.h>
43
44 #include "SALOME_NamingService.hxx"
45
46 #include "utilities.h"
47 #include "Basics_Utils.hxx"
48 #include "Utils_ORB_INIT.hxx"
49 #include "Utils_SINGLETON.hxx"
50 #include "SALOMETraceCollector.hxx"
51 #include "OpUtil.hxx"
52
53 #include "Container_init_python.hxx"
54
55 #ifdef _DEBUG_
56 #include <signal.h>
57 using namespace std;
58
59 typedef void (*sighandler_t)(int);
60 sighandler_t setsig(int sig, sighandler_t handler)
61 {
62   struct sigaction context, ocontext;
63   context.sa_handler = handler;
64   sigemptyset(&context.sa_mask);
65   context.sa_flags = 0;
66   if (sigaction(sig, &context, &ocontext) == -1)
67     return SIG_ERR;
68   return ocontext.sa_handler;
69 }
70
71 void AttachDebugger()
72 {
73   if(getenv ("DEBUGGER"))
74   {
75     std::stringstream exec;
76     exec << "$DEBUGGER SALOME_ParallelContainerProxyMpi " << getpid() << "&";
77     std::cerr << exec.str() << std::endl;
78     system(exec.str().c_str());
79     while(1);
80   }
81 }
82
83 void Handler(int theSigId)
84 {
85   std::cerr << "SIGSEGV: "  << std::endl;
86   AttachDebugger();
87   //to exit or not to exit
88   exit(1);
89 }
90
91 void terminateHandler(void)
92 {
93   std::cerr << "Terminate: not managed exception !"  << std::endl;
94   AttachDebugger();
95 }
96
97 void unexpectedHandler(void)
98 {
99   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
100   AttachDebugger();
101 }
102
103 void handler(int t) {
104   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
105   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
106   cerr << "SIGSEGV in :" << getpid() << endl;
107   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
108   cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
109   while (1) {}
110 }
111 #endif
112
113 int main(int argc, char* argv[])
114 {
115   INFOS("Launching a parallel Mpi proxy container");
116
117 #ifdef _DEBUG_
118   signal(SIGSEGV, handler);
119 #endif
120
121   // MPI Init
122   int provided;
123   MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED ,&provided);
124   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
125   KERNEL_PYTHON::init_python(argc,argv);
126
127 #ifdef _DEBUG_
128   if(getenv ("DEBUGGER"))
129   {
130     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
131     setsig(SIGSEGV,&Handler);
132     //set_terminate(&terminateHandler);
133     set_terminate(__gnu_cxx::__verbose_terminate_handler);
134     set_unexpected(&unexpectedHandler);
135   }
136 #endif
137
138   std::string containerName("");
139   containerName = argv[1];
140   int nb_nodes;
141   sscanf(argv[2],"%d",&nb_nodes);
142
143   try {  
144     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
145     ASSERT(!CORBA::is_nil(obj));
146     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
147     PortableServer::POAManager_var pman = root_poa->the_POAManager();
148
149 #ifndef WIN32
150     // add this container to the kill list
151     char aCommand[100];
152     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerProxyMpi", getpid());
153     system(aCommand);
154 #endif
155
156     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
157
158     // PaCO++ code
159     paco_fabrique_manager* pfm = paco_getFabriqueManager();
160     pfm->register_com("dummy", new paco_dummy_fabrique());
161     pfm->register_thread("omnithread", new paco_omni_fabrique());
162
163     Container_proxy_impl_final * proxy =   new Container_proxy_impl_final(orb,
164                                                                           pfm->get_thread("omnithread"),
165                                                                           root_poa,
166                                                                           containerName);
167
168     // PaCO++ code
169     proxy->setLibCom("dummy", proxy);
170     proxy->setLibThread("omnithread");
171     PaCO::PacoTopology_t serveur_topo;
172     serveur_topo.total = nb_nodes;
173     proxy->setTopology(serveur_topo);
174
175     // Activation
176     //PortableServer::ObjectId_var _id = root_poa->activate_object(proxy);
177     //obj = root_poa->id_to_reference(_id);
178     obj = proxy->_this();
179
180     // in the NamingService
181     string hostname = Kernel_Utils::GetHostname();
182     Engines::Container_var pCont = Engines::Container::_narrow(obj);
183     string _containerName = ns->BuildContainerNameForNS(containerName.c_str(),
184                                                         hostname.c_str());
185     cerr << "---------" << _containerName << "----------" << endl;
186     ns->Register(pCont, _containerName.c_str());
187     pman->activate();
188     orb->run();
189     PyGILState_Ensure();
190     //Delete python container that destroy orb from python (pyCont._orb.destroy())
191     Py_Finalize();
192     MPI_Finalize();
193     delete ns;
194   }
195   catch (PaCO::PACO_Exception& e)
196   {
197     INFOS("Caught PaCO::PACO_Exception");
198     std::cerr << e << std::endl;
199   }
200   catch(CORBA::SystemException&)
201   {
202     INFOS("Caught CORBA::SystemException.");
203   }
204   catch(PortableServer::POA::ServantAlreadyActive&)
205   {
206     INFOS("Caught CORBA::ServantAlreadyActiveException");
207   }
208   catch(CORBA::Exception&)
209   {
210     INFOS("Caught CORBA::Exception.");
211   }
212   catch(std::exception& exc)
213   {
214     INFOS("Caught std::exception - "<<exc.what()); 
215   }
216   catch(...)
217   {
218     INFOS("Caught unknown exception.");
219   }
220   return 0 ;
221 }
222