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