Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/yacs.git] / src / MPIContainer / SALOME_MPIContainer.cxx
1 #include <iostream>
2 #include "MPIContainer_i.hxx"
3 #include "Utils_ORB_INIT.hxx"
4 #include "Utils_SINGLETON.hxx"
5 #include "utilities.h"
6 #include <mpi.h>
7 using namespace std;
8
9 int main(int argc, char* argv[])
10 {
11   int nbproc, numproc;
12   int flag;
13   Engines_MPIContainer_i * myContainer=NULL;
14
15   BEGIN_OF(argv[0])
16   try {
17     
18     MESSAGE("Connection MPI");
19     MPI_Init(&argc,&argv);
20     MPI_Comm_size(MPI_COMM_WORLD,&nbproc);
21     MPI_Comm_rank(MPI_COMM_WORLD,&numproc);
22
23     MESSAGE("[" << numproc << "] Initialisation CORBA");
24     // Initialise the ORB.
25     //    CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
26     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
27     ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
28     CORBA::ORB_var &orb = init( argc , argv ) ;
29  
30     // Obtain a reference to the root POA.
31     CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
32     PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
33
34     // obtain the root poa manager
35     PortableServer::POAManager_var pman = root_poa->the_POAManager();
36
37     // define policy objects     
38     PortableServer::ImplicitActivationPolicy_var implicitActivation =
39       root_poa->create_implicit_activation_policy(PortableServer::NO_IMPLICIT_ACTIVATION) ;
40
41       // default = NO_IMPLICIT_ACTIVATION
42     PortableServer::ThreadPolicy_var threadPolicy =
43       root_poa->create_thread_policy(PortableServer::ORB_CTRL_MODEL) ;
44       // default = ORB_CTRL_MODEL, other choice SINGLE_THREAD_MODEL
45
46     // create policy list
47     CORBA::PolicyList policyList;
48     policyList.length(2);
49     policyList[0] = PortableServer::ImplicitActivationPolicy::_duplicate(implicitActivation) ;
50     policyList[1] = PortableServer::ThreadPolicy::_duplicate(threadPolicy) ;
51
52     // create the child POA
53     PortableServer::POAManager_var nil_mgr = PortableServer::POAManager::_nil() ;
54     PortableServer::POA_var factory_poa =
55       root_poa->create_POA("factory_poa", pman, policyList) ;
56       //with nil_mgr instead of pman, a new POA manager is created with the new POA
57     
58     // destroy policy objects
59     implicitActivation->destroy() ;
60     threadPolicy->destroy() ;
61
62     char *containerName = "";
63     if (argc >1) 
64     {
65         containerName = argv[1] ;
66     }
67
68     MESSAGE("[" << numproc << "] Chargement container");
69     myContainer = new Engines_MPIContainer_i(nbproc,numproc,orb,factory_poa, containerName,argc,argv);
70
71     pman->activate();
72
73     orb->run();
74     orb->destroy();
75
76   }
77   catch(CORBA::SystemException&){
78     INFOS("Caught CORBA::SystemException.");
79   }
80   catch(PortableServer::POA::WrongPolicy&){
81     INFOS("Caught CORBA::WrongPolicyException.");
82   }
83   catch(PortableServer::POA::ServantAlreadyActive&){
84     INFOS("Caught CORBA::ServantAlreadyActiveException");
85   }
86   catch(CORBA::Exception&){
87     INFOS("Caught CORBA::Exception.");
88   }
89   catch(...){
90     INFOS("Caught unknown exception.");
91   }
92
93   if(myContainer)
94     delete myContainer;
95   MPI_Initialized(&flag);
96   if(flag)
97     MPI_Finalize();
98
99   END_OF(argv[0]);
100 }
101