Salome HOME
merge from branch BR_For40_DSC tag mergeto_BR_Dev_For_4_0_16apr07
[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_PARALLEL
57 #include <signal.h>
58
59 void handler(int t) {
60         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
62         cerr << "SIGSEGV in :" << getpid() << endl;
63         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
64         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
65         while (1) {}
66 }
67 #endif
68
69 int main(int argc, char* argv[])
70 {
71         INFOS("Launching a parallel Mpi container node");
72
73 #ifdef DEBUG_PARALLEL
74         signal(SIGSEGV, handler);
75 #endif
76         
77         // MPI Init
78         int provided;
79         MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE ,&provided);
80
81 #ifdef DEBUG_PARALLEL
82         cerr << "Level MPI_THREAD_SINGLE : " << MPI_THREAD_SINGLE << endl;
83         cerr << "Level MPI_THREAD_SERIALIZED : " << MPI_THREAD_SERIALIZED << endl;
84         cerr << "Level MPI_THREAD_FUNNELED : " << MPI_THREAD_FUNNELED << endl;
85         cerr << "Level MPI_THREAD_MULTIPLE : " << MPI_THREAD_MULTIPLE << endl;
86         cerr << "Level provided : " << provided << endl;
87 #endif
88         // Initialise the ORB.
89         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
90         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
91         CORBA::ORB_var orb = init(0, 0);
92         //CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
93
94         // Code pour choisir le reseau infiniband .....
95 /*      string hostname_temp = GetHostname();
96         hostent * t = gethostbyname(hostname_temp.c_str());
97         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << t->h_addr << " " << hostname_temp << endl;
98         cerr << t->h_addr << endl;
99         in_addr * address=(in_addr * ) t->h_addr;
100         cerr << inet_ntoa(* address) << endl;
101         string ip = inet_ntoa(* address);
102         cerr << " AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA " << endl;
103         string com = "giop:tcp:" + ip + ":";
104         const char* options[][2] = { { "endPoint", com.c_str() }, { 0, 0 } };
105         CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB4", options);
106 */
107         char * containerName = "";
108         if(argc > 1) {
109                 containerName = argv[1];
110         }
111
112         char * hostname = "";
113         if(argc > 3) {
114                 hostname = argv[3];
115         }
116
117         try {  
118                 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
119                 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
120                 PortableServer::POAManager_var pman = root_poa->the_POAManager();
121
122 #ifndef WNT
123                 // add this container to the kill list
124                 char aCommand[100];
125                 sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerNodeMpi", getpid());
126                 system(aCommand);
127 #endif
128
129                 SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
130                 // On récupère le proxy 
131                 string proxyNameInNS = ns->BuildContainerNameForNS(containerName, hostname);
132                 obj = ns->Resolve(proxyNameInNS.c_str());
133                 char * proxy_ior = orb->object_to_string(obj);
134
135                 // Node creation
136                 string name(containerName);
137                 string node_name = name + "Node";
138                 Engines_Parallel_Container_i * servant = new Engines_Parallel_Container_i(CORBA::ORB::_duplicate(orb), proxy_ior,
139                                                                                                                                                                                                                                                                                                                         root_poa,
140                                                                                                                                                                                                                                                                                                                         (char*) node_name.c_str(),
141                                                                                                                                                                                                                                                                                                                         argc, argv);
142                 // PaCO++ init
143                 paco_fabrique_manager * pfm = paco_getFabriqueManager();
144                 pfm->register_com("mpi", new paco_mpi_fabrique());
145                 pfm->register_thread("omni", new paco_omni_fabrique());
146
147                 // Global context
148                 PaCO_operation * global_ptr = servant->getContext("global_paco_context");
149                 MPI_Comm group = MPI_COMM_WORLD;
150                 global_ptr->setLibCom("mpi", &group);
151                 global_ptr->setLibThread("omni");
152
153                 // Activation
154                 PortableServer::ObjectId * _id = root_poa->activate_object(servant);
155                 servant->set_id(_id);
156                 obj = root_poa->id_to_reference(*_id);
157
158                 // In the NamingService
159                 string hostname = GetHostname();
160
161                 int myid;
162                 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
163                 char buffer [5];
164                 snprintf(buffer, 5, "%d", myid);
165                 node_name = node_name + buffer;
166                 string _containerName = ns->BuildContainerNameForNS((char*) node_name.c_str(),
167                                                                                                                                                                                                                                 hostname.c_str());
168                 cerr << "---------" << _containerName << "----------" << endl;
169                 ns->Register(obj, _containerName.c_str());
170                 pman->activate();
171                 orb->run();
172         }
173         catch(CORBA::SystemException&)
174         {
175                 INFOS("Caught CORBA::SystemException.");
176         }
177         catch(PortableServer::POA::ServantAlreadyActive&)
178         {
179                 INFOS("Caught CORBA::ServantAlreadyActiveException");
180         }
181         catch(CORBA::Exception&)
182         {
183                 INFOS("Caught CORBA::Exception.");
184         }
185         catch(std::exception& exc)
186         {
187                 INFOS("Caught std::exception - "<<exc.what()); 
188         }
189         catch(...)
190         {
191                 INFOS("Caught unknown exception.");
192         }
193
194         MPI_Finalize();
195
196         return 0 ;
197 }
198