1 // Copyright (C) 2007-2015 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
19 // Author : Anthony Geay (CEA/DEN)
21 #include "OverlapElementLocator.hxx"
23 #include "CommInterface.hxx"
24 #include "Topology.hxx"
25 #include "BlockTopology.hxx"
26 #include "ParaFIELD.hxx"
27 #include "ParaMESH.hxx"
28 #include "ProcessorGroup.hxx"
29 #include "MPIProcessorGroup.hxx"
30 #include "OverlapInterpolationMatrix.hxx"
31 #include "MEDCouplingFieldDouble.hxx"
32 #include "MEDCouplingFieldDiscretization.hxx"
33 #include "DirectedBoundingBox.hxx"
34 #include "InterpKernelAutoPtr.hxx"
42 const int OverlapElementLocator::START_TAG_MESH_XCH = 1140;
44 OverlapElementLocator::OverlapElementLocator(const ParaFIELD *sourceField, const ParaFIELD *targetField,
45 const ProcessorGroup& group, double epsAbs)
46 : _local_source_field(sourceField),
47 _local_target_field(targetField),
48 _local_source_mesh(0),
49 _local_target_mesh(0),
50 _domain_bounding_boxes(0),
54 if(_local_source_field)
55 _local_source_mesh=_local_source_field->getSupport()->getCellMesh();
56 if(_local_target_field)
57 _local_target_mesh=_local_target_field->getSupport()->getCellMesh();
58 _comm=getCommunicator();
59 computeBoundingBoxesAndTodoList();
62 OverlapElementLocator::~OverlapElementLocator()
64 delete [] _domain_bounding_boxes;
67 const MPI_Comm *OverlapElementLocator::getCommunicator() const
69 const MPIProcessorGroup* group=static_cast<const MPIProcessorGroup*>(&_group);
70 return group->getComm();
73 void OverlapElementLocator::computeBoundingBoxesAndTodoList()
75 CommInterface comm_interface=_group.getCommInterface();
76 const MPIProcessorGroup* group=static_cast<const MPIProcessorGroup*> (&_group);
78 if(_local_source_mesh)
79 _local_space_dim=_local_source_mesh->getSpaceDimension();
81 _local_space_dim=_local_target_mesh->getSpaceDimension();
83 const MPI_Comm* comm = group->getComm();
84 int bbSize=2*2*_local_space_dim;//2 (for source/target) 2 (min/max)
85 _domain_bounding_boxes=new double[bbSize*_group.size()];
86 INTERP_KERNEL::AutoPtr<double> minmax=new double[bbSize];
87 //Format minmax : Xmin_src,Xmax_src,Ymin_src,Ymax_src,Zmin_src,Zmax_src,Xmin_trg,Xmax_trg,Ymin_trg,Ymax_trg,Zmin_trg,Zmax_trg
88 if(_local_source_mesh)
89 _local_source_mesh->getBoundingBox(minmax);
92 for(int i=0;i<_local_space_dim;i++)
94 minmax[i*2]=std::numeric_limits<double>::max();
95 minmax[i*2+1]=-std::numeric_limits<double>::max();
98 if(_local_target_mesh)
99 _local_target_mesh->getBoundingBox(minmax+2*_local_space_dim);
102 for(int i=0;i<_local_space_dim;i++)
104 minmax[i*2+2*_local_space_dim]=std::numeric_limits<double>::max();
105 minmax[i*2+1+2*_local_space_dim]=-std::numeric_limits<double>::max();
108 comm_interface.allGather(minmax, bbSize, MPI_DOUBLE,
109 _domain_bounding_boxes,bbSize, MPI_DOUBLE,
112 // Computation of all pairs needing an interpolation pairs are duplicated now !
114 _proc_pairs.clear();//first is source second is target
115 _proc_pairs.resize(_group.size());
116 for(int i=0;i<_group.size();i++)
117 for(int j=0;j<_group.size();j++)
119 if(intersectsBoundingBox(i,j))
121 _proc_pairs[i].push_back(j);
124 // OK now let's assigning as balanced as possible, job to each proc of group
125 std::vector< std::vector< ProcCouple > > pairsToBeDonePerProc(_group.size());
127 for(std::vector< std::vector< int > >::const_iterator it1=_proc_pairs.begin();it1!=_proc_pairs.end();it1++,i++)
128 for(std::vector< int >::const_iterator it2=(*it1).begin();it2!=(*it1).end();it2++)
130 if(pairsToBeDonePerProc[i].size()<=pairsToBeDonePerProc[*it2].size())//it includes the fact that i==*it2
131 pairsToBeDonePerProc[i].push_back(ProcCouple(i,*it2));
133 pairsToBeDonePerProc[*it2].push_back(ProcCouple(i,*it2));
135 //Keeping todo list of current proc. _to_do_list contains a set of pair where at least _group.myRank() appears once.
136 //This proc will be in charge to perform interpolation of any of element of '_to_do_list'
137 //If _group.myRank()==myPair.first, current proc should fetch target mesh of myPair.second (if different from _group.myRank()).
138 //If _group.myRank()==myPair.second, current proc should fetch source mesh of myPair.second.
140 int myProcId=_group.myRank();
141 _to_do_list=pairsToBeDonePerProc[myProcId];
144 std::stringstream scout;
145 scout << "(" << myProcId << ") my TODO list is: ";
146 for (std::vector< ProcCouple >::const_iterator itdbg=_to_do_list.begin(); itdbg!=_to_do_list.end(); itdbg++)
147 scout << "(" << (*itdbg).first << "," << (*itdbg).second << ")";
148 std::cout << scout.str() << "\n";
151 // Feeding now '_procs_to_send*'. A same id can appears twice. The second parameter in pair means what
152 // to send true=source, false=target
153 _procs_to_send_mesh.clear();
154 _procs_to_send_field.clear();
155 for(int i=_group.size()-1;i>=0;i--)
157 const std::vector< ProcCouple >& anRemoteProcToDoList=pairsToBeDonePerProc[i];
158 for(std::vector< ProcCouple >::const_iterator it=anRemoteProcToDoList.begin();it!=anRemoteProcToDoList.end();it++)
160 if((*it).first==myProcId)
163 _procs_to_send_mesh.push_back(Proc_SrcOrTgt(i,true));
164 _procs_to_send_field.push_back((*it).second);
166 if((*it).second==myProcId)
168 _procs_to_send_mesh.push_back(Proc_SrcOrTgt(i,false));
174 * The aim of this method is to perform the communication to get data corresponding to '_to_do_list' attribute.
175 * The principle is the following : if proc n1 and n2 need to perform a cross sending with n1<n2, then n1 will send first and receive then.
177 void OverlapElementLocator::exchangeMeshes(OverlapInterpolationMatrix& matrix)
179 int myProcId=_group.myRank();
180 //starting to receive every procs whose id is lower than myProcId.
181 std::vector< ProcCouple > toDoListForFetchRemaining;
182 for(std::vector< ProcCouple >::const_iterator it=_to_do_list.begin();it!=_to_do_list.end();it++)
184 int first = (*it).first, second = (*it).second;
190 receiveRemoteMeshFrom(second,false);
192 toDoListForFetchRemaining.push_back(ProcCouple(first,second));
195 {//(*it).second==myProcId
197 receiveRemoteMeshFrom(first,true);
199 toDoListForFetchRemaining.push_back(ProcCouple(first,second));
203 //sending source or target mesh to remote procs
204 for(std::vector< Proc_SrcOrTgt >::const_iterator it2=_procs_to_send_mesh.begin();it2!=_procs_to_send_mesh.end();it2++)
205 sendLocalMeshTo((*it2).first,(*it2).second,matrix);
206 //fetching remaining meshes
207 for(std::vector< ProcCouple >::const_iterator it=toDoListForFetchRemaining.begin();it!=toDoListForFetchRemaining.end();it++)
209 if((*it).first!=(*it).second)
211 if((*it).first==myProcId)
212 receiveRemoteMeshFrom((*it).second,false);
213 else//(*it).second==myProcId
214 receiveRemoteMeshFrom((*it).first,true);
219 std::string OverlapElementLocator::getSourceMethod() const
221 return _local_source_field->getField()->getDiscretization()->getStringRepr();
224 std::string OverlapElementLocator::getTargetMethod() const
226 return _local_target_field->getField()->getDiscretization()->getStringRepr();
229 const MEDCouplingPointSet *OverlapElementLocator::getSourceMesh(int procId) const
231 int myProcId=_group.myRank();
233 return _local_source_mesh;
234 Proc_SrcOrTgt p(procId,true);
235 std::map<Proc_SrcOrTgt, AutoMCPointSet >::const_iterator it=_remote_meshes.find(p);
239 const DataArrayInt *OverlapElementLocator::getSourceIds(int procId) const
241 int myProcId=_group.myRank();
244 Proc_SrcOrTgt p(procId,true);
245 std::map<Proc_SrcOrTgt, AutoDAInt >::const_iterator it=_remote_elems.find(p);
249 const MEDCouplingPointSet *OverlapElementLocator::getTargetMesh(int procId) const
251 int myProcId=_group.myRank();
253 return _local_target_mesh;
254 Proc_SrcOrTgt p(procId,false);
255 std::map<Proc_SrcOrTgt, AutoMCPointSet >::const_iterator it=_remote_meshes.find(p);
259 const DataArrayInt *OverlapElementLocator::getTargetIds(int procId) const
261 int myProcId=_group.myRank();
264 Proc_SrcOrTgt p(procId,false);
265 std::map<Proc_SrcOrTgt, AutoDAInt >::const_iterator it=_remote_elems.find(p);
269 bool OverlapElementLocator::isInMyTodoList(int i, int j) const
271 ProcCouple cpl = std::make_pair(i, j);
272 return std::find(_to_do_list.begin(), _to_do_list.end(), cpl)!=_to_do_list.end();
275 bool OverlapElementLocator::intersectsBoundingBox(int isource, int itarget) const
277 const double *source_bb=_domain_bounding_boxes+isource*2*2*_local_space_dim;
278 const double *target_bb=_domain_bounding_boxes+itarget*2*2*_local_space_dim+2*_local_space_dim;
280 for (int idim=0; idim < _local_space_dim; idim++)
282 bool intersects = (target_bb[idim*2]<source_bb[idim*2+1]+_epsAbs)
283 && (source_bb[idim*2]<target_bb[idim*2+1]+_epsAbs);
291 * This methods sends (part of) local source if 'sourceOrTarget'==True to proc 'procId'.
292 * This methods sends (part of) local target if 'sourceOrTarget'==False to proc 'procId'.
294 * This method prepares the matrix too, for matrix assembling and future matrix-vector computation.
296 void OverlapElementLocator::sendLocalMeshTo(int procId, bool sourceOrTarget, OverlapInterpolationMatrix& matrix) const
298 //int myProcId=_group.myRank();
299 const double *distant_bb=0;
300 MEDCouplingPointSet *local_mesh=0;
301 const ParaFIELD *field=0;
302 if(sourceOrTarget)//source for local mesh but target for distant mesh
304 distant_bb=_domain_bounding_boxes+procId*2*2*_local_space_dim+2*_local_space_dim;
305 local_mesh=_local_source_mesh;
306 field=_local_source_field;
308 else//target for local but source for distant
310 distant_bb=_domain_bounding_boxes+procId*2*2*_local_space_dim;
311 local_mesh=_local_target_mesh;
312 field=_local_target_field;
314 AutoDAInt elems=local_mesh->getCellsInBoundingBox(distant_bb,getBoundingBoxAdjustment());
315 DataArrayInt *old2new_map;
316 MEDCouplingPointSet *send_mesh=static_cast<MEDCouplingPointSet *>(field->getField()->buildSubMeshData(elems->begin(),elems->end(),old2new_map));
318 matrix.keepTracksOfSourceIds(procId,old2new_map);
320 matrix.keepTracksOfTargetIds(procId,old2new_map);
321 sendMesh(procId,send_mesh,old2new_map);
322 send_mesh->decrRef();
323 old2new_map->decrRef();
327 * This method recieves source remote mesh on proc 'procId' if sourceOrTarget==True
328 * This method recieves target remote mesh on proc 'procId' if sourceOrTarget==False
330 void OverlapElementLocator::receiveRemoteMeshFrom(int procId, bool sourceOrTarget)
332 DataArrayInt *old2new_map=0;
333 MEDCouplingPointSet *m=0;
334 receiveMesh(procId,m,old2new_map);
335 Proc_SrcOrTgt p(procId,sourceOrTarget);
337 _remote_elems[p]=old2new_map;
340 void OverlapElementLocator::sendMesh(int procId, const MEDCouplingPointSet *mesh, const DataArrayInt *idsToSend) const
342 CommInterface comInterface=_group.getCommInterface();
344 // First stage : exchanging sizes
345 vector<double> tinyInfoLocalD;//tinyInfoLocalD not used for the moment
346 vector<int> tinyInfoLocal;
347 vector<string> tinyInfoLocalS;
348 mesh->getTinySerializationInformation(tinyInfoLocalD,tinyInfoLocal,tinyInfoLocalS);
349 const MPI_Comm *comm=getCommunicator();
352 lgth[0]=tinyInfoLocal.size();
353 lgth[1]=idsToSend->getNbOfElems();
354 comInterface.send(&lgth,2,MPI_INT,procId,START_TAG_MESH_XCH,*_comm);
355 comInterface.send(&tinyInfoLocal[0],tinyInfoLocal.size(),MPI_INT,procId,START_TAG_MESH_XCH+1,*comm);
357 DataArrayInt *v1Local=0;
358 DataArrayDouble *v2Local=0;
359 mesh->serialize(v1Local,v2Local);
360 comInterface.send(v1Local->getPointer(),v1Local->getNbOfElems(),MPI_INT,procId,START_TAG_MESH_XCH+2,*comm);
361 comInterface.send(v2Local->getPointer(),v2Local->getNbOfElems(),MPI_DOUBLE,procId,START_TAG_MESH_XCH+3,*comm);
362 //finished for mesh, ids now
363 comInterface.send(const_cast<int *>(idsToSend->getConstPointer()),lgth[1],MPI_INT,procId,START_TAG_MESH_XCH+4,*comm);
369 void OverlapElementLocator::receiveMesh(int procId, MEDCouplingPointSet* &mesh, DataArrayInt *&ids) const
373 const MPI_Comm *comm=getCommunicator();
374 CommInterface comInterface=_group.getCommInterface();
375 comInterface.recv(lgth,2,MPI_INT,procId,START_TAG_MESH_XCH,*_comm,&status);
376 std::vector<int> tinyInfoDistant(lgth[0]);
377 ids=DataArrayInt::New();
378 ids->alloc(lgth[1],1);
379 comInterface.recv(&tinyInfoDistant[0],lgth[0],MPI_INT,procId,START_TAG_MESH_XCH+1,*comm,&status);
380 mesh=MEDCouplingPointSet::BuildInstanceFromMeshType((MEDCouplingMeshType)tinyInfoDistant[0]);
381 std::vector<std::string> unusedTinyDistantSts;
382 vector<double> tinyInfoDistantD(1);//tinyInfoDistantD not used for the moment
383 DataArrayInt *v1Distant=DataArrayInt::New();
384 DataArrayDouble *v2Distant=DataArrayDouble::New();
385 mesh->resizeForUnserialization(tinyInfoDistant,v1Distant,v2Distant,unusedTinyDistantSts);
386 comInterface.recv(v1Distant->getPointer(),v1Distant->getNbOfElems(),MPI_INT,procId,START_TAG_MESH_XCH+2,*comm,&status);
387 comInterface.recv(v2Distant->getPointer(),v2Distant->getNbOfElems(),MPI_DOUBLE,procId,START_TAG_MESH_XCH+3,*comm,&status);
388 mesh->unserialization(tinyInfoDistantD,tinyInfoDistant,v1Distant,v2Distant,unusedTinyDistantSts);
389 //finished for mesh, ids now
390 comInterface.recv(ids->getPointer(),lgth[1],MPI_INT,procId,1144,*comm,&status);
392 v1Distant->decrRef();
393 v2Distant->decrRef();