Salome HOME
ded09fa940d7fbe42c6f40f3708cf393aedbab8d
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerNodeMpi.cxx
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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
23 //  SALOME ParallelContainerNodeMpi : Launch mpi PaCO++ object nodes
24 //  File   : SALOME_ParallelContainerNodeMpi.cxx
25 //  Author : André Ribes, EDF
26 //  Module : SALOME PARALLEL
27 //
28 #include <iostream>
29 #include <string>
30 #include <stdio.h>
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #else
35 #include <process.h>
36 #endif
37
38 #include "SALOME_ParallelContainer_i.hxx"
39
40 // PaCO++ include
41 #include <paco_omni.h>
42 #include <paco_mpi.h>
43
44 #include <mpi.h>
45
46 #include "SALOME_NamingService.hxx"
47
48 #include "utilities.h"
49 #include "Basics_Utils.hxx"
50 #include "Utils_ORB_INIT.hxx"
51 #include "Utils_SINGLETON.hxx"
52 #include "SALOMETraceCollector.hxx"
53 #include "OpUtil.hxx"
54
55 #include "Container_init_python.hxx"
56
57
58 #ifdef _DEBUG_
59 #include <signal.h>
60
61 void handler(int t) {
62   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
63   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
64   std::cerr << "SIGSEGV in :" << getpid() << std::endl;
65   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
66   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
67   while (1) {}
68 }
69 #endif
70
71 typedef void (*sighandler_t)(int);
72 sighandler_t setsig(int sig, sighandler_t handler)
73 {
74   struct sigaction context, ocontext;
75   context.sa_handler = handler;
76   sigemptyset(&context.sa_mask);
77   context.sa_flags = 0;
78   if (sigaction(sig, &context, &ocontext) == -1)
79     return SIG_ERR;
80   return ocontext.sa_handler;
81 }
82
83 void AttachDebugger()
84 {
85   if(getenv ("DEBUGGER"))
86   {
87     std::stringstream exec;
88     exec << "$DEBUGGER SALOME_ParallelContainerNodeMpi " << getpid() << "&";
89     std::cerr << exec.str() << std::endl;
90     system(exec.str().c_str());
91     while(1);
92   }
93 }
94
95 void Handler(int theSigId)
96 {
97   std::cerr << "SIGSEGV: "  << std::endl;
98   AttachDebugger();
99   //to exit or not to exit
100   exit(1);
101 }
102
103 void terminateHandler(void)
104 {
105   std::cerr << "Terminate: not managed exception !"  << std::endl;
106   AttachDebugger();
107 }
108
109 void unexpectedHandler(void)
110 {
111   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
112   AttachDebugger();
113 }
114
115 int main(int argc, char* argv[])
116 {
117   INFOS("Launching a parallel Mpi container node");
118
119   // MPI Init
120   int provided;
121   int myid;
122   MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
123   MPI_Comm_rank(MPI_COMM_WORLD,&myid);
124
125 #ifdef _DEBUG_
126   if(getenv ("DEBUGGER"))
127   {
128     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
129     setsig(SIGSEGV,&Handler);
130     set_terminate(&terminateHandler);
131     //set_terminate(__gnu_cxx::__verbose_terminate_handler);
132     set_unexpected(&unexpectedHandler);
133   }
134 #endif
135
136   std::cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << std::endl;
137   std::cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << std::endl;
138   std::cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << std::endl;
139   std::cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << std::endl;
140   std::cerr << "Level provided : " << provided << std::endl;
141   // Initialise the ORB.
142   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
143   KERNEL_PYTHON::init_python(argc,argv);
144
145   // Code pour choisir le reseau infiniband .....
146   /*    std::string hostname_temp = GetHostname();
147         hostent * t = gethostbyname(hostname_temp.c_str());
148         std::cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << t->h_addr << " " << hostname_temp << std::endl;
149         std::cerr << t->h_addr << std::endl;
150         in_addr * address=(in_addr * ) t->h_addr;
151         std::cerr << inet_ntoa(* address) << std::endl;
152         std::string ip = inet_ntoa(* address);
153         std::cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << std::endl;
154         std::string com = "giop:tcp:" + ip + ":";
155         const char* options[][2] = { { "endPoint", com.c_str() }, { 0, 0 } };
156         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
157         */
158   std::string containerName("");
159   containerName = argv[1];
160
161   std::string proxy_hostname("");
162   if(argc > 3) {
163     proxy_hostname = argv[3];
164   }
165
166   try {  
167     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
168     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
169     PortableServer::POAManager_var pman = root_poa->the_POAManager();
170
171 #ifndef WIN32
172     // add this container to the kill list
173     char aCommand[100];
174     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNodeMpi", getpid());
175     system(aCommand);
176 #endif
177
178     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
179     // On récupère le proxy 
180     std::string proxyNameInNS = ns->BuildContainerNameForNS(containerName.c_str(), 
181                                                        proxy_hostname.c_str());
182     obj = ns->Resolve(proxyNameInNS.c_str());
183     char * proxy_ior = orb->object_to_string(obj);
184
185     // Node creation
186     std::string node_name = containerName + "Node";
187     Engines_Parallel_Container_i * servant =  new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), 
188                                                                                proxy_ior,
189                                                                                myid,
190                                                                                root_poa,
191                                                                                node_name);
192     // PaCO++ init
193     paco_fabrique_manager * pfm = paco_getFabriqueManager();
194     pfm->register_com("mpi", new paco_mpi_fabrique());
195     pfm->register_thread("omni", new paco_omni_fabrique());
196     servant->setLibCom("mpi", MPI_COMM_WORLD);
197     servant->setLibThread("omni");
198
199     // Activation
200     obj = servant->_this();
201
202     // In the NamingService
203     std::string hostname = Kernel_Utils::GetHostname();
204
205     int myid;
206     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
207     char buffer [5];
208     snprintf(buffer, 5, "%d", myid);
209     node_name = node_name + buffer;
210
211     // We register nodes in two different parts
212     // In the real machine name and in the proxy machine
213     std::string _containerName = ns->BuildContainerNameForNS(node_name.c_str(),
214                                                         hostname.c_str());
215     std::string _proxymachine_containerName = ns->BuildContainerNameForNS(node_name.c_str(),
216                                                                      proxy_hostname.c_str());
217     std::cerr << "Register container node : " << _containerName << std::endl;
218     std::cerr << "Register container node : " << _proxymachine_containerName << std::endl;
219     ns->Register(obj, _containerName.c_str());
220     ns->Register(obj, _proxymachine_containerName.c_str());
221     pman->activate();
222     orb->run();
223     PyGILState_Ensure();
224     //Delete python container that destroy orb from python (pyCont._orb.destroy())
225     Py_Finalize();
226     MPI_Finalize();
227     CORBA::string_free(proxy_ior);
228     delete ns;
229   }
230   catch (PaCO::PACO_Exception& e)
231   {
232     INFOS("Caught PaCO::PACO_Exception");
233     std::cerr << e << std::endl;
234   }
235   catch(CORBA::SystemException&)
236   {
237     INFOS("Caught CORBA::SystemException.");
238   }
239   catch(PortableServer::POA::ServantAlreadyActive&)
240   {
241     INFOS("Caught CORBA::ServantAlreadyActiveException");
242   }
243   catch(CORBA::Exception&)
244   {
245     INFOS("Caught CORBA::Exception.");
246   }
247   catch(std::exception& exc)
248   {
249     INFOS("Caught std::exception - "<<exc.what()); 
250   }
251   catch(...)
252   {
253     INFOS("Caught unknown exception.");
254   }
255   return 0 ;
256 }
257