]> SALOME platform Git repositories - modules/kernel.git/blob - src/ParallelContainer/SALOME_ParallelContainerNodeMpi.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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   // MPI Init
127   int provided;
128   int myid;
129   MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
130   MPI_Comm_rank(MPI_COMM_WORLD,&myid);
131
132   if(getenv ("DEBUGGER"))
133   {
134     std::cerr << "Unexpected: unexpected exception !"  << std::endl;
135     setsig(SIGSEGV,&Handler);
136     set_terminate(&terminateHandler);
137     set_unexpected(&unexpectedHandler);
138   }
139   cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << endl;
140   cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << endl;
141   cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << endl;
142   cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << endl;
143   cerr << "Level provided : " << provided << endl;
144   // Initialise the ORB.
145   CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
146
147   // Code pour choisir le reseau infiniband .....
148   /*    string hostname_temp = GetHostname();
149         hostent * t = gethostbyname(hostname_temp.c_str());
150         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << t->h_addr << " " << hostname_temp << endl;
151         cerr << t->h_addr << endl;
152         in_addr * address=(in_addr * ) t->h_addr;
153         cerr << inet_ntoa(* address) << endl;
154         string ip = inet_ntoa(* address);
155         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << endl;
156         string com = "giop:tcp:" + ip + ":";
157         const char* options[][2] = { { "endPoint", com.c_str() }, { 0, 0 } };
158         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
159         */
160   std::string containerName("");
161   containerName = argv[1];
162
163   std::string hostname("");
164   if(argc > 3) {
165     hostname = argv[3];
166   }
167
168   try {  
169     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
170     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
171     PortableServer::POAManager_var pman = root_poa->the_POAManager();
172
173 #ifndef WNT
174     // add this container to the kill list
175     char aCommand[100];
176     sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNodeMpi", getpid());
177     system(aCommand);
178 #endif
179
180     SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
181     // On récupère le proxy 
182     string proxyNameInNS = ns->BuildContainerNameForNS(containerName.c_str(), 
183                                                        hostname.c_str());
184     obj = ns->Resolve(proxyNameInNS.c_str());
185     char * proxy_ior = orb->object_to_string(obj);
186
187     // Node creation
188     string node_name = containerName + "Node";
189     Engines_Parallel_Container_i * servant = 
190       new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), 
191                                        proxy_ior,
192                                        myid,
193                                        root_poa,
194                                        (char*) node_name.c_str(),
195                                        argc, argv);
196     // PaCO++ init
197     paco_fabrique_manager * pfm = paco_getFabriqueManager();
198     pfm->register_com("mpi", new paco_mpi_fabrique());
199     pfm->register_thread("omni", new paco_omni_fabrique());
200     MPI_Comm group = MPI_COMM_WORLD;
201     servant->setLibCom("mpi", &group);
202     servant->setLibThread("omni");
203
204     // Activation
205     PortableServer::ObjectId * _id = root_poa->activate_object(servant);
206     servant->set_id(_id);
207     obj = root_poa->id_to_reference(*_id);
208
209     // In the NamingService
210     string hostname = GetHostname();
211
212     int myid;
213     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
214     char buffer [5];
215     snprintf(buffer, 5, "%d", myid);
216     node_name = node_name + buffer;
217     string _containerName = ns->BuildContainerNameForNS(node_name.c_str(),
218                                                         hostname.c_str());
219     cerr << "---------" << _containerName << "----------" << endl;
220     ns->Register(obj, _containerName.c_str());
221     pman->activate();
222     orb->run();
223   }
224   catch (PaCO::PACO_Exception& e)
225   {
226     INFOS("Caught PaCO::PACO_Exception");
227     std::cerr << e << std::endl;
228   }
229   catch(CORBA::SystemException&)
230   {
231     INFOS("Caught CORBA::SystemException.");
232   }
233   catch(PortableServer::POA::ServantAlreadyActive&)
234   {
235     INFOS("Caught CORBA::ServantAlreadyActiveException");
236   }
237   catch(CORBA::Exception&)
238   {
239     INFOS("Caught CORBA::Exception.");
240   }
241   catch(std::exception& exc)
242   {
243     INFOS("Caught std::exception - "<<exc.what()); 
244   }
245   catch(...)
246   {
247     INFOS("Caught unknown exception.");
248   }
249
250   MPI_Finalize();
251
252   return 0 ;
253 }
254