Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/kernel.git] / src / MPIContainer / MPIObject_i.cxx
1 //  SALOME MPIContainer : implemenation of container based on MPI libraries
2 //
3 //  Copyright (C) 2003  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   : MPIObject_i.cxx
25 //  Module : SALOME
26
27 #include "MPIObject_i.hxx"
28 #include "utilities.h"
29 #include <mpi.h>
30 using namespace std;
31
32 MPIObject_i::MPIObject_i()
33 {
34   _nbproc = 1;
35   _numproc = 0;
36   _tior=NULL;
37 }
38
39 MPIObject_i::MPIObject_i(int nbproc, int numproc)
40 {
41   _nbproc = nbproc;
42   _numproc = numproc;
43   _tior=NULL;
44 }
45
46 MPIObject_i::~MPIObject_i()
47 {
48   if(_tior) delete _tior;
49 }
50
51 Engines::IORTab* MPIObject_i::tior()
52 {
53   Engines::IORTab* tior = new Engines::IORTab;
54   tior->length(_tior->length());
55   for(unsigned int ip=0;ip<tior->length();ip++)
56     (*tior)[ip] = (*_tior)[ip];
57   return tior; 
58 };
59
60 void MPIObject_i::tior(const Engines::IORTab& ior)
61 {
62   _tior = new Engines::IORTab;
63   _tior->length(ior.length());
64   for(unsigned int ip=0;ip<ior.length();ip++)
65     (*_tior)[ip] = ior[ip];
66 }
67
68 void MPIObject_i::BCastIOR(CORBA::ORB_ptr orb, Engines::MPIObject_var pobj, 
69                            bool amiCont)
70 {
71   int err, ip, n;
72   char *ior;
73   MPI_Status status; /* status de reception de message MPI */
74
75   // Conversion IOR vers string
76   CORBA::String_var sior(orb->object_to_string(pobj));
77
78   if( _numproc == 0 ){
79
80     //Allocation du tableau des IOR
81     Engines::IORTab *iort = new Engines::IORTab;
82     iort->length(_nbproc);
83
84     (*iort)[0] = pobj;
85
86     // Process 0 recupere les ior de l'object sur les autres process
87     for(ip=1;ip<_nbproc;ip++){
88       err = MPI_Recv(&n,1,MPI_INTEGER,ip,ip,MPI_COMM_WORLD,&status);
89       if(err){
90         MESSAGE("[" << _numproc << "] MPI_RECV error");
91         exit(1);
92       }
93       // Allocation de la chaine de longueur n
94       ior = (char*)calloc(n,sizeof(char));
95       err = MPI_Recv(ior,n,MPI_CHARACTER,ip,2*ip,MPI_COMM_WORLD,&status);
96       if(err){
97         MESSAGE("[" << _numproc << "] MPI_RECV error");
98         exit(1);
99       }
100       (*iort)[ip] = Engines::MPIObject::_narrow(orb->string_to_object(ior));
101       free(ior);
102     }
103     // On donne le tableau des ior a l'objet Corba du process 0
104     if( amiCont )
105       tior(*iort);
106     else
107       pobj->tior(*iort);
108
109   }
110   else{
111     // On envoie l'IOR au process 0
112     ior = sior._retn();
113     n = strlen(ior) + 1;
114     err = MPI_Send(&n,1,MPI_INTEGER,0,_numproc,MPI_COMM_WORLD);
115     if(err){
116       MESSAGE("[" << _numproc << "] MPI_SEND error");
117       exit(1);
118     }
119     err = MPI_Send(ior,n,MPI_CHARACTER,0,2*_numproc,MPI_COMM_WORLD);
120     if(err){
121       MESSAGE("[" << _numproc << "] MPI_SEND error");
122       exit(1);
123     }
124   }
125
126   MPI_Barrier(MPI_COMM_WORLD);
127
128 }
129