Debian Codegraph @

dune/uggrid/parallel/dddif/partition.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: partition.c */
8 /* */
9 /* Purpose: check and restrict partitioning for grid adaption */
10 /* */
11 /* Author: Stefan Lang */
12 /* Institut fuer Computeranwendungen III */
13 /* Universitaet Stuttgart */
14 /* Pfaffenwaldring 27 */
15 /* 70569 Stuttgart */
16 /* */
17 /* History: 970204 sl begin */
18 /* */
19 /* */
20 /****************************************************************************/
21
22 #ifdef ModelP
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 <cassert>
35 #include <cmath>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39
40 /* low module */
41 #include <dune/uggrid/low/debug.h>
42 #include <dune/uggrid/low/heaps.h>
43 #include <dune/uggrid/low/misc.h>
44 #include <dune/uggrid/low/namespace.h>
45 #include <dune/uggrid/low/ugtypes.h>
46
47 /* dev module */
48 #include <dune/uggrid/ugdevices.h>
49
50 /* gm module */
51 #include <dune/uggrid/gm/algebra.h>
52 #include <dune/uggrid/gm/evm.h>
53 #include <dune/uggrid/gm/gm.h>
54 #include <dune/uggrid/gm/pargm.h>
55 #include <dune/uggrid/gm/refine.h>
56 #include <dune/uggrid/gm/rm.h>
57 #include <dune/uggrid/gm/ugm.h>
58
59 /* parallel modules */
60 #include <dune/uggrid/parallel/ddd/include/ddd.h>
61 #include <dune/uggrid/parallel/ppif/ppif.h>
62 #include "identify.h"
63 #include "parallel.h"
64
65 /* UG namespaces: */
66 USING_UG_NAMESPACES
67
68 /* PPIF namespace: */
69 using namespace PPIF;
70
71
72 /****************************************************************************/
73 /* */
74 /* defines in the following order */
75 /* */
76 /* compile time constants defining static data size (i.e. arrays) */
77 /* other constants */
78 /* macros */
79 /* */
80 /****************************************************************************/
81
82 /* undefine if overlap should be only updated where needed */
83 /* This does not work since the connection of the overlap needs the
84 fatherelements on both sides (ghost and master sons) and this is
85 not ensured.
86 #define UPDATE_FULLOVERLAP
87 */
88
89 /****************************************************************************/
90 /* */
91 /* data structures used in this source file (exported data structures are */
92 /* in the corresponding include file!) */
93 /* */
94 /****************************************************************************/
95
96
97 /****************************************************************************/
98 /* */
99 /* definition of exported global variables */
100 /* */
101 /****************************************************************************/
102
103
104 /****************************************************************************/
105 /* */
106 /* definition of variables global to this source file only (static!) */
107 /* */
108 /****************************************************************************/
109
110
111 /****************************************************************************/
112 /* */
113 /* forward declarations of functions used before they are defined */
114 /* */
115 /****************************************************************************/
116
117
118 /****************************************************************************/
119 /*
120 CheckPartitioning - check whether all master copies have master copies of the sons
121
122 SYNOPSIS:
123 INT CheckPartitioning (MULTIGRID *theMG);
124
125 PARAMETERS:
126 . theMG
127
128 DESCRIPTION:
129 This function checks whether all master copies of elements which may be involved in next refinement step have master copies of the sons (if existing) at the same processor.
130
131 RETURN VALUE:
132 INT
133 .n GM_OK - ok
134 .n GM_ERROR - error
135 */
136 /****************************************************************************/
137
138 INT NS_DIM_PREFIX CheckPartitioning (MULTIGRID *theMG)
139 {
140 INT i,_restrict_;
141 ELEMENT *theElement;
142 ELEMENT *theFather;
143
144 _restrict_ = 0;
145
146 /* reset used flags */
147 for (i=TOPLEVEL(theMG); i>0; i--)
148 {
149 const GRID *theGrid = GRID_ON_LEVEL(theMG,i);
150 for (theElement=FIRSTELEMENT(theGrid); theElement!=NULL;
151 theElement=SUCCE(theElement))
152 {
153 if (LEAFELEM(theElement))
154 {
155 theFather = theElement;
156 while (EMASTER(theFather) && ECLASS(theFather)!=RED_CLASS
157 && LEVEL(theFather)>0)
158 {
159 theFather = EFATHER(theFather);
160 }
161
162 /* if element with red element class does not exist */
163 /* or is ghost -> partitioning must be restricted */
164 if (!EMASTER(theFather))
165 {
166 UserWriteF("elem=" EID_FMTX " cannot be refined\n",
167 EID_PRTX(theFather));
168 _restrict_ = 1;
169 continue;
170 }
171 if (COARSEN(theFather))
172 {
173 /* level 0 elements cannot be coarsened */
174 if (LEVEL(theFather)<=1) continue;
175 if (!EMASTER(EFATHER(theFather)))
176 {
177 UserWriteF("elem=" EID_FMTX " cannot be coarsened\n",
178 EID_PRTX(theFather));
179 _restrict_ = 1;
180 }
181 }
182 }
183 }
184 }
185
186 _restrict_ = UG_GlobalMaxINT(theMG->ppifContext(), _restrict_);
187 if (theMG->dddContext().isMaster() && _restrict_==1)
188 {
189 UserWriteF("CheckPartitioning(): partitioning is not valid for refinement\n");
190 UserWriteF(" cleaning up ...\n");
191 }
192
193 return(_restrict_);
194 }
195
196 /****************************************************************************/
197 /*
198 Gather_ElementRestriction -
199
200 SYNOPSIS:
201 static int Gather_ElementRestriction (DDD_OBJ obj, void *data);
202
203 PARAMETERS:
204 . obj
205 . data
206
207 DESCRIPTION:
208
209 RETURN VALUE:
210 int
211 */
212 /****************************************************************************/
213
214 static int Gather_ElementRestriction (DDD::DDDContext&, DDD_OBJ obj, void *data)
215 {
216 ELEMENT *theElement = (ELEMENT *)obj;
217
218 PRINTDEBUG(gm,4,(PFMT "Gather_ElementRestriction(): e=" EID_FMTX "\n",
219 me,EID_PRTX(theElement)))
220 ((int *)data)[0] = USED(theElement);
221
222 return(GM_OK);
223 }
224
225
226 /****************************************************************************/
227 /*
228 Scatter_ElementRestriction -
229
230 SYNOPSIS:
231 static int Scatter_ElementRestriction (DDD_OBJ obj, void *data);
232
233 PARAMETERS:
234 . obj
235 . data
236
237 DESCRIPTION:
238
239 RETURN VALUE:
240 int
241 */
242 /****************************************************************************/
243
244 static int Scatter_ElementRestriction (DDD::DDDContext&, DDD_OBJ obj, void *data)
245 {
246 ELEMENT *theElement = (ELEMENT *)obj;
247
248 PRINTDEBUG(gm,4,(PFMT "Scatter_ElementRestriction(): e=" EID_FMTX "\n",
249 me,EID_PRTX(theElement)))
250 if (EMASTER(theElement))
251 {
252 PRINTDEBUG(gm,4,(PFMT "Scatter_ElementRestriction(): restricting sons of e=" EID_FMTX "\n",
253 me,EID_PRTX(theElement)))
254 int used = std::max((INT)USED(theElement),((int *)data)[0]);
255 SETUSED(theElement,used);
256 }
257
258 return(GM_OK);
259 }
260
261
262 /****************************************************************************/
263 /*
264 Gather_RestrictedPartition -
265
266 SYNOPSIS:
267 static int Gather_RestrictedPartition (DDD_OBJ obj, void *data, DDD_PROC proc, DDD_PRIO prio);
268
269 PARAMETERS:
270 . obj
271 . data
272 . proc
273 . prio
274
275 DESCRIPTION:
276
277 RETURN VALUE:
278 int
279 */
280 /****************************************************************************/
281
282 static int Gather_RestrictedPartition (DDD::DDDContext&, DDD_OBJ obj, void *data, DDD_PROC proc, DDD_PRIO prio)
283 {
284 ELEMENT *theElement = (ELEMENT *)obj;
285
286 if (EMASTER(theElement))
287 {
288 PRINTDEBUG(gm,4,(PFMT "Gather_RestrictedPartition(): e=" EID_FMTX "\n",
289 me,EID_PRTX(theElement)))
290 ((int *)data)[0] = PARTITION(theElement);
291 }
292
293 return(GM_OK);
294 }
295
296
297 /****************************************************************************/
298 /*
299 Scatter_RestrictedPartition -
300
301 SYNOPSIS:
302 static int Scatter_RestrictedPartition (DDD_OBJ obj, void *data, DDD_PROC proc, DDD_PRIO prio);
303
304 PARAMETERS:
305 . obj
306 . data
307 . proc
308 . prio
309
310 DESCRIPTION:
311
312 RETURN VALUE:
313 int
314 */
315 /****************************************************************************/
316
317 static int Scatter_RestrictedPartition (DDD::DDDContext&, DDD_OBJ obj, void *data, DDD_PROC proc, DDD_PRIO prio)
318 {
319 ELEMENT *theElement = (ELEMENT *)obj;
320 ELEMENT *SonList[MAX_SONS];
321
322 if (USED(theElement) && EMASTERPRIO(prio))
323 {
324 PRINTDEBUG(gm,4,(PFMT "Scatter_ElementRestriction(): restricting sons of e=" EID_FMTX "\n",
325 me,EID_PRTX(theElement)))
326
327 int partition = ((int *)data)[0];
328 /* send master sons to master element partition */
329 if (GetSons(theElement,SonList)) RETURN(GM_ERROR);
330 for (int i = 0; SonList[i] != NULL; i++)
331 PARTITION(SonList[i]) = partition;
332 }
333
334 return(GM_OK);
335 }
336
337 /****************************************************************************/
338 /*
339 RestrictPartitioning -
340
341 SYNOPSIS:
342 INT RestrictPartitioning (MULTIGRID *theMG);
343
344 PARAMETERS:
345 . theMG
346
347 DESCRIPTION:
348
349 RETURN VALUE:
350 INT
351 */
352 /****************************************************************************/
353
354 INT NS_DIM_PREFIX RestrictPartitioning (MULTIGRID *theMG)
355 {
356 auto& context = theMG->dddContext();
357 const auto& dddctrl = ddd_ctrl(context);
358
359 INT i,j;
360 ELEMENT *theElement;
361 ELEMENT *theFather;
362 ELEMENT *SonList[MAX_SONS];
363 GRID *theGrid;
364
365 /* reset used flags */
366 for (i=TOPLEVEL(theMG); i>=0; i--)
367 {
368 theGrid = GRID_ON_LEVEL(theMG,i);
369 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
370 theElement=SUCCE(theElement))
371 {
372 SETUSED(theElement,0);
373 }
374 }
375
376 /* set flags on elements which violate restriction */
377 for (i=TOPLEVEL(theMG); i>=0; i--)
378 {
379 theGrid = GRID_ON_LEVEL(theMG,i);
380 for (theElement=FIRSTELEMENT(theGrid); theElement!=NULL;
381 theElement=SUCCE(theElement))
382 {
383 if (GLEVEL(theGrid) == 0) break;
384 if (LEAFELEM(theElement) || USED(theElement))
385 {
386 theFather = theElement;
387 while (EMASTER(theFather) && ECLASS(theFather)!=RED_CLASS
388 && LEVEL(theFather)>0)
389 {
390 theFather = EFATHER(theFather);
391 }
392
393 /* if father with red refine class is not master */
394 /* partitioning must be restricted */
395 if (!EMASTER(theFather))
396 {
397 /* the sons of father will be sent to partition of father */
398 SETUSED(theFather,1);
399 }
400
401 /* if element is marked for coarsening and father */
402 /* of element is not master -> restriction is needed */
403 if (COARSEN(theFather))
404 {
405 /* level 0 elements are not coarsened */
406 if (LEVEL(theFather)<=1) continue;
407 if (!EMASTER(EFATHER(theFather)))
408 SETUSED(EFATHER(theFather),1);
409 }
410 }
411 }
412 /* transfer restriction flags to master copies of father */
413 DDD_IFAOneway(context,
414 dddctrl.ElementVHIF,GRID_ATTR(theGrid),IF_BACKWARD,sizeof(INT),
415 Gather_ElementRestriction, Scatter_ElementRestriction);
416 }
417
418 /* send restricted sons to partition of father */
419 for (i=0; i<=TOPLEVEL(theMG); i++)
420 {
421 theGrid = GRID_ON_LEVEL(theMG,i);
422
423 /* transfer (new) partitions of elements to non master copies */
424 DDD_IFAOnewayX(context,
425 dddctrl.ElementVHIF,GRID_ATTR(theGrid),IF_FORWARD,sizeof(INT),
426 Gather_RestrictedPartition, Scatter_RestrictedPartition);
427
428 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
429 theElement=SUCCE(theElement))
430 {
431 if (!USED(theElement)) continue;
432
433 /* push partition to the sons */
434 GetAllSons(theElement,SonList);
435 for (j=0; SonList[j]!=NULL; j++)
436 {
437 SETUSED(SonList[j],1);
438 if (EMASTER(SonList[j]))
439 PARTITION(SonList[j]) = PARTITION(theElement);
440 }
441 }
442 }
443
444 if (TransferGrid(theMG) != 0) RETURN(GM_FATAL);
445
446 return(GM_OK);
447 }
448
449 #endif