1 // Copyright (C) 2007-2023 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CommInterface.hxx"
21 #include "ProcessorGroup.hxx"
22 #include "MPIProcessorGroup.hxx"
23 #include "MPIAccessDEC.hxx"
24 #include "MxN_Mapping.hxx"
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()
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);
43 MxN_Mapping::~MxN_Mapping()
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
55 void MxN_Mapping::addElementFromSource(int distant_proc, mcIdType distant_element)
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]++;
62 void MxN_Mapping::initialize()
65 std::fill(_send_proc_offsets.begin(),_send_proc_offsets.end(),0);
68 void MxN_Mapping::prepareSendRecv()
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++)
76 nbsend[i]=_send_proc_offsets[i+1]-_send_proc_offsets[i];
79 MPIProcessorGroup* group = static_cast<MPIProcessorGroup*>(_union_group);
80 const MPI_Comm* comm=group->getComm();
81 comm_interface.allToAll(nbsend, 1, MPI_INT,
85 for (int i=0; i<_union_group->size(); i++)
87 for (int j=i+1;j<_union_group->size()+1; j++)
88 _recv_proc_offsets[j]+=nbrecv[i];
95 _recv_ids.resize(_recv_proc_offsets[_union_group->size()]);
98 if (_sending_ids.size()>0)
99 isendbuf = new mcIdType[_sending_ids.size()];
100 if (_recv_ids.size()>0)
101 irecvbuf = new mcIdType[_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++)
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];
113 vector<int> offsets = _send_proc_offsets;
114 for (std::size_t i=0; i<_sending_ids.size();i++)
116 int iproc = _sending_ids[i].first;
117 isendbuf[offsets[iproc]]=_sending_ids[i].second;
120 comm_interface.allToAllV(isendbuf, sendcounts, senddispls, MPI_ID_TYPE,
121 irecvbuf, recvcounts, recvdispls, MPI_ID_TYPE,
124 for (int i=0; i< _recv_proc_offsets[_union_group->size()]; i++)
125 _recv_ids[i]=irecvbuf[i];
127 if (_sending_ids.size()>0)
129 if (_recv_ids.size()>0)
137 /*! Exchanging field data between two groups of processes
139 * \param field MEDCoupling field containing the values to be sent
141 * The ids that were defined by addElementFromSource method
144 void MxN_Mapping::sendRecv(double* sendfield, MEDCouplingFieldDouble& field) const
146 CommInterface comm_interface=_union_group->getCommInterface();
147 const MPIProcessorGroup* group = static_cast<const MPIProcessorGroup*>(_union_group);
149 int nbcomp=(int)field.getArray()->getNumberOfComponents();
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];
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()];
162 for (int i=0; i< _union_group->size(); i++)
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]);
169 //building the buffer of the elements to be sent
170 vector<int> offsets = _send_proc_offsets;
172 for (int i=0; i<(int)_sending_ids.size();i++)
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];
180 //communication phase
181 switch (getAllToAllMethod())
185 const MPI_Comm* comm = group->getComm();
186 comm_interface.allToAllV(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
187 recvbuf, recvcounts, recvdispls, MPI_DOUBLE,
192 _access_DEC->allToAllv(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
193 recvbuf, recvcounts, recvdispls, MPI_DOUBLE);
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++)
202 for (int icomp=0; icomp<nbcomp; icomp++)
204 double temp = fieldArr->getIJ(_recv_ids[i],icomp);
205 fieldArr->setIJ(_recv_ids[i],icomp,temp+*recvptr);
209 if (sendbuf!=0 && getAllToAllMethod()== Native)
220 /*! Exchanging field data between two groups of processes
222 * \param field MEDCoupling field containing the values to be sent
224 * The ids that were defined by addElementFromSource method
227 void MxN_Mapping::reverseSendRecv(double* recvfield, MEDCouplingFieldDouble& field) const
229 CommInterface comm_interface=_union_group->getCommInterface();
230 const MPIProcessorGroup* group = static_cast<const MPIProcessorGroup*>(_union_group);
232 int nbcomp=(int)field.getArray()->getNumberOfComponents();
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];
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()];
245 for (int i=0; i< _union_group->size(); i++)
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]);
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++)
258 for (int icomp=0; icomp<nbcomp; icomp++)
259 sendbuf[i*nbcomp+icomp]=fieldArr->getIJ(_recv_ids[i],icomp);
262 //communication phase
263 switch (getAllToAllMethod())
267 const MPI_Comm* comm = group->getComm();
268 comm_interface.allToAllV(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
269 recvbuf, recvcounts, recvdispls, MPI_DOUBLE,
274 _access_DEC->allToAllv(sendbuf, sendcounts, senddispls, MPI_DOUBLE,
275 recvbuf, recvcounts, recvdispls, MPI_DOUBLE);
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++)
283 for (int icomp=0; icomp<nbcomp; icomp++)
285 recvfield[i*nbcomp+icomp]=*recvptr;
289 if (sendbuf!=0 && getAllToAllMethod() == Native)
299 ostream & operator<< (ostream & f ,const AllToAllMethod & alltoallmethod )
301 switch (alltoallmethod)
307 f << " PointToPoint ";
310 f << " UnknownAllToAllMethod ";