Salome HOME
Removed references to OCC
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerNodeMpi.cxx
1 //  SALOME ParallelContainerNodeMpi : Launch mpi PaCO++ object nodes
2 //
3 //  Copyright (C) 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
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 WNT
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 "Utils_ORB_INIT.hxx"
50 #include "Utils_SINGLETON.hxx"
51 #include "SALOMETraceCollector.hxx"
52 #include "OpUtil.hxx"
53
54 using namespace std;
55
56 #ifdef _DEBUG_
57 #include <signal.h>
58
59 void test(int sigval) {
60         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
62         cerr << "SIGSEGV in :" << getpid() << endl;
63         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
64         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
65         while (1) {}
66 }
67
68 void handler(int t) {
69         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
70         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
71         cerr << "SIGSEGV in :" << getpid() << endl;
72         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
73         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
74         while (1) {}
75 }
76 #endif
77
78 typedef void (*sighandler_t)(int);
79 sighandler_t setsig(int sig, sighandler_t handler)
80 {
81   struct sigaction context, ocontext;
82   context.sa_handler = handler;
83   sigemptyset(&context.sa_mask);
84   context.sa_flags = 0;
85   if (sigaction(sig, &context, &ocontext) == -1)
86     return SIG_ERR;
87   return ocontext.sa_handler;
88 }
89
90 void AttachDebugger()
91 {
92   if(getenv ("DEBUGGER"))
93     {
94       std::stringstream exec;
95       exec << "$DEBUGGER SALOME_ParallelContainerNodeMpi " << getpid() << "&";
96       std::cerr << exec.str() << std::endl;
97       system(exec.str().c_str());
98       while(1);
99     }
100 }
101
102 void Handler(int theSigId)
103 {
104   std::cerr << "SIGSEGV: "  << std::endl;
105   AttachDebugger();
106   //to exit or not to exit
107   exit(1);
108 }
109
110 void terminateHandler(void)
111 {
112   std::cerr << "Terminate: not managed exception !"  << std::endl;
113   AttachDebugger();
114 }
115
116 void unexpectedHandler(void)
117 {
118   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
119   AttachDebugger();
120 }
121
122 int main(int argc, char* argv[])
123 {
124         INFOS("Launching a parallel Mpi container node");
125
126 #ifdef _DEBUG_
127 //      struct sigaction action;
128 //      action.sa_handler = &test;
129 //      sigaction(SIGSEGV, &action, NULL);
130 #endif
131         
132         // MPI Init
133         int provided;
134         MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
135
136   if(getenv ("DEBUGGER"))
137     {
138   std::cerr << "Unexpected: unexpected exception !"  << std::endl;
139       setsig(SIGSEGV,&Handler);
140       set_terminate(&terminateHandler);
141       set_unexpected(&unexpectedHandler);
142     }
143 #ifdef _DEBUG_
144         cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << endl;
145         cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << endl;
146         cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << endl;
147         cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << endl;
148         cerr << "Level provided : " << provided << endl;
149 #endif
150         // Initialise the ORB.
151         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
152         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
153         CORBA::ORB_var orb = init(0, 0);
154         //CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
155
156         // Code pour choisir le reseau infiniband .....
157 /*      string hostname_temp = GetHostname();
158         hostent * t = gethostbyname(hostname_temp.c_str());
159         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << t->h_addr << " " << hostname_temp << endl;
160         cerr << t->h_addr << endl;
161         in_addr * address=(in_addr * ) t->h_addr;
162         cerr << inet_ntoa(* address) << endl;
163         string ip = inet_ntoa(* address);
164         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << endl;
165         string com = "giop:tcp:" + ip + ":";
166         const char* options[][2] = { { "endPoint", com.c_str() }, { 0, 0 } };
167         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
168 */
169         char * containerName = "";
170         if(argc > 1) {
171                 containerName = argv[1];
172         }
173
174         char * hostname = "";
175         if(argc > 3) {
176                 hostname = argv[3];
177         }
178
179         try {  
180                 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
181                 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
182                 PortableServer::POAManager_var pman = root_poa->the_POAManager();
183
184 #ifndef WNT
185                 // add this container to the kill list
186                 char aCommand[100];
187                 sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNodeMpi", getpid());
188                 system(aCommand);
189 #endif
190
191                 SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
192                 // On récupère le proxy 
193                 string proxyNameInNS = ns->BuildContainerNameForNS(containerName, hostname);
194                 obj = ns->Resolve(proxyNameInNS.c_str());
195                 char * proxy_ior = orb->object_to_string(obj);
196
197                 // Node creation
198                 string name(containerName);
199                 string node_name = name + "Node";
200                 Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), proxy_ior,
201                                                                                                                                                                                                                                                                                                                         root_poa,
202                                                                                                                                                                                                                                                                                                                         (char*) node_name.c_str(),
203                                                                                                                                                                                                                                                                                                                         argc, argv);
204                 // PaCO++ init
205                 paco_fabrique_manager * pfm = paco_getFabriqueManager();
206                 pfm->register_com("mpi", new paco_mpi_fabrique());
207                 pfm->register_thread("omni", new paco_omni_fabrique());
208
209                 // Global context
210                 PaCO_operation * global_ptr = servant->getContext("global_paco_context");
211                 MPI_Comm group = MPI_COMM_WORLD;
212                 global_ptr->setLibCom("mpi", &group);
213                 global_ptr->setLibThread("omni");
214
215                 // Activation
216                 PortableServer::ObjectId * _id = root_poa->activate_object(servant);
217                 servant->set_id(_id);
218                 obj = root_poa->id_to_reference(*_id);
219
220                 // In the NamingService
221                 string hostname = GetHostname();
222
223                 int myid;
224                 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
225                 char buffer [5];
226                 snprintf(buffer, 5, "%d", myid);
227                 node_name = node_name + buffer;
228                 string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
229                                                                                                                                                                                                                                 hostname.c_str());
230                 cerr << "---------" << _containerName << "----------" << endl;
231                 ns->Register(obj, _containerName.c_str());
232                 pman->activate();
233                 orb->run();
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
256         MPI_Finalize();
257
258         return 0 ;
259 }
260