Debian Codegraph @

dune/uggrid/parallel/ddd/xfer/unpack.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 /****************************************************************************/
4 /* */
5 /* File: unpack.c */
6 /* */
7 /* Purpose: receives and unpacks messages */
8 /* */
9 /* Author: Klaus Birken */
10 /* Rechenzentrum Uni Stuttgart */
11 /* Universitaet Stuttgart */
12 /* Allmandring 30 */
13 /* 70550 Stuttgart */
14 /* internet: [email protected] */
15 /* */
16 /* History: 940201 kb begin */
17 /* 960508 kb restructured completely. efficiency improvement. */
18 /* 960718 kb introduced lowcomm-layer (sets of messages) */
19 /* */
20 /* Remarks: */
21 /* */
22 /****************************************************************************/
23
24 /****************************************************************************/
25 /* */
26 /* include files */
27 /* system include files */
28 /* application include files */
29 /* */
30 /****************************************************************************/
31
32 /* standard C library */
33 #include <config.h>
34 #include <cinttypes>
35 #include <cstdlib>
36 #include <cstdio>
37 #include <cstring>
38 #include <cassert>
39
40 #include <algorithm>
41 #include <iomanip>
42 #include <tuple>
43
44 #include <dune/common/exceptions.hh>
45 #include <dune/common/stdstreams.hh>
46
47 #include <dune/uggrid/low/namespace.h>
48 #include <dune/uggrid/low/ugtypes.h>
49
50 #include <dune/uggrid/parallel/ddd/dddi.h>
51 #include "xfer.h"
52
53 USING_UG_NAMESPACE
54 using namespace PPIF;
55
56 START_UGDIM_NAMESPACE
57
58 /* TODO kb 961210
59 #define DEBUG_MERGE_MODE
60 */
61 #define MERGE_MODE_IN_TESTZUSTAND
62
63
64
65 /*#define DebugCouplingCons*/
66
67
68 /*
69 #define AddCoupling(context, a,b,c) printf("%4d: AC %05d, %d/%d %08x\n",context.me(),__LINE__,b,c,(int) AddCoupling(context, a,b,c))
70 */
71
72
73 static void NEW_AddCpl(DDD::DDDContext& context, DDD_PROC destproc, DDD_GID objgid, DDD_PROC cplproc, DDD_PRIO cplprio)
74 {
75 XIAddCpl *xc = NewXIAddCpl(context);
76 assert(xc);
77 xc->to = destproc;
78 xc->te.gid = objgid;
79 xc->te.proc = cplproc;
80 xc->te.prio = cplprio;
81 }
82
83
84 static bool sort_TENewCpl (const TENewCpl& a, const TENewCpl& b)
85 {
86 /* sorting according to priority is not necessary anymore,
87 equal items with different priorities will be sorted
88 out according to PriorityMerge(). KB 970326
89 if (ci1->prio < ci2->prio) return(-1);
90 if (ci1->prio > ci2->prio) return(1);
91 */
92 return std::tie(a._gid, a._dest) < std::tie(b._gid, b._dest);
93 }
94
95
96 static bool sort_ObjTabPtrs (const OBJTAB_ENTRY* a, const OBJTAB_ENTRY* b)
97 {
98 /* sort with ascending gid */
99 /* sort with decreasing priority */
100 /* not necessary anymore. see first phase of
101 AcceptReceivedObjects() for details. KB 970128
102 if (OBJ_PRIO(ci1) < OBJ_PRIO(ci2)) return(1);
103 if (OBJ_PRIO(ci1) > OBJ_PRIO(ci2)) return(-1);
104 */
105 return OBJ_GID(a->hdr) < OBJ_GID(b->hdr);
106 }
107
108
109
110 /****************************************************************************/
111
112
113 /*
114 convert indices to symtab into references (pointers).
115 the object objmem gets all references from template msgmem.
116 msgmem and objmem may point to the same storage (this feature is
117 used by PutDepData() ).
118 */
119
120 static void LocalizeObject (DDD::DDDContext& context, bool merge_mode, TYPE_DESC *desc,
121 const char *msgmem,
122 DDD_OBJ objmem,
123 const SYMTAB_ENTRY *theSymTab)
124 {
125 ELEM_DESC *theElem;
126 int e;
127 DDD_OBJ obj = objmem;
128
129 /* prepare map of structure elements */
130 theElem = desc->element;
131
132 /* loop over all pointers inside of object obj */
133 for(e=0; e<desc->nElements; e++, theElem++)
134 {
135 if (theElem->type==EL_OBJPTR)
136 {
137 TYPE_DESC *refdesc;
138 int rt_on_the_fly = (EDESC_REFTYPE(theElem)==DDD_TYPE_BY_HANDLER);
139
140 const char *msgrefarray = msgmem+theElem->offset;
141 char *objrefarray = objmem+theElem->offset;
142
143
144 /* determine reftype of this elem */
145 if (! rt_on_the_fly)
146 {
147 refdesc = &context.typeDefs()[EDESC_REFTYPE(theElem)];
148 }
149 /* else determine reftype on the fly */
150
151
152
153 /* loop over single pointer array */
154 for(std::size_t l=0; l<theElem->size; l+=sizeof(void*))
155 {
156 INT stIdx;
157
158 /* ref points to a reference inside objmem */
159 DDD_OBJ *ref = (DDD_OBJ *) (objrefarray+l);
160
161
162 /* reference had been replaced by SymTab-index */
163 stIdx = (*(std::uintptr_t *)(msgrefarray+l)) - 1;
164 /* test for Localize execution in merge_mode */
165 if (merge_mode && (*ref!=NULL))
166 {
167 if (rt_on_the_fly)
168 {
169 /* determine reftype on the fly by calling handler */
170 DDD_TYPE rt;
171
172 assert(obj!=NULL);
173
174 rt = theElem->reftypeHandler(context, obj, *ref);
175
176 if (rt>=MAX_TYPEDESC)
177 DUNE_THROW(Dune::Exception,
178 "invalid referenced DDD_TYPE returned by handler");
179
180 refdesc = &context.typeDefs()[rt];
181 }
182
183 /* if we are in merge_mode, we do not update
184 existing references. */
185 #ifdef DEBUG_MERGE_MODE
186 printf("loc-merge curr=%08x keep e=%d l=%d\n",
187 OBJ_GID(OBJ2HDR(*ref,refdesc)), e,l);
188 #endif
189
190 /* it may happen here that different references
191 are in incoming and existing object. this is implicitly
192 resolved by using the existing reference and ignoring
193 the incoming one. if the REF_COLLISION option is set,
194 we will issue a warning.
195 */
196
197 if (stIdx>=0 &&
198 DDD_GetOption(context, OPT_WARNING_REF_COLLISION)==OPT_ON)
199 {
200 /* get corresponding symtab entry */
201 if (theSymTab[stIdx].adr.hdr!=OBJ2HDR(*ref,refdesc))
202 {
203 Dune::dwarn
204 << "LocalizeObject: "
205 << "reference collision in " << OBJ_GID(OBJ2HDR(obj,desc))
206 << " (old=" << OBJ_GID(OBJ2HDR(*ref,refdesc))
207 << ", inc=" << OBJ_GID(theSymTab[stIdx].adr.hdr) << ")\n";
208 /* assert(0); ??? */
209 }
210 }
211
212 continue;
213 }
214
215
216 /*
217 at this point, we are either not in merge_mode
218 or we are in merge_mode, but existing reference is zero.
219
220 NOTE: only in merge_mode the objmem array points
221 to the reference array inside the local
222 local object!
223 */
224
225 if (stIdx>=0)
226 {
227 /* get corresponding symtab entry */
228 const SYMTAB_ENTRY *st = &(theSymTab[stIdx]);
229
230 /*
231 convert reference from header to object itself
232 and replace index by pointer; if header==NULL,
233 referenced object is not known and *ref should
234 therefore be NULL, too!
235 */
236
237 #ifdef MERGE_MODE_IN_TESTZUSTAND
238 if (merge_mode)
239 {
240 if (st->adr.hdr!=NULL)
241 {
242 #ifdef DEBUG_MERGE_MODE
243 printf("loc-merge curr=%08x "
244 "have_sym e=%d l=%d to %08x\n",
245 *ref, e,l,OBJ_GID(st->adr.hdr));
246 #endif
247
248 /* distinction for efficiency: if we know refdesc
249 in advance, we can compute DDD_OBJ more
250 efficient. */
251 if (!rt_on_the_fly)
252 {
253 *ref = HDR2OBJ(st->adr.hdr,refdesc);
254 }
255 else
256 {
257 *ref = OBJ_OBJ(context, st->adr.hdr);
258 }
259 }
260
261 #ifdef DEBUG_MERGE_MODE
262 else
263 {
264 printf(
265 "loc-merge curr=%08x "
266 "have_sym e=%d l=%d to NULL\n",
267 *ref, e, l);
268 }
269 #endif
270 }
271 else
272 #endif
273 {
274 if (st->adr.hdr!=NULL)
275 {
276 /* distinction for efficiency: if we know refdesc
277 in advance, we can compute DDD_OBJ more
278 efficient. */
279 if (!rt_on_the_fly)
280 {
281 *ref = HDR2OBJ(st->adr.hdr,refdesc);
282 }
283 else
284 {
285 *ref = OBJ_OBJ(context, st->adr.hdr);
286 }
287 }
288 else
289 *ref = NULL;
290 }
291 }
292 else
293 {
294 #ifdef MERGE_MODE_IN_TESTZUSTAND
295 if (merge_mode)
296 {
297 #ifdef DEBUG_MERGE_MODE
298 printf("loc-merge curr=%08x "
299 "no_sym e=%d l=%d\n",
300 *ref, e,l);
301 #endif
302 }
303 else
304 #endif
305 {
306 *ref = NULL;
307 }
308 }
309 }
310 }
311 }
312 }
313
314
315
316 /****************************************************************************/
317
318
319
320 static void PutDepData (DDD::DDDContext& context,
321 char *data,
322 const TYPE_DESC *desc,
323 DDD_OBJ obj,
324 const SYMTAB_ENTRY *theSymTab,
325 int newness)
326 {
327 /* get overall number of chunks */
328 const int chunks = ((int *)data)[0];
329 char* chunk = data + CEIL(sizeof(int));
330
331 char* curr = nullptr;
332
333 /* loop through all chunks */
334 for(int j=0; j<chunks; j++)
335 {
336 /* first entries of chunk are addCnt and addTyp */
337 int addCnt = ((int *)chunk)[0];
338 DDD_TYPE addTyp = ((DDD_TYPE *)chunk)[1];
339 chunk += CEIL(sizeof(int)+sizeof(DDD_TYPE));
340
341 if (addCnt>=0)
342 {
343 if (addTyp<DDD_USER_DATA || addTyp>DDD_USER_DATA_MAX)
344 {
345 /* convert pointers using SymTab */
346 TYPE_DESC* descDep = &context.typeDefs()[addTyp];
347 curr = chunk;
348 for(int i=0; i<addCnt; i++)
349 {
350 /* insert pointers into copy using SymTab */
351 if (descDep->nPointers>0)
352 {
353 LocalizeObject(context, false, descDep,
354 curr,
355 (DDD_OBJ)curr,
356 theSymTab);
357 }
358 curr += CEIL(descDep->size);
359 }
360 }
361 else
362 {
363 /* addType>=DDD_USER_DATA && addType <= DDD_USER_DATA_MAX ->
364 scatter stream of bytes with len addCnt */
365 curr = chunk + CEIL(addCnt);
366 }
367
368 /* scatter data via handler */
369 if (desc->handlerXFERSCATTER)
370 {
371 desc->handlerXFERSCATTER(context, obj, addCnt, addTyp, (void *)chunk, newness);
372 }
373 }
374 else
375 {
376 /* variable sized chunks */
377 addCnt *= -1;
378
379 /* convert offset table into pointer table */
380 TYPE_DESC* descDep = &context.typeDefs()[addTyp];
381 char** table = (char **)chunk;
382 chunk += CEIL(sizeof(int)*addCnt);
383 char* adr = chunk;
384 for(int i=0; i<addCnt; i++)
385 {
386 table[i] = ((long int)table[i])+adr;
387
388 /* insert pointers into copy using SymTab */
389 if (addTyp<DDD_USER_DATA || addTyp>DDD_USER_DATA_MAX)
390 {
391 curr = table[i];
392 if (descDep->nPointers>0)
393 LocalizeObject(context, false, descDep,
394 curr,
395 (DDD_OBJ)curr,
396 theSymTab);
397 }
398 }
399
400 /* scatter data via handler */
401 if (desc->handlerXFERSCATTERX)
402 {
403 desc->handlerXFERSCATTERX(context, obj, addCnt, addTyp, table, newness);
404 }
405 }
406
407 chunk = curr;
408 }
409 }
410
411
412
413
414 /****************************************************************************/
415
416
417 static void AcceptObjFromMsg (
418 DDD::DDDContext& context,
419 OBJTAB_ENTRY *theObjTab, int lenObjTab,
420 const char *theObjects,
421 const DDD_HDR *localCplObjs, int nLocalCplObjs)
422 {
423 int i, j;
424
425 for(i=0, j=0; i<lenObjTab; i++)
426 {
427 OBJTAB_ENTRY *ote = &theObjTab[i];
428 TYPE_DESC *desc = &context.typeDefs()[OBJ_TYPE(ote->hdr)];
429
430 if (ote->is_new == OTHERMSG)
431 {
432 /* object is in another message with higher priority */
433 continue;
434 }
435
436 while ((j<nLocalCplObjs) && (OBJ_GID(localCplObjs[j]) < OBJ_GID(ote->hdr)))
437 j++;
438
439 if ((j<nLocalCplObjs) && (OBJ_GID(localCplObjs[j])==OBJ_GID(ote->hdr)))
440 {
441 /* object already here, compare priorities.
442 this is the implementation of rule XFER-C3. */
443 DDD_PRIO newprio;
444
445 /* if local object should have been XferDelete'd, but the
446 delete-cmd had been pruned (see cmdmsg.c), a flag has been
447 set in its header. we have to ensure here that all incoming
448 objects win against pruned-deleted ones, because the object
449 serves only as 'cache' for data (esp. pointers) */
450 if (OBJ_PRUNED(localCplObjs[j]))
451 {
452 # if DebugUnpack<=1
453 Dune::dvverb << "NewPrio wins due to PruneDel. "
454 << OBJ_GID(ote->hdr) << "\n";
455 # endif
456
457 /* reset flag */
458 SET_OBJ_PRUNED(localCplObjs[j], 0);
459
460 /* simply copy new priority, disregard old one */
461 newprio = OBJ_PRIO(ote->hdr);
462
463 ote->is_new = PRUNEDNEW;
464 }
465 else
466 {
467 enum PrioMergeVals ret = PriorityMerge(desc,
468 OBJ_PRIO(ote->hdr), OBJ_PRIO(localCplObjs[j]), &newprio);
469
470 if (ret==PRIO_FIRST || ret==PRIO_UNKNOWN) /* incoming is higher or equal */
471 {
472 DDD_OBJ copy;
473
474 # if DebugUnpack<=1
475 Dune::dvverb << "NewPrio wins. " << OBJ_GID(ote->hdr) << "\n";
476 # endif
477
478 /* new priority wins -> recreate */
479 /* all GDATA-parts are overwritten by contents of message */
480 copy = OTE_OBJ(context, theObjects,ote);
481 ObjCopyGlobalData(desc,
482 HDR2OBJ(localCplObjs[j],desc), copy, ote->size);
483
484 ote->is_new = PARTNEW;
485 }
486 else /* existing is higher than incoming */
487 {
488 # if DebugUnpack<=1
489 Dune::dvverb << "OldPrio wins. " << OBJ_GID(ote->hdr) << "\n";
490 # endif
491
492 /* new priority loses -> keep existing obj */
493 ote->is_new = NOTNEW;
494 }
495 }
496
497 /* store pointer to local object */
498 /* overwrite pointer to hdr inside message */
499 if (OBJ_TYPE(ote->hdr) != OBJ_TYPE(localCplObjs[j]))
500 {
501 printf("ERROR, copying changed the object type!\n");
502 printf(" was: %s, becomes: %s\n",
503 context.typeDefs()[OBJ_TYPE(ote->hdr)].name,
504 context.typeDefs()[OBJ_TYPE(localCplObjs[j])].name);
505 assert(OBJ_TYPE(ote->hdr) == OBJ_TYPE(localCplObjs[j]));
506 }
507 ote->hdr = localCplObjs[j];
508
509 /* store old priority and set new one */
510 OTE_PRIO(theObjects,ote) = newprio;
511 ote->oldprio = OBJ_PRIO(localCplObjs[j]);
512
513 /* the next line is not useful. the current priority
514 of the involved object will be changed to newprio-value
515 after calling of handler SETPRIORITY. KB 970417 */
516 /* OBJ_PRIO(localCplObjs[j]) = newprio; */
517 }
518 else
519 {
520 DDD_OBJ msgcopy, newcopy;
521 DDD_PRIO new_prio = OBJ_PRIO(ote->hdr);
522
523 # if DebugUnpack<=1
524 Dune::dvverb << "NewObject " << OBJ_GID(ote->hdr)
525 << ", prio=" << new_prio << "\n";
526 # endif
527
528 /* new object, create local copy */
529 msgcopy = OTE_OBJ(context, theObjects,ote);
530 newcopy = DDD_ObjNew(ote->size,
531 OBJ_TYPE(ote->hdr), new_prio, OBJ_ATTR(ote->hdr));
532
533 /* overwrite pointer to hdr inside message */
534 ote->hdr = OBJ2HDR(newcopy,desc);
535
536 /* copy GDATA */
537 ObjCopyGlobalData(desc, newcopy, msgcopy, ote->size);
538 ote->is_new = TOTALNEW;
539
540 /* construct HDR */
541 DDD_HdrConstructorCopy(context, ote->hdr, new_prio);
542
543 /* construct LDATA */
544 if (desc->handlerLDATACONSTRUCTOR)
545 desc->handlerLDATACONSTRUCTOR(context, newcopy);
546 }
547 }
548 }
549
550
551
552 static void AcceptReceivedObjects (DDD::DDDContext& context,
553 const LC_MSGHANDLE *theMsgs, int nRecvMsgs,
554 OBJTAB_ENTRY **allRecObjs, int nRecObjs,
555 const DDD_HDR *localCplObjs, int nLocalCplObjs)
556 {
557 /*
558 allRecObjs is a pointer array to all OBJTAB_ENTRYs
559 received in incoming messages. it is sorted according
560 to (gid/ascending).
561
562 1. collision detection for incoming objects with same
563 gid: accept object with merged priority. if several
564 such objects exist, an arbitrary one is chosen.
565 discard all other objects with same gid. (RULE XFER-C2)
566
567 2. transfer objects from message into local memory.
568
569 3. propagate hdr-pointer to all OBJTAB_ENTRYs with equal gid.
570 */
571
572 auto& ctx = context.xferContext();
573 int i;
574
575 if (nRecObjs==0)
576 return;
577
578 /* 1. collision detection */
579 for(i=nRecObjs-1; i>0; i--)
580 {
581 if (OBJ_GID(allRecObjs[i]->hdr) != OBJ_GID(allRecObjs[i-1]->hdr))
582 {
583 allRecObjs[i]->is_new = THISMSG;
584 }
585 else
586 {
587 DDD_PRIO newprio;
588 int ret;
589
590 ret = PriorityMerge(&context.typeDefs()[OBJ_TYPE(allRecObjs[i]->hdr)],
591 OBJ_PRIO(allRecObjs[i]->hdr), OBJ_PRIO(allRecObjs[i-1]->hdr), &newprio);
592
593 if (ret==PRIO_FIRST || ret==PRIO_UNKNOWN)
594 {
595 /* item i is winner */
596 OBJTAB_ENTRY *tmp;
597
598 OBJ_PRIO(allRecObjs[i]->hdr) = newprio;
599
600 /* switch first item i in second position i-1 */
601 /* item on first position will be discarded */
602 tmp = allRecObjs[i];
603 allRecObjs[i] = allRecObjs[i-1];
604 allRecObjs[i-1] = tmp;
605 }
606 else
607 {
608 /* item i-1 is winner */
609 OBJ_PRIO(allRecObjs[i-1]->hdr) = newprio;
610 }
611
612 /* mark item i invalid */
613 allRecObjs[i]->is_new = OTHERMSG;
614 }
615 }
616 allRecObjs[0]->is_new = THISMSG;
617
618 /* now the first item in a series of items with equal gid is the
619 THISMSG-item, the following are OTHERMSG-items */
620
621
622 /* 2. transfer from message into local memory */
623 for(i=0; i<nRecvMsgs; i++)
624 {
625 LC_MSGHANDLE xm = theMsgs[i];
626
627 AcceptObjFromMsg(
628 context,
629 (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id),
630 (int) LC_GetTableLen(xm, ctx.objtab_id),
631 (char *) LC_GetPtr(xm, ctx.objmem_id),
632 localCplObjs, nLocalCplObjs
633 );
634 }
635
636
637 /* 3. propagate hdr-pointer */
638 for(i=1; i<nRecObjs; i++)
639 {
640 if (allRecObjs[i]->is_new == OTHERMSG)
641 {
642 /* propagate hdr-pointer */
643 allRecObjs[i]->hdr = allRecObjs[i-1]->hdr;
644 }
645 }
646 }
647
648
649
650 /****************************************************************************/
651
652
653 static void AddAndSpread (DDD::DDDContext& context,
654 DDD_HDR hdr, DDD_GID gid, DDD_PROC dest, DDD_PRIO prio,
655 XICopyObj **itemsNO, int nNO)
656 {
657 int k;
658
659 if (hdr!=NULL)
660 AddCoupling(context, hdr, dest, prio);
661
662 for(k=0; k<nNO; k++)
663 {
664 if (itemsNO[k]->dest != dest)
665 NEW_AddCpl(context, itemsNO[k]->dest, gid, dest, prio);
666 }
667 }
668
669
670 /*
671 this function updates the couplings of local objects.
672 the inputs for deciding which couplings have to be added are:
673
674 for prev. existing objects:
675 - sending to NEWOWNER-destinations
676 - incoming NewCpl-items for previously existing objects
677
678 for new (incoming) objects:
679 - incoming NewCpl-items for new objects
680
681 as a side effect this function sends XIAddCpl-items
682 to NEWOWNER-procs.
683 */
684
685
686 enum UpdateCpl_Cases { UCC_NONE, UCC_NO, UCC_NC, UCC_NO_AND_NC };
687
688
689 static void UpdateCouplings (
690 DDD::DDDContext& context,
691 TENewCpl *itemsNC, int nNC, /* NewCpl */
692 OBJTAB_ENTRY **itemsO, int nO, /* Objects */
693 const DDD_HDR *itemsLCO, int nLCO, /* local objs with coupling */
694 XIDelObj **itemsDO, int nDO, /* XIDelObj */
695 XICopyObj **itemsNO, int nNO) /* NewOwners */
696 {
697 const auto& me = context.me();
698 const auto& procs = context.procs();
699 int iNC, iO, iDO, iNO, iLCO;
700
701 /*
702 each NewCpl either corresponds to an incoming object
703 or to a local object, but not both.
704 */
705
706 /*** loop for all incoming objects ***/
707 for(iO=0, iNC=0, iDO=0; iO<nO; iO++)
708 {
709 DDD_HDR hdr = itemsO[iO]->hdr;
710 DDD_GID gid = OBJ_GID(hdr);
711
712 /* scan DelObj-entries for given gid */
713 while (iDO<nDO && itemsDO[iDO]->gid < gid)
714 iDO++;
715
716
717 /* if there is a DelObj-item with same gid, then the object
718 has been deleted and send by a remote proc afterwards.
719 we must:
720 - restore old couplings locally
721 - invalidate XIDelCpl-items
722 */
723 if (iDO<nDO && itemsDO[iDO]->gid == gid)
724 {
725 XIDelCpl *dc = itemsDO[iDO]->delcpls;
726 for( ; dc!=NULL; dc=dc->next)
727 {
728 /* restore previous coupling */
729 if (dc->prio!=PRIO_INVALID)
730 AddCoupling(context, hdr, dc->to, dc->prio);
731
732 /* invalidate XIDelCpl-item */
733 dc->to=procs;
734 }
735
736 /* restore only one time */
737 itemsDO[iDO]->delcpls = NULL;
738 }
739
740
741 /* scan NewCpl-entries for given gid */
742 while (iNC<nNC && NewCpl_GetGid(itemsNC[iNC]) < gid)
743 iNC++;
744
745 /* for all NewCpl-Items with same gid as incoming object */
746 while (iNC<nNC && NewCpl_GetGid(itemsNC[iNC]) == gid)
747 {
748 /* there is a corresponding NewCpl-item */
749 AddCoupling(context, hdr,
750 NewCpl_GetDest(itemsNC[iNC]),
751 NewCpl_GetPrio(itemsNC[iNC]));
752 NEW_AddCpl(context, NewCpl_GetDest(itemsNC[iNC]), gid, me, OBJ_PRIO(hdr));
753
754 iNC++;
755 }
756 }
757
758
759
760 /*** loop for previously existing objects ***/
761 iNO=iNC=iLCO=iDO=iO=0;
762 while (iNO<nNO || iNC<nNC)
763 {
764 DDD_HDR hdrNO, hdrNC;
765 DDD_GID gidNO, gidNC;
766 int moreNO, moreNC, curr_case;
767 int firstNC,lastNC;
768 int firstNO,lastNO,nNOset;
769 XICopyObj **setNO;
770
771 /* scan all NewOwner-items with same gid, and set first/last indices */
772 firstNO = iNO;
773 while (iNO<nNO-1 && itemsNO[iNO+1]->gid==itemsNO[iNO]->gid)
774 iNO++;
775 lastNO = iNO;
776 nNOset = 1+lastNO-firstNO;
777 setNO = itemsNO+firstNO;
778
779 /* scan all NewCpl-items with same gid, and set first/last indices */
780 firstNC = iNC;
781 while (iNC<nNC-1 && NewCpl_GetGid(itemsNC[iNC+1])==NewCpl_GetGid(itemsNC[iNC]))
782 iNC++;
783 lastNC = iNC;
784
785 /*
786 printf("%4d: MULTILOOP NewOwner %3d-%3d of %3d NewCpl %3d-%3d of %3d\n",
787 me, firstNO, lastNO, nNO, firstNC, lastNC, nNC);
788 */
789
790 /* set control flags */
791 moreNO = (iNO<nNO); if (moreNO) gidNO = setNO[0]->gid;
792 moreNC = (iNC<nNC); if (moreNC) gidNC = NewCpl_GetGid(itemsNC[firstNC]);
793
794 curr_case = UCC_NONE;
795 if (moreNO && (!moreNC || gidNO<gidNC))
796 curr_case = UCC_NO;
797 if (moreNC && (!moreNO || gidNC<gidNO))
798 curr_case = UCC_NC;
799 if (moreNO && moreNC && gidNC==gidNO)
800 curr_case = UCC_NO_AND_NC;
801
802
803 /* find DDD_HDR for given gid */
804 hdrNO = hdrNC = NULL;
805 if (moreNC)
806 {
807 /* scan local objects with couplings */
808 while (iLCO<nLCO && OBJ_GID(itemsLCO[iLCO])<gidNC) iLCO++;
809 if (iLCO<nLCO && OBJ_GID(itemsLCO[iLCO])==gidNC)
810 hdrNC = itemsLCO[iLCO];
811 }
812 if (moreNO)
813 {
814 /* check whether obj has been deleted during this xfer */
815 while (iDO<nDO && itemsDO[iDO]->gid < gidNO) iDO++;
816 if (! (iDO<nDO && itemsDO[iDO]->gid==gidNO))
817 {
818 /* there is no DelObj-item */
819 hdrNO = setNO[0]->hdr;
820 }
821
822 /* scan received objects */
823 while (iO<nO && OBJ_GID(itemsO[iO]->hdr) < gidNO) iO++;
824 if (iO<nO && OBJ_GID(itemsO[iO]->hdr)==gidNO)
825 {
826 /* obj has been deleted and received again */
827 assert(hdrNO==NULL || hdrNO==itemsO[iO]->hdr);
828 hdrNO = itemsO[iO]->hdr;
829 }
830 }
831
832
833 switch (curr_case)
834 {
835 case UCC_NONE : /* no other case is valid -> do nothing */
836 break;
837
838
839 case UCC_NO : /* there is a NewOwner set without NewCpl set */
840 {
841 int jNO;
842
843 for(jNO=0; jNO<nNOset; jNO++)
844 {
845 /* there is no NewCpl-item for given dest */
846 AddAndSpread(context, hdrNO, gidNO, setNO[jNO]->dest,
847 setNO[jNO]->prio, setNO, nNOset);
848 }
849
850 /* step to next gid */
851 iNO = lastNO+1;
852 iNC = firstNC;
853 }
854 break;
855
856
857 case UCC_NC : /* there is a NewCpl set without NewOwner set */
858 {
859 int jNC;
860
861 for(jNC=firstNC; jNC<=lastNC; jNC++)
862 {
863 if (hdrNC!=NULL)
864 AddCoupling(context, hdrNC,
865 NewCpl_GetDest(itemsNC[jNC]),
866 NewCpl_GetPrio(itemsNC[jNC]));
867 /* else: dont need to AddCpl to deleted object */
868 }
869
870 /* step to next gid */
871 iNC = lastNC+1;
872 iNO = firstNO;
873 }
874 break;
875
876
877 case UCC_NO_AND_NC : /* there are both NewCpl and NewOwner sets */
878 {
879 DDD_HDR hdr;
880 int jNO, jNC;
881
882 /* same gids -> same object and header */
883 assert(hdrNO==NULL || hdrNC==NULL || hdrNO==hdrNC);
884
885 if (hdrNO==NULL) hdr = hdrNC;
886 else hdr = hdrNO;
887
888 jNC = firstNC;
889 for(jNO=0; jNO<nNOset; jNO++)
890 {
891 /* scan NewCpl-items for given dest processor */
892 while (jNC<=lastNC && NewCpl_GetDest(itemsNC[jNC]) < setNO[jNO]->dest)
893 {
894 AddAndSpread(context, hdr, gidNO,
895 NewCpl_GetDest(itemsNC[jNC]),
896 NewCpl_GetPrio(itemsNC[jNC]),
897 setNO, nNOset);
898 jNC++;
899 }
900
901 if (jNC<=lastNC && NewCpl_GetDest(itemsNC[jNC]) == setNO[jNO]->dest)
902 {
903 /* found NewCpl-item */
904 DDD_PRIO newprio;
905
906 PriorityMerge(&context.typeDefs()[NewCpl_GetType(itemsNC[jNC])],
907 setNO[jNO]->prio, NewCpl_GetPrio(itemsNC[jNC]), &newprio);
908
909 AddAndSpread(context, hdr, gidNO, setNO[jNO]->dest, newprio,
910 setNO, nNOset);
911 jNC++;
912 }
913 else
914 {
915 /* there is no NewCpl-item for given dest */
916 AddAndSpread(context, hdr, gidNO, setNO[jNO]->dest, setNO[jNO]->prio,
917 setNO, nNOset);
918 }
919 }
920 while (jNC<=lastNC)
921 {
922 AddAndSpread(context, hdr, gidNO,
923 NewCpl_GetDest(itemsNC[jNC]),
924 NewCpl_GetPrio(itemsNC[jNC]),
925 setNO, nNOset);
926 jNC++;
927 }
928
929 /* step to next gid */
930 iNO = lastNO+1;
931 iNC = lastNC+1;
932 }
933 break;
934
935 default :
936 assert(0);
937 break;
938 }
939 }
940 }
941
942
943
944
945 /*
946 this function handles local objects, which had been here before
947 xfer. during xfer, another object with same gid was received
948 and lead to a higher priority. this priority change must
949 be communicated to all destination-procs, to which the local
950 processor sent a copy during xfer.
951 */
952 static void PropagateIncomings (
953 DDD::DDDContext& context,
954 XICopyObj **arrayNO, int nNO,
955 OBJTAB_ENTRY **allRecObjs, int nRecObjs)
956 {
957 int iRO, iNO;
958
959 for(iRO=0, iNO=0; iRO<nRecObjs; iRO++)
960 {
961 int newness = allRecObjs[iRO]->is_new;
962
963 if (newness==PARTNEW || newness==PRUNEDNEW || newness==TOTALNEW)
964 {
965 COUPLING *cpl;
966
967 /* object has been local before, but changed its prio */
968 OBJTAB_ENTRY *ote = allRecObjs[iRO];
969
970 /* scan received objects */
971 while ((iNO<nNO) && (arrayNO[iNO]->gid < OBJ_GID(ote->hdr)))
972 iNO++;
973
974 /* communicate to all NEWOWNER-destinations */
975 while (iNO<nNO && arrayNO[iNO]->gid == OBJ_GID(ote->hdr))
976 {
977 if (newness==PARTNEW || newness==PRUNEDNEW)
978 {
979 XIModCpl *xc = NewXIModCpl(context);
980 if (xc==NULL)
981 throw std::bad_alloc();
982
983 xc->to = arrayNO[iNO]->dest; /* receiver of XIModCpl*/
984 xc->te.gid = OBJ_GID(ote->hdr); /* the object's gid */
985 xc->te.prio = OBJ_PRIO(ote->hdr); /* the obj's new prio */
986 xc->typ = OBJ_TYPE(ote->hdr); /* the obj's ddd-type */
987 }
988
989 iNO++;
990 }
991
992 /* communicate to all procs in coupling */
993 for(cpl=ObjCplList(context, ote->hdr); cpl!=NULL; cpl=CPL_NEXT(cpl))
994 {
995 /*
996 if (newness==PARTNEW || newness==PRUNEDNEW)
997 {
998 */
999 XIModCpl *xc = NewXIModCpl(context);
1000 if (xc==NULL)
1001 throw std::bad_alloc();
1002
1003 xc->to = CPL_PROC(cpl); /* receiver of XIModCpl*/
1004 xc->te.gid = OBJ_GID(ote->hdr); /* the object's gid */
1005 xc->te.prio = OBJ_PRIO(ote->hdr); /* the obj's new prio */
1006 xc->typ = OBJ_TYPE(ote->hdr); /* the obj's ddd-type */
1007 /*
1008 }
1009 */
1010 }
1011 }
1012 }
1013 }
1014
1015
1016 /****************************************************************************/
1017
1018
1019
1020 static void LocalizeSymTab (DDD::DDDContext& context, LC_MSGHANDLE xm,
1021 OBJTAB_ENTRY **allRecObjs, int nRecObjs,
1022 const DDD_HDR *localCplObjs, int nLocalCplObjs)
1023 {
1024 auto& ctx = context.xferContext();
1025
1026 SYMTAB_ENTRY *theSymTab;
1027 int i, j;
1028 int lenSymTab = (int) LC_GetTableLen(xm, ctx.symtab_id);
1029
1030
1031 /* get table addresses inside message buffer */
1032 theSymTab = (SYMTAB_ENTRY *) LC_GetPtr(xm, ctx.symtab_id);
1033
1034
1035 /* insert pointers to known objects into SymTab */
1036 for(i=0, j=0; i<lenSymTab; i++)
1037 {
1038 while ((j<nLocalCplObjs) &&
1039 (OBJ_GID(localCplObjs[j]) < theSymTab[i].gid))
1040 j++;
1041
1042 if (j==nLocalCplObjs)
1043 {
1044 /* no more valid local objects */
1045 theSymTab[i].adr.hdr = NULL;
1046 }
1047 else
1048 {
1049 if (OBJ_GID(localCplObjs[j]) == theSymTab[i].gid)
1050 {
1051 theSymTab[i].adr.hdr = localCplObjs[j];
1052 }
1053 else
1054 {
1055 theSymTab[i].adr.hdr = NULL;
1056 }
1057 }
1058 }
1059
1060
1061 /* insert new pointers in SymTab */
1062 for(i=0, j=0; i<lenSymTab; i++)
1063 {
1064 while ((j<nRecObjs) && (OBJ_GID(allRecObjs[j]->hdr)<theSymTab[i].gid))
1065 j++;
1066
1067 if ((j<nRecObjs) && (OBJ_GID(allRecObjs[j]->hdr)==theSymTab[i].gid))
1068 {
1069 theSymTab[i].adr.hdr = allRecObjs[j]->hdr;
1070 }
1071 }
1072 }
1073
1074
1075 static void LocalizeObjects (DDD::DDDContext& context, LC_MSGHANDLE xm, bool required_newness)
1076 {
1077 auto& ctx = context.xferContext();
1078 const SYMTAB_ENTRY *theSymTab;
1079 const OBJTAB_ENTRY *theObjTab;
1080 const char *theObjects;
1081 int i;
1082 int lenObjTab = (int) LC_GetTableLen(xm, ctx.objtab_id);
1083
1084
1085 /* get table addresses inside message buffer */
1086 theSymTab = (const SYMTAB_ENTRY *) LC_GetPtr(xm, ctx.symtab_id);
1087 theObjTab = (const OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id);
1088 theObjects = (const char *) LC_GetPtr(xm, ctx.objmem_id);
1089
1090
1091 for(i=0; i<lenObjTab; i++) /* for all message items */
1092 {
1093 if (required_newness && theObjTab[i].is_new==TOTALNEW)
1094 {
1095 TYPE_DESC *desc = &context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1096 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, desc);
1097
1098 if (desc->nPointers>0)
1099 {
1100 LocalizeObject(context, false, desc,
1101 (char *)(OTE_OBJ(context, theObjects,&(theObjTab[i]))),
1102 obj,
1103 theSymTab);
1104 }
1105 }
1106
1107
1108 #ifdef MERGE_MODE_IN_TESTZUSTAND
1109
1110 if (required_newness && theObjTab[i].is_new!=TOTALNEW)
1111 {
1112 /*
1113 implemented merge_mode for Localize. references from all copies
1114 will be merged into the local copy. 960813 KB
1115 */
1116 TYPE_DESC *desc = &context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1117 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, desc);
1118
1119 if (desc->nPointers>0)
1120 {
1121 #ifdef DEBUG_MERGE_MODE
1122 printf("LocalizeObject in merge_mode, %08x prio %d\n",
1123 OBJ_GID(theObjTab[i].hdr), OBJ_PRIO(theObjTab[i].hdr));
1124 #endif
1125
1126 /* execute Localize in merge_mode */
1127 LocalizeObject(context, true, desc,
1128 (char *)(OTE_OBJ(context, theObjects,&(theObjTab[i]))),
1129 obj,
1130 theSymTab);
1131 }
1132 }
1133
1134 #endif
1135 }
1136 }
1137
1138
1139
1140 static void CallUpdateHandler (DDD::DDDContext& context, LC_MSGHANDLE xm)
1141 {
1142 auto& ctx = context.xferContext();
1143 OBJTAB_ENTRY *theObjTab;
1144 int lenObjTab = (int) LC_GetTableLen(xm, ctx.objtab_id);
1145 int i;
1146
1147 /* get table addresses inside message buffer */
1148 theObjTab = (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id);
1149
1150 /* initialize new objects corresponding to application: update */
1151 for(i=0; i<lenObjTab; i++) /* for all message items */
1152 {
1153 if (theObjTab[i].is_new == TOTALNEW)
1154 {
1155 const TYPE_DESC& desc = context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1156
1157 /* call application handler for object updating */
1158 if (desc.handlerUPDATE)
1159 {
1160 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, &desc);
1161 desc.handlerUPDATE(context, obj);
1162 }
1163 }
1164 }
1165 }
1166
1167
1168
1169 static void UnpackAddData (DDD::DDDContext& context, LC_MSGHANDLE xm, bool required_newness)
1170 {
1171 auto& ctx = context.xferContext();
1172 SYMTAB_ENTRY *theSymTab;
1173 OBJTAB_ENTRY *theObjTab;
1174 char *theObjects;
1175 int i;
1176 int lenObjTab = (int) LC_GetTableLen(xm, ctx.objtab_id);
1177
1178
1179 /* get table addresses inside message buffer */
1180 theSymTab = (SYMTAB_ENTRY *) LC_GetPtr(xm, ctx.symtab_id);
1181 theObjTab = (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id);
1182 theObjects = (char *) LC_GetPtr(xm, ctx.objmem_id);
1183
1184
1185 /* scatter additional data via handler */
1186 for(i=0; i<lenObjTab; i++) /* for all message items */
1187 {
1188 if (theObjTab[i].addLen>0)
1189 {
1190 int newness = -1;
1191
1192 if (required_newness)
1193 {
1194 if (theObjTab[i].is_new==TOTALNEW)
1195 {
1196 newness = XFER_NEW;
1197 }
1198 }
1199 else
1200 {
1201 if (theObjTab[i].is_new!=TOTALNEW)
1202 {
1203 switch (theObjTab[i].is_new)
1204 {
1205 case OTHERMSG : newness=XFER_REJECT; break;
1206 case NOTNEW : newness=XFER_REJECT; break;
1207 case PARTNEW : newness=XFER_UPGRADE; break;
1208 case PRUNEDNEW : newness=XFER_UPGRADE; break;
1209 /* TODO: for PRUNEDNEW we should merge prios; might be XFER_DOWNGRADE... */
1210 }
1211 }
1212 }
1213
1214 if (newness!=-1)
1215 {
1216 TYPE_DESC *desc = &context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1217 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, desc);
1218 char *data;
1219
1220 /*
1221 compute begin of data section. theObjTab[i].size is equal to
1222 desc->len for fixed sized objects and different for variable
1223 sized objects
1224 */
1225 data = (char *)(OTE_OBJ(context, theObjects,&(theObjTab[i])) +
1226 CEIL(theObjTab[i].size));
1227
1228 PutDepData(context, data, desc, obj, theSymTab, newness);
1229 }
1230 }
1231 }
1232 }
1233
1234
1235
1236 /*
1237 in order to allow application reactions on a priority
1238 change, the SETPRIORITY-handler is called.
1239
1240 TODO: is this really a reason for calling SETPRIORITY? or
1241 should there be a separate handler for this task?
1242
1243 NOTE: due to the current implementation, the new priority
1244 has already been set in the local object's DDD_HEADER.
1245 but the SETPRIORITY-handler has to get the old priority inside
1246 the object and the new one as second argument. so we restore
1247 the old prio before calling the handler and set the newprio
1248 afterwards.
1249 */
1250 static void CallSetPriorityHandler (DDD::DDDContext& context, LC_MSGHANDLE xm)
1251 {
1252 auto& ctx = context.xferContext();
1253 OBJTAB_ENTRY *theObjTab;
1254 int lenObjTab = (int) LC_GetTableLen(xm, ctx.objtab_id);
1255 int i;
1256 char *theObjects;
1257
1258 /* get table addresses inside message buffer */
1259 theObjTab = (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id);
1260 theObjects = (char *) LC_GetPtr(xm, ctx.objmem_id);
1261
1262 for(i=0; i<lenObjTab; i++) /* for all message items */
1263 {
1264 /*
1265 the next condition is crucial. the SETPRIORITY-handler is
1266 called for _all_ object collisions, even if the old priority
1267 and the new priority are equal! this is to give the user
1268 a chance to note object collisions at all. (BTW, he could
1269 note it also during handlerMKCONS, but this could be too
1270 late...)
1271 970410 kb
1272 */
1273 if ((theObjTab[i].is_new==NOTNEW ||
1274 theObjTab[i].is_new==PARTNEW ||
1275 theObjTab[i].is_new==PRUNEDNEW)
1276 /* && (theObjTab[i].oldprio != theObjTab[i].prio) */)
1277 {
1278 const TYPE_DESC& desc = context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1279
1280 /* call application handler for object consistency */
1281 if (desc.handlerSETPRIORITY)
1282 {
1283 /* restore old priority in object */
1284 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, &desc);
1285 DDD_PRIO new_prio = OTE_PRIO(theObjects, &(theObjTab[i])); /* remember new prio */
1286 OBJ_PRIO(theObjTab[i].hdr) = theObjTab[i].oldprio;
1287
1288 desc.handlerSETPRIORITY(context, obj, new_prio);
1289
1290 /* restore new priority */
1291 OBJ_PRIO(theObjTab[i].hdr) = new_prio;
1292 }
1293 }
1294 }
1295 }
1296
1297
1298
1299 static void CallObjMkConsHandler (DDD::DDDContext& context, LC_MSGHANDLE xm, bool required_newness)
1300 {
1301 auto& ctx = context.xferContext();
1302 OBJTAB_ENTRY *theObjTab;
1303 int lenObjTab = (int) LC_GetTableLen(xm, ctx.objtab_id);
1304 int i;
1305
1306 /*STAT_RESET4;*/
1307
1308 /* get table addresses inside message buffer */
1309 theObjTab = (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id);
1310
1311
1312 /* initialize new objects corresponding to application: consistency */
1313 for(i=0; i<lenObjTab; i++) /* for all message items */
1314 {
1315 int newness = -1;
1316
1317 if (required_newness)
1318 {
1319 if (theObjTab[i].is_new==TOTALNEW)
1320 {
1321 newness = XFER_NEW;
1322 }
1323 }
1324 else
1325 {
1326 switch (theObjTab[i].is_new)
1327 {
1328 case NOTNEW : newness=XFER_REJECT; break;
1329 case PARTNEW : newness=XFER_UPGRADE; break;
1330 case PRUNEDNEW : newness=XFER_UPGRADE; break;
1331 /* TODO: for PRUNEDNEW we should merge prios; might be XFER_DOWNGRADE... */
1332 }
1333 }
1334
1335 if (newness!=-1)
1336 {
1337 const TYPE_DESC& desc = context.typeDefs()[OBJ_TYPE(theObjTab[i].hdr)];
1338 DDD_OBJ obj = HDR2OBJ(theObjTab[i].hdr, &desc);
1339
1340 assert(theObjTab[i].is_new!=OTHERMSG);
1341
1342 /* call application handler for object consistency */
1343 if (desc.handlerOBJMKCONS)
1344 {
1345 desc.handlerOBJMKCONS(context, obj, newness);
1346 }
1347 }
1348 }
1349
1350 /*STAT_INCTIMER4(23);*/
1351 }
1352
1353
1354
1355
1356 /****************************************************************************/
1357
1358
1359 /*
1360 unpack table of TEOldCpl-items.
1361
1362 this function is called for each incoming message. for each
1363 incoming object which hasn't been here before, a set of old
1364 couplings is added as an estimate until the second xfer-
1365 communication gives more details.
1366
1367 for OTHERMSG-objects there is always another object copy
1368 with TOTALNEW-flag. only the one with TOTALNEW-flag submits
1369 the set of TEOldCpl, the other sets will be equal (for consistent
1370 datasets before the Xfer) and are therefore redundant.
1371 */
1372 static void UnpackOldCplTab (
1373 DDD::DDDContext& context,
1374 TEOldCpl *tabOC, int nOC,
1375 OBJTAB_ENTRY *tabO, int nO)
1376 {
1377 int iO, iOC;
1378
1379 iO = iOC = 0;
1380 while (iOC<nOC && iO<nO)
1381 {
1382 /* skip ObjTab-items until a TOTALNEW is found */
1383 while (iO<nO && tabO[iO].is_new!=TOTALNEW)
1384 iO++;
1385
1386 if (iO<nO)
1387 {
1388 /* look for TEOldCpl-items with same gid.
1389 note: this relies on previous sorting via
1390 sort_XIOldCpl on sender side. */
1391 while (iOC<nOC && tabOC[iOC].gid<OBJ_GID(tabO[iO].hdr))
1392 iOC++;
1393
1394 /* found some TEOldCpl-items with same gid */
1395 /* add couplings now */
1396 while (iOC<nOC && tabOC[iOC].gid==OBJ_GID(tabO[iO].hdr))
1397 {
1398 AddCoupling(context, tabO[iO].hdr,tabOC[iOC].proc,tabOC[iOC].prio);
1399 iOC++;
1400 }
1401
1402 iO++;
1403 }
1404 }
1405 }
1406
1407
1408
1409 /*
1410 compress table of TENewCpl-items.
1411
1412 forall sets of NewCpl-items with same gid and same dest,
1413 compress set according to PriorityMerge(). only the winning
1414 priority has to survive.
1415
1416 at first, the table is sorted according to (gid/dest), which
1417 will construct the NewCpl-sets. afterwards, for each set the winner
1418 is determined and stored into table. the tablesize will be equal or
1419 smaller at the end of this function.
1420 */
1421 static int CompressNewCpl(DDD::DDDContext& context, TENewCpl *tabNC, int nNC)
1422 {
1423 int nNCnew;
1424 int iNC;
1425
1426 std::sort(tabNC, tabNC + nNC, sort_TENewCpl);
1427
1428 nNCnew = iNC = 0;
1429 while (iNC<nNC)
1430 {
1431 /* TENewCpl.type component is needed here (for merging priorities)! */
1432 TYPE_DESC *desc = &context.typeDefs()[NewCpl_GetType(tabNC[iNC])];
1433 DDD_PRIO newprio;
1434
1435 newprio = NewCpl_GetPrio(tabNC[iNC]);
1436 while (iNC<nNC-1 && NewCpl_GetGid(tabNC[iNC+1])==NewCpl_GetGid(tabNC[iNC]) &&
1437 NewCpl_GetDest(tabNC[iNC+1])==NewCpl_GetDest(tabNC[iNC]))
1438 {
1439 PriorityMerge(desc, newprio, NewCpl_GetPrio(tabNC[iNC+1]), &newprio);
1440 iNC++;
1441 }
1442
1443 if (iNC<nNC)
1444 {
1445 NewCpl_SetGid(tabNC[nNCnew], NewCpl_GetGid(tabNC[iNC]));
1446 NewCpl_SetDest(tabNC[nNCnew], NewCpl_GetDest(tabNC[iNC]));
1447 NewCpl_SetPrio(tabNC[nNCnew], newprio);
1448 NewCpl_SetType(tabNC[nNCnew], NewCpl_GetType(tabNC[iNC]));
1449 nNCnew++;
1450
1451 iNC++;
1452 }
1453 }
1454
1455 return(nNCnew);
1456 }
1457
1458
1459 /****************************************************************************/
1460
1461
1462
1463 /*
1464 main unpack procedure.
1465 */
1466
1467 void XferUnpack (DDD::DDDContext& context, LC_MSGHANDLE *theMsgs, int nRecvMsgs,
1468 const DDD_HDR *localCplObjs, int nLocalCplObjs,
1469 std::vector<XISetPrio*>& theSP,
1470 XIDelObj **arrayDO, int nDO,
1471 const std::vector<XICopyObj*>& arrayCO,
1472 XICopyObj **arrayNewOwners, int nNewOwners)
1473 {
1474 auto& ctx = context.xferContext();
1475
1476 TENewCpl *allNewCpl;
1477 OBJTAB_ENTRY **unionObjTab;
1478 int lenObjTab, lenSymTab, nNewCpl;
1479 int i, pos1, pos2;
1480 XISetPrio** arraySP = theSP.data();
1481 const int nSP = theSP.size();
1482
1483
1484
1485 lenObjTab=lenSymTab=nNewCpl=0;
1486
1487 for(i=0; i<nRecvMsgs; i++)
1488 {
1489 LC_MSGHANDLE xm = theMsgs[i];
1490 lenObjTab += (int)LC_GetTableLen(xm, ctx.objtab_id);
1491 lenSymTab += (int)LC_GetTableLen(xm, ctx.symtab_id);
1492 nNewCpl += (int)LC_GetTableLen(xm, ctx.newcpl_id);
1493 }
1494
1495 # if DebugUnpack<=4
1496 Dune::dverb << "SUM OF"
1497 << " OBJ=" << std::setw(3) << lenObjTab
1498 << " SYM=" << std::setw(3) << lenSymTab
1499 << " NEW=" << std::setw(3) << nNewCpl
1500 << " FROM " << std::setw(2) << nRecvMsgs << " MSGS\n";
1501 # endif
1502
1503 /*STAT_RESET3;*/
1504
1505 if (nNewCpl>0)
1506 {
1507 allNewCpl = (TENewCpl *) OO_Allocate (sizeof(TENewCpl)*nNewCpl);
1508 if (allNewCpl == nullptr)
1509 throw std::bad_alloc();
1510 } else {
1511 allNewCpl = NULL;
1512 }
1513
1514
1515 if (lenObjTab>0)
1516 {
1517 unionObjTab = (OBJTAB_ENTRY **) OO_Allocate (sizeof(OBJTAB_ENTRY *)*lenObjTab);
1518 if (unionObjTab == nullptr)
1519 throw std::bad_alloc();
1520 } else {
1521 unionObjTab = NULL;
1522 }
1523
1524
1525 /*STAT_TIMER3(25); STAT_RESET3;*/
1526
1527
1528 /* create union tables: allNewCpl, unionObjTab */
1529 for(i=0, pos1=pos2=0; i<nRecvMsgs; i++)
1530 {
1531 LC_MSGHANDLE xm = theMsgs[i];
1532 char *theObjects = (char *) LC_GetPtr(xm, ctx.objmem_id);
1533
1534 int len = (int) LC_GetTableLen(xm, ctx.newcpl_id);
1535 if (len>0)
1536 {
1537 memcpy(allNewCpl+pos1, LC_GetPtr(xm,ctx.newcpl_id),
1538 sizeof(TENewCpl)*len);
1539 pos1 += len;
1540 }
1541
1542 len = (int) LC_GetTableLen(xm, ctx.objtab_id);
1543 if (len>0)
1544 {
1545 OBJTAB_ENTRY *msg_ot = (OBJTAB_ENTRY *)
1546 LC_GetPtr(xm,ctx.objtab_id);
1547 OBJTAB_ENTRY **all_ot = unionObjTab+pos2;
1548 int oti;
1549 for(oti=0; oti<len; oti++, all_ot++, msg_ot++)
1550 {
1551 *all_ot = msg_ot;
1552
1553 /* enter pointer to HDR-copy inside message, temporarily */
1554 msg_ot->hdr = OTE_HDR(theObjects,msg_ot);
1555 }
1556
1557 pos2 += len;
1558 }
1559 }
1560
1561 if (nNewCpl>0)
1562 {
1563 nNewCpl = CompressNewCpl(context, allNewCpl, nNewCpl);
1564 }
1565
1566 if (lenObjTab>0)
1567 {
1568 std::sort(unionObjTab, unionObjTab + lenObjTab, sort_ObjTabPtrs);
1569 }
1570
1571
1572 # if DebugUnpack<=2
1573 for(i=0; i<nNewCpl; i++)
1574 {
1575 Dune::dvverb << " TAB allNewCpl " << NewCpl_GetGid(allNewCpl[i])
1576 << " on " << std::setw(4) NewCpl_GetDest(allNewCpl[i])
1577 << "/" << NewCpl_GetPrio(allNewCpl[i]) << "\n";
1578 }
1579 # endif
1580
1581
1582
1583 /* accept all received objects */
1584 if (nRecvMsgs>0)
1585 {
1586 AcceptReceivedObjects(
1587 context,
1588 theMsgs, nRecvMsgs,
1589 unionObjTab, lenObjTab,
1590 localCplObjs, nLocalCplObjs
1591 );
1592 }
1593
1594
1595 /*
1596 TODO: the following loops can be implemented more
1597 efficiently. in each loop, there is another loop
1598 across all objects inside the message. for each object,
1599 the TypeDesc is computed. the typedesc pointers should
1600 be computed once and stored somewhere. kb 970115
1601 */
1602
1603 /* insert local references into symtabs */
1604 for(i=0; i<nRecvMsgs; i++)
1605 LocalizeSymTab(context, theMsgs[i], unionObjTab, lenObjTab,
1606 localCplObjs, nLocalCplObjs);
1607
1608
1609 /*
1610 TODO. perhaps the following loops across all messages
1611 should be split up even further. (i.e., XFER_NEW,
1612 then XFER_UPGRADE, then XFER_REJECT).
1613 */
1614
1615
1616 /* unpack all messages and update local topology */
1617 for(i=0; i<nRecvMsgs; i++) LocalizeObjects(context, theMsgs[i], true);
1618 for(i=0; i<nRecvMsgs; i++) LocalizeObjects(context, theMsgs[i], false);
1619
1620 /*
1621 at this point all new objects are established,
1622 their references point correctly to the neighbour objects.
1623 note: the references from neighbours to the new objects
1624 are not actualized yet! this has to be done via the
1625 application handler OBJMKCONS.
1626 */
1627
1628 /* KB 941109
1629 the order of the next steps is crucial:
1630 1. update objects via handler
1631 2. add additional data items
1632 3. call set-prio handlers
1633 4. update consistency
1634 */
1635
1636 /* for NOTNEW,PARTNEW,PRUNEDNEW objects */
1637 for(i=0; i<nRecvMsgs; i++)
1638 CallSetPriorityHandler(context, theMsgs[i]);
1639
1640 /* for TOTALNEW objects */
1641 for(i=0; i<nRecvMsgs; i++)
1642 CallUpdateHandler(context, theMsgs[i]);
1643
1644 /* for all incoming objects */
1645 for(i=0; i<nRecvMsgs; i++) UnpackAddData(context, theMsgs[i], true);
1646 for(i=0; i<nRecvMsgs; i++) UnpackAddData(context, theMsgs[i], false);
1647
1648 /* for PARTNEW and TOTALNEW objects */
1649 for(i=0; i<nRecvMsgs; i++) CallObjMkConsHandler(context, theMsgs[i], true);
1650 for(i=0; i<nRecvMsgs; i++) CallObjMkConsHandler(context, theMsgs[i], false);
1651
1652
1653
1654 /*
1655 # if DebugXfer>1
1656 if (DDD_GetOption(context, OPT_DEBUG_XFERMESGS)==OPT_ON)
1657 # endif
1658 {
1659 for(i=0; i<nRecvMsgs; i++)
1660 XferDisplayMsg(context, "OR", theMsgs[i]);
1661 }
1662 */
1663
1664
1665
1666 /* unpack all OldCpl-tabs */
1667 for(i=0; i<nRecvMsgs; i++)
1668 {
1669 LC_MSGHANDLE xm = theMsgs[i];
1670 UnpackOldCplTab(
1671 context,
1672 (TEOldCpl *) LC_GetPtr(xm,ctx.oldcpl_id),
1673 (int) LC_GetTableLen(xm, ctx.oldcpl_id),
1674 (OBJTAB_ENTRY *) LC_GetPtr(xm, ctx.objtab_id),
1675 (int) LC_GetTableLen(xm, ctx.objtab_id) );
1676 }
1677
1678
1679
1680
1681 /* update couplings according to global cpl tab */
1682 UpdateCouplings(context,
1683 allNewCpl, nNewCpl,
1684 unionObjTab, lenObjTab,
1685 localCplObjs, nLocalCplObjs,
1686 arrayDO, nDO,
1687 arrayNewOwners, nNewOwners
1688 );
1689
1690 /* create new XI???Cpl-infos depending on allNewCpls for existing
1691 objects */
1692 PropagateCplInfos(context, arraySP, nSP, arrayDO, nDO, allNewCpl, nNewCpl);
1693
1694
1695 /* create some more XIModCpl-items due to incoming objects */
1696 PropagateIncomings(context, arrayNewOwners, nNewOwners, unionObjTab, lenObjTab);
1697
1698
1699 /* free temporary memory */
1700 if (allNewCpl!=NULL)
1701 OO_Free (allNewCpl /*,0*/);
1702 if (unionObjTab!=NULL)
1703 OO_Free (unionObjTab /*,0*/);
1704 }
1705
1706
1707
1708
1709 END_UGDIM_NAMESPACE