Salome HOME
06a06d663b422847d39e954f0b6aeb38865b99c3
[tools/medcoupling.git] / src / ParaMEDMEM / OverlapDEC.cxx
1 // Copyright (C) 2007-2012  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 #include "OverlapDEC.hxx"
21 #include "CommInterface.hxx"
22 #include "ParaFIELD.hxx"
23 #include "MPIProcessorGroup.hxx"
24 #include "OverlapElementLocator.hxx"
25 #include "OverlapInterpolationMatrix.hxx"
26 /*!
27     \defgroup overlapdec OverlapDEC
28     The \c OverlapDEC enables the \ref InterpKerRemapGlobal "conservative remapping" of fields between two parallel codes. This remapping is based on the computation of intersection volumes on a \b same \b processor \b group. On this processor group are defined two field-templates called A and B. The computation is possible for 3D meshes, 2D meshes, 3D-surface meshes, 1D meshes and 2D-curve meshes. Dimensions must be similar for the distribution templates A and B.
29     The main difference with \ref interpkerneldec is that this \ref dec manages 2 field templates on each processor of the processor group (A and B) called source and target.
30     Furthermore all processors in processor group cooperates in global interpolation matrix computation. In this respect \ref InterpKernelIDEC is a specialization of \c OverlapDEC.
31
32     \section ParaMEDMEMOverlapDECAlgorithmDescription Algorithm Description
33
34     Let's consider the following use case that is ran in ParaMEDMEMTest_OverlapDEC.cxx to describes the different steps of the computation. The processor group contains 3 processors.
35     \anchor ParaMEDMEMOverlapDECImgTest1
36     \image html OverlapDEC1.png "Example showing the use case in order to explain the different steps."
37
38     \subsection ParaMEDMEMOverlapDECAlgoStep1 Step 1 : Bounding box exchange and global interaction between procs computation.
39
40     In order to reduce as much as possible the amount of communications between distant processors, every processor computes a bounding box for A and B. Then a AllToAll communication is performed so that
41     every processor can compute the \b global interactions between processor.
42     This computation leads every processor to compute the same global TODO list expressed as a list of pair. A pair (x,y) means that proc \b x fieldtemplate A can interact with fieltemplate B of proc \b y because the two bounding boxes interact.
43     In the \ref ParaMEDMEMOverlapDECImgTest1 "example above" this computation leads to the following a \b global TODO list :
44
45     \b (0,0),(0,1),(1,0),(1,2),(2,0),(2,1),(2,2)
46
47     Here the pair (0,2) does not appear because the bounding box of fieldtemplateA of proc#2 does not intersect that of fieldtemplate B on proc#0.
48
49     Stage performed by ParaMEDMEM::OverlapElementLocator::computeBoundingBoxes.
50
51     \subsection ParaMEDMEMOverlapDECAlgoStep2 Step 2 : Computation of local TODO list
52
53     Starting from the global interaction previously computed in \ref ParaMEDMEMOverlapDECAlgoStep1 "Step 1", each proc computes the TODO list per proc.
54     The following rules is chosen : a pair (x,y) can be treated by either proc #x or proc #y, in order to reduce the amount of data transfert among
55     processors. The algorithm chosen for load balancing is the following : Each processor has an empty \b local TODO list at the beginning. Then for each pair (k,m) in
56     \b global TODO list, if proc#k has less temporary local list than proc#m pair, (k,m) is added to temparary local TODO list of proc#k.
57     If proc#m has less temporary local TODO list than proc#k pair, (k,m) is added to temporary local TODO list of proc#m.
58     If proc#k and proc#m have the same amount of temporary local TODO list pair, (k,m) is added to temporary local TODO list of proc#k.
59
60     In the \ref ParaMEDMEMOverlapDECImgTest1 "example above" this computation leads to the following local TODO list :
61
62     - proc#0 : (0,0)
63     - proc#1 : (0,1),(1,0)
64     - proc#2 : (1,2),(2,0),(2,1),(2,2)
65     
66     The algorithm described here is not perfect for this use case, we hope to enhance it soon.
67
68     At this stage each proc knows precisely its \b local TODO list (with regard to interpolation). The \b local TODO list of other procs than local
69     is kept for future computations.
70
71     \subsection ParaMEDMEMOverlapDECAlgoStep3 Step 3 : Matrix echange between procs
72
73     Knowing the \b local TODO list, the aim now is to exchange field-templates between procs. Each proc computes knowing TODO list per
74     proc computed in \ref ParaMEDMEMOverlapDECAlgoStep2 "Step 2" the exchange TODO list :
75
76     In the \ref ParaMEDMEMOverlapDECImgTest1 "example above" the exchange TODO list gives the following results :
77
78     Sending TODO list per proc :
79
80     - proc #0 : Send fieldtemplate A to Proc#1, Send fieldtemplate B to Proc#1, Send fieldtemplate B to Proc#2
81     - Proc #1 : Send fieldtemplate A to Proc#2, Send fieldtemplate B to Proc#2
82     - Proc #2 : No send.
83
84     Receiving TODO list per proc :
85
86     - proc #0 : No receiving
87     - proc #1 : receiving fieldtemplate A from Proc#0,  receiving fieldtemplate B from Proc#0
88     - proc #2 : receiving fieldtemplate B from Proc#0, receiving fieldtemplate A from Proc#1,  receiving fieldtemplate B from Proc#1
89
90     To avoid as much as possible large volumes of transfers between procs, only relevant parts of meshes are sent. In order for proc#k to send fieldtemplate A to fieldtemplate B
91     of proc #m., proc#k computes the part of mesh A contained in the boundingbox B of proc#m. It implies that the corresponding cellIds or nodeIds of the
92     corresponding part are sent to proc #m too.
93
94     Let's consider the couple (k,m) in the TODO list. This couple is treated by either k or m as seen in \ref ParaMEDMEMOverlapDECAlgoStep2 "here in Step2".
95
96     As will be dealt in Step 6, for final matrix-vector computations, the resulting matrix of the couple (k,m) whereever it is computed (proc #k or proc #m)
97     will be stored in \b proc#m.
98
99     - If proc #k is in charge (performs the matrix computation) for this couple (k,m), target ids (cells or nodes) of the mesh in proc #m are renumbered, because proc #m has seelected a sub mesh of the target mesh to avoid large amounts of data to transfer. In this case as proc #m is ultimately in charge of the matrix, proc #k must keep preciously the
100     source ids needed to be sent to proc#m. No problem will appear for matrix assembling in proc m for source ids because no restriction was done.
101     Concerning source ids to be sent for the matrix-vector computation, proc k will know precisely which source ids field values to send to proc #m.
102     This is embodied by OverlapMapping::keepTracksOfTargetIds in proc m.
103
104     - If proc #m is in charge (performs matrix computation) for this couple (k,m), source ids (cells or nodes) of the mesh in proc #k are renumbered, because proc #k has selected a sub mesh of the source mesh to avoid large amounts of data to transfer. In this case as proc #k is ultimately in charge of the matrix, proc #m receives the source ids
105     from remote proc #k, and thus the matrix is directly correct, no need for renumbering as in \ref ParaMEDMEMOverlapDECAlgoStep5 "Step 5". However proc #k must
106     keep track of the ids sent to proc #m for te matrix-vector computation.
107     This is incarnated by OverlapMapping::keepTracksOfSourceIds in proc k.
108
109     This step is performed in ParaMEDMEM::OverlapElementLocator::exchangeMeshes method.
110
111     \subsection ParaMEDMEMOverlapDECAlgoStep4 Step 4 : Computation of the interpolation matrix
112
113     After mesh exchange in \ref ParaMEDMEMOverlapDECAlgoStep3 "Step3" each processor has all the required information to treat its \b local TODO list computed in
114     \ref ParaMEDMEMOverlapDECAlgoStep2 "Step2". This step is potentially CPU costly, which is why the \b local TODO list per proc is expected to
115     be as well balanced as possible.
116
117     The interpolation is performed as \ref ParaMEDMEM::MEDCouplingRemapper "Remapper" does.
118
119     This operation is performed by OverlapInterpolationMatrix::addContribution method.
120
121     \subsection ParaMEDMEMOverlapDECAlgoStep5 Step 5 : Global matrix construction.
122     
123     After having performed the TODO list at the end of \ref ParaMEDMEMOverlapDECAlgoStep4 "Step4" we need to assemble the final matrix.
124     
125     The final aim is to have a distributed matrix \f$ M_k \f$ on each proc#k. In order to reduce data exchange during the matrix product process,
126     \f$ M_k \f$ is built using sizeof(Proc group) \c std::vector< \c std::map<int,double> \c >.
127
128     For a proc#k, it is necessary to fetch info of all matrices built in \ref ParaMEDMEMOverlapDECAlgoStep4 "Step4" where the first element in pair (i,j)
129     is equal to k.
130
131     After this step, the matrix repartition is the following after a call to ParaMEDMEM::OverlapMapping::prepare :
132
133     - proc#0 : (0,0),(1,0),(2,0)
134     - proc#1 : (0,1),(2,1)
135     - proc#2 : (1,2),(2,2)
136
137     Tuple (2,1) computed on proc 2 is stored in proc 1 after execution of the function "prepare". This is an example of item 0 in \ref ParaMEDMEMOverlapDECAlgoStep2 "Step2".
138     Tuple (0,1) computed on proc 1 is stored in proc 1 too. This is an example of item 1 in \ref ParaMEDMEMOverlapDECAlgoStep2 "Step2".
139
140     In the end ParaMEDMEM::OverlapMapping::_proc_ids_to_send_vector_st will contain :
141
142     - Proc#0 : 0,1
143     - Proc#1 : 0,2
144     - Proc#2 : 0,1,2
145
146     In the end ParaMEDMEM::OverlapMapping::_proc_ids_to_recv_vector_st will contain :
147
148     - Proc#0 : 0,1,2
149     - Proc#1 : 0,2
150     - Proc#2 : 1,2
151
152     The method in charge to perform this is : ParaMEDMEM::OverlapMapping::prepare.
153 */
154 namespace ParaMEDMEM
155 {
156   OverlapDEC::OverlapDEC(const std::set<int>& procIds, const MPI_Comm& world_comm):_own_group(true),_interpolation_matrix(0),
157                                                                                    _source_field(0),_own_source_field(false),
158                                                                                    _target_field(0),_own_target_field(false)
159   {
160     ParaMEDMEM::CommInterface comm;
161     int *ranks_world=new int[procIds.size()]; // ranks of sources and targets in world_comm
162     std::copy(procIds.begin(),procIds.end(),ranks_world);
163     MPI_Group group,world_group;
164     comm.commGroup(world_comm,&world_group);
165     comm.groupIncl(world_group,procIds.size(),ranks_world,&group);
166     delete [] ranks_world;
167     MPI_Comm theComm;
168     comm.commCreate(world_comm,group,&theComm);
169     comm.groupFree(&group);
170     if(theComm==MPI_COMM_NULL)
171       {
172         _group=0;
173         return ;
174       }
175     std::set<int> idsUnion;
176     for(std::size_t i=0;i<procIds.size();i++)
177       idsUnion.insert(i);
178     _group=new MPIProcessorGroup(comm,idsUnion,theComm);
179   }
180
181   OverlapDEC::~OverlapDEC()
182   {
183     if(_own_group)
184       delete _group;
185     if(_own_source_field)
186       delete _source_field;
187     if(_own_target_field)
188       delete _target_field;
189     delete _interpolation_matrix;
190   }
191
192   void OverlapDEC::sendRecvData(bool way)
193   {
194     if(way)
195       sendData();
196     else
197       recvData();
198   }
199
200   void OverlapDEC::sendData()
201   {
202     _interpolation_matrix->multiply();
203   }
204
205   void OverlapDEC::recvData()
206   {
207     throw INTERP_KERNEL::Exception("Not implemented yet !!!!");
208     //_interpolation_matrix->transposeMultiply();
209   }
210   
211   void OverlapDEC::synchronize()
212   {
213     if(!isInGroup())
214       return ;
215     delete _interpolation_matrix;
216     _interpolation_matrix=new OverlapInterpolationMatrix(_source_field,_target_field,*_group,*this,*this);
217     OverlapElementLocator locator(_source_field,_target_field,*_group);
218     locator.copyOptions(*this);
219     locator.exchangeMeshes(*_interpolation_matrix);
220     std::vector< std::pair<int,int> > jobs=locator.getToDoList();
221     std::string srcMeth=locator.getSourceMethod();
222     std::string trgMeth=locator.getTargetMethod();
223     for(std::vector< std::pair<int,int> >::const_iterator it=jobs.begin();it!=jobs.end();it++)
224       {
225         const MEDCouplingPointSet *src=locator.getSourceMesh((*it).first);
226         const DataArrayInt *srcIds=locator.getSourceIds((*it).first);
227         const MEDCouplingPointSet *trg=locator.getTargetMesh((*it).second);
228         const DataArrayInt *trgIds=locator.getTargetIds((*it).second);
229         _interpolation_matrix->addContribution(src,srcIds,srcMeth,(*it).first,trg,trgIds,trgMeth,(*it).second);
230       }
231     _interpolation_matrix->prepare(locator.getProcsInInteraction());
232     _interpolation_matrix->computeDeno();
233   }
234
235   void OverlapDEC::attachSourceLocalField(ParaFIELD *field, bool ownPt)
236   {
237     if(!isInGroup())
238       return ;
239     if(_own_source_field)
240       delete _source_field;
241     _source_field=field;
242     _own_source_field=ownPt;
243   }
244
245   void OverlapDEC::attachTargetLocalField(ParaFIELD *field, bool ownPt)
246   {
247     if(!isInGroup())
248       return ;
249     if(_own_target_field)
250       delete _target_field;
251     _target_field=field;
252     _own_target_field=ownPt;
253   }
254
255   bool OverlapDEC::isInGroup() const
256   {
257     if(!_group)
258       return false;
259     return _group->containsMyRank();
260   }
261 }