Salome HOME
PR: mergefrom_BR_CCRT_11Nov04
[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   MPI_Comm_size( MPI_COMM_WORLD, &_nbproc );
35   MPI_Comm_rank( MPI_COMM_WORLD, &_numproc );
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_var 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._retn(); 
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_ptr 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   if( _numproc == 0 ){
76
77     //Allocation du tableau des IOR
78     Engines::IORTab_var iort = new Engines::IORTab;
79     iort->length(_nbproc);
80
81     iort[0] = pobj;
82
83     // Process 0 recupere les ior de l'object sur les autres process
84     for(ip=1;ip<_nbproc;ip++){
85       err = MPI_Recv(&n,1,MPI_INT,ip,ip,MPI_COMM_WORLD,&status);
86       if(err){
87         MESSAGE("[" << _numproc << "] MPI_RECV error");
88         exit(1);
89       }
90       // Allocation de la chaine de longueur n
91       ior = new char[n];
92       err = MPI_Recv(ior,n,MPI_CHAR,ip,2*ip,MPI_COMM_WORLD,&status);
93       if(err){
94         MESSAGE("[" << _numproc << "] MPI_RECV error");
95         exit(1);
96       }
97       iort[ip] = orb->string_to_object(ior);
98       delete [] ior;
99     }
100     // On donne le tableau des ior a l'objet Corba du process 0
101     if( amiCont )
102       tior(*(iort._retn()));
103     else
104       pobj->tior(*(iort._retn()));
105
106   }
107   else{
108     // Conversion IOR vers string
109     ior = orb->object_to_string(pobj);
110     n = strlen(ior) + 1;
111     // On envoie l'IOR au process 0
112     err = MPI_Send(&n,1,MPI_INT,0,_numproc,MPI_COMM_WORLD);
113     if(err){
114       MESSAGE("[" << _numproc << "] MPI_SEND error");
115       exit(1);
116     }
117     err = MPI_Send(ior,n,MPI_CHAR,0,2*_numproc,MPI_COMM_WORLD);
118     if(err){
119       MESSAGE("[" << _numproc << "] MPI_SEND error");
120       exit(1);
121     }
122     CORBA::string_free(ior);
123   }
124
125 }
126