Salome HOME
5648dfe1d84cb92620da70e42a61d7558025134d
[tools/medcoupling.git] / src / ParaMEDMEM / MxN_Mapping.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include "CommInterface.hxx" 
21 #include "ProcessorGroup.hxx"
22 #include "MPIProcessorGroup.hxx"
23 #include "MPIAccessDEC.hxx"
24 #include "MxN_Mapping.hxx"
25
26 using namespace std;
27
28 namespace MEDCoupling
29 {
30
31   MxN_Mapping::MxN_Mapping(const ProcessorGroup& source_group, const ProcessorGroup& target_group,const DECOptions& dec_options)
32     : DECOptions(dec_options),
33       _union_group(source_group.fuse(target_group)),
34       _nb_comps(0), _sending_ids(), _recv_ids()
35   {
36     _access_DEC = new MPIAccessDEC(source_group,target_group,getAsynchronous());
37     _access_DEC->setTimeInterpolator(getTimeInterpolationMethod());
38     _send_proc_offsets.resize(_union_group->size()+1,0);
39     _recv_proc_offsets.resize(_union_group->size()+1,0);
40   
41   }
42
43   MxN_Mapping::~MxN_Mapping()
44   {
45     delete _union_group;
46     delete _access_DEC;
47   }
48
49
50   /*!
51     Method registering a new element for correspondence with a distant element
52     \param distant_proc proc rank of the distant processor (in terms of the union group)
53     \param distant_element id of the element on the distant processor
54   */
55   void MxN_Mapping::addElementFromSource(int distant_proc, int distant_element)
56   {
57     _sending_ids.push_back(make_pair(distant_proc,distant_element));
58     for (int i=distant_proc; i<_union_group->size(); i++)
59       _send_proc_offsets[i+1]++;
60   }
61
62   void MxN_Mapping::initialize()
63   {
64     _sending_ids.clear();
65     std::fill(_send_proc_offsets.begin(),_send_proc_offsets.end(),0);
66   }
67
68   void MxN_Mapping::prepareSendRecv()
69   {
70     CommInterface comm_interface=_union_group->getCommInterface();
71     // sending count pattern
72     int* nbsend=new int[_union_group->size()];
73     int* nbrecv=new int[_union_group->size()];
74     for (int i=0; i<_union_group->size(); i++)
75       {
76         nbsend[i]=_send_proc_offsets[i+1]-_send_proc_offsets[i];
77       }
78   
79     MPIProcessorGroup* group = static_cast<MPIProcessorGroup*>(_union_group);
80     const MPI_Comm* comm=group->getComm();
81     comm_interface.allToAll(nbsend, 1, MPI_INT,
82                             nbrecv, 1, MPI_INT,
83                             *comm);
84          
85     for (int i=0; i<_union_group->size(); i++)
86       {
87         for (int j=i+1;j<_union_group->size()+1; j++)
88           _recv_proc_offsets[j]+=nbrecv[i];
89     
90       } 
91
92     delete[] nbsend;
93     delete[] nbrecv;
94
95     _recv_ids.resize(_recv_proc_offsets[_union_group->size()]);
96     int* isendbuf=0;
97     int* irecvbuf=0;
98     if (_sending_ids.size()>0)
99       isendbuf = new int[_sending_ids.size()];
100     if (_recv_ids.size()>0)  
101       irecvbuf = new int[_recv_ids.size()];
102     int* sendcounts = new int[_union_group->size()];
103     int* senddispls=new int[_union_group->size()];
104     int* recvcounts=new int[_union_group->size()];
105     int* recvdispls=new int[_union_group->size()];
106     for (int i=0; i< _union_group->size(); i++)
107       {
108         sendcounts[i]=_send_proc_offsets[i+1]-_send_proc_offsets[i];
109         senddispls[i]=_send_proc_offsets[i];
110         recvcounts[i]=_recv_proc_offsets[i+1]-_recv_proc_offsets[i];
111         recvdispls[i]=_recv_proc_offsets[i];
112       }
113     vector<int> offsets = _send_proc_offsets;
114     for (int i=0; i<(int)_sending_ids.size();i++)
115       {
116         int iproc = _sending_ids[i].first;
117         isendbuf[offsets[iproc]]=_sending_ids[i].second;
118         offsets[iproc]++;
119       }
120     comm_interface.allToAllV(isendbuf, sendcounts, senddispls, MPI_INT,
121                              irecvbuf, recvcounts, recvdispls, MPI_INT,
122                              *comm);
123                            
124     for (int i=0; i< _recv_proc_offsets[_union_group->size()]; i++)
125       _recv_ids[i]=irecvbuf[i];                           
126  
127     if (_sending_ids.size()>0)
128       delete[] isendbuf;
129     if (_recv_ids.size()>0)  
130       delete[] irecvbuf;
131     delete[] sendcounts;
132     delete[]recvcounts;
133     delete[]senddispls;
134     delete[] recvdispls;
135   }
136
137   /*! Exchanging field data between two groups of processes
138    * 
139    * \param field MEDCoupling field containing the values to be sent
140    * 
141    * The ids that were defined by addElementFromSource method
142    * are sent.
143    */ 
144   void MxN_Mapping::sendRecv(double* sendfield, MEDCouplingFieldDouble& field) const 
145   {
146     CommInterface comm_interface=_union_group->getCommInterface();
147     const MPIProcessorGroup* group = static_cast<const MPIProcessorGroup*>(_union_group);
148  
149     int nbcomp=field.getArray()->getNumberOfComponents();
150     double* sendbuf=0;
151     double* recvbuf=0;
152     if (_sending_ids.size() >0)
153       sendbuf = new double[_sending_ids.size()*nbcomp];
154     if (_recv_ids.size()>0)
155       recvbuf = new double[_recv_ids.size()*nbcomp];
156     
157     int* sendcounts = new int[_union_group->size()];
158     int* senddispls=new int[_union_group->size()];
159     int* recvcounts=new int[_union_group->size()];
160     int* recvdispls=new int[_union_group->size()];
161   
162     for (int i=0; i< _union_group->size(); i++)
163       {
164         sendcounts[i]=nbcomp*(_send_proc_offsets[i+1]-_send_proc_offsets[i]);
165         senddispls[i]=nbcomp*(_send_proc_offsets[i]);
166         recvcounts[i]=nbcomp*(_recv_proc_offsets[i+1]-_recv_proc_offsets[i]);
167         recvdispls[i]=nbcomp*(_recv_proc_offsets[i]);
168       }
169     //building the buffer of the elements to be sent
170     vector<int> offsets = _send_proc_offsets;
171
172     for (int i=0; i<(int)_sending_ids.size();i++)
173       { 
174         int iproc = _sending_ids[i].first;
175         for (int icomp=0; icomp<nbcomp; icomp++)
176           sendbuf[offsets[iproc]*nbcomp+icomp]=sendfield[i*nbcomp+icomp];
177         offsets[iproc]++;
178       }
179   
180     //communication phase
181     switch (getAllToAllMethod())
182       {
183       case Native:
184         {
185           const MPI_Comm* comm = group->getComm();
186           comm_interface.allToAllV(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
187                                    recvbuf, recvcounts, recvdispls, MPI_DOUBLE,
188                                    *comm);
189         }
190         break;
191       case PointToPoint:
192         _access_DEC->allToAllv(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
193                               recvbuf, recvcounts, recvdispls, MPI_DOUBLE);
194         break;
195       }
196   
197     //setting the received values in the field
198     DataArrayDouble *fieldArr=field.getArray();
199     double* recvptr=recvbuf;                         
200     for (int i=0; i< _recv_proc_offsets[_union_group->size()]; i++)
201       {
202         for (int icomp=0; icomp<nbcomp; icomp++)
203           {
204             double temp = fieldArr->getIJ(_recv_ids[i],icomp);
205             fieldArr->setIJ(_recv_ids[i],icomp,temp+*recvptr);
206             recvptr++;
207           }
208       }   
209     if (sendbuf!=0 && getAllToAllMethod()== Native)
210       delete[] sendbuf;
211     if (recvbuf !=0)
212       delete[] recvbuf;
213     delete[] sendcounts;
214     delete[] recvcounts;
215     delete[] senddispls; 
216     delete[] recvdispls;
217   
218   }
219
220   /*! Exchanging field data between two groups of processes
221    * 
222    * \param field MEDCoupling field containing the values to be sent
223    * 
224    * The ids that were defined by addElementFromSource method
225    * are sent.
226    */ 
227   void MxN_Mapping::reverseSendRecv(double* recvfield, MEDCouplingFieldDouble& field) const 
228   {
229     CommInterface comm_interface=_union_group->getCommInterface();
230     const MPIProcessorGroup* group = static_cast<const MPIProcessorGroup*>(_union_group);
231
232     int nbcomp=field.getArray()->getNumberOfComponents();
233     double* sendbuf=0;
234     double* recvbuf=0;
235     if (_recv_ids.size() >0)
236       sendbuf = new double[_recv_ids.size()*nbcomp];
237     if (_sending_ids.size()>0)
238       recvbuf = new double[_sending_ids.size()*nbcomp];
239
240     int* sendcounts = new int[_union_group->size()];
241     int* senddispls=new int[_union_group->size()];
242     int* recvcounts=new int[_union_group->size()];
243     int* recvdispls=new int[_union_group->size()];
244
245     for (int i=0; i< _union_group->size(); i++)
246       {
247         sendcounts[i]=nbcomp*(_recv_proc_offsets[i+1]-_recv_proc_offsets[i]);
248         senddispls[i]=nbcomp*(_recv_proc_offsets[i]);
249         recvcounts[i]=nbcomp*(_send_proc_offsets[i+1]-_send_proc_offsets[i]);
250         recvdispls[i]=nbcomp*(_send_proc_offsets[i]);
251       }
252     //building the buffer of the elements to be sent
253     vector<int> offsets = _recv_proc_offsets;
254     DataArrayDouble *fieldArr=field.getArray();
255     for (int iproc=0; iproc<_union_group->size();iproc++)
256       for (int i=_recv_proc_offsets[iproc]; i<_recv_proc_offsets[iproc+1]; i++)
257         {
258           for (int icomp=0; icomp<nbcomp; icomp++)
259             sendbuf[i*nbcomp+icomp]=fieldArr->getIJ(_recv_ids[i],icomp);
260         }
261
262     //communication phase
263     switch (getAllToAllMethod())
264       {
265       case Native:
266         {
267           const MPI_Comm* comm = group->getComm();
268           comm_interface.allToAllV(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
269                                    recvbuf, recvcounts, recvdispls, MPI_DOUBLE,
270                                    *comm);
271         }
272         break;
273       case PointToPoint:
274         _access_DEC->allToAllv(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
275                                recvbuf, recvcounts, recvdispls, MPI_DOUBLE);
276         break;
277       }
278
279     //setting the received values in the field
280     double* recvptr=recvbuf;                         
281     for (int i=0; i< _send_proc_offsets[_union_group->size()]; i++)
282       {
283         for (int icomp=0; icomp<nbcomp; icomp++)
284           {
285             recvfield[i*nbcomp+icomp]=*recvptr;
286             recvptr++;
287           }
288       }
289     if (sendbuf!=0 && getAllToAllMethod() == Native)
290       delete[] sendbuf;
291     if (recvbuf!=0)
292       delete[] recvbuf;
293     delete[] sendcounts;
294     delete[] recvcounts;
295     delete[] senddispls; 
296     delete[] recvdispls;
297   }
298
299   ostream & operator<< (ostream & f ,const AllToAllMethod & alltoallmethod )
300   {
301     switch (alltoallmethod)
302       {
303       case Native :
304         f << " Native ";
305         break;
306       case PointToPoint :
307         f << " PointToPoint ";
308         break;
309       default :
310         f << " UnknownAllToAllMethod ";
311         break;
312       }
313     return f;
314   }
315 }