Salome HOME
ef730c3e807b58c35dc674e8b8b65eb7f02763a4
[modules/med.git] / src / ParaMEDMEM / MPIAccessDEC.hxx
1 // Copyright (C) 2007-2013  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
20 #ifndef __MPIACCESSDEC_HXX__
21 #define __MPIACCESSDEC_HXX__
22
23 #include "MPIAccess.hxx"
24 #include "DEC.hxx"
25 #include "LinearTimeInterpolator.hxx"
26
27 #include <map>
28 #include <iostream>
29
30 namespace ParaMEDMEM
31 {
32   class MPIAccessDEC
33   {
34   public:  
35     MPIAccessDEC( const ProcessorGroup& local_group, const ProcessorGroup& distant_group,
36                   bool Asynchronous = true );
37     virtual ~MPIAccessDEC();
38     MPIAccess * getMPIAccess() { return _MPI_access; }
39     const MPI_Comm* getComm() { return _MPI_union_group->getComm(); }
40     void asynchronous( bool Asynchronous = true ) { _asynchronous = Asynchronous; }
41     void setTimeInterpolator( TimeInterpolationMethod anInterp , double InterpPrecision=0 ,
42                               int n_step_before=1, int nStepAfter=1 );
43
44     void setTime( double t ) { _t = t; _dt = -1; }
45     void setTime( double t , double dt ) { _t = t; _dt = dt; }
46     bool outOfTime( int target ) { return (*_out_of_time)[target]; }
47
48     int send( void* sendbuf, int sendcount , MPI_Datatype sendtype , int target );
49     int recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target );
50     int recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target ,
51               int &RecvRequestId , bool Asynchronous=false );
52     int sendRecv( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
53                   void* recvbuf, int recvcount , MPI_Datatype recvtype , int target );
54
55     int allToAll( void* sendbuf, int sendcount, MPI_Datatype sendtype ,
56                   void* recvbuf, int recvcount, MPI_Datatype recvtype );
57     int allToAllv( void* sendbuf, int* sendcounts, int* sdispls, MPI_Datatype sendtype ,
58                    void* recvbuf, int* recvcounts, int* rdispls, MPI_Datatype recvtype );
59
60     int allToAllTime( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
61                       void* recvbuf, int recvcount , MPI_Datatype recvtype );
62     int allToAllvTime( void* sendbuf, int* sendcounts, int* sdispls,
63                        MPI_Datatype sendtype ,
64                        void* recvbuf, int* recvcounts, int* rdispls,
65                        MPI_Datatype recvtype );
66     int checkTime( int recvcount , MPI_Datatype recvtype , int target , bool UntilEnd );
67     int checkSent(bool WithWait=false);
68     int checkFinalSent() { return checkSent( true ); }
69     int checkFinalRecv();
70   protected:
71     int send( void* sendbuf, int sendcount , int sendoffset , MPI_Datatype sendtype ,
72               int target, int &SendRequestId );
73     int recv( void* recvbuf, int recvcount , int recvoffset , MPI_Datatype recvtype ,
74               int target, int &RecvRequestId );
75     int sendRecv( void* sendbuf, int sendcount , int sendoffset ,
76                   MPI_Datatype sendtype , 
77                   void* recvbuf, int recvcount , int recvoffset ,
78                   MPI_Datatype recvtype , int target ,
79                   int &SendRequestId ,int &RecvRequestId );
80   private :
81     bool _asynchronous;
82     MPIProcessorGroup* _MPI_union_group;
83
84     TimeInterpolator* _time_interpolator;
85     int _n_step_before;
86     int _n_step_after;
87
88     int _my_rank;
89     int _group_size;
90     MPIAccess* _MPI_access;
91
92     // Current time and deltatime of current process
93     double _t;
94     double _dt;
95
96     // TimeMessages from each target _TimeMessages[target][Step] : TimeMessage
97     std::vector< std::vector< TimeMessage > > *_time_messages;
98     // Corresponding DataMessages from each target _DataMessages[target][~TimeStep]
99     std::vector< bool >* _out_of_time;
100     std::vector< int >* _data_messages_recv_count;
101     std::vector< MPI_Datatype >* _data_messages_type;
102     std::vector< std::vector< void * > >* _data_messages;
103
104     typedef struct
105     {
106       void * SendBuffer;
107       int Counter;
108       MPI_Datatype DataType; }
109       SendBuffStruct;
110     std::map< int ,  SendBuffStruct * > *_map_of_send_buffers;
111   };
112
113   inline int MPIAccessDEC::send( void* sendbuf, int sendcount , MPI_Datatype sendtype , int target )
114   {
115     int SendRequestId;
116     int sts;
117     if ( _asynchronous )
118       {
119         sts = _MPI_access->ISend( sendbuf , sendcount , sendtype , target ,
120                                   SendRequestId );
121       }
122     else
123       {
124         sts = _MPI_access->send( sendbuf , sendcount , sendtype , target ,
125                                  SendRequestId );
126         if ( sts == MPI_SUCCESS )
127           free( sendbuf );
128       }
129     return sts;
130   }
131
132   inline int MPIAccessDEC::recv( void* recvbuf, int recvcount , MPI_Datatype recvtype , int target )
133   {
134     int RecvRequestId;
135     int sts;
136     if ( _asynchronous )
137       sts = _MPI_access->IRecv( recvbuf , recvcount , recvtype , target , RecvRequestId );
138     else
139       sts = _MPI_access->recv( recvbuf , recvcount , recvtype , target ,  RecvRequestId );
140     return sts;
141   }
142
143   inline int MPIAccessDEC::recv( void* recvbuf, int recvcount , MPI_Datatype recvtype ,
144                                  int target ,  int &RecvRequestId , bool Asynchronous )
145   {
146     int sts;
147     if ( Asynchronous )
148       sts = _MPI_access->IRecv( recvbuf , recvcount , recvtype , target ,
149                                 RecvRequestId );
150     else
151       sts = _MPI_access->recv( recvbuf , recvcount , recvtype , target ,
152                                RecvRequestId );
153     return sts;
154   }
155   
156   inline int MPIAccessDEC::sendRecv( void* sendbuf, int sendcount , MPI_Datatype sendtype ,
157                                      void* recvbuf, int recvcount , MPI_Datatype recvtype ,
158                                      int target )
159   {
160     int SendRequestId;
161     int RecvRequestId;
162     int sts;
163     if ( _asynchronous )
164       sts = _MPI_access->ISendRecv( sendbuf , sendcount , sendtype , target ,
165                                     SendRequestId ,
166                                     recvbuf , recvcount , recvtype , target ,
167                                     RecvRequestId );
168     else
169       sts = _MPI_access->sendRecv( sendbuf , sendcount , sendtype , target ,
170                                    SendRequestId ,
171                                    recvbuf , recvcount , recvtype , target ,
172                                    RecvRequestId );
173     return sts;
174   }
175
176   std::ostream & operator<< (std::ostream &,const TimeInterpolationMethod &);
177 }
178
179 #endif