Debian Codegraph @

dune/uggrid/parallel/ddd/xfer/pack.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: pack.c */
8 /* */
9 /* Purpose: packs objects into messages */
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/01/31 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 <cinttypes>
36 #include <cstdlib>
37 #include <cstdio>
38 #include <cstring>
39
40 #include <algorithm>
41
42 #include <dune/common/exceptions.hh>
43 #include <dune/common/stdstreams.hh>
44
45 #include <dune/uggrid/parallel/ddd/dddi.h>
46 #include "xfer.h"
47
48 #include <dune/uggrid/low/namespace.h>
49 #include <dune/uggrid/low/ugtypes.h>
50
51 USING_UG_NAMESPACE
52
53 START_UGDIM_NAMESPACE
54
55 /****************************************************************************/
56 /* */
57 /* defines in the following order */
58 /* */
59 /* compile time constants defining static data size (i.e. arrays) */
60 /* other constants */
61 /* macros */
62 /* */
63 /****************************************************************************/
64
65
66
67 /****************************************************************************/
68 /* */
69 /* data structures */
70 /* */
71 /****************************************************************************/
72
73
74
75
76 /****************************************************************************/
77 /* */
78 /* definition of exported global variables */
79 /* */
80 /****************************************************************************/
81
82
83
84 /****************************************************************************/
85 /* */
86 /* definition of variables global to this source file only (static!) */
87 /* */
88 /****************************************************************************/
89
90
91
92
93
94 /****************************************************************************/
95 /* */
96 /* routines */
97 /* */
98 /****************************************************************************/
99
100
101 static bool sort_SymTabEntries (const SYMTAB_ENTRY& a, const SYMTAB_ENTRY& b)
102 {
103 return a.gid < b.gid;
104 }
105
106 /****************************************************************************/
107 /* */
108 /* Function: BuildSymTab */
109 /* */
110 /* Purpose: compute message SymTab entries for one single ddd-object. */
111 /* */
112 /* Input: desc: descriptor of object */
113 /* obj: DDD_OBJ ptr to ddd-object (in local mem) or NULL */
114 /* copy: copy of ddd-object (inside message buffer) */
115 /* theSymTab: actual portion of message SymTab */
116 /* */
117 /* Output: number of new entries into SymTab */
118 /* */
119 /****************************************************************************/
120
121 static int BuildSymTab (DDD::DDDContext& context,
122 TYPE_DESC *desc,
123 DDD_OBJ obj,
124 const char *copy,
125 SYMTAB_ENTRY *theSymTab)
126 {
127 ELEM_DESC *theElem;
128 int e, actSym;
129
130 /* reset local portion of SymTab */
131 actSym = 0;
132
133 /* prepare map of structure elements */
134 theElem = desc->element;
135
136 /* loop over all pointers inside of object obj */
137 for(e=0; e<desc->nElements; e++, theElem++)
138 {
139 if (theElem->type==EL_OBJPTR)
140 {
141 TYPE_DESC *refdesc;
142 int l;
143 int rt_on_the_fly = (EDESC_REFTYPE(theElem)==DDD_TYPE_BY_HANDLER);
144
145 /* determine reftype of this elem */
146 if (! rt_on_the_fly)
147 {
148 /* we know the reftype of this element in advance */
149 refdesc = &context.typeDefs()[EDESC_REFTYPE(theElem)];
150 }
151 /* else: determine reftype on the fly by calling handler */
152
153
154 /* loop over single pointer array */
155 for(l=0; l<theElem->size; l+=sizeof(void *))
156 {
157 /* get address of outside reference */
158 DDD_OBJ *ref = (DDD_OBJ *)(copy+theElem->offset+l);
159
160
161 /* create symbol table entry */
162 if (*ref!=NULL)
163 {
164 DDD_HDR refhdr;
165
166 if (rt_on_the_fly)
167 {
168 DDD_TYPE rt;
169
170 /* determine reftype on the fly by calling handler */
171 assert(obj!=NULL); /* we need a real object here */
172
173 rt = theElem->reftypeHandler(context, obj, *ref);
174 if (rt>=MAX_TYPEDESC)
175 DUNE_THROW(Dune::Exception,
176 "invalid referenced DDD_TYPE returned by handler");
177
178 refdesc = &context.typeDefs()[rt];
179 }
180
181 /* get header of referenced object */
182 refhdr = OBJ2HDR(*ref,refdesc);
183
184 /* remember the GID of the referenced object */
185 theSymTab[actSym].gid = OBJ_GID(refhdr);
186
187 /* remember the address of the reference (in obj-copy) */
188 theSymTab[actSym].adr.ref = ref;
189 actSym++;
190 }
191 }
192 }
193 }
194
195 /* return SymTab increment */
196 return(actSym);
197 }
198
199
200
201 /****************************************************************************/
202 /* */
203 /* Function: GetDepData */
204 /* */
205 /* Purpose: fill object-dependent data into message. an appl. routine */
206 /* will be called to fill in the data actually. pointers are */
207 /* localized and the message SymTab is actualized. */
208 /* */
209 /* Input: data: portion of message buffer reserved for dependent data */
210 /* desc: descriptor of object */
211 /* obj: current ddd-object */
212 /* theSymTab: actual portion of message SymTab */
213 /* xi: single xferinfo for current ddd-object */
214 /* */
215 /* Output: number of new entries into SymTab */
216 /* */
217 /****************************************************************************/
218
219 static int GetDepData (DDD::DDDContext& context,
220 char *data,
221 TYPE_DESC *desc,
222 DDD_OBJ obj,
223 SYMTAB_ENTRY *theSymTab,
224 XICopyObj *xi)
225 {
226 XFERADDDATA *xa;
227 char *chunk, *adr, **table1, *next_chunk;
228 int chunks, i, actSym, *table2;
229
230
231 if (xi->addLen==0) return(0);
232
233 chunks = 0;
234 actSym = 0;
235
236
237 /* first entry will be number of dependency chunks */
238 chunk = data + CEIL(sizeof(int));
239
240
241 /* loop through whole dependency data descriptor */
242 for(xa=xi->add; xa!=NULL; xa=xa->next)
243 {
244 /* first entries of chunk are addCnt and addTyp */
245 ((int *)chunk)[0] = xa->addCnt;
246 ((DDD_TYPE *)chunk)[1] = xa->addTyp;
247
248 if (xa->sizes==NULL)
249 {
250 chunk += CEIL(sizeof(int)+sizeof(DDD_TYPE));
251
252 /* then all records should be gathered via handler */
253 if (desc->handlerXFERGATHER)
254 {
255 desc->handlerXFERGATHER( context, obj,
256 xa->addCnt, xa->addTyp, (void *)chunk);
257 }
258
259 if (xa->addTyp<DDD_USER_DATA || xa->addTyp>DDD_USER_DATA_MAX)
260 {
261 /* insert pointers into symtab */
262 TYPE_DESC* descDep = &context.typeDefs()[xa->addTyp];
263 for(i=0; i<xa->addCnt; i++)
264 {
265 actSym += BuildSymTab(context, descDep, NULL,
266 chunk, &(theSymTab[actSym]));
267 chunk += CEIL(descDep->size);
268 }
269 }
270 else
271 {
272 /* no regular type -> send byte stream with length addCnt */
273 chunk += CEIL(xa->addCnt);
274 }
275 }
276 else
277 {
278 /* var-sized AddData items */
279 ((int *)chunk)[0] *= -1;
280 chunk += CEIL(sizeof(int)+sizeof(DDD_TYPE));
281
282 /* create pointer array inside message */
283 table1 = (char **)chunk;
284 chunk += CEIL(sizeof(int)*xa->addCnt);
285 for(i=0, adr=chunk; i<xa->addCnt; i++)
286 {
287 table1[i] = adr;
288 adr += CEIL(xa->sizes[i]);
289 }
290 next_chunk = adr;
291
292 /* then all records should be gathered via handler */
293 if (desc->handlerXFERGATHERX)
294 {
295 desc->handlerXFERGATHERX( context, obj,
296 xa->addCnt, xa->addTyp, table1);
297 }
298
299 /* convert pointer table into offset table */
300 table2 = (int *)table1;
301 TYPE_DESC* descDep = &context.typeDefs()[xa->addTyp];
302 adr = chunk;
303 for(i=0; i<xa->addCnt; i++)
304 {
305 /* insert pointers into symtab */
306 if (xa->addTyp<DDD_USER_DATA || xa->addTyp>DDD_USER_DATA_MAX)
307 {
308 actSym += BuildSymTab(context, descDep, NULL,
309 table1[i], &(theSymTab[actSym]));
310 }
311
312 table2[i] = (int)(table1[i]-adr);
313 }
314
315 chunk = next_chunk;
316 }
317
318 /* count chunks */
319 chunks++;
320 }
321
322
323 /* remember number of chunks at the beginning of the deplist */
324 ((int *)data)[0] = chunks;
325
326
327 return(actSym);
328 }
329
330
331
332 /****************************************************************************/
333 /* */
334 /* Function: XferPackSingleMsgs */
335 /* */
336 /* Purpose: build up one outgoing message completely, fill data into */
337 /* message buffer. objects and couplings will be packed, and */
338 /* several message tables will be constructed. pointers are */
339 /* localized inside the message. several applications handlers */
340 /* are called to fill in dependent data. */
341 /* */
342 /* Input: msg: single message-send-info structure */
343 /* */
344 /* Output: - */
345 /* */
346 /****************************************************************************/
347
348 static void XferPackSingleMsg (DDD::DDDContext& context, XFERMSG *msg)
349 {
350 auto& ctx = context.xferContext();
351
352 SYMTAB_ENTRY *theSymTab;
353 OBJTAB_ENTRY *theObjTab;
354 TENewCpl *theNewCpl;
355 TEOldCpl *theOldCpl;
356 char *theObjects, *currObj;
357 int i, actSym, actNewCpl, actOldCpl, actObj;
358
359 /* get table addresses inside message */
360 theSymTab = (SYMTAB_ENTRY *)LC_GetPtr(msg->msg_h, ctx.symtab_id);
361 theObjTab = (OBJTAB_ENTRY *)LC_GetPtr(msg->msg_h, ctx.objtab_id);
362 theNewCpl = (TENewCpl *) LC_GetPtr(msg->msg_h, ctx.newcpl_id);
363 theOldCpl = (TEOldCpl *) LC_GetPtr(msg->msg_h, ctx.oldcpl_id);
364 theObjects= (char *)LC_GetPtr(msg->msg_h, ctx.objmem_id);
365
366
367 /* build several tables inside message */
368 actSym = actNewCpl = actOldCpl = actObj = 0;
369 currObj = theObjects;
370 for(i=0; i<msg->nObjItems; i++) /* for all XICopyObj-items */
371 {
372 XICopyObj *xi = msg->xferObjArray[i];
373 DDD_HDR hdr = xi->hdr;
374 TYPE_DESC *desc = &context.typeDefs()[OBJ_TYPE(hdr)];
375 DDD_OBJ obj = HDR2OBJ(hdr,desc);
376 /*COUPLING *cpl;*/
377 DDD_HDR copyhdr;
378
379 /* build coupling table */
380 /* skip cpl which describes object itself (receive proc) */
381 /*
382 for(cpl=THECOUPLING(hdr); cpl!=NULL; cpl=cpl->next)
383 {
384 if (cpl->proc!=msg->proc)
385 {
386 theNewCpl[actNewCpl].gid = OBJ_GID(hdr);
387 theNewCpl[actNewCpl].proc = cpl->proc;
388 theNewCpl[actNewCpl].prio = cpl->prio;
389 actNewCpl++;
390 }
391 }
392 */
393
394 /* one coupling for object itself (send proc) */
395 /*
396 theNewCpl[actNewCpl].gid = OBJ_GID(hdr);
397 theNewCpl[actNewCpl].proc = context.me();
398 theNewCpl[actNewCpl].prio = OBJ_PRIO(hdr);
399 actNewCpl++;
400 */
401
402
403 copyhdr = OBJ2HDR(currObj,desc);
404
405
406 /* update object table */
407 theObjTab[actObj].h_offset = (int)(((char *)copyhdr)-theObjects);
408 theObjTab[actObj].hdr = NULL;
409 theObjTab[actObj].addLen = xi->addLen;
410 theObjTab[actObj].size = xi->size; /* needed for variable-sized objects */
411 actObj++;
412
413
414
415 /*
416 copy object into message. in the following xi->size
417 equals desc->len for fixed-size objects.
418 */
419 /*STAT_RESET3;*/
420 /* NOTE: object memory is copied _completely_, i.e., also LDATA-
421 components are copied into message and sent to destination.
422 then, on the receiving processor the data is sorted out... */
423 memcpy(currObj, obj, xi->size);
424
425 /* insert priority into copy */
426 OBJ_PRIO(copyhdr) = xi->prio;
427
428
429 /* call application handler for direct manipulation */
430 /* KB 941110: moved from objmgr.c */
431 /*
432 Caution: this is a very, very dirty situation.
433 HANDLER_XFERCOPYMANIP is able to manipulate the
434 obj-copy inside the message. this handler should
435 be removed in future DDD versions.
436 */
437 if (desc->handlerXFERCOPYMANIP)
438 {
439 /*
440 NOTE: OBJ_TYPE could change during the
441 execution of HANDLER_XFERCOPYMANIP. however,
442 the position of DDD_HEADER inside the object
443 should not change. therefore, we can remember
444 the offsetHeader here and use it afterwards
445 to adjust the desc.
446 */
447 int offset = desc->offsetHeader;
448
449 /* now call handler */
450 desc->handlerXFERCOPYMANIP(context, currObj);
451
452 /* adjust new description according to new type */
453 desc = &context.typeDefs()[OBJ_TYPE((DDD_HDR)(currObj+offset))];
454 }
455
456 /* build symbol table portion from object copy */
457 actSym += BuildSymTab(context, desc, obj, (char *)currObj, &(theSymTab[actSym]));
458
459
460 /* advance to next free object slot in message, c.f. alignment */
461 currObj += CEIL(xi->size);
462
463
464 /* gather additional data */
465 if (xi->addLen>0)
466 {
467 actSym += GetDepData(context, currObj,
468 desc, obj, &(theSymTab[actSym]), xi);
469 currObj += xi->addLen;
470 }
471 }
472
473
474 /* for all XINewCpl items in this message */
475 for(i=0; i<msg->nNewCpl; i++)
476 {
477 theNewCpl[actNewCpl] = msg->xferNewCpl[i]->te;
478 actNewCpl++;
479 }
480
481 /* for all XIOldCpl items in this message */
482 for(i=0; i<msg->nOldCpl; i++)
483 {
484 theOldCpl[actOldCpl] = msg->xferOldCpl[i]->te;
485 actOldCpl++;
486 }
487
488
489 /* sort SymTab, ObjTab and CplTab */
490 /* sort SymTab according to the global ids stored there */
491 std::sort(theSymTab, theSymTab + actSym, sort_SymTabEntries);
492
493 /* sort ObjTab according to their global ids */
494 /* sorting of objtab is necessary!! (see AcceptObjFromMsg) KB 960812 */
495 const auto sort_ObjTabEntries = [=](const OBJTAB_ENTRY& a, const OBJTAB_ENTRY& b) {
496 /* sort with ascending gid */
497 return OTE_GID(theObjects, &a) < OTE_GID(theObjects, &b);
498 };
499 std::sort(theObjTab, theObjTab + msg->nObjects, sort_ObjTabEntries);
500
501
502 /* substitute all pointers by index into SymTab */
503 for(std::uintptr_t mi=0; mi<actSym; mi++)
504 {
505 /* patch SymTab index into reference location inside message */
506 *(theSymTab[mi].adr.ref) = (DDD_OBJ)(mi+1);
507 }
508
509
510 /* TODO: theSymtab[].ref wird ab hier nicht mehr verwendet und muss nicht uebertragen werden! */
511
512
513 /* set valid table entries */
514 LC_SetTableLen(msg->msg_h, ctx.symtab_id, actSym);
515 LC_SetTableLen(msg->msg_h, ctx.objtab_id, msg->nObjects);
516 LC_SetTableLen(msg->msg_h, ctx.newcpl_id, actNewCpl);
517 LC_SetTableLen(msg->msg_h, ctx.oldcpl_id, actOldCpl);
518
519
520 #if DebugXfer>1
521 if (DDD_GetOption(context, OPT_DEBUG_XFERMESGS)==OPT_ON)
522 #endif
523 XferDisplayMsg(context, "OS", msg->msg_h);
524 }
525
526
527
528
529 /****************************************************************************/
530 /* */
531 /* Function: XferPackMsgs */
532 /* */
533 /* Purpose: allocate one message buffer for each outgoing message, */
534 /* fill buffer with message contents and initiate asynchronous */
535 /* send for each message. */
536 /* */
537 /* Input: theMsgs: list of message-send-infos */
538 /* */
539 /* Output: RET_ON_ERROR if error occurred, RET_ON_OK otherwise */
540 /* */
541 /****************************************************************************/
542
543 RETCODE XferPackMsgs (DDD::DDDContext& context, XFERMSG *theMsgs)
544 {
545 XFERMSG *xm;
546
547 #if DebugPack<=3
548 Dune::dverb << "XferPackMsgs" << std::endl;
549 #endif
550
551 /* allocate buffer, pack messages and send away */
552 for(xm=theMsgs; xm!=NULL; xm=xm->next)
553 {
554 if (! LC_MsgAlloc(context, xm->msg_h))
555 {
556 Dune::dwarn << STR_NOMEM " in XferPackMsgs (size="
557 << LC_GetBufferSize(xm->msg_h) << ")\n";
558 RET_ON_ERROR;
559 }
560 XferPackSingleMsg(context, xm);
561 LC_MsgSend(context, xm->msg_h);
562 }
563
564 RET_ON_OK;
565 }
566
567
568
569
570 END_UGDIM_NAMESPACE