Debian Codegraph @

dune/uggrid/parallel/ddd/ctrl/cons.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /****************************************************************************/
6 /* */
7 /* File: cons.c */
8 /* */
9 /* Purpose: consistency checker for ddd structures */
10 /* */
11 /* Author: Klaus Birken */
12 /* Rechenzentrum Uni Stuttgart */
13 /* Universitaet Stuttgart */
14 /* Allmandring 30 */
15 /* 70550 Stuttgart */
16 /* internet: [email protected] */
17 /* */
18 /* History: 94/03/21 kb begin */
19 /* 960718 kb introduced lowcomm-layer (sets of messages) */
20 /* */
21 /* Remarks: */
22 /* */
23 /****************************************************************************/
24
25 /****************************************************************************/
26 /* */
27 /* include files */
28 /* system include files */
29 /* application include files */
30 /* */
31 /****************************************************************************/
32
33 /* standard C library */
34 #include <config.h>
35 #include <cstdlib>
36 #include <cstdio>
37 #include <cstring>
38 #include <cassert>
39
40 #include <algorithm>
41 #include <vector>
42
43 #include <dune/uggrid/parallel/ddd/dddi.h>
44 #include <dune/uggrid/parallel/ddd/basic/lowcomm.h>
45
46 #include <dune/common/stdstreams.hh>
47
48 #include <dune/uggrid/parallel/ddd/dddcontext.hh>
49
50 USING_UG_NAMESPACES
51
52 /* PPIF namespace: */
53 using namespace PPIF;
54
55 START_UGDIM_NAMESPACE
56
57 /****************************************************************************/
58 /* */
59 /* defines in the following order */
60 /* */
61 /* compile time constants defining static data size (i.e. arrays) */
62 /* other constants */
63 /* macros */
64 /* */
65 /****************************************************************************/
66
67 /*
68 PAIRS: check existence of object for each coupling
69 ALLTOALL: check if all coupling lists are equal
70 */
71 #define CHECK_CPL_PAIRS
72 #define CHECK_CPL_ALLTOALL
73
74
75
76 /****************************************************************************/
77 /* */
78 /* data structures */
79 /* */
80 /****************************************************************************/
81
82 struct CONS_INFO
83 {
84 DDD_GID gid;
85 DDD_TYPE typ;
86 DDD_PROC dest;
87 DDD_PROC proc;
88 DDD_PRIO prio;
89 };
90
91
92
93
94 struct CONSMSG
95 {
96 DDD_PROC dest;
97
98 CONSMSG *next;
99
100
101 CONS_INFO *consArray;
102 int nItems;
103
104 /* lowcomm message handle */
105 LC_MSGHANDLE msg_h;
106 };
107
108
109
110 /****************************************************************************/
111 /* */
112 /* routines */
113 /* */
114 /****************************************************************************/
115
116
117 void ddd_ConsInit(DDD::DDDContext& context)
118 {
119 auto& ctx = context.consContext();
120 ctx.consmsg_t = LC_NewMsgType(context, "ConsCheckMsg");
121 ctx.constab_id = LC_NewMsgTable("ConsTab", ctx.consmsg_t, sizeof(CONS_INFO));
122 }
123
124
125 void ddd_ConsExit(DDD::DDDContext&)
126 {}
127
128
129 /****************************************************************************/
130
131 static int ConsBuildMsgInfos(DDD::DDDContext& context, CONS_INFO *allItems, int nXferItems, CONSMSG **theMsgs)
132 {
133 CONSMSG *cm, *lastCm;
134 int i, lastdest, nMsgs;
135
136 auto& ctx = context.consContext();
137
138 lastdest = -1;
139 lastCm = cm = NULL;
140 nMsgs = 0;
141 for(i=0; i<nXferItems; i++)
142 {
143 /* create new message item, if necessary */
144 if (allItems[i].dest != lastdest)
145 {
146 cm = (CONSMSG *) AllocTmpReq(sizeof(CONSMSG), TMEM_CONS);
147 if (cm==NULL)
148 {
149 DDD_PrintError('E', 9900, STR_NOMEM " in ConsBuildMsgInfos");
150 return(0);
151 }
152
153 cm->nItems = 0;
154 cm->consArray = &allItems[i];
155 cm->dest = allItems[i].dest;
156 cm->next = lastCm;
157 lastCm = cm;
158 lastdest = cm->dest;
159 nMsgs++;
160 }
161 cm->nItems++;
162 }
163 *theMsgs = cm;
164
165
166 /* initiate send messages */
167 for(cm=*theMsgs; cm!=NULL; cm=cm->next)
168 {
169 /* create new send message */
170 cm->msg_h = LC_NewSendMsg(context, ctx.consmsg_t, cm->dest);
171
172 /* init table inside message */
173 LC_SetTableSize(cm->msg_h, ctx.constab_id, cm->nItems);
174
175 /* prepare message for sending away */
176 LC_MsgPrepareSend(context, cm->msg_h);
177 }
178
179 return(nMsgs);
180 }
181
182
183
184 static void ConsSend(DDD::DDDContext& context, CONSMSG *theMsgs)
185 {
186 auto& ctx = context.consContext();
187
188 for(CONSMSG* cm=theMsgs; cm != nullptr; cm=cm->next)
189 {
190 /* copy data into message */
191 memcpy(LC_GetPtr(cm->msg_h, ctx.constab_id),
192 cm->consArray, sizeof(CONS_INFO)*cm->nItems);
193
194 /* send message */
195 LC_MsgSend(context, cm->msg_h);
196 }
197 }
198
199
200
201 static int ConsCheckSingleMsg (DDD::DDDContext& context, LC_MSGHANDLE xm, DDD_HDR *locObjs)
202 {
203 CONS_INFO *theCplBuf;
204 int i, j, nItems;
205 int error_cnt = 0;
206
207 auto& ctx = context.consContext();
208 const auto& me = context.me();
209
210 nItems = (int) LC_GetTableLen(xm, ctx.constab_id);
211 theCplBuf = (CONS_INFO *) LC_GetPtr(xm, ctx.constab_id);
212
213
214 /*
215 sprintf(cBuffer, "%4d: checking message from proc %d (%d items)\n",
216 me, LC_MsgGetProc(xm), nItems);
217 DDD_PrintDebug(cBuffer);
218 */
219
220 const int nObjs = context.nObjs();
221
222 /* test whether there are consistent objects for all couplings */
223 for(i=0, j=0; i<nItems; i++)
224 {
225 while ((j < nObjs) && (OBJ_GID(locObjs[j]) < theCplBuf[i].gid))
226 j++;
227
228 if ((j < nObjs) && (OBJ_GID(locObjs[j])==theCplBuf[i].gid))
229 {
230 if (OBJ_PRIO(locObjs[j])!=theCplBuf[i].prio)
231 {
232 Dune::dwarn
233 << " DDD-CCC Warning: obj " << OBJ_GID(locObjs[j]) << " type "
234 << OBJ_TYPE(locObjs[j]) << " on " << me << " has prio "
235 << OBJ_PRIO(locObjs[j]) << ", cpl from " << LC_MsgGetProc(xm)
236 << " has prio " << theCplBuf[i].prio << "!\n";
237
238 error_cnt++;
239 }
240 }
241 else
242 {
243 Dune::dwarn
244 << " DDD-GCC Warning: obj " << theCplBuf[i].gid << " type "
245 << theCplBuf[i].typ << " on " << me << " for cpl from "
246 << LC_MsgGetProc(xm) << "missing!\n";
247
248 error_cnt++;
249 }
250 }
251
252 return(error_cnt);
253 }
254
255
256
257 static bool sort_CplBufDest (const CONS_INFO& a, const CONS_INFO& b)
258 {
259 return std::tie(a.dest, a.gid) < std::tie(b.dest, b.gid);
260 }
261
262
263 static int ConsCheckGlobalCpl(DDD::DDDContext& context)
264 {
265 COUPLING *cpl;
266 int i, j, lenCplBuf, nRecvMsgs;
267 CONSMSG *sendMsgs=NULL, *cm=NULL;
268 LC_MSGHANDLE *recvMsgs;
269 int error_cnt = 0;
270
271 auto& ctx = context.consContext();
272 const auto procs = context.procs();
273 const auto& objTable = context.objTable();
274
275 /* count overall number of couplings */
276 const auto& nCpls = context.couplingContext().nCpls;
277 for(i=0, lenCplBuf=0; i < nCpls; i++)
278 lenCplBuf += IdxNCpl(context, i);
279
280 /* get storage for messages */
281 std::vector<CONS_INFO> cplBuf(lenCplBuf);
282
283 /* copy CONS_INFOs into message buffer */
284 for(i=0, j=0; i < nCpls; i++)
285 {
286 for(cpl=IdxCplList(context, i); cpl!=NULL; cpl=CPL_NEXT(cpl))
287 {
288 if ((DDD_PROC)CPL_PROC(cpl) >= procs)
289 {
290 error_cnt++;
291 Dune::dwarn << "DDD-GCC Warning: invalid proc=" << CPL_PROC(cpl)
292 << " (" << OBJ_GID(cpl->obj) << "/" << OBJ_GID(objTable[i]) << ")\n";
293 }
294 cplBuf[j].gid = OBJ_GID(cpl->obj);
295 cplBuf[j].typ = OBJ_TYPE(cpl->obj);
296 cplBuf[j].dest = CPL_PROC(cpl);
297 cplBuf[j].proc = CPL_PROC(cpl);
298 cplBuf[j].prio = cpl->prio;
299 j++;
300 }
301 }
302 assert(j==lenCplBuf);
303
304 /* sort couplings */
305 std::sort(cplBuf.begin(), cplBuf.end(), sort_CplBufDest);
306
307 /* accumulate messages (one for each partner); inform receivers */
308 ConsBuildMsgInfos(context, cplBuf.data(), cplBuf.size(), &sendMsgs);
309
310 /* init communication topology */
311 nRecvMsgs = LC_Connect(context, ctx.consmsg_t);
312 if (nRecvMsgs==ERROR)
313 {
314 error_cnt = -1;
315 goto exit_ConsCheckGlobalCpl;
316 }
317
318 /* build and send messages */
319 ConsSend(context, sendMsgs);
320
321
322 /* communicate set of messages (send AND receive) */
323 recvMsgs = LC_Communicate(context);
324
325
326 /* perform checking of received data */
327 if (nRecvMsgs>0)
328 {
329 std::vector<DDD_HDR> locObjs = LocalObjectsList(context);
330
331 for(i=0; i<nRecvMsgs; i++)
332 {
333 error_cnt += ConsCheckSingleMsg(context, recvMsgs[i], locObjs.data());
334 }
335 }
336
337
338
339 exit_ConsCheckGlobalCpl:
340
341 /* cleanup low-comm layer */
342 LC_Cleanup(context);
343
344
345 /* free temporary storage */
346 for(; sendMsgs!=NULL; sendMsgs=cm)
347 {
348 cm = sendMsgs->next;
349 FreeTmpReq(sendMsgs, sizeof(CONSMSG), TMEM_CONS);
350 }
351
352 return(error_cnt);
353 }
354
355
356
357
358 /****************************************************************************/
359
360
361 static int Cons2CheckSingleMsg (DDD::DDDContext& context, LC_MSGHANDLE xm, DDD_HDR *locObjs)
362 {
363 CONS_INFO *theCplBuf;
364 int i, inext=0, j, nItems;
365 int error_cnt = 0;
366
367 auto& ctx = context.consContext();
368 const auto& me = context.me();
369
370 nItems = (int) LC_GetTableLen(xm, ctx.constab_id);
371 theCplBuf = (CONS_INFO *) LC_GetPtr(xm, ctx.constab_id);
372
373
374 /*
375 sprintf(cBuffer, "%4d: checking message from proc %d (%d items)\n",
376 me, LC_MsgGetProc(xm), nItems);
377 DDD_PrintDebug(cBuffer);
378 */
379
380 const int nObjs = context.nObjs();
381
382 /* test whether there are consistent objects for all couplings */
383 for(i=0, j=0; i<nItems; i=inext)
384 {
385 inext = i+1;
386
387 while ((j < nObjs) && (OBJ_GID(locObjs[j]) < theCplBuf[i].gid))
388 j++;
389
390 if ((j < nObjs) && (OBJ_GID(locObjs[j])==theCplBuf[i].gid))
391 {
392 if (theCplBuf[i].proc == me)
393 {
394 if (OBJ_PRIO(locObjs[j])!=theCplBuf[i].prio)
395 {
396 Dune::dwarn
397 << " DDD-GCC Warning: obj " << OBJ_GID(locObjs[j]) << " type "
398 << OBJ_TYPE(locObjs[j]) << " on " << me << " has prio "
399 << OBJ_PRIO(locObjs[j]) << ", cpl from " << LC_MsgGetProc(xm)
400 << " has prio " << theCplBuf[i].prio << "!\n";
401
402 error_cnt++;
403 }
404 }
405 else
406 {
407 int i2;
408 COUPLING *j2;
409
410 for(j2=ObjCplList(context, locObjs[j]); j2!=NULL; j2=CPL_NEXT(j2))
411 {
412 int ifound = -1;
413
414 for(i2=i; i2<nItems && theCplBuf[i2].gid==theCplBuf[i].gid;
415 i2++)
416 {
417 if (theCplBuf[i2].proc==CPL_PROC(j2))
418 {
419 ifound = i2;
420 break;
421 }
422 }
423
424 if (ifound==-1)
425 {
426 Dune::dwarn
427 << " DDD-GCC Warning: obj " << theCplBuf[i].gid << " type "
428 << theCplBuf[i].typ << " on " << me << " has cpl from "
429 << CPL_PROC(j2) << ", but " << LC_MsgGetProc(xm) << " hasn't!\n";
430
431 error_cnt++;
432 }
433 }
434
435
436 /* the next loop does the backward checking, i.e., it checks
437 the couplings symmetric to the previous loop. if inconsistencies
438 are detected, then these inconsistencies are a local phenomenon
439 and can be "healed" by adding a coupling locally. this feature
440 is switched off, because this healing indicates that the
441 data structure is inconsistent, which should be repaired
442 elsewhere (in the DDD application).
443 */
444 #ifdef ConsCheckWithAutomaticHealing
445
446 for(i2=i; i2<nItems && theCplBuf[i2].gid==theCplBuf[i].gid; i2++)
447 {
448 if (theCplBuf[i2].proc!=me)
449 {
450 int ifound = -1;
451
452 for(j2=ObjCplList(context, locObjs[j]); j2!=NULL; j2=CPL_NEXT(j2))
453 {
454 if (theCplBuf[i2].proc==j2->proc)
455 {
456 ifound = i2;
457 break;
458 }
459 }
460
461 if (ifound==-1)
462 {
463 Dune::dwarn
464 << "healing with AddCpl(" << theCplBuf[i].gid << ", "
465 << theCplBuf[i2].proc << ", " << theCplBuf[i2].prio << ")\n";
466
467 AddCoupling(context, locObjs[j], theCplBuf[i2].proc, theCplBuf[i2].prio);
468 }
469 }
470 }
471 #endif /* ConsCheckWithAutomaticHealing */
472
473
474 for(; inext<nItems && theCplBuf[inext].gid==theCplBuf[i].gid; inext++)
475 ;
476 }
477 }
478 /* this is superfluous and (more important:) wrong!
479 else
480 {
481 sprintf(cBuffer, " X DDD-GCC Warning: obj %08x type %d on %d for cpl"
482 " from %3d missing!\n",
483 theCplBuf[i].gid, theCplBuf[i].typ, me, LC_MsgGetProc(xm));
484 DDD_PrintLine(cBuffer);
485
486 error_cnt++;
487 }
488 */
489 }
490
491 /* the next too lines are wrong, bug find by PURIFY. KB 970416.
492 the locObjs list is freed in Cons2CheckGlobalCpl!
493 if (locObjs!=NULL)
494 FreeTmp(locObjs,0);
495 */
496
497
498 return(error_cnt);
499 }
500
501
502
503 static int Cons2CheckGlobalCpl(DDD::DDDContext& context)
504 {
505 COUPLING *cpl, *cpl2;
506 int i, j, lenCplBuf, nRecvMsgs;
507 CONSMSG *sendMsgs, *cm=0;
508 LC_MSGHANDLE *recvMsgs;
509 int error_cnt = 0;
510
511 auto& ctx = context.consContext();
512 const auto& me = context.me();
513 const auto& nCpls = context.couplingContext().nCpls;
514
515 /* count overall number of couplings */
516 for(i=0, lenCplBuf=0; i < nCpls; i++)
517 lenCplBuf += (IdxNCpl(context, i) * (IdxNCpl(context, i)+1));
518
519 /* get storage for messages */
520 std::vector<CONS_INFO> cplBuf(lenCplBuf);
521
522 /* copy CONS_INFOs into message buffer */
523 for(i=0, j=0; i < nCpls; i++)
524 {
525 for(cpl=IdxCplList(context, i); cpl!=NULL; cpl=CPL_NEXT(cpl))
526 {
527 cplBuf[j].gid = OBJ_GID(cpl->obj);
528 cplBuf[j].typ = OBJ_TYPE(cpl->obj);
529 cplBuf[j].dest = CPL_PROC(cpl);
530 cplBuf[j].proc = me;
531 cplBuf[j].prio = OBJ_PRIO(cpl->obj);
532 j++;
533
534 for(cpl2=IdxCplList(context, i); cpl2!=NULL; cpl2=CPL_NEXT(cpl2))
535 {
536 cplBuf[j].gid = OBJ_GID(cpl->obj);
537 cplBuf[j].typ = OBJ_TYPE(cpl->obj);
538 cplBuf[j].dest = CPL_PROC(cpl);
539 cplBuf[j].proc = CPL_PROC(cpl2);
540 cplBuf[j].prio = cpl2->prio;
541 j++;
542 }
543 }
544 }
545 assert(j==lenCplBuf);
546
547 /* sort couplings */
548 std::sort(cplBuf.begin(), cplBuf.end(), sort_CplBufDest);
549
550 /* accumulate messages (one for each partner); inform receivers */
551 ConsBuildMsgInfos(context, cplBuf.data(), cplBuf.size(), &sendMsgs);
552
553 /* init communication topology */
554 nRecvMsgs = LC_Connect(context, ctx.consmsg_t);
555
556 /* build and send messages */
557 ConsSend(context, sendMsgs);
558
559 /* communicate set of messages (send AND receive) */
560 recvMsgs = LC_Communicate(context);
561
562
563 /* perform checking of received data */
564 if (nRecvMsgs>0)
565 {
566 std::vector<DDD_HDR> locObjs = LocalObjectsList(context);
567
568 for(i=0; i<nRecvMsgs; i++) {
569 error_cnt += Cons2CheckSingleMsg(context, recvMsgs[i], locObjs.data());
570 }
571 }
572
573
574 /* cleanup low-comm layer */
575 LC_Cleanup(context);
576
577
578 /* free temporary storage */
579 for(; sendMsgs!=NULL; sendMsgs=cm)
580 {
581 cm = sendMsgs->next;
582 FreeTmpReq(sendMsgs, sizeof(CONSMSG), TMEM_CONS);
583 }
584
585 return(error_cnt);
586 }
587
588
589
590 /****************************************************************************/
591
592 static int ConsCheckDoubleObj(const DDD::DDDContext& context)
593 {
594 std::vector<DDD_HDR> locObjs = LocalObjectsList(context);
595 const int nObjs = context.nObjs();
596
597 int error_cnt = 0;
598 for(int i=1; i < nObjs; i++)
599 {
600 if (OBJ_GID(locObjs[i-1])==OBJ_GID(locObjs[i]))
601 {
602 error_cnt++;
603 Dune::dwarn << " DDD-GCC Warning: obj " << OBJ_GID(locObjs[i])
604 << " on " << context.me() << " doubled\n";
605 }
606 }
607
608 return(error_cnt);
609 }
610
611
612
613 /****************************************************************************/
614 /* */
615 /* Function: DDD_ConsCheck */
616 /* */
617 /****************************************************************************/
618
619 /**
620 Check DDD runtime consistency.
621 This function performs a combined local/global consistency
622 check on the object data structures and interfaces managed by DDD.
623 This may be used for debugging purposes; if errors are detected,
624 then some understanding of internal DDD structures will be useful.
625
626 The following single aspects will be checked:
627 \begin{itemize}
628 \item double existence of {\em global ID} numbers in each processor's
629 set of local objects.
630 \item consistency of coupling lists and object copies
631 \item non-symmetric interfaces between processor pairs
632 \item non-symmetric number of items in each interface
633 \end{itemize}
634
635 @returns total number of errors (sum of all procs)
636 */
637
638 int DDD_ConsCheck(DDD::DDDContext& context)
639 {
640 int cpl_errors;
641 int total_errors=0;
642
643 DDD_Flush();
644 Synchronize(context.ppifContext());
645 if (DDD_GetOption(context, OPT_QUIET_CONSCHECK)==OPT_OFF)
646 {
647 if (context.isMaster())
648 DDD_PrintLine(" DDD-GCC (Global Consistency Check)\n");
649 }
650
651 total_errors += ConsCheckDoubleObj(context);
652
653 #ifdef CHECK_CPL_PAIRS
654 cpl_errors = ConsCheckGlobalCpl(context);
655 #endif
656 #ifdef CHECK_CPL_ALLTOALL
657 cpl_errors = Cons2CheckGlobalCpl(context);
658 #endif
659
660 if (cpl_errors==-1)
661 {
662 Dune::dgrave << " DDD-GCC Error: out of memory in ConsCheckGlobalCpl()\n";
663 total_errors++;
664 }
665 else
666 total_errors += cpl_errors;
667
668 total_errors += DDD_CheckInterfaces(context);
669
670
671 /* compute sum of errors over all processors */
672 total_errors = ddd_GlobalSumInt(context, total_errors);
673
674 DDD_Flush();
675 Synchronize(context.ppifContext());
676 if (DDD_GetOption(context, OPT_QUIET_CONSCHECK)==OPT_OFF)
677 {
678 if (context.isMaster())
679 Dune::dwarn << " DDD-GCC ready (" << total_errors << " errors)\n";
680 }
681
682
683 return(total_errors);
684 }
685
686
687 END_UGDIM_NAMESPACE