Debian Codegraph @

dune/uggrid/parallel/ddd/xfer/xfer.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: xfer.c */
8 /* */
9 /* Purpose: main module for object transfer */
10 /* (contains basic functionality used by the rest of the source */
11 /* files in the Xfer-module) */
12 /* */
13 /* Author: Klaus Birken */
14 /* Rechenzentrum Uni Stuttgart */
15 /* Universitaet Stuttgart */
16 /* Allmandring 30 */
17 /* 70550 Stuttgart */
18 /* internet: [email protected] */
19 /* */
20 /* History: 93/11/30 kb begin */
21 /* 95/03/21 kb added variable sized objects (XferCopyObjX) */
22 /* 96/07/03 kb split XferInfo-list into ObjXfer and CplXfer */
23 /* 960718 kb introduced lowcomm-layer (sets of messages) */
24 /* */
25 /* Remarks: */
26 /* */
27 /****************************************************************************/
28
29 /****************************************************************************/
30 /* */
31 /* include files */
32 /* system include files */
33 /* application include files */
34 /* */
35 /****************************************************************************/
36
37 /* standard C library */
38 #include <config.h>
39 #include <cstdlib>
40 #include <cstdio>
41 #include <cstring>
42
43 #include <algorithm>
44 #include <iomanip>
45 #include <tuple>
46
47 #include <dune/common/stdstreams.hh>
48
49 #include <dune/uggrid/parallel/ddd/dddi.h>
50 #include "xfer.h"
51
52 using namespace PPIF;
53
54 START_UGDIM_NAMESPACE
55
56
57 /****************************************************************************/
58 /* */
59 /* routines */
60 /* */
61 /****************************************************************************/
62
63
64
65 static bool sort_NewOwners (const XICopyObj* a, const XICopyObj* b)
66 {
67 return std::tie(a->gid, a->dest) < std::tie(b->gid, b->dest);
68 }
69
70
71 /****************************************************************************/
72
73 /*
74 collect a temporary list of XINewCpl-items.
75
76 for each XICopyObj-command whose destination doesn't have
77 an object copy already do: create a set of XINewCpl-items, one
78 for every processor which owns a copy of the local object.
79
80 this is an estimate, because without communication the sending
81 processor cannot know whether the object copy will be accepted.
82 this final information will be transferred in a second pass, as
83 soon as the receiver has decided whether he accepts the incoming
84 object or not (depending on rules XFER-C2, XFER-C3, XFER-C4,
85 XFER-P and XFER-D).
86 */
87
88 XICopyObj **CplClosureEstimate (DDD::DDDContext& context, const std::vector<XICopyObj*>& arrayItems, int *nRet)
89 {
90 const auto& me = context.me();
91
92 int i, nNewOwners;
93 XICopyObj **arrayNewOwners = NULL;
94 XICopyObj* const* items = arrayItems.data();
95 const int n = arrayItems.size();
96
97
98
99 nNewOwners=0;
100 for(i=0; i<n; i++)
101 {
102 XICopyObj *xi = items[i];
103 DDD_PROC dest = xi->dest; /* destination proc */
104 COUPLING *cpl, *xicpl = ObjCplList(context, xi->hdr);
105 DDD_GID xigid = xi->gid;
106 DDD_TYPE xitype = OBJ_TYPE(xi->hdr);
107
108 SET_CO_NEWOWNER(xi);
109
110 /* look if there's a coupling for dest */
111 for(cpl=xicpl; cpl!=NULL; cpl=CPL_NEXT(cpl))
112 {
113 if (dest==CPL_PROC(cpl))
114 {
115 /* got one coupling, destination is not a new owner */
116 CLEAR_CO_NEWOWNER(xi);
117
118 /* destination proc had a copy before xfer */
119 /* check whether priority of that copy will change */
120 /*
121 if (WhichPrioWins(xi->prio, cpl->prio)==1)
122 {
123 */
124 /* new prio will win on other proc -> adapt coupling */
125 /*
126 printf("%4d: XXXCoupling %08x proc=%d prio=%d\n",
127 me,xigid,dest,xi->prio);
128 cpl->prio = xi->prio;
129 }
130 */
131
132 /* this should be the only coupling for that proc.
133 leave loop prematurely. */
134 break;
135 }
136 }
137
138
139 if (CO_NEWOWNER(xi))
140 {
141 nNewOwners++;
142
143 /* destination proc didn't have a copy before xfer */
144
145 /* inform other owners of local copies (XINewCpl) */
146 for(cpl=xicpl; cpl!=NULL; cpl=CPL_NEXT(cpl))
147 {
148 XINewCpl *xc = NewXINewCpl(context);
149 if (xc==NULL)
150 throw std::bad_alloc();
151
152 xc->to = CPL_PROC(cpl); /* receiver of XINewCpl */
153 NewCpl_SetDest(xc->te,dest); /* destination of XICopyObj*/
154 NewCpl_SetGid(xc->te,xigid); /* the object's gid */
155 NewCpl_SetPrio(xc->te,xi->prio); /* new obj's priority */
156 NewCpl_SetType(xc->te,xitype); /* the object's type */
157 }
158
159 /* send current couplings (XIOldCpl) to new destination */
160 /* note: destination proc can get this information
161 multiple times, once for each incoming object
162 with same gid (from different senders) */
163 for(cpl=xicpl; cpl!=NULL; cpl=CPL_NEXT(cpl))
164 {
165 XIOldCpl *xc = NewXIOldCpl(context);
166 if (xc==NULL)
167 throw std::bad_alloc();
168
169 xc->to = dest; /* receiver of XIOldCpl */
170 xc->te.gid = xigid; /* the object's gid */
171 xc->te.proc = CPL_PROC(cpl); /* coupling proc */
172 xc->te.prio = cpl->prio; /* coupling priority */
173 }
174
175 /* send one coupling (XIOldCpl) for local copy */
176 {
177 XIOldCpl *xc = NewXIOldCpl(context);
178 if (xc==NULL)
179 throw std::bad_alloc();
180
181 xc->to = dest; /* receiver of XIOldCpl */
182 xc->te.gid = xigid; /* the object's gid */
183 xc->te.proc = me; /* coupling proc */
184 xc->te.prio = OBJ_PRIO(xi->hdr); /* coupling priority */
185 }
186 }
187 }
188
189 *nRet = nNewOwners;
190 /*
191 printf("%4d: nNewOwners=%d\n", me, nNewOwners);
192 */
193
194 /* check multiple new-owner-destinations for same gid */
195 if (nNewOwners>0)
196 {
197 int j, k;
198
199 arrayNewOwners =
200 (XICopyObj **) OO_Allocate (sizeof(XICopyObj *)* nNewOwners);
201 if (arrayNewOwners==NULL)
202 {
203 DDD_PrintError('E', 6102, STR_NOMEM " in XferEnd()");
204 return NULL;
205 }
206
207 /* fill pointer array XICopyObj-items marked CO_NEWOWNER */
208 for(j=0, k=0; j<n; j++)
209 {
210 if (CO_NEWOWNER(items[j]))
211 {
212 arrayNewOwners[k] = items[j];
213 k++;
214 }
215 }
216
217 if (nNewOwners==1)
218 return(arrayNewOwners);
219
220 /* sort according to gid (items is sorted according to dest) */
221 std::sort(arrayNewOwners, arrayNewOwners + nNewOwners, sort_NewOwners);
222
223
224 for(j=0; j<nNewOwners-1; j++)
225 {
226 XICopyObj *no1 = arrayNewOwners[j];
227 DDD_GID gid1 = no1->gid;
228
229 for(k=j+1; k<nNewOwners; k++)
230 {
231 XICopyObj *no2 = arrayNewOwners[k];
232 DDD_TYPE no2type;
233
234 if (no2->gid != gid1)
235 break;
236
237 no2type = OBJ_TYPE(no2->hdr);
238
239 /* inform other new-owners of same obj (also XINewCpl!) */
240 /* tell no1-dest that no2-dest gets a copy with no2->prio */
241 {
242 XINewCpl *xc = NewXINewCpl(context);
243 if (xc==NULL)
244 throw std::bad_alloc();
245
246 xc->to = no1->dest; /* receiver of XINewCpl */
247 NewCpl_SetDest(xc->te,no2->dest); /* dest of XICopyObj */
248 NewCpl_SetGid(xc->te,gid1); /* the obj's gid */
249 NewCpl_SetPrio(xc->te,no2->prio); /* new obj's priority*/
250 NewCpl_SetType(xc->te,no2type); /* the obj's type */
251 }
252 /* tell no2->dest that no1-dest gets a copy with no1->prio */
253 {
254 XINewCpl *xc = NewXINewCpl(context);
255 if (xc==NULL)
256 throw std::bad_alloc();
257
258 xc->to = no2->dest; /* receiver of XINewCpl */
259 NewCpl_SetDest(xc->te,no1->dest); /* dest of XICopyObj */
260 NewCpl_SetGid(xc->te,gid1); /* the obj's gid */
261 NewCpl_SetPrio(xc->te,no1->prio); /* new obj's priority*/
262 NewCpl_SetType(xc->te,no2type); /* the obj's type */
263 }
264 }
265 }
266 }
267
268 return(arrayNewOwners);
269 }
270
271
272 /****************************************************************************/
273
274 /*
275 auxiliary functions for PrepareObjMsgs()
276 */
277
278
279 static void BuildDepDataInfo (XFERMSG *xm, XICopyObj *xi)
280 {
281 XFERADDDATA *xa;
282 int ptr, chunks;
283
284
285 /* count characteristic values for each chunk */
286 chunks = ptr = 0;
287 for(xa=xi->add; xa!=NULL; xa=xa->next)
288 {
289 ptr += xa->addNPointers;
290
291 /* add control information size for var-sized AddData-Items */
292 if (xa->sizes!=NULL)
293 xi->addLen += CEIL(sizeof(int) * xa->addCnt);
294
295 chunks++;
296 }
297
298
299 /* add size of control information */
300 if (xi->addLen>0)
301 xi->addLen += CEIL(sizeof(int)) + chunks*CEIL(2*sizeof(int));
302
303 /* add to current message size information */
304 xm->size += xi->addLen;
305 xm->nPointers += ptr;
306 }
307
308
309 static XFERMSG *CreateXferMsg (DDD_PROC dest, XFERMSG *lastxm)
310 {
311 XFERMSG *xm;
312
313 xm = (XFERMSG *) OO_Allocate (sizeof(XFERMSG));
314 if (xm==NULL)
315 {
316 DDD_PrintError('E', 6100, STR_NOMEM " in PrepareObjMsgs");
317 return NULL;
318 }
319 xm->nPointers = 0;
320 xm->nObjects = 0;
321 xm->proc = dest;
322 xm->size = 0;
323
324 xm->xferObjArray = NULL;
325 xm->xferNewCpl = NULL;
326 xm->xferOldCpl = NULL;
327 xm->nObjItems = 0;
328 xm->nNewCpl = 0;
329 xm->nOldCpl = 0;
330
331 xm->next = lastxm;
332
333 return xm;
334 }
335
336
337
338 static XFERMSG *AccumXICopyObj (const DDD::DDDContext& context,
339 XFERMSG *currxm, int *nMsgs, int *nItems,
340 XICopyObj **items, DDD_PROC dest, int nmax)
341 {
342 XFERMSG *xm;
343 int i;
344
345 if (currxm!=NULL && currxm->proc==dest)
346 {
347 /* there is a XFERMSG with correct processor number -> reuse it */
348 xm = currxm;
349 }
350 else
351 {
352 /* create new XFERMSG structure */
353 xm = CreateXferMsg(dest, currxm);
354 (*nMsgs)++;
355 }
356
357 # if DebugXfer<=2
358 Dune::dvverb << "PrepareObjMsgs, XferMsg"
359 << " proc=" << dest << " nmax=" << nmax << "\n";
360 # endif
361
362
363 for (i=0; i<nmax && items[i]->dest==dest; i++)
364 {
365 XICopyObj *xi = items[i];
366 DDD_HDR hdr = xi->hdr;
367 const TYPE_DESC& desc = context.typeDefs()[OBJ_TYPE(hdr)];
368
369 # if DebugXfer<=0
370 Dune::dvverb
371 << "PrepareObjMsgs, proc=" << dest
372 << " i=" i << "/" << nmax << " (" << xi->gid << ")\n";
373 # endif
374
375 /* accumulate xfer-items in message-info */
376 xm->nObjects++;
377
378 /* length of object itself, possibly variable */
379 xm->size += CEIL(xi->size);
380 xm->nPointers += desc.nPointers;
381
382 if (xi->add != NULL)
383 BuildDepDataInfo(xm, xi);
384 }
385
386 *nItems = i;
387 return xm;
388 }
389
390
391
392
393 static XFERMSG *AccumXINewCpl (XFERMSG *currxm, int *nMsgs, int *nItems,
394 XINewCpl **items, DDD_PROC dest, int nmax)
395 {
396 XFERMSG *xm;
397 int i;
398
399 if (currxm!=NULL && currxm->proc==dest)
400 {
401 /* there is a XFERMSG with correct processor number -> reuse it */
402 xm = currxm;
403 }
404 else
405 {
406 /* create new XFERMSG structure */
407 xm = CreateXferMsg(dest, currxm);
408 (*nMsgs)++;
409 }
410
411 # if DebugXfer<=2
412 Dune::dvverb << "PrepareObjMsgs, XferMsg"
413 << " proc=" << dest << " nmax=" << nmax << "\n";
414 # endif
415
416
417 for (i=0; i<nmax && items[i]->to==dest; i++)
418 {
419 # if DebugXfer<=0
420 XINewCpl *xi = items[i];
421 Dune::dvverb
422 << "PrepareObjMsgs, proc=" << dest
423 << " i=" i << "/" << nmax << " (" << NewCpl_GetGid(xi->te) << ")\n";
424 # endif
425 }
426
427 *nItems = i;
428 return xm;
429 }
430
431
432
433 static XFERMSG *AccumXIOldCpl (XFERMSG *currxm, int *nMsgs, int *nItems,
434 XIOldCpl **items, DDD_PROC dest, int nmax)
435 {
436 XFERMSG *xm;
437 int i;
438
439 if (currxm!=NULL && currxm->proc==dest)
440 {
441 /* there is a XFERMSG with correct processor number -> reuse it */
442 xm = currxm;
443 }
444 else
445 {
446 /* create new XFERMSG structure */
447 xm = CreateXferMsg(dest, currxm);
448 (*nMsgs)++;
449 }
450
451 # if DebugXfer<=2
452 Dune::dvverb << "PrepareObjMsgs, XferMsg"
453 << " proc=" << dest << " nmax=" << nmax << "\n";
454 # endif
455
456
457 for (i=0; i<nmax && items[i]->to==dest; i++)
458 {
459 # if DebugXfer<=0
460 XIOldCpl *xi = items[i];
461 Dune::dvverb
462 << "PrepareObjMsgs, proc=" << dest
463 << " i=" i << "/" << nmax << " (" << xi->te.gid << ")\n";
464 # endif
465 }
466
467 *nItems = i;
468 return xm;
469 }
470
471
472
473 /****************************************************************************/
474
475 /*
476 prepare messages for phase 1.
477
478 object copies will be sent as well as the estimated
479 coupling closure from CplClosureEstimate().
480 */
481
482 int PrepareObjMsgs (DDD::DDDContext& context,
483 std::vector<XICopyObj*>& arrayO,
484 XINewCpl **itemsNC, int nNC,
485 XIOldCpl **itemsOC, int nOC,
486 XFERMSG **theMsgs, size_t *memUsage)
487 {
488 auto& ctx = context.xferContext();
489 const auto& procs = context.procs();
490
491 XFERMSG *xm=NULL;
492 int iO, iNC, iOC, nMsgs=0;
493
494 XICopyObj** itemsO = arrayO.data();
495 const int nO = arrayO.size();
496
497
498 # if DebugXfer<=3
499 Dune::dverb << "PrepareObjMsgs,"
500 << " nXICopyObj=" << nO
501 << " nXINewCpl=" << nNC
502 << " nXIOldCpl=" << nOC << "\n";
503 # endif
504
505
506 /*
507 run through both itemsO and itemsNC/itemsOC simultaneously,
508 each time a new proc-nr is encountered in one of these
509 lists, create a new XFERMSG item.
510
511 (the lists have been sorted according to proc-nr previously.)
512 */
513
514 iO=0; iNC=0; iOC=0;
515 while (iO<nO || iNC<nNC || iOC<nOC)
516 {
517 int n;
518 DDD_PROC pO = (iO<nO) ? itemsO[iO]->dest : procs;
519 DDD_PROC pNC = (iNC<nNC) ? itemsNC[iNC]->to : procs;
520 DDD_PROC pOC = (iOC<nOC) ? itemsOC[iOC]->to : procs;
521
522 if (pO<=pNC && pO<=pOC && pO<procs)
523 {
524 xm = AccumXICopyObj(context, xm, &nMsgs, &n, itemsO+iO, pO, nO-iO);
525 xm->xferObjArray = itemsO+iO;
526 xm->nObjItems = n;
527 iO += n;
528 }
529
530 if (pNC<=pO && pNC<=pOC && pNC<procs)
531 {
532 xm = AccumXINewCpl(xm, &nMsgs, &n, itemsNC+iNC, pNC, nNC-iNC);
533 xm->xferNewCpl = itemsNC+iNC;
534 xm->nNewCpl = n;
535 iNC += n;
536 }
537
538 if (pOC<=pO && pOC<=pNC && pOC<procs)
539 {
540 xm = AccumXIOldCpl(xm, &nMsgs, &n, itemsOC+iOC, pOC, nOC-iOC);
541 xm->xferOldCpl = itemsOC+iOC;
542 xm->nOldCpl = n;
543 iOC += n;
544 }
545
546 if (pO==procs) iO = nO;
547 if (pNC==procs) iNC = nNC;
548 if (pOC==procs) iOC = nOC;
549 }
550 *theMsgs = xm;
551
552
553 /* compute brutto message size from netto message size */
554 for(xm=*theMsgs; xm!=NULL; xm=xm->next)
555 {
556 size_t bufSize;
557 xm->msg_h = LC_NewSendMsg(context, ctx.objmsg_t, xm->proc);
558 LC_SetTableSize(xm->msg_h, ctx.symtab_id, xm->nPointers);
559 LC_SetTableSize(xm->msg_h, ctx.objtab_id, xm->nObjects);
560 LC_SetTableSize(xm->msg_h, ctx.newcpl_id, xm->nNewCpl);
561 LC_SetTableSize(xm->msg_h, ctx.oldcpl_id, xm->nOldCpl);
562 LC_SetChunkSize(xm->msg_h, ctx.objmem_id, xm->size);
563
564 bufSize = LC_MsgFreeze(xm->msg_h);
565 *memUsage += bufSize;
566
567 if (DDD_GetOption(context, OPT_INFO_XFER) & XFER_SHOW_MEMUSAGE)
568 {
569 using std::setw;
570 Dune::dwarn
571 << "DDD MESG [" << setw(3) << "]: SHOW_MEM send msg "
572 << " dest=" << setw(4) << xm->proc
573 << " size=" << setw(10) << bufSize << "\n";
574 }
575 }
576
577
578 # if DebugXfer<=3
579 Dune::dverb << "PrepareObjMsgs, nMsgs=" << nMsgs << "\n";
580 # endif
581
582 return(nMsgs);
583 }
584
585
586
587 /****************************************************************************/
588
589 /*
590 execute SetPrio-commands and create those XIModCpl-items,
591 which can be computed without knowledge of information sent by other
592 procs during first message phase.
593 */
594 void ExecLocalXISetPrio (
595 DDD::DDDContext& context,
596 const std::vector<XISetPrio*>& arrayP,
597 XIDelObj **itemsD, int nD,
598 XICopyObj **itemsNO, int nNO)
599 {
600 int iP, iD, iNO;
601 XISetPrio* const* itemsP = arrayP.data();
602 const int nP = arrayP.size();
603
604 /*
605 execute SetPrio only if no corresponding DelObj exists!
606 */
607 for(iP=0, iD=0, iNO=0; iP<nP; iP++)
608 {
609 XISetPrio *sp = itemsP[iP];
610 DDD_HDR hdr = sp->hdr;
611 DDD_GID gid = sp->gid;
612 DDD_PRIO newprio = sp->prio;
613
614 while ((iD<nD) && (itemsD[iD]->gid<gid))
615 iD++;
616
617 /* skip XICopyObj-items until entries for gid found */
618 while (iNO<nNO && itemsNO[iNO]->gid<gid)
619 iNO++;
620
621
622 sp->is_valid = (! ((iD<nD) && (itemsD[iD]->gid==gid)));
623
624 if (sp->is_valid)
625 {
626 /* SetPrio, but _no_ DelObj: execute SetPrio */
627 DDD_TYPE typ = OBJ_TYPE(hdr);
628 const TYPE_DESC& desc = context.typeDefs()[typ];
629
630 /* call application handler for changing prio of dependent objects */
631 if (desc.handlerSETPRIORITY)
632 {
633 DDD_OBJ obj = HDR2OBJ(hdr, &desc);
634
635 desc.handlerSETPRIORITY(context, obj, newprio);
636 }
637
638 /* change actual priority to new value */
639 OBJ_PRIO(hdr) = newprio;
640
641
642 /* generate XIModCpl-items */
643
644 /* 1. for all existing couplings */
645 for (COUPLING *cpl = ObjCplList(context, hdr); cpl != NULL; cpl = CPL_NEXT(cpl))
646 {
647 XIModCpl *xc = NewXIModCpl(context);
648 if (xc==NULL)
649 throw std::bad_alloc();
650
651 xc->to = CPL_PROC(cpl); /* receiver of XIModCpl */
652 xc->te.gid = gid; /* the object's gid */
653 xc->te.prio = newprio; /* the object's new prio */
654 xc->typ = typ; /* the object's type */
655 }
656 /* 2. for all CopyObj-items with new-owner destinations */
657 while (iNO<nNO && itemsNO[iNO]->gid==gid)
658 {
659 XIModCpl *xc = NewXIModCpl(context);
660 if (xc==NULL)
661 throw std::bad_alloc();
662
663 xc->to = itemsNO[iNO]->dest; /* receiver of XIModCpl */
664 xc->te.gid = gid; /* the object's gid */
665 xc->te.prio = newprio; /* the object's new prio */
666 xc->typ = typ; /* the object's type */
667
668 iNO++;
669 }
670 }
671 /*
672 else: SetPrio _and_ DelObj, SetPrio is invalid,
673 DelObj will be executed lateron
674 (this is rule XFER-M1).
675 */
676 }
677
678 }
679
680
681
682 /*
683 execute local DelObj-commands and create those XIDelCpl-items,
684 which can be computed without knowledge of information sent by other
685 procs during first message phase.
686 */
687
688
689 void ExecLocalXIDelCmd (DDD::DDDContext& context, XIDelCmd **itemsD, int nD)
690 {
691 int iD;
692 XIDelCmd **origD;
693
694 if (nD==0)
695 return;
696
697 /* reconstruct original order of DelObj commands */
698 origD = (XIDelCmd **) OO_Allocate (sizeof(XIDelCmd *) * nD);
699 if (origD==NULL)
700 throw std::bad_alloc();
701
702 /* copy pointer array and resort it */
703 memcpy(origD, itemsD, sizeof(XIDelCmd *) * nD);
704 OrigOrderXIDelCmd(context, origD, nD);
705
706
707 /* loop in original order (order of Del-cmd issuing) */
708 for(iD=0; iD<nD; iD++)
709 {
710 DDD_HDR hdr = origD[iD]->hdr;
711 DDD_TYPE typ = OBJ_TYPE(hdr);
712 const TYPE_DESC& desc = context.typeDefs()[typ];
713 DDD_OBJ obj = HDR2OBJ(hdr, &desc);
714
715 /* do deletion */
716 if (desc.handlerDELETE)
717 desc.handlerDELETE(context, obj);
718 else
719 {
720 /* TODO the following three calls should be collected in
721 one ObjMgr function */
722
723 /* destruct LDATA and GDATA */
724 if (desc.handlerDESTRUCTOR)
725 desc.handlerDESTRUCTOR(context, obj);
726
727 /* HdrDestructor will call ddd_XferRegisterDelete() */
728 DDD_HdrDestructor(context, hdr);
729 DDD_ObjDelete(obj, desc.size, typ);
730 }
731 }
732
733 OO_Free (origD /*,0*/);
734 }
735
736
737
738
739 void ExecLocalXIDelObj (
740 DDD::DDDContext& context,
741 XIDelObj **itemsD, int nD,
742 XICopyObj **itemsNO, int nNO)
743 {
744 int iD, iNO;
745
746
747 /* create XIDelCpl for all DelObj-commands (sorted acc. to gid) */
748 for(iD=0, iNO=0; iD<nD; iD++)
749 {
750 DDD_GID gid = itemsD[iD]->gid;
751
752
753 /* skip XICopyObj-items until entries for gid found */
754 while (iNO<nNO && itemsNO[iNO]->gid<gid)
755 iNO++;
756
757
758 /* generate XIDelCpl-items */
759 /* 1. for all existing couplings, has been done during
760 ddd_XferRegisterDelete. */
761
762 /* 2. for all CopyObj-items with new-owner destinations */
763 while (iNO<nNO && itemsNO[iNO]->gid==gid)
764 {
765 XIDelCpl *xc = NewXIDelCpl(context);
766 if (xc==NULL)
767 throw std::bad_alloc();
768
769 xc->to = itemsNO[iNO]->dest; /* receiver of XIDelCpl */
770 xc->prio = PRIO_INVALID; /* dont remember priority */
771 xc->te.gid = gid; /* the object's gid */
772
773
774 /* we must remember couplings for eventual restoring
775 (if this object is received from another proc) */
776 xc->next = itemsD[iD]->delcpls;
777 itemsD[iD]->delcpls = xc;
778
779 iNO++;
780 }
781 }
782 }
783
784
785
786
787 /****************************************************************************/
788
789
790 /*
791 create those XI???Cpl-items, which require knowledge of information
792 sent by other procs during first message phase.
793 */
794 void PropagateCplInfos (
795 DDD::DDDContext& context,
796 XISetPrio **itemsP, int nP,
797 XIDelObj **itemsD, int nD,
798 TENewCpl *arrayNC, int nNC)
799 {
800 int iP, iD, iNC;
801
802 /*
803 step 1: create XIModCpl-items from SetPrio-cmds
804 (only if no DelObj-items exist)
805 */
806 for(iP=0, iNC=0; iP<nP; iP++)
807 {
808 XISetPrio *sp = itemsP[iP];
809
810 if (sp->is_valid)
811 {
812 DDD_HDR hdr = sp->hdr;
813 DDD_GID gid = sp->gid;
814 DDD_PRIO newprio = sp->prio;
815
816 /* skip TENewCpl-entries until one for gid found */
817 while (iNC<nNC && NewCpl_GetGid(arrayNC[iNC])<gid)
818 iNC++;
819
820 /* generate additional XIModCpl-items for all valid NewCpl-items */
821 while (iNC<nNC && NewCpl_GetGid(arrayNC[iNC])==gid)
822 {
823 XIModCpl *xc = NewXIModCpl(context);
824 if (xc==NULL)
825 throw std::bad_alloc();
826
827 /* receiver of XIModCpl */
828 xc->to = NewCpl_GetDest(arrayNC[iNC]);
829 xc->te.gid = gid; /* the object's gid */
830 xc->te.prio = newprio; /* the object's new prio */
831 xc->typ = OBJ_TYPE(hdr); /* the object's type */
832
833 iNC++;
834 }
835 }
836 }
837
838
839
840 /*
841 step 2: create XIDelCpl-items from DelObj-cmds
842 */
843 for(iD=0, iNC=0; iD<nD; iD++)
844 {
845 DDD_GID gid = itemsD[iD]->gid;
846
847 /* skip TENewCpl-entries until one for gid found */
848 while (iNC<nNC && NewCpl_GetGid(arrayNC[iNC])<gid)
849 iNC++;
850
851 /* generate additional XIDelCpl-items for all valid NewCpl-items */
852 while (iNC<nNC && NewCpl_GetGid(arrayNC[iNC])==gid)
853 {
854 XIDelCpl *xc = NewXIDelCpl(context);
855 if (xc==NULL)
856 throw std::bad_alloc();
857
858 xc->to = NewCpl_GetDest(arrayNC[iNC]); /* receiver of XIDelCpl */
859 xc->prio = PRIO_INVALID;
860 xc->te.gid = gid; /* the object's gid */
861 /*
862 printf("%4d: DelCpl 3 %08x %d %d\n",me,gid,xc->to,xc->prio);
863 */
864
865 iNC++;
866 }
867 }
868 }
869
870
871
872
873
874 /****************************************************************************/
875
876
877 /*
878 this function is called by DDD_HdrDestructor!
879 */
880 void ddd_XferRegisterDelete (DDD::DDDContext& context, DDD_HDR hdr)
881 {
882 COUPLING *cpl;
883 XIDelObj *xi;
884
885 /* create new XIDelObj */
886 xi = NewXIDelObj(context);
887 if (xi==NULL)
888 throw std::bad_alloc();
889
890 xi->gid = OBJ_GID(hdr);
891 xi->delcpls = NULL;
892
893 /*
894 now generate XIDelCpl-items, one for each existing coupling.
895 these items serve as notification of this delete operation
896 for remote processors with same object.
897 these items are also a intermediate storage for the object's
898 coupling list, in case the object is received after deletion
899 and the coupling list must be restored.
900 */
901 for(cpl=ObjCplList(context, hdr); cpl!=NULL; cpl=CPL_NEXT(cpl))
902 {
903 XIDelCpl *xc = NewXIDelCpl(context);
904 if (xc==NULL)
905 throw std::bad_alloc();
906
907 xc->to = CPL_PROC(cpl); /* receiver of XIDelCpl */
908 xc->prio = cpl->prio; /* remember priority */
909 xc->te.gid = OBJ_GID(hdr); /* the object's gid */
910
911 /* we must remember couplings for eventual restoring
912 (if this object is received from another proc) */
913 xc->next = xi->delcpls;
914 xi->delcpls = xc;
915 }
916 }
917
918
919
920 /****************************************************************************/
921
922 /*
923 management functions for XferMode.
924
925 these functions control the mode the xfer-module is
926 currently in. this is used for error detection, but
927 also for correct detection of coupling inconsistencies
928 and recovery.
929 */
930
931 const char *XferModeName (enum XferMode mode)
932 {
933 switch(mode)
934 {
935 case DDD::Xfer::XferMode::XMODE_IDLE : return "idle-mode";
936 case DDD::Xfer::XferMode::XMODE_CMDS : return "commands-mode";
937 case DDD::Xfer::XferMode::XMODE_BUSY : return "busy-mode";
938 }
939 return "unknown-mode";
940 }
941
942
943 static void XferSetMode(DDD::DDDContext& context, enum XferMode mode)
944 {
945 auto& ctx = context.xferContext();
946
947 ctx.xferMode = mode;
948
949 # if DebugXfer<=8
950 Dune::dwarn << "XferMode=" << XferModeName(ctx.xferMode) << "\n";
951 # endif
952 }
953
954
955 static enum XferMode XferSuccMode (enum XferMode mode)
956 {
957 switch(mode)
958 {
959 case DDD::Xfer::XferMode::XMODE_IDLE : return DDD::Xfer::XferMode::XMODE_CMDS;
960 case DDD::Xfer::XferMode::XMODE_CMDS : return DDD::Xfer::XferMode::XMODE_BUSY;
961 case DDD::Xfer::XferMode::XMODE_BUSY : return DDD::Xfer::XferMode::XMODE_IDLE;
962 }
963 DUNE_THROW(Dune::InvalidStateException, "invalid XferMode");
964 }
965
966
967
968 enum XferMode XferMode (const DDD::DDDContext& context)
969 {
970 return context.xferContext().xferMode;
971 }
972
973
974 int ddd_XferActive(const DDD::DDDContext& context)
975 {
976 return context.xferContext().xferMode != DDD::Xfer::XferMode::XMODE_IDLE;
977 }
978
979
980 int XferStepMode (DDD::DDDContext& context, enum XferMode old)
981 {
982 const auto& ctx = context.xferContext();
983
984 if (ctx.xferMode!=old)
985 {
986 Dune::dwarn
987 << "wrong xfer-mode (currently in " << XferModeName(ctx.xferMode)
988 << ", expected " << XferModeName(old) << ")\n";
989 return false;
990 }
991
992 XferSetMode(context, XferSuccMode(ctx.xferMode));
993 return true;
994 }
995
996
997 /****************************************************************************/
998
999
1000 void ddd_XferInit(DDD::DDDContext& context)
1001 {
1002 auto& ctx = context.xferContext();
1003
1004 /* init control structures for XferInfo-items in first (?) message */
1005 ctx.setXICopyObj = reinterpret_cast<DDD::Xfer::XICopyObjSet*>(New_XICopyObjSet());
1006 reinterpret_cast<XICopyObjSet*>(ctx.setXICopyObj)->tree->context = &context;
1007 ctx.setXISetPrio = reinterpret_cast<DDD::Xfer::XISetPrioSet*>(New_XISetPrioSet());
1008 reinterpret_cast<XISetPrioSet*>(ctx.setXISetPrio)->tree->context = &context;
1009 InitXIDelCmd(context);
1010 InitXIDelObj(context);
1011 InitXINewCpl(context);
1012 InitXIOldCpl(context);
1013
1014 /* init control structures for XferInfo-items for second (?) message */
1015 InitXIDelCpl(context);
1016 InitXIModCpl(context);
1017 InitXIAddCpl(context);
1018
1019
1020 XferSetMode(context, DDD::Xfer::XferMode::XMODE_IDLE);
1021
1022 ctx.objmsg_t = LC_NewMsgType(context, "XferMsg");
1023 ctx.symtab_id = LC_NewMsgTable("SymTab",
1024 ctx.objmsg_t, sizeof(SYMTAB_ENTRY));
1025 ctx.objtab_id = LC_NewMsgTable("ObjTab",
1026 ctx.objmsg_t, sizeof(OBJTAB_ENTRY));
1027 ctx.newcpl_id = LC_NewMsgTable("NewCpl",
1028 ctx.objmsg_t, sizeof(TENewCpl));
1029 ctx.oldcpl_id = LC_NewMsgTable("OldCpl",
1030 ctx.objmsg_t, sizeof(TEOldCpl));
1031 ctx.objmem_id = LC_NewMsgChunk("ObjMem",
1032 ctx.objmsg_t);
1033
1034 /* not used anymore
1035 ctx.deltab_id =
1036 LC_NewMsgTable(ctx.objmsg_t, sizeof(DELTAB_ENTRY));
1037 ctx.priotab_id =
1038 LC_NewMsgTable(ctx.objmsg_t, sizeof(CPLTAB_ENTRY));
1039 */
1040
1041 CplMsgInit(context);
1042 CmdMsgInit(context);
1043 }
1044
1045
1046 void ddd_XferExit(DDD::DDDContext& context)
1047 {
1048 auto& ctx = context.xferContext();
1049
1050 CmdMsgExit(context);
1051 CplMsgExit(context);
1052
1053 XICopyObjSet_Free(reinterpret_cast<XICopyObjSet*>(ctx.setXICopyObj));
1054 XISetPrioSet_Free(reinterpret_cast<XISetPrioSet*>(ctx.setXISetPrio));
1055 }
1056
1057
1058
1059
1060 END_UGDIM_NAMESPACE