]> SALOME platform Git repositories - modules/kernel.git/blob - src/ParallelContainer/SALOME_ParallelContainerNodeMpi.cxx
Salome HOME
[EDF29150] : Make the trace messages clearer and more efficient.
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerNodeMpi.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 "ArgvKeeper.hxx"
50 #include "Basics_Utils.hxx"
51 #include "SALOMETraceCollector.hxx"
52 #include "OpUtil.hxx"
53
54 #include "Container_init_python.hxx"
55
56
57 #ifdef _DEBUG_
58 #include <signal.h>
59
60 void handler(int t) {
61   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
62   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
63   std::cerr << "SIGSEGV in :" << getpid() << std::endl;
64   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
65   std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
66   while (1) {}
67 }
68 #endif
69
70 typedef void (*sighandler_t)(int);
71 sighandler_t setsig(int sig, sighandler_t handler)
72 {
73   struct sigaction context, ocontext;
74   context.sa_handler = handler;
75   sigemptyset(&context.sa_mask);
76   context.sa_flags = 0;
77   if (sigaction(sig, &context, &ocontext) == -1)
78     return SIG_ERR;
79   return ocontext.sa_handler;
80 }
81
82 void AttachDebugger()
83 {
84   if(getenv ("DEBUGGER"))
85   {
86     std::stringstream exec;
87     exec << "$DEBUGGER SALOME_ParallelContainerNodeMpi " << getpid() << "&";
88     std::cerr << exec.str() << std::endl;
89     system(exec.str().c_str());
90     while(1);
91   }
92 }
93
94 void Handler(int theSigId)
95 {
96   std::cerr << "SIGSEGV: "  << std::endl;
97   AttachDebugger();
98   //to exit or not to exit
99   exit(1);
100 }
101
102 void terminateHandler(void)
103 {
104   std::cerr << "Terminate: not managed exception !"  << std::endl;
105   AttachDebugger();
106 }
107
108 void unexpectedHandler(void)
109 {
110   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
111   AttachDebugger();
112 }
113
114 int main(int argc, char* argv[])
115 {
116   INFOS("Launching a parallel Mpi container node");
117
118   // MPI Init
119   int provided;
120   int myid;
121   MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
122   MPI_Comm_rank(MPI_COMM_WORLD,&myid);
123
124 #ifdef _DEBUG_
125   if(getenv ("DEBUGGER"))
126   {
127     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
128     setsig(SIGSEGV,&Handler);
129     set_terminate(&terminateHandler);
130     //set_terminate(__gnu_cxx::__verbose_terminate_handler);
131     set_unexpected(&unexpectedHandler);
132   }
133 #endif
134
135   std::cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << std::endl;
136   std::cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << std::endl;
137   std::cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << std::endl;
138   std::cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << std::endl;
139   std::cerr << "Level provided : " << provided << std::endl;
140   // Initialise the ORB.
141   SetArgcArgv(argc, argv);
142   CORBA::ORB_var orb = KERNEL::GetRefToORB();
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