File: CheckOrientation.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (410 lines) | stat: -rw-r--r-- 11,717 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Cecile Dobrzynski, Jean-Francois Remacle, Gaetan Compere
// -------------------------------------------------------------------

#ifdef PARALLEL
#include <cassert>

#include "mpi.h"
#include "autopack.h"

#include "MeshDataBase.h"
#include "MeshDataBaseParallelInterface.h"
#include "CheckOrientation.h"

namespace MAd {
  
  struct edge_comm
  {
    pVertex p1,p2;                //pointer in dest
    int     sendID;       
  };
  
  struct face_comm {
    pVertex pdest[3];
  };  

  int CheckEdgesOrientation(pMesh mesh){
#ifdef DEBUG
    int norient = 0;
#endif
    int nproc,myrank;
    MPI_Comm_size(MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

    int *sendcounts = new int[nproc];
    for(int i=0;i<nproc;i++)sendcounts[i]=0;

    pMeshDataId tagData   = MD_lookupMeshDataId("RemotePoint");
    EIter eit = M_edgeIter(mesh);
    pEdge pedge;  
    while ((pedge = EIter_next(eit))) {
      pVertex p[2];
      p[0] = E_vertex(pedge,0);
      p[1] = E_vertex(pedge,1);
      int tmp1;
      int isInterface1 = EN_getDataInt((pEntity) p[0] ,tagData, &tmp1);
      int tmp2;
      int isInterface2 = EN_getDataInt((pEntity) p[1] ,tagData, &tmp2);
      if(!isInterface1 || !isInterface2) continue;
      const std::vector<std::pair<int , pVertex> > *recup1 = (std::vector<std::pair<int , pVertex> > *) tmp1;
      const std::vector<std::pair<int , pVertex> > *recup2 = (std::vector<std::pair<int , pVertex> > *) tmp2;
      int size1 = (*recup1).size();
      int size2 = (*recup2).size();
      assert(size1);assert(size2);
      int *tab=new int[size1];
      for(int i=0 ; i< size1 ; i++) tab[i] = (*recup1)[i].first;
      //check if myrank must send
      int nSender = myrank;
      for(int j=0 ; j< size2 ; j++) {    
        int iProc = (*recup2)[j].first;
        int i;
        for(i=0 ; i<size1 ; i++) {
          if(iProc == tab[i]) break;
        }
        if(i<size1) {
          if(iProc < nSender) nSender = iProc;
        }
      }
      if(nSender != myrank) continue;
      for(int j=0 ; j< size2 ; j++) {    
        int iProc = (*recup2)[j].first;
        int i;
        for(i=0 ; i<size1 ; i++) {
          if(iProc == tab[i]) break;
        }
        if(i < size1) {  
          pVertex remote1 = (*recup1)[i].second;
          pVertex remote2 = (*recup2)[j].second;
          assert(iProc != myrank);
          assert(iProc > myrank);
          void *buf = AP_alloc(iProc,444,sizeof(edge_comm));
          edge_comm *castbuf = (edge_comm *) buf;
          castbuf->p1	   = remote1;
          castbuf->p2	   = remote2,
            castbuf->sendID    = myrank;
          AP_send(buf);
          sendcounts[iProc]++;         
        }
      }  
      delete []tab;
    }
    EIter_delete(eit); 
    AP_check_sends(AP_NOFLAGS);
    AP_reduce_nsends(sendcounts);
  
    int message=0;
    int count;
  
    while (!AP_recv_count(&count) || message<count) {
      void *msg;
      int from;
      int tag;
      int size;
      int rc;
      rc=AP_recv(MPI_ANY_SOURCE,444, AP_BLOCKING|AP_DROPOUT,
                 &msg, &size, &from, &tag);
      if (rc) {
        message++;
        edge_comm * castbuf = (edge_comm*) msg;
        pVertex p1recv,p2recv;
        p1recv = castbuf -> p1;
        p2recv = castbuf -> p2;
        assert(p1recv);assert(p2recv);
        int nprocrecv = castbuf->sendID;
        assert(nprocrecv==from);
        pEdge pe = E_exist(p1recv,p2recv);
        if(pe) {
          //check orientation
          if(E_vertex(pe,0) != p1recv) {
#ifdef DEBUG
            norient++;
#endif
            pe->p1 = p1recv;
            pe->p2 = p2recv;
            assert(E_vertex(pe,0) == p1recv);
          }
        } 
        AP_free(msg);
      }
    }    
    AP_check_sends(AP_WAITALL); 
    MPI_Barrier(MPI_COMM_WORLD);
    delete [] sendcounts;
#ifdef DEBUG
    return norient;
#else    
    return 0;
#endif  
  }


  int CheckFacesOrientation(pMesh mesh){
#ifdef DEBUG
    int norient = 0;
#endif
    int mysize,myrank;
    MPI_Comm_size(MPI_COMM_WORLD, &mysize);
    MPI_Comm_rank(MPI_COMM_WORLD, &myrank); 
 
    int *sendcounts = new int[mysize];
    for(int i=0;i<mysize;i++) sendcounts[i]=0;
  
    pMeshDataId tagData = MD_lookupMeshDataId("RemotePoint");
  
    FIter fit = M_faceIter(mesh);
    pFace pface;  
    while ((pface = FIter_next(fit))) {
      pVertex p1 = F_vertex(pface,0);
      pVertex p2 = F_vertex(pface,1);
      pVertex p3 = F_vertex(pface,2);
      void *temp_ptr1,*temp_ptr2,*temp_ptr3; 
      int isInterface1 = EN_getDataPtr((pEntity) p1 , tagData, &temp_ptr1);
      int isInterface2 = EN_getDataPtr((pEntity) p2 , tagData, &temp_ptr2);
      int isInterface3 = EN_getDataPtr((pEntity) p3 , tagData, &temp_ptr3);
      if(!(isInterface1 && isInterface2 && isInterface3)) continue;
   
      const std::vector<std::pair<int , pVertex> > *recup1 = (std::vector<std::pair<int , pVertex> > *) temp_ptr1;
      const std::vector<std::pair<int , pVertex> > *recup2 = (std::vector<std::pair<int , pVertex> > *) temp_ptr2;
      const std::vector<std::pair<int , pVertex> > *recup3 = (std::vector<std::pair<int , pVertex> > *) temp_ptr3;
      int size = (*recup1).size();
      int *tab=new int[size];
      for(int i=0 ; i< size ; i++) tab[i] = (*recup1)[i].first;
    
      int size2 = (*recup2).size();
      int *tab2=new int[size2];
      for(int i=0 ; i< size2 ; i++) tab2[i] = (*recup2)[i].first;

      // check if I must send
      int nSender = myrank;
      for(unsigned int k=0 ; k<(*recup3).size() ; k++) {	 
        int iProc = (*recup3)[k].first;
        int i;
        for(i=0 ; i<size ; i++) {
          if(iProc == tab[i]) break;
        }
        int j;
        for(j=0 ; j<size2 ; j++) {
          if(iProc == tab2[j]) break;
        }       
        if(i < size && j < size2) {
          if(iProc < nSender) nSender = iProc;
        }
      }
    
      if(nSender != myrank) continue;
    
      for(unsigned int k=0 ; k<(*recup3).size() ; k++) {	 
        int iProc = (*recup3)[k].first;
        int i;
        for(i=0 ; i<size ; i++) {
          if(iProc == tab[i]) break;
        }
        int j;
        for(j=0 ; j<size2 ; j++) {
          if(iProc == tab2[j]) break;
        }       
        if(i < size && j < size2) {
          assert(tab[i]==iProc);
          assert(tab2[j]==iProc);
        
          pVertex remote1 = (*recup1)[i].second;
          pVertex remote2 = (*recup2)[j].second;
          pVertex remote3 = (*recup3)[k].second;
        
          void *buf = AP_alloc(iProc,444,sizeof(face_comm));       
          face_comm *castbuf = (face_comm *) buf;
          castbuf->pdest[0]	   = remote1;
          castbuf->pdest[1]	   = remote2,
            castbuf->pdest[2]	   = remote3,
            AP_send(buf);
          sendcounts[iProc]++;    
        }
      }
      delete []tab;
      delete []tab2;
    }      
    FIter_delete(fit);  
    AP_check_sends(AP_NOFLAGS);
    AP_reduce_nsends(sendcounts); 
  
    int message=0;
    int count;
    while (!AP_recv_count(&count) || message<count) {
      void *msg;
      int  from;
      int  tag;
      int  size;
      int  recv;
      recv = AP_recv(MPI_ANY_SOURCE, 444, AP_BLOCKING|AP_DROPOUT,
                     &msg, &size, &from, &tag);
      if(recv) {
        message++;
        face_comm * facecom = (face_comm * ) msg;
        pVertex  p1 = facecom->pdest[0];
        pVertex  p2 = facecom->pdest[1];
        pVertex  p3 = facecom->pdest[2];
        pEdge    pe[3];
        pe[0] =  E_exist(p1,p2);
        pe[1] =  E_exist(p2,p3);
        pe[2] =  E_exist(p1,p3);
      
        if(pe[0] && pe[1] && pe[2]) {
          // pFace pface = F_exist(2,p1,p2,p3,0);
          pFace pface = F_exist(p1,p2,p3,0);
          assert(pface);
      
          //check orientation
          if(F_edge(pface,0) != pe[0] || F_edge(pface,1) != pe[1]) {
            ((MDB_Triangle*)pface)->e1 = pe[0];
            ((MDB_Triangle*)pface)->e2 = pe[1];
            ((MDB_Triangle*)pface)->e3 = pe[2];
#ifdef DEBUG
            norient++;
#endif	  	
          }        
        }       
        AP_free(msg);      
      }    
    }      
    AP_check_sends(AP_WAITALL);
    MPI_Barrier(MPI_COMM_WORLD);
    delete [] sendcounts;
#ifdef DEBUG
    return norient;
#else  
    return 0;
#endif	  	
  }

/*
// version with sets for the parallel nodes
int CheckEdgesOrientation(pMesh mesh){
#ifdef DEBUG
int norient = 0;
#endif
int nproc,myrank;
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

int *sendcounts = new int[nproc];
for(int i=0;i<nproc;i++)sendcounts[i]=0;

pMeshDataId tagData   = MD_lookupMeshDataId("RemotePoint");
EIter eit = M_edgeIter(mesh);
pEdge pedge;  
while ((pedge = EIter_next(eit))) {
pVertex p[2];
p[0] = E_vertex(pedge,0);
p[1] = E_vertex(pedge,1);
int tmp1;
int isInterface1 = EN_getDataInt((pEntity) p[0] ,tagData, &tmp1);
int tmp2;
int isInterface2 = EN_getDataInt((pEntity) p[1] ,tagData, &tmp2);
if(!isInterface1 || !isInterface2) continue;
const std::set<std::pair<int, MDB_Point*> > *recup1 = (std::set<std::pair<int, MDB_Point*> > *) temp_ptr1;
const std::set<std::pair<int, MDB_Point*> > *recup2 = (std::set<std::pair<int, MDB_Point*> > *) temp_ptr2;
int size1 = (*recup1).size();
int size2 = (*recup2).size();
assert(size1);assert(size2);
int *tab=new int[size1];
std::set<std::pair<int, MDB_Point*> >::const_iterator rIter1 = recup1->begin();
for (int i=0; rIter1 != recup1->end(); rIter1++, i++) tab[i] = (*rIter1).first;
int nSender = myrank;
std::set<std::pair<int, MDB_Point*> >::const_iterator rIter2 = recup2->begin();
for (int j=0; rIter2 != recup2->end(); rIter2++, j++) {
int iProc = (*rIter2).first;
int i;
for(i=0 ; i<size1 ; i++) {
if(iProc == tab[i]) break;
}
if(i<size1) {
if(iProc < nSender) nSender = iProc;
}
}
if(nSender != myrank) continue;
rIter2 = recup2->begin();
for (int j=0; rIter2 != recup2->end(); rIter2++, j++) {
int iProc = (*rIter2).first;
int i;
rIter1 = recup1->begin();
for (i=0; rIter1 != recup1->end(), i<size1; rIter1++, i++) {
if(iProc == tab[i]) break;
}
if(i < size1) {
pVertex remote1 = (*rIter1).second;
pVertex remote2 = (*rIter2).second;
assert(iProc != myrank);
assert(iProc > myrank);
void *buf = AP_alloc(iProc,444,sizeof(edge_comm));
edge_comm *castbuf = (edge_comm *) buf;
castbuf->p1	   = remote1;
castbuf->p2	   = remote2,
castbuf->sendID    = myrank;
AP_send(buf);
sendcounts[iProc]++;         
}
}  
delete []tab;
}
EIter_delete(eit); 
AP_check_sends(AP_NOFLAGS);
AP_reduce_nsends(sendcounts);
  
int message=0;
int count;
  
while (!AP_recv_count(&count) || message<count) {
void *msg;
int from;
int tag;
int size;
int rc;
rc=AP_recv(MPI_ANY_SOURCE,444, AP_BLOCKING|AP_DROPOUT,
&msg, &size, &from, &tag);
if (rc) {
message++;
edge_comm * castbuf = (edge_comm*) msg;
pVertex p1recv,p2recv;
p1recv = castbuf -> p1;
p2recv = castbuf -> p2;
assert(p1recv);assert(p2recv);
int nprocrecv = castbuf->sendID;
assert(nprocrecv==from);
pEdge pe = E_exist(p1recv,p2recv);
if(pe) {
//check orientation
if(E_vertex(pe,0) != p1recv) {
#ifdef DEBUG
norient++;
#endif
pe->p1 = p1recv;
pe->p2 = p2recv;
assert(E_vertex(pe,0) == p1recv);
}
} 
AP_free(msg);
}
}    
AP_check_sends(AP_WAITALL); 
MPI_Barrier(MPI_COMM_WORLD);
delete [] sendcounts;
#ifdef DEBUG
return norient;
#else    
return 0;
#endif  
}
*/

}

#endif