Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / ParallelContainer / SALOME_ParallelContainerProxyMpi.cxx
1 //  SALOME ParallelContainerProxyMpi : Launching the proxy of a MPI PaCO++ object
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_ParallelContainerProxyMpi.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_ComponentPaCO_Engines_Container_server.h"
39 #include <paco_omni.h>
40 #include <paco_mpi.h>
41
42 #include <mpi.h>
43
44 #include "SALOME_NamingService.hxx"
45
46 #include "utilities.h"
47 #include "Utils_ORB_INIT.hxx"
48 #include "Utils_SINGLETON.hxx"
49 #include "SALOMETraceCollector.hxx"
50 #include "OpUtil.hxx"
51 using namespace std;
52
53 #ifdef DEBUG_PARALLEL
54 #include <signal.h>
55
56 void handler(int t) {
57         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
58         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
59         cerr << "SIGSEGV in :" << getpid() << endl;
60         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
61         cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
62         while (1) {}
63 }
64 #endif
65
66 int main(int argc, char* argv[])
67 {
68         INFOS("Launching a parallel Mpi proxy container");
69
70 #ifdef DEBUG_PARALLEL
71         signal(SIGSEGV, handler);
72 #endif
73
74         // MPI Init
75         int provided;
76         MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED ,&provided);
77         
78         // Initialise the ORB.
79         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
80         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
81         CORBA::ORB_var orb = init(0, 0);
82         //CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
83
84         char *containerName = "";
85         if(argc > 1) {
86                 containerName = argv[1];
87         }
88
89         char * nb_nodes = "";
90         if(argc > 2) {
91                 nb_nodes = argv[2];
92         }
93
94         try {  
95                 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
96                 ASSERT(!CORBA::is_nil(obj));
97                 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
98                 PortableServer::POAManager_var pman = root_poa->the_POAManager();
99
100 #ifndef WNT
101                 // add this container to the kill list
102                 char aCommand[100];
103                 sprintf(aCommand, "addToKillList.py %d SALOME_ParallelContainerProxyMpi", getpid());
104                 system(aCommand);
105 #endif
106
107                 SALOME_NamingService * ns = new SALOME_NamingService(CORBA::ORB::_duplicate(orb));
108                 Engines::Container_proxy_impl * proxy = new Engines::Container_proxy_impl(CORBA::ORB::_duplicate(orb)); 
109
110                 // PaCO++ code
111                 paco_fabrique_manager* pfm = paco_getFabriqueManager();
112                 // Global context
113                 PaCO_operation * global_ptr =  proxy->getContext("global_paco_context");
114                 pfm->register_com("mpi", new paco_mpi_fabrique());
115                 MPI_Comm group = MPI_COMM_WORLD;
116                 global_ptr->setLibCom("mpi", &group);
117                 pfm->register_thread("omnithread", new paco_omni_fabrique());
118                 global_ptr->setLibThread("omnithread");
119                 global_ptr->setTypeClient(true);
120                 PaCO::PacoTopology_t client_topo;
121                 client_topo.total = 1;
122                 global_ptr->setClientTopo(client_topo);
123                 PaCO::PacoTopology_t serveur_topo;
124                 serveur_topo.total = atoi(nb_nodes);
125                 proxy->setTopo(serveur_topo);
126
127                 // Activation
128                 PortableServer::ObjectId_var _id = root_poa->activate_object(proxy);
129                 obj = root_poa->id_to_reference(_id);
130
131                 // in the NamingService
132                 string hostname = GetHostname();
133                 Engines::Container_var pCont = Engines::Container::_narrow(obj);
134                 string _containerName = ns->BuildContainerNameForNS(containerName,
135                                                                                                                                                                                                                                 hostname.c_str());
136                 cerr << "---------" << _containerName << "----------" << endl;
137                 ns->Register(pCont, _containerName.c_str());
138                 pman->activate();
139                 orb->run();
140         }
141         catch(CORBA::SystemException&)
142         {
143                 INFOS("Caught CORBA::SystemException.");
144         }
145         catch(PortableServer::POA::ServantAlreadyActive&)
146         {
147                 INFOS("Caught CORBA::ServantAlreadyActiveException");
148         }
149         catch(CORBA::Exception&)
150         {
151                 INFOS("Caught CORBA::Exception.");
152         }
153         catch(std::exception& exc)
154         {
155                 INFOS("Caught std::exception - "<<exc.what()); 
156         }
157         catch(...)
158         {
159                 INFOS("Caught unknown exception.");
160         }
161
162         MPI_Finalize();
163
164         return 0 ;
165 }
166