Debian Codegraph @

dune/uggrid/parallel/ddd/mgr/cplmgr.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: cplmgr.c */
8 /* */
9 /* Purpose: management of couplings */
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: 93/11/30 kb begin */
19 /* 94/08/24 kb added DDD_InfoProcList() */
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 <new>
41
42 #include <dune/common/exceptions.hh>
43 #include <dune/common/stdstreams.hh>
44
45 #include <dune/uggrid/parallel/ddd/dddcontext.hh>
46
47 #include <dune/uggrid/parallel/ddd/dddi.h>
48
49 USING_UG_NAMESPACES
50
51 /* PPIF namespace: */
52 using namespace PPIF;
53
54 #define DebugCoupling 10 /* 10 is off */
55
56 START_UGDIM_NAMESPACE
57
58 using CplSegm = DDD::Mgr::CplSegm;
59
60 /*
61 the storage of COUPLING items is done with the following scheme:
62 allocation in segments of couplings, freeing into freelist.
63
64 ALLOC: try first to get one item out of freelist (memlistCpl),
65 if that's not possible, get one from current segment;
66 alloc segments from MemMgr.
67
68 FREE: put coupling into freelist.
69 */
70
71 static CplSegm *NewCplSegm(DDD::DDDContext& context)
72 {
73 auto& mctx = context.cplmgrContext();
74
75 CplSegm *segm;
76
77 segm = (CplSegm *) AllocTmpReq(sizeof(CplSegm), TMEM_CPL);
78 if (segm==NULL)
79 throw std::bad_alloc();
80
81 segm->next = mctx.segmCpl;
82 mctx.segmCpl = segm;
83 segm->nItems = 0;
84 mctx.nCplSegms++;
85
86 return(segm);
87 }
88
89
90 static void FreeCplSegms(DDD::DDDContext& context)
91 {
92 auto& mctx = context.cplmgrContext();
93
94 CplSegm *segm = mctx.segmCpl;
95 CplSegm *next = NULL;
96
97 while (segm!=NULL)
98 {
99 next = segm->next;
100 FreeTmpReq(segm, sizeof(CplSegm), TMEM_CPL);
101
102 segm = next;
103 }
104
105 mctx.segmCpl = nullptr;
106 mctx.nCplSegms = 0;
107 mctx.memlistCpl = nullptr;
108 }
109
110
111 /****************************************************************************/
112
113 /* auxiliary function to init Coupling memory and initial data */
114 static void InitNewCoupling (COUPLING* cpl)
115 {
116 /* set coupling to initial value, in order to find any bugs lateron.
117 (hint by C.Wieners) */
118 memset(cpl, 0, sizeof(COUPLING));
119
120 /* init private data */
121 cpl->_flags = 0;
122 }
123
124
125 static COUPLING *NewCoupling (DDD::DDDContext& context)
126 {
127 auto& ctx = context.couplingContext();
128 auto& mctx = context.cplmgrContext();
129
130 COUPLING *cpl;
131
132 if (DDD_GetOption(context, OPT_CPLMGR_USE_FREELIST)==OPT_ON)
133 {
134 /* allocate coupling from segments (which are allocated
135 from segment-freelists) */
136
137 if (mctx.memlistCpl == nullptr)
138 {
139 CplSegm *segm = mctx.segmCpl;
140
141 if (segm==NULL || segm->nItems==CPLSEGM_SIZE)
142 {
143 segm = NewCplSegm(context);
144 }
145
146 cpl = &(segm->item[segm->nItems++]);
147 }
148 else
149 {
150 cpl = mctx.memlistCpl;
151 mctx.memlistCpl = CPL_NEXT(cpl);
152 }
153
154 /* init coupling memory and its private data */
155 InitNewCoupling(cpl);
156
157 /* remember memory origin for later disposal */
158 SETCPLMEM_FREELIST(cpl);
159 }
160 else
161 {
162 /* allocate coupling directly */
163 cpl = (COUPLING *) AllocTmpReq(sizeof(COUPLING), TMEM_CPL);
164
165 if (cpl==NULL)
166 throw std::bad_alloc();
167
168 /* init coupling memory and its private data */
169 InitNewCoupling(cpl);
170
171 /* remember memory origin for later disposal */
172 SETCPLMEM_EXTERNAL(cpl);
173 }
174
175 ctx.nCplItems += 1;
176
177 return(cpl);
178 }
179
180
181 static void DisposeCoupling (DDD::DDDContext& context, COUPLING *cpl)
182 {
183 auto& ctx = context.couplingContext();
184 auto& mctx = context.cplmgrContext();
185
186 if (CPLMEM(cpl)==CPLMEM_FREELIST)
187 {
188 CPL_NEXT(cpl) = mctx.memlistCpl;
189 mctx.memlistCpl = cpl;
190 }
191 else
192 {
193 FreeTmpReq(cpl, sizeof(COUPLING), TMEM_CPL);
194 }
195
196 ctx.nCplItems -= 1;
197 }
198
199
200 /****************************************************************************/
201
202
203 static void AllocCplTables (DDD::DDDContext& context, long n)
204 {
205 auto& ctx = context.couplingContext();
206
207 /* allocate coupling table */
208 ctx.cplTable.resize(n);
209 ctx.nCplTable.resize(n);
210 }
211
212
213 static void IncreaseCplTabSize(DDD::DDDContext& context)
214 {
215 auto& ctx = context.couplingContext();
216
217 /* compute new size (currently: double size) */
218 const std::size_t n = ctx.cplTable.size() * 2;
219
220 /* allocate new coupling table */
221 ctx.cplTable.resize(n);
222 ctx.nCplTable.resize(n);
223
224 /* issue a warning in order to inform user */
225 Dune::dwarn << "increased coupling table, now " << n << " entries\n";
226
227 ddd_EnsureObjTabSize(context, n);
228 }
229
230
231
232
233 /****************************************************************************/
234 /* */
235 /* Function: AddCoupling */
236 /* */
237 /* Purpose: get new coupling record and init contents */
238 /* if coupling is already there, no additional coupling is */
239 /* created. priority is adapted in this case. */
240 /* */
241 /* Input: hdr: DDD-header of object with new coupling */
242 /* proc: owner of copy to be registered */
243 /* prio: priority of copy */
244 /* */
245 /* Output: ptr to new cpl record (or old one, if existed before) */
246 /* NULL on error */
247 /* */
248 /****************************************************************************/
249
250 COUPLING *AddCoupling(DDD::DDDContext& context, DDD_HDR hdr, DDD_PROC proc, DDD_PRIO prio)
251 {
252 auto& ctx = context.couplingContext();
253
254 COUPLING *cp, *cp2;
255 int objIndex;
256 int freeCplIdx = ctx.nCpls;
257
258 assert(proc!=context.me());
259
260 # if DebugCoupling<=1
261 Dune::dvverb << "AddCoupling " << OBJ_GID(hdr)
262 << " proc=" << proc << " prio=" << prio << "\n";
263 # endif
264
265 /* find or free position in coupling array */
266 objIndex = OBJ_INDEX(hdr);
267 if (! ObjHasCpl(context, hdr))
268 {
269 if (freeCplIdx == ctx.cplTable.size())
270 {
271 /* try to make CplTables larger ... */
272 IncreaseCplTabSize(context);
273 }
274
275 auto& objTable = context.objTable();
276 #ifdef WithFullObjectTable
277 DDD_HDR oldObj = objTable[freeCplIdx];
278
279 /* exchange object without coupling and object with coupling */
280 /* free position freeCplIdx, move corresponding hdr reference
281 elsewhere. */
282 objTable[objIndex] = oldObj;
283 OBJ_INDEX(oldObj) = objIndex;
284 #else
285 assert(IsHdrLocal(hdr));
286
287 /* hdr has been local, therefore not known by DDD, we have
288 to register it now. */
289 context.nObjs(context.nObjs() + 1);
290 #endif
291
292
293 assert(freeCplIdx < context.objTable().size());
294 objTable[freeCplIdx] = hdr;
295 OBJ_INDEX(hdr) = freeCplIdx;
296
297 objIndex = freeCplIdx;
298 IdxCplList(context, objIndex) = nullptr;
299 IdxNCpl(context, objIndex) = 0;
300
301 ctx.nCpls += 1;
302 }
303 else
304 {
305 for(cp2=IdxCplList(context, objIndex); cp2!=NULL; cp2=CPL_NEXT(cp2))
306 {
307 if (CPL_PROC(cp2)==proc)
308 {
309 cp2->prio = prio;
310 return(cp2);
311 }
312 }
313 }
314
315 /* create new coupling record */
316 cp = NewCoupling(context);
317 if (cp==NULL) {
318 DDD_PrintError('E', 2500, STR_NOMEM " in AddCoupling");
319 return(NULL);
320 }
321
322 /* init contents */
323 cp->obj = hdr;
324 CPL_PROC(cp) = proc;
325 cp->prio = prio;
326
327 /* insert into theCpl array */
328 CPL_NEXT(cp) = IdxCplList(context, objIndex);
329 IdxCplList(context, objIndex) = cp;
330 IdxNCpl(context, objIndex)++;
331
332 return(cp);
333 }
334
335
336
337
338
339 /****************************************************************************/
340 /* */
341 /* Function: ModCoupling */
342 /* */
343 /* Purpose: find existing coupling record and modify priority */
344 /* this function does coupling upgrade/downgrade without */
345 /* complaining. */
346 /* */
347 /* Input: hdr: DDD-header of object with new coupling */
348 /* proc: owner of copy to be modified */
349 /* prio: new priority of copy */
350 /* */
351 /* Output: ptr to old cpl record */
352 /* NULL on error */
353 /* */
354 /****************************************************************************/
355
356 COUPLING *ModCoupling(DDD::DDDContext& context, DDD_HDR hdr, DDD_PROC proc, DDD_PRIO prio)
357 {
358 int objIndex;
359
360 assert(proc!=context.me());
361
362 # if DebugCoupling<=1
363 Dune::dvverb << "ModCoupling " << OBJ_GID(hdr)
364 << " proc=" << proc << " prio=" << prio << "\n";
365 # endif
366
367 /* find or free position in coupling array */
368 objIndex = OBJ_INDEX(hdr);
369 if (! ObjHasCpl(context, hdr))
370 {
371 /* there are no couplings for this object! */
372 Dune::dwarn << "ModCoupling: no couplings for " << OBJ_GID(hdr) << "\n";
373 return(NULL);
374 }
375 else
376 {
377 /* look if coupling exists and change it */
378 for (COUPLING *cp2 = IdxCplList(context, objIndex); cp2 != NULL; cp2 = CPL_NEXT(cp2))
379 {
380 if (CPL_PROC(cp2)==proc)
381 {
382 cp2->prio = prio;
383 return(cp2);
384 }
385 }
386 }
387
388 /* coupling not found */
389 DUNE_THROW(Dune::Exception,
390 "no coupling from " << proc << " for " << OBJ_GID(hdr));
391 }
392
393
394
395
396 /****************************************************************************/
397 /* */
398 /* Function: DelCoupling */
399 /* */
400 /* Purpose: remove coupling record from object */
401 /* */
402 /* Input: hdr: DDD-header of object with old coupling */
403 /* proc: owner of copy to be un-registered */
404 /* */
405 /* Output: - */
406 /* */
407 /****************************************************************************/
408
409 void DelCoupling (DDD::DDDContext& context, DDD_HDR hdr, DDD_PROC proc)
410 {
411 COUPLING *cpl, *cplLast;
412 auto& objTable = context.objTable();
413 const int objIndex = OBJ_INDEX(hdr);
414 auto& ctx = context.couplingContext();
415
416 if (objIndex < ctx.nCpls)
417 {
418 for(cpl=IdxCplList(context, objIndex), cplLast=NULL; cpl!=NULL; cpl=CPL_NEXT(cpl))
419 {
420 if(CPL_PROC(cpl)==proc)
421 {
422 if (cplLast==NULL)
423 {
424 IdxCplList(context, objIndex) = CPL_NEXT(cpl);
425 }
426 else {
427 CPL_NEXT(cplLast) = CPL_NEXT(cpl);
428 }
429 # if DebugCoupling<=1
430 Dune::dvverb << "DelCoupling " << OBJ_GID(hdr) << " on proc=" << proc
431 << ", now " << (IdxNCpl(context, objIndex)-1) << " cpls\n";
432 # endif
433
434 DisposeCoupling(context, cpl);
435
436 IdxNCpl(context, objIndex)--;
437
438 if (IdxNCpl(context, objIndex)==0)
439 {
440 ctx.nCpls -= 1;
441
442 #ifdef WithFullObjectTable
443 OBJ_INDEX(hdr) = ctx.nCpls;
444 OBJ_INDEX(objTable[ctx.nCpls]) = objIndex;
445 objTable[objIndex] = objTable[ctx.nCpls];
446 objTable[ctx.nCpls] = hdr;
447 #else
448 /* we will not register objects without coupling,
449 so we have to forget about hdr and mark it as local. */
450 context.nObjs(context.nObjs() - 1);
451 assert(context.nObjs() == ctx.nCpls);
452
453 objTable[objIndex] = objTable[ctx.nCpls];
454 OBJ_INDEX(objTable[ctx.nCpls]) = objIndex;
455
456 MarkHdrLocal(hdr);
457 #endif
458
459 IdxCplList(context, objIndex) = IdxCplList(context, ctx.nCpls);
460 IdxNCpl(context, objIndex) = IdxNCpl(context, ctx.nCpls);
461 }
462 break;
463 }
464 cplLast = cpl;
465 }
466 }
467 }
468
469
470 /****************************************************************************/
471 /* */
472 /* Function: DisposeCouplingList */
473 /* */
474 /* Purpose: dispose complete coupling list */
475 /* */
476 /* Input: cpl: first element of coupling list */
477 /* */
478 /* Output: - */
479 /* */
480 /****************************************************************************/
481
482 void DisposeCouplingList (DDD::DDDContext& context, COUPLING *cpl)
483 {
484 COUPLING *c, *next;
485
486 c = cpl;
487 while (c!=NULL)
488 {
489 next = CPL_NEXT(c);
490 DisposeCoupling(context, c);
491 c = next;
492 }
493 }
494
495
496 /*
497 * DDD_InfoProcListRange
498 */
499
500 DDD_InfoProcListRange::DDD_InfoProcListRange(DDDContext& context, const DDD_HDR hdr, bool includeDummy) noexcept
501 : includeDummy_(includeDummy)
502 {
503 dummy_._proc = context.me();
504 dummy_.prio = OBJ_PRIO(hdr);
505
506 const auto objIndex = OBJ_INDEX(hdr);
507 if (objIndex < context.couplingContext().nCpls) {
508 dummy_._next = IdxCplList(context, objIndex);
509 }
510 else {
511 dummy_._next = nullptr;
512 }
513 }
514
515
516
517 /****************************************************************************/
518 /* */
519 /* Function: DDD_InfoProcPrio */
520 /* */
521 /* Purpose: return first processor number with a given priority */
522 /* */
523 /* Input: hdr: DDD-header of object with coupling */
524 /* prio: priority to search for */
525 /* */
526 /* Output: id of processor which holds the object copy with prio */
527 /* (or procs if no such copy exists) */
528 /* */
529 /****************************************************************************/
530
531 DDD_PROC DDD_InfoProcPrio(const DDD::DDDContext& context, DDD_HDR hdr, DDD_PRIO prio)
532 {
533 COUPLING *cpl;
534 int objIndex = OBJ_INDEX(hdr);
535
536 /* append descriptions of foreign copies */
537 if (objIndex < context.couplingContext().nCpls)
538 {
539 for(cpl=IdxCplList(context, objIndex); cpl!=NULL; cpl=CPL_NEXT(cpl))
540 {
541 if (cpl->prio == prio)
542 return(CPL_PROC(cpl));
543 }
544 }
545
546 /* eventually local copy has priority we are looking for */
547 if (OBJ_PRIO(hdr)==prio)
548 return context.me();
549
550 return context.procs();
551 }
552
553
554 bool DDD_InfoIsLocal(const DDD::DDDContext& context, DDD_HDR hdr)
555 {
556 return(! ObjHasCpl(context, hdr));
557 }
558
559
560 int DDD_InfoNCopies(const DDD::DDDContext& context, DDD_HDR hdr)
561 {
562 /*
563 COUPLING *cpl;
564 int n = 0;
565
566 if (ObjHasCpl(context, hdr))
567 {
568 for(cpl=IdxCplList(context, OBJ_INDEX(hdr)); cpl!=NULL; cpl=CPL_NEXT(cpl))
569 n++;
570 }
571 */
572
573 return(ObjNCpl(context, hdr));
574 }
575
576
577 /****************************************************************************/
578 /* */
579 /* Function: DDD_InfoCoupling */
580 /* */
581 /* Purpose: displays list of coupling for certain object */
582 /* */
583 /* Input: hdr: DDD-header of object with coupling */
584 /* */
585 /* Output: - */
586 /* */
587 /****************************************************************************/
588
589 void DDD_InfoCoupling(const DDD::DDDContext& context, DDD_HDR hdr)
590 {
591 int objIndex = OBJ_INDEX(hdr);
592 const auto& nCpls = context.couplingContext().nCpls;
593
594 std::cout << "InfoCoupling for object " << OBJ_GID(hdr)
595 << " (" << objIndex << "/" << nCpls << ")\n";
596
597 if (objIndex < nCpls)
598 {
599 for(const COUPLING* cpl=IdxCplList(context, objIndex); cpl!=NULL; cpl=CPL_NEXT(cpl))
600 std::cout << " cpl " << cpl << " proc=" << CPL_PROC(cpl)
601 << " prio=" << cpl->prio << "\n";
602 }
603 }
604
605
606
607
608 /****************************************************************************/
609 /* */
610 /* Function: DDD_InfoCplMemory */
611 /* */
612 /* Purpose: returns number of bytes used for coupling data */
613 /* */
614 /* Input: - */
615 /* */
616 /* Output: size of memory used for couplings */
617 /* */
618 /****************************************************************************/
619
620 size_t DDD_InfoCplMemory(const DDD::DDDContext& context)
621 {
622 return sizeof(CplSegm) * context.cplmgrContext().nCplSegms;
623 }
624
625
626
627 /****************************************************************************/
628 /* */
629 /* Function: CplMgrInit and CplMgrExit */
630 /* */
631 /* Purpose: init/exit coupling manager */
632 /* */
633 /* Input: - */
634 /* */
635 /* Output: - */
636 /* */
637 /****************************************************************************/
638
639 void ddd_CplMgrInit(DDD::DDDContext& context)
640 {
641 auto& mctx = context.cplmgrContext();
642
643 /* allocate first (smallest) coupling tables */
644 AllocCplTables(context, MAX_CPL_START);
645
646
647 mctx.localIBuffer = (int*)AllocFix((2*context.procs()+1)*sizeof(int));
648 if (mctx.localIBuffer == nullptr)
649 throw std::bad_alloc();
650
651 mctx.memlistCpl = nullptr;
652 mctx.segmCpl = nullptr;
653 mctx.nCplSegms = 0;
654 }
655
656
657 void ddd_CplMgrExit(DDD::DDDContext& context)
658 {
659 auto& ctx = context.couplingContext();
660 auto& mctx = context.cplmgrContext();
661
662 FreeFix(mctx.localIBuffer);
663 FreeCplSegms(context);
664
665 ctx.cplTable.clear();
666 ctx.nCplTable.clear();
667 }
668
669 /****************************************************************************/
670
671 END_UGDIM_NAMESPACE