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