1 // Copyright (C) 2007-2020 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, int workSharingAlgo)
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();
60 computeBoundingBoxesAndInteractionList();
61 switch(workSharingAlgo)
64 computeTodoList_original(); break;
66 computeTodoList_new(false); break;
68 computeTodoList_new(true); break;
70 throw INTERP_KERNEL::Exception("OverlapElementLocator::OverlapElementLocator(): invalid algorithm selected!");
76 OverlapElementLocator::~OverlapElementLocator()
78 delete [] _domain_bounding_boxes;
81 const MPI_Comm *OverlapElementLocator::getCommunicator() const
83 const MPIProcessorGroup* group=static_cast<const MPIProcessorGroup*>(&_group);
84 return group->getComm();
87 void OverlapElementLocator::computeBoundingBoxesAndInteractionList()
89 CommInterface comm_interface=_group.getCommInterface();
90 const MPIProcessorGroup* group=static_cast<const MPIProcessorGroup*> (&_group);
92 if(_local_source_mesh)
93 _local_space_dim=_local_source_mesh->getSpaceDimension();
95 _local_space_dim=_local_target_mesh->getSpaceDimension();
97 const MPI_Comm* comm = group->getComm();
98 int bbSize=2*2*_local_space_dim;//2 (for source/target) 2 (min/max)
99 _domain_bounding_boxes=new double[bbSize*_group.size()];
100 INTERP_KERNEL::AutoPtr<double> minmax=new double[bbSize];
101 //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
102 if(_local_source_mesh)
103 _local_source_mesh->getBoundingBox(minmax);
106 for(int i=0;i<_local_space_dim;i++)
108 minmax[i*2]=std::numeric_limits<double>::max();
109 minmax[i*2+1]=-std::numeric_limits<double>::max();
112 if(_local_target_mesh)
113 _local_target_mesh->getBoundingBox(minmax+2*_local_space_dim);
116 for(int i=0;i<_local_space_dim;i++)
118 minmax[i*2+2*_local_space_dim]=std::numeric_limits<double>::max();
119 minmax[i*2+1+2*_local_space_dim]=-std::numeric_limits<double>::max();
122 comm_interface.allGather(minmax, bbSize, MPI_DOUBLE,
123 _domain_bounding_boxes,bbSize, MPI_DOUBLE,
126 // Computation of all pairs needing an interpolation pairs are duplicated now !
128 _proc_pairs.clear();//first is source second is target
129 _proc_pairs.resize(_group.size());
130 for(int i=0;i<_group.size();i++)
131 for(int j=0;j<_group.size();j++)
132 if(intersectsBoundingBox(i,j))
133 _proc_pairs[i].push_back(j);
136 void OverlapElementLocator::computeTodoList_original()
138 // OK now let's assigning as balanced as possible, job to each proc of group
139 _all_todo_lists.resize(_group.size());
141 for(std::vector< std::vector< int > >::const_iterator it1=_proc_pairs.begin();it1!=_proc_pairs.end();it1++,i++)
142 for(std::vector< int >::const_iterator it2=(*it1).begin();it2!=(*it1).end();it2++)
144 if(_all_todo_lists[i].size()<=_all_todo_lists[*it2].size())//it includes the fact that i==*it2
145 _all_todo_lists[i].push_back(ProcCouple(i,*it2));
147 _all_todo_lists[*it2].push_back(ProcCouple(i,*it2));
149 //Keeping todo list of current proc. _to_do_list contains a set of pair where at least _group.myRank() appears once.
150 //This proc will be in charge to perform interpolation of any of element of '_to_do_list'
151 //If _group.myRank()==myPair.first, current proc should fetch target mesh of myPair.second (if different from _group.myRank()).
152 //If _group.myRank()==myPair.second, current proc should fetch source mesh of myPair.second.
154 int myProcId=_group.myRank();
155 _to_do_list=_all_todo_lists[myProcId];
158 std::stringstream scout;
159 scout << "(" << myProcId << ") my TODO list is: ";
160 for (std::vector< ProcCouple >::const_iterator itdbg=_to_do_list.begin(); itdbg!=_to_do_list.end(); itdbg++)
161 scout << "(" << (*itdbg).first << "," << (*itdbg).second << ")";
162 std::cout << scout.str() << "\n";
166 /* More efficient (?) work sharing algorithm: a job (i,j) is initially assigned twice: to proc#i and to proc#j.
167 * Then try to reduce as much as possible the variance of the num of jobs per proc by selecting the right duplicate
169 * - take the most loaded proc i,
170 * + select the job (i,j) for which proc#j is the less loaded
171 * + remove this job from proc#i, and mark it as 'unremovable' from proc#j
172 * - repeat until no more duplicates are found
174 void OverlapElementLocator::computeTodoList_new(bool revertIter)
177 int infinity = std::numeric_limits<int>::max();
179 int grp_size = _group.size();
180 vector < map<ProcCouple, int> > full_set(grp_size );
182 for(vector< vector< int > >::const_iterator it = _proc_pairs.begin(); it != _proc_pairs.end(); it++, srcProcID++)
183 for (vector< int >::const_iterator it2=(*it).begin(); it2 != (*it).end(); it2++)
185 // Here a pair of the form (i,i) is added only once!
186 int tgtProcID = *it2;
187 ProcCouple cpl = make_pair(srcProcID, tgtProcID);
188 full_set[srcProcID][cpl] = -1;
189 full_set[tgtProcID][cpl] = -1;
192 vector < map<ProcCouple, int> > ::iterator itVector;
193 map<ProcCouple, int>::iterator itMap;
194 for(itVector = full_set.begin(); itVector != full_set.end(); itVector++, procID++)
195 for (itMap=(*itVector).begin(); itMap != (*itVector).end(); itMap++)
197 const ProcCouple & cpl = (*itMap).first;
198 if (cpl.first == cpl.second)
199 // special case - this couple can not be removed in the future
200 (*itMap).second = infinity;
203 if(cpl.first == procID)
204 (*itMap).second = (int)full_set[cpl.second].size();
205 else // cpl.second == srcProcID
206 (*itMap).second = (int)full_set[cpl.first].size();
209 INTERP_KERNEL::AutoPtr<bool> proc_valid = new bool[grp_size];
210 fill((bool *)proc_valid, proc_valid+grp_size, true);
213 while (find((bool *)proc_valid, proc_valid+grp_size, true) != proc_valid+grp_size)
216 int max_sz = -1, max_id = -1;
217 for(itVector = full_set.begin(), procID=0; itVector != full_set.end(); itVector++, procID++)
219 int sz = (int)(*itVector).size();
220 if (proc_valid[procID] && sz > max_sz)
227 // Nothing more to do:
230 // For this proc, job with less loaded second proc:
231 int min_sz = infinity;
232 map<ProcCouple, int> & max_map = full_set[max_id];
233 ProcCouple hit_cpl = make_pair(-1,-1);
236 // Use a reverse iterator here increases our chances to hit a couple of the form (i, myProcId)
237 // meaning that the final matrix computed won't have to be sent: save some comm.
238 map<ProcCouple, int> ::const_reverse_iterator ritMap;
239 for(ritMap=max_map.rbegin(); ritMap != max_map.rend(); ritMap++)
240 if ((*ritMap).second < min_sz)
241 hit_cpl = (*ritMap).first;
245 for(itMap=max_map.begin(); itMap != max_map.end(); itMap++)
246 if ((*itMap).second < min_sz)
247 hit_cpl = (*itMap).first;
249 if (hit_cpl.first == -1)
251 // Plouf. Current proc 'max_id' can not be reduced. Invalid it:
252 proc_valid[max_id] = false;
255 // Remove item from proc 'max_id'
256 full_set[max_id].erase(hit_cpl);
257 // And mark it as not removable on the other side:
258 if (hit_cpl.first == max_id)
259 full_set[hit_cpl.second][hit_cpl] = infinity;
260 else // hit_cpl.second == max_id
261 full_set[hit_cpl.first][hit_cpl] = infinity;
263 // Now update all counts of valid maps:
265 for(itVector = full_set.begin(); itVector != full_set.end(); itVector++, procID++)
266 if(proc_valid[procID] && procID != max_id)
267 for(itMap = (*itVector).begin(); itMap != (*itVector).end(); itMap++)
269 const ProcCouple & cpl = (*itMap).first;
271 if ((*itMap).second == infinity)
273 if (cpl.first == max_id || cpl.second == max_id)
277 // Final formatting - extract remaining keys in each map:
278 int myProcId=_group.myRank();
279 _all_todo_lists.resize(grp_size);
281 for(itVector = full_set.begin(); itVector != full_set.end(); itVector++, procID++)
282 for(itMap = (*itVector).begin(); itMap != (*itVector).end(); itMap++)
283 _all_todo_lists[procID].push_back((*itMap).first);
284 _to_do_list=_all_todo_lists[myProcId];
287 std::stringstream scout;
288 scout << "(" << myProcId << ") my TODO list is: ";
289 for (std::vector< ProcCouple >::const_iterator itdbg=_to_do_list.begin(); itdbg!=_to_do_list.end(); itdbg++)
290 scout << "(" << (*itdbg).first << "," << (*itdbg).second << ")";
291 std::cout << scout.str() << "\n";
295 void OverlapElementLocator::debugPrintWorkSharing(std::ostream & ostr) const
297 std::vector< std::vector< ProcCouple > >::const_iterator it = _all_todo_lists.begin();
298 ostr << "TODO list lengths: ";
299 for(; it != _all_todo_lists.end(); ++it)
300 ostr << (*it).size() << " ";
304 void OverlapElementLocator::fillProcToSend()
306 // Feeding now '_procs_to_send*'. A same id can appears twice. The second parameter in pair means what
307 // to send true=source, false=target
308 int myProcId=_group.myRank();
309 _procs_to_send_mesh.clear();
310 _procs_to_send_field.clear();
311 for(int i=_group.size()-1;i>=0;i--)
313 const std::vector< ProcCouple >& anRemoteProcToDoList=_all_todo_lists[i];
314 for(std::vector< ProcCouple >::const_iterator it=anRemoteProcToDoList.begin();it!=anRemoteProcToDoList.end();it++)
316 if((*it).first==myProcId)
319 _procs_to_send_mesh.push_back(Proc_SrcOrTgt(i,true));
320 _procs_to_send_field.push_back((*it).second);
322 if((*it).second==myProcId)
324 _procs_to_send_mesh.push_back(Proc_SrcOrTgt(i,false));
331 * The aim of this method is to perform the communication to get data corresponding to '_to_do_list' attribute.
332 * 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.
334 void OverlapElementLocator::exchangeMeshes(OverlapInterpolationMatrix& matrix)
336 int myProcId=_group.myRank();
337 //starting to receive every procs whose id is lower than myProcId.
338 std::vector< ProcCouple > toDoListForFetchRemaining;
339 for(std::vector< ProcCouple >::const_iterator it=_to_do_list.begin();it!=_to_do_list.end();it++)
341 int first = (*it).first, second = (*it).second;
347 receiveRemoteMeshFrom(second,false);
349 toDoListForFetchRemaining.push_back(ProcCouple(first,second));
352 {//(*it).second==myProcId
354 receiveRemoteMeshFrom(first,true);
356 toDoListForFetchRemaining.push_back(ProcCouple(first,second));
360 //sending source or target mesh to remote procs
361 for(std::vector< Proc_SrcOrTgt >::const_iterator it2=_procs_to_send_mesh.begin();it2!=_procs_to_send_mesh.end();it2++)
362 sendLocalMeshTo((*it2).first,(*it2).second,matrix);
363 //fetching remaining meshes
364 for(std::vector< ProcCouple >::const_iterator it=toDoListForFetchRemaining.begin();it!=toDoListForFetchRemaining.end();it++)
366 if((*it).first!=(*it).second)
368 if((*it).first==myProcId)
369 receiveRemoteMeshFrom((*it).second,false);
370 else//(*it).second==myProcId
371 receiveRemoteMeshFrom((*it).first,true);
376 std::string OverlapElementLocator::getSourceMethod() const
378 return _local_source_field->getField()->getDiscretization()->getStringRepr();
381 std::string OverlapElementLocator::getTargetMethod() const
383 return _local_target_field->getField()->getDiscretization()->getStringRepr();
386 const MEDCouplingPointSet *OverlapElementLocator::getSourceMesh(int procId) const
388 int myProcId=_group.myRank();
390 return _local_source_mesh;
391 Proc_SrcOrTgt p(procId,true);
392 std::map<Proc_SrcOrTgt, AutoMCPointSet >::const_iterator it=_remote_meshes.find(p);
396 const DataArrayIdType *OverlapElementLocator::getSourceIds(int procId) const
398 int myProcId=_group.myRank();
401 Proc_SrcOrTgt p(procId,true);
402 std::map<Proc_SrcOrTgt, AutoDAInt >::const_iterator it=_remote_elems.find(p);
406 const MEDCouplingPointSet *OverlapElementLocator::getTargetMesh(int procId) const
408 int myProcId=_group.myRank();
410 return _local_target_mesh;
411 Proc_SrcOrTgt p(procId,false);
412 std::map<Proc_SrcOrTgt, AutoMCPointSet >::const_iterator it=_remote_meshes.find(p);
416 const DataArrayIdType *OverlapElementLocator::getTargetIds(int procId) const
418 int myProcId=_group.myRank();
421 Proc_SrcOrTgt p(procId,false);
422 std::map<Proc_SrcOrTgt, AutoDAInt >::const_iterator it=_remote_elems.find(p);
426 bool OverlapElementLocator::isInMyTodoList(int i, int j) const
428 ProcCouple cpl = std::make_pair(i, j);
429 return std::find(_to_do_list.begin(), _to_do_list.end(), cpl)!=_to_do_list.end();
432 bool OverlapElementLocator::intersectsBoundingBox(int isource, int itarget) const
434 const double *source_bb=_domain_bounding_boxes+isource*2*2*_local_space_dim;
435 const double *target_bb=_domain_bounding_boxes+itarget*2*2*_local_space_dim+2*_local_space_dim;
437 for (int idim=0; idim < _local_space_dim; idim++)
439 bool intersects = (target_bb[idim*2]<source_bb[idim*2+1]+_epsAbs)
440 && (source_bb[idim*2]<target_bb[idim*2+1]+_epsAbs);
448 * This methods sends (part of) local source if 'sourceOrTarget'==True to proc 'procId'.
449 * This methods sends (part of) local target if 'sourceOrTarget'==False to proc 'procId'.
451 * This method prepares the matrix too, for matrix assembling and future matrix-vector computation.
453 void OverlapElementLocator::sendLocalMeshTo(int procId, bool sourceOrTarget, OverlapInterpolationMatrix& matrix) const
455 //int myProcId=_group.myRank();
456 const double *distant_bb=0;
457 MEDCouplingPointSet *local_mesh=0;
458 const ParaFIELD *field=0;
459 if(sourceOrTarget)//source for local mesh but target for distant mesh
461 distant_bb=_domain_bounding_boxes+procId*2*2*_local_space_dim+2*_local_space_dim;
462 local_mesh=_local_source_mesh;
463 field=_local_source_field;
465 else//target for local but source for distant
467 distant_bb=_domain_bounding_boxes+procId*2*2*_local_space_dim;
468 local_mesh=_local_target_mesh;
469 field=_local_target_field;
471 AutoDAInt elems=local_mesh->getCellsInBoundingBox(distant_bb,getBoundingBoxAdjustment());
472 DataArrayIdType *old2new_map;
473 MEDCouplingPointSet *send_mesh=static_cast<MEDCouplingPointSet *>(field->getField()->buildSubMeshData(elems->begin(),elems->end(),old2new_map));
475 matrix.keepTracksOfSourceIds(procId,old2new_map);
477 matrix.keepTracksOfTargetIds(procId,old2new_map);
478 sendMesh(procId,send_mesh,old2new_map);
479 send_mesh->decrRef();
480 old2new_map->decrRef();
484 * This method receives source remote mesh on proc 'procId' if sourceOrTarget==True
485 * This method receives target remote mesh on proc 'procId' if sourceOrTarget==False
487 void OverlapElementLocator::receiveRemoteMeshFrom(int procId, bool sourceOrTarget)
489 DataArrayIdType *old2new_map=0;
490 MEDCouplingPointSet *m=0;
491 receiveMesh(procId,m,old2new_map);
492 Proc_SrcOrTgt p(procId,sourceOrTarget);
494 _remote_elems[p]=old2new_map;
497 void OverlapElementLocator::sendMesh(int procId, const MEDCouplingPointSet *mesh, const DataArrayIdType *idsToSend) const
499 CommInterface comInterface=_group.getCommInterface();
501 // First stage : exchanging sizes
502 vector<double> tinyInfoLocalD;//tinyInfoLocalD not used for the moment
503 vector<mcIdType> tinyInfoLocal;
504 vector<string> tinyInfoLocalS;
505 mesh->getTinySerializationInformation(tinyInfoLocalD,tinyInfoLocal,tinyInfoLocalS);
506 const MPI_Comm *comm=getCommunicator();
509 lgth[0]=ToIdType(tinyInfoLocal.size());
510 lgth[1]=idsToSend->getNbOfElems();
511 comInterface.send(&lgth,2,MPI_ID_TYPE,procId,START_TAG_MESH_XCH,*_comm);
512 comInterface.send(&tinyInfoLocal[0],(int)tinyInfoLocal.size(),MPI_ID_TYPE,procId,START_TAG_MESH_XCH+1,*comm);
514 DataArrayIdType *v1Local=0;
515 DataArrayDouble *v2Local=0;
516 mesh->serialize(v1Local,v2Local);
517 comInterface.send(v1Local->getPointer(),(int)v1Local->getNbOfElems(),MPI_ID_TYPE,procId,START_TAG_MESH_XCH+2,*comm);
518 comInterface.send(v2Local->getPointer(),(int)v2Local->getNbOfElems(),MPI_DOUBLE,procId,START_TAG_MESH_XCH+3,*comm);
519 //finished for mesh, ids now
520 comInterface.send(const_cast<mcIdType *>(idsToSend->getConstPointer()),(int)lgth[1],MPI_ID_TYPE,procId,START_TAG_MESH_XCH+4,*comm);
526 void OverlapElementLocator::receiveMesh(int procId, MEDCouplingPointSet* &mesh, DataArrayIdType *&ids) const
530 const MPI_Comm *comm=getCommunicator();
531 CommInterface comInterface=_group.getCommInterface();
532 comInterface.recv(lgth,2,MPI_ID_TYPE,procId,START_TAG_MESH_XCH,*_comm,&status);
533 std::vector<mcIdType> tinyInfoDistant(lgth[0]);
534 ids=DataArrayIdType::New();
535 ids->alloc(lgth[1],1);
536 comInterface.recv(&tinyInfoDistant[0],(int)lgth[0],MPI_ID_TYPE,procId,START_TAG_MESH_XCH+1,*comm,&status);
537 mesh=MEDCouplingPointSet::BuildInstanceFromMeshType((MEDCouplingMeshType)tinyInfoDistant[0]);
538 std::vector<std::string> unusedTinyDistantSts;
539 vector<double> tinyInfoDistantD(1);//tinyInfoDistantD not used for the moment
540 DataArrayIdType *v1Distant=DataArrayIdType::New();
541 DataArrayDouble *v2Distant=DataArrayDouble::New();
542 mesh->resizeForUnserialization(tinyInfoDistant,v1Distant,v2Distant,unusedTinyDistantSts);
543 comInterface.recv(v1Distant->getPointer(),(int)v1Distant->getNbOfElems(),MPI_ID_TYPE,procId,START_TAG_MESH_XCH+2,*comm,&status);
544 comInterface.recv(v2Distant->getPointer(),(int)v2Distant->getNbOfElems(),MPI_DOUBLE,procId,START_TAG_MESH_XCH+3,*comm,&status);
545 mesh->unserialization(tinyInfoDistantD,tinyInfoDistant,v1Distant,v2Distant,unusedTinyDistantSts);
546 //finished for mesh, ids now
547 comInterface.recv(ids->getPointer(),(int)lgth[1],MPI_ID_TYPE,procId,1144,*comm,&status);
549 v1Distant->decrRef();
550 v2Distant->decrRef();