Salome HOME
Merge from BR_V5_DEV 16Feb09
[tools/medcoupling.git] / src / ParaMEDMEM / MPIAccessDEC.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
2 //
3 //  This library is free software; you can redistribute it and/or
4 //  modify it under the terms of the GNU Lesser General Public
5 //  License as published by the Free Software Foundation; either
6 //  version 2.1 of the License.
7 //
8 //  This library is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 //  Lesser General Public License for more details.
12 //
13 //  You should have received a copy of the GNU Lesser General Public
14 //  License along with this library; if not, write to the Free Software
15 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef __MPIACCESSDEC_HXX__
20 #define __MPIACCESSDEC_HXX__
21
22 #include "MPIAccess.hxx"
23 #include "DEC.hxx"
24 #include "LinearTimeInterpolator.hxx"
25
26 #include <map>
27 #include <iostream>
28
29 namespace ParaMEDMEM
30 {
31   class MPIAccessDEC
32   {
33   public:  
34     MPIAccessDEC( const ProcessorGroup& local_group, const ProcessorGroup& distant_group,
35                   bool Asynchronous = true );
36     virtual ~MPIAccessDEC();
37     MPIAccess * getMPIAccess() { return _MPI_access; }
38     const MPI_Comm* getComm() { return _MPI_union_group->getComm(); }
39     void asynchronous( bool Asynchronous = true ) { _asynchronous = Asynchronous; }
40     void setTimeInterpolator( TimeInterpolationMethod anInterp , double InterpPrecision=0 ,
41                               int n_step_before=1, int nStepAfter=1 );
42
43     void setTime( double t ) { _t = t; _dt = -1; }
44     void setTime( double t , double dt ) { _t = t; _dt = dt; }
45     bool outOfTime( int target ) { return (*_out_of_time)[target]; }
46
47     int send( void* sendbuf, int sendcount , MPI_Datatype sendtype , int target );
48     int recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target );
49     int recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target ,
50               int &RecvRequestId , bool Asynchronous=false );
51     int sendRecv( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
52                   void* recvbuf, int recvcount , MPI_Datatype recvtype , int target );
53
54     int allToAll( void* sendbuf, int sendcount, MPI_Datatype sendtype ,
55                   void* recvbuf, int recvcount, MPI_Datatype recvtype );
56     int allToAllv( void* sendbuf, int* sendcounts, int* sdispls, MPI_Datatype sendtype ,
57                    void* recvbuf, int* recvcounts, int* rdispls, MPI_Datatype recvtype );
58
59     int allToAllTime( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
60                       void* recvbuf, int recvcount , MPI_Datatype recvtype );
61     int allToAllvTime( void* sendbuf, int* sendcounts, int* sdispls,
62                        MPI_Datatype sendtype ,
63                        void* recvbuf, int* recvcounts, int* rdispls,
64                        MPI_Datatype recvtype );
65     int checkTime( int recvcount , MPI_Datatype recvtype , int target , bool UntilEnd );
66     int checkSent(bool WithWait=false);
67     int checkFinalSent() { return checkSent( true ); }
68     int checkFinalRecv();
69   protected:
70     int send( void* sendbuf, int sendcount , int sendoffset , MPI_Datatype sendtype ,
71               int target, int &SendRequestId );
72     int recv( void* recvbuf, int recvcount , int recvoffset , MPI_Datatype recvtype ,
73               int target, int &RecvRequestId );
74     int sendRecv( void* sendbuf, int sendcount , int sendoffset ,
75                   MPI_Datatype sendtype , 
76                   void* recvbuf, int recvcount , int recvoffset ,
77                   MPI_Datatype recvtype , int target ,
78                   int &SendRequestId ,int &RecvRequestId );
79   private :
80     bool _asynchronous;
81     MPIProcessorGroup* _MPI_union_group;
82
83     TimeInterpolator* _time_interpolator;
84     int _n_step_before;
85     int _n_step_after;
86
87     int _my_rank;
88     int _group_size;
89     MPIAccess* _MPI_access;
90
91     // Current time and deltatime of current process
92     double _t;
93     double _dt;
94
95     // TimeMessages from each target _TimeMessages[target][Step] : TimeMessage
96     std::vector< std::vector< TimeMessage > > *_time_messages;
97     // Corresponding DataMessages from each target _DataMessages[target][~TimeStep]
98     std::vector< bool >* _out_of_time;
99     std::vector< int >* _data_messages_recv_count;
100     std::vector< MPI_Datatype >* _data_messages_type;
101     std::vector< std::vector< void * > >* _data_messages;
102
103     typedef struct
104     {
105       void * SendBuffer;
106       int Counter;
107       MPI_Datatype DataType; }
108       SendBuffStruct;
109     std::map< int ,  SendBuffStruct * > *_map_of_send_buffers;
110   };
111
112   inline int MPIAccessDEC::send( void* sendbuf, int sendcount , MPI_Datatype sendtype , int target )
113   {
114     int SendRequestId;
115     int sts;
116     if ( _asynchronous )
117       {
118         sts = _MPI_access->ISend( sendbuf , sendcount , sendtype , target ,
119                                   SendRequestId );
120       }
121     else
122       {
123         sts = _MPI_access->send( sendbuf , sendcount , sendtype , target ,
124                                  SendRequestId );
125         if ( sts == MPI_SUCCESS )
126           free( sendbuf );
127       }
128     return sts;
129   }
130
131   inline int MPIAccessDEC::recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target )
132   {
133     int RecvRequestId;
134     int sts;
135     if ( _asynchronous )
136       sts = _MPI_access->IRecv( recvbuf , recvcount , recvtype , target , RecvRequestId );
137     else
138       sts = _MPI_access->recv( recvbuf , recvcount , recvtype , target ,  RecvRequestId );
139     return sts;
140   }
141
142   inline int MPIAccessDEC::recv( void* recvbuf, int recvcount , MPI_Datatype recvtype ,
143                                  int target ,  int &RecvRequestId , bool Asynchronous )
144   {
145     int sts;
146     if ( Asynchronous )
147       sts = _MPI_access->IRecv( recvbuf , recvcount , recvtype , target ,
148                                 RecvRequestId );
149     else
150       sts = _MPI_access->recv( recvbuf , recvcount , recvtype , target ,
151                                RecvRequestId );
152     return sts;
153   }
154   
155   inline int MPIAccessDEC::sendRecv( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
156                                      void* recvbuf, int recvcount , MPI_Datatype recvtype ,
157                                      int target )
158   {
159     int SendRequestId;
160     int RecvRequestId;
161     int sts;
162     if ( _asynchronous )
163       sts = _MPI_access->ISendRecv( sendbuf , sendcount , sendtype , target ,
164                                     SendRequestId ,
165                                     recvbuf , recvcount , recvtype , target ,
166                                     RecvRequestId );
167     else
168       sts = _MPI_access->sendRecv( sendbuf , sendcount , sendtype , target ,
169                                    SendRequestId ,
170                                    recvbuf , recvcount , recvtype , target ,
171                                    RecvRequestId );
172     return sts;
173   }
174
175   std::ostream & operator<< (std::ostream &,const TimeInterpolationMethod &);
176 }
177
178 #endif