Debian Codegraph @

dune/uggrid/parallel/dddif/lb.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: lb.c */
8 /* */
9 /* Purpose: a collect of simple and special load balancing functionality */
10 /* */
11 /* Author: Stefan Lang, Klaus Birken, Christian Wieners */
12 /* Institut fuer Computeranwendungen III */
13 /* Universitaet Stuttgart */
14 /* Pfaffenwaldring 27 */
15 /* 70550 Stuttgart */
16 /* */
17 /* History: 960906 kb begin */
18 /* 980204 sl renamed test.c to lb.c */
19 /* */
20 /* Remarks: */
21 /* */
22 /****************************************************************************/
23
24 #ifdef ModelP
25
26 /****************************************************************************/
27 /* */
28 /* include files */
29 /* system include files */
30 /* application include files */
31 /* */
32 /****************************************************************************/
33
34 #include <config.h>
35 #include <cstdio>
36
37 #include "parallel.h"
38 #include <dune/uggrid/gm/evm.h>
39 #include <dune/uggrid/gm/ugm.h>
40 #include <dune/uggrid/ugdevices.h>
41 #include <dune/uggrid/low/namespace.h>
42
43 /* UG namespaces: */
44 USING_UG_NAMESPACES
45
46 /* PPIF namespaces: */
47 using namespace PPIF;
48
49 START_UGDIM_NAMESPACE
50
51 /****************************************************************************/
52 /* */
53 /* defines in the following order */
54 /* */
55 /* compile time constants defining static data size (i.e. arrays) */
56 /* other constants */
57 /* macros */
58 /* */
59 /****************************************************************************/
60
61
62 /****************************************************************************/
63 /* */
64 /* data structures used in this source file (exported data structures are */
65 /* in the corresponding include file!) */
66 /* */
67 /****************************************************************************/
68
69
70 /****************************************************************************/
71 /* */
72 /* definition of exported global variables */
73 /* */
74 /****************************************************************************/
75
76
77 /****************************************************************************/
78 /* */
79 /* forward declarations of functions used before they are defined */
80 /* */
81 /****************************************************************************/
82
83
84 /****************************************************************************/
85 /*
86 TransferGridComplete-
87
88 SYNOPSIS:
89 static int TransferGridComplete (MULTIGRID *theMG, INT level);
90
91 PARAMETERS:
92 . theMG
93 . level
94
95 DESCRIPTION:
96
97 RETURN VALUE:
98 int
99 */
100 /****************************************************************************/
101
102 static int TransferGridComplete (MULTIGRID *theMG, INT level)
103 {
104 ELEMENT *e;
105 GRID *theGrid = GRID_ON_LEVEL(theMG,level);
106
107 if (theGrid==NULL)
108 {
109 const auto& me = theMG->dddContext().me();
110 UserWriteF(PFMT "TransferGridComplete(): no grid on level=%d\n",me,level);
111 return(0);
112 }
113
114 /* assign elements of level 0 */
115 if (theMG->dddContext().isMaster()) {
116 for (e=FIRSTELEMENT(theGrid); e!=NULL; e=SUCCE(e))
117 PARTITION(e) = 1;
118 }
119
120 IFDEBUG(dddif,1);
121 for (e=FIRSTELEMENT(theGrid); e!=NULL; e=SUCCE(e))
122 {
123 UserWriteF("elem %08x has dest=%d\n",
124 DDD_InfoGlobalId(PARHDRE(e)), PARTITION(e));
125 }
126 ENDDEBUG
127
128 return(0);
129 }
130
131
132 /****************************************************************************/
133 /*
134 TransferGridToMaster -
135
136 SYNOPSIS:
137 static int TransferGridToMaster (MULTIGRID *theMG, INT fl, INT tl);
138
139 PARAMETERS:
140 . theMG
141 . fl
142 . tl
143
144 DESCRIPTION:
145
146 RETURN VALUE:
147 int
148 */
149 /****************************************************************************/
150
151 static int TransferGridToMaster (MULTIGRID *theMG, INT fl, INT tl)
152 {
153 /* send all levels to master */
154 if (not theMG->dddContext().isMaster())
155 {
156 for (int l = fl; l <= tl; l++)
157 {
158 const GRID *theGrid = GRID_ON_LEVEL(theMG,l);
159
160 /* create element copies */
161 for (ELEMENT *e = FIRSTELEMENT(theGrid); e != NULL; e = SUCCE(e))
162 {
163 PARTITION(e) = 0;
164 }
165 }
166 }
167
168 return(0);
169 }
170
171
172 /****************************************************************************/
173 /*
174 CollectElementsNearSegment -
175
176 SYNOPSIS:
177 static int CollectElementsNearSegment(MULTIGRID *theMG, int level, int dest);
178
179 PARAMETERS:
180 . theMG
181 . level
182 . part
183 . dest
184
185 DESCRIPTION:
186
187 RETURN VALUE:
188 int
189 */
190 /****************************************************************************/
191
192 static int CollectElementsNearSegment(MULTIGRID *theMG,
193 int fl, int tl, int dest)
194 {
195 ELEMENT *theElement;
196 INT side,sid,nbsid,level;
197
198 for (level=fl; level<=tl; level ++)
199 for (theElement=FIRSTELEMENT(GRID_ON_LEVEL(theMG,level));
200 theElement!=NULL; theElement=SUCCE(theElement))
201 if (OBJT(theElement) == BEOBJ)
202 for (side=0; side<SIDES_OF_ELEM(theElement); side++) {
203 if (INNER_SIDE(theElement,side))
204 continue;
205 BNDS_BndSDesc(ELEM_BNDS(theElement,side),
206 &sid,&nbsid);
207 PARTITION(theElement) = dest;
208 }
209
210 return(0);
211 }
212
213
214 static int PartitionElementsForDD(GRID *theGrid, int hor_boxes, int vert_boxes )
215 {
216 ELEMENT *theElement;
217 INT i, nrcorners;
218 DOUBLE xmax, ymax;
219
220 for (theElement=FIRSTELEMENT(theGrid); theElement!=NULL;
221 theElement=SUCCE(theElement))
222 {
223 nrcorners = CORNERS_OF_ELEM(theElement);
224 ASSERT(nrcorners==4 || nrcorners==3 ); /* works only for triangle and quadrilateral grids */
225
226 /* calculate the coordinates xmax, ymax of the element */
227 xmax = ymax = 0.0;
228 for( i=0; i<nrcorners; i++ )
229 {
230 const FieldVector<DOUBLE,DIM>& coord = CVECT(MYVERTEX(CORNER(theElement,i)));
231 xmax = std::max(xmax,coord[0]);
232 ymax = std::max(ymax,coord[1]);
233 }
234 ASSERT(xmax>0.0 && xmax<1.00001); /* this function works only for the unit square! */
235 ASSERT(ymax>0.0 && ymax<1.00001);
236 /*printf( PFMT "element coord %g %g %d %d\n", me, xmax, ymax, hor_boxes, vert_boxes );*/
237
238 /* the according subdomain is determined by the upper right corner
239 the idea: for each dimension: calculate from the element coord the position in the
240 corresponding array dimension
241 then map the array coordinates onto a PE number */
242 PARTITION(theElement) = (int)((ymax-0.00001)*vert_boxes) * hor_boxes + (int)((xmax-0.00001)*hor_boxes);
243 /*printf( PFMT "element partition %d\n", me, PARTITION(theElement) );*/
244 }
245
246 return(0);
247 }
248
249 static void CreateDD(MULTIGRID *theMG, INT level, int hor_boxes, int vert_boxes )
250 /* the final call to TransferGridFromLevel() must be done by the current caller */
251 {
252 INT elements;
253 GRID *theGrid;
254
255 theGrid = GRID_ON_LEVEL(theMG,level);
256 if( hor_boxes*vert_boxes >= 4 )
257 {
258 elements = NT(theGrid);
259 elements = UG_GlobalMaxINT(theMG->ppifContext(), elements);
260
261 if( elements > 20000 )
262 {
263 /* we have a case with too heavy load for DDD; thus subdivide */
264 /* find a coarser auxiliary partition */
265 int hor = hor_boxes;
266 int vert = vert_boxes;
267
268 if( hor%2 == 0 )
269 hor /= 2; /* hor_boxes can be halfened */
270 else if( vert%2 == 0 )
271 vert /= 2; /* vert_boxes can be halfened */
272 else
273 assert(0); /* further consideration for subdividing necessary */
274
275 CreateDD(theMG, level, hor, vert ); /* simplify the problem */
276 TransferGridFromLevel(theMG,level);
277 }
278 }
279
280 PartitionElementsForDD(theGrid, hor_boxes, vert_boxes );
281 }
282
283 static int SimpleSubdomainDistribution (MULTIGRID *theMG, INT Procs, INT from, INT to)
284 {
285 INT i;
286 ELEMENT *e;
287
288 for (i=from; i<=to; i++)
289 for (e=FIRSTELEMENT(GRID_ON_LEVEL(theMG,i)); e!=NULL; e=SUCCE(e))
290 PARTITION(e)=SUBDOMAIN(e)-1;
291 return(0);
292 }
293
294 /****************************************************************************/
295 /*
296 lbs - interface for simple or special load balancing functionality
297
298 SYNOPSIS:
299 void lbs (char *argv, MULTIGRID *theMG);
300
301 PARAMETERS:
302 . argv
303 . theMG
304
305 DESCRIPTION:
306
307 RETURN VALUE:
308 void
309 */
310 /****************************************************************************/
311
312 void lbs (const char *argv, MULTIGRID *theMG)
313 {
314 int n,mode,param,fromlevel,tolevel,hor_boxes,vert_boxes,dest;
315
316 const auto& me = theMG->dddContext().me();
317 const auto procs = theMG->dddContext().procs();
318
319 mode = param = fromlevel = tolevel = 0;
320
321 n = sscanf(argv,"%d %d %d",&param,&fromlevel,&tolevel);
322
323 UserWriteF(PFMT "lbs() param=%d",me,param);
324 if (n > 1)
325 UserWriteF(" fromlevel=%d",fromlevel);
326 if (n > 2)
327 UserWriteF(" tolevel=%d",tolevel);
328 UserWriteF("\n");
329
330 /* param>100 is used as switch for DDD xfer statistics */
331 if (param>=100)
332 mode = param-100;
333 else
334 mode = param;
335
336 /* switch DDD infos on */
337 if (param>=100)
338 DDD_SetOption(theMG->dddContext(), OPT_INFO_XFER, XFER_SHOW_MEMUSAGE);
339
340 switch (mode)
341 {
342 /* dies balanciert ein GRID mit RCB */
343 case (0) :
344 BalanceGridRCB(theMG,0);
345 fromlevel = 0;
346 break;
347
348 /* dies verschickt ein GRID komplett */
349 case (1) :
350 TransferGridComplete(theMG,fromlevel);
351 break;
352
353 /* dies verschickt ein verteiltes GRID zum master */
354 case (2) :
355 TransferGridToMaster(theMG,fromlevel,tolevel);
356 fromlevel = 0;
357 break;
358
359 /* dies balanciert ein GRID mit RCB ab fromlevel */
360 case (3) :
361 if (fromlevel>=0 && fromlevel<=TOPLEVEL(theMG))
362 {
363 BalanceGridRCB(theMG,fromlevel);
364 }
365 else
366 {
367 UserWriteF(PFMT "lbs(): gridlevel=%d not "
368 "existent!\n",me,fromlevel);
369 }
370 break;
371
372 /* dies balanciert ein GRID mit RCB ab fromlevel */
373 case (4) :
374 if ((fromlevel>=0 && fromlevel<=TOPLEVEL(theMG)) ||
375 (tolevel>=0 && tolevel<=TOPLEVEL(theMG)) ||
376 tolevel < fromlevel)
377 {
378 int j;
379
380 for (j=fromlevel; j<=tolevel; j++)
381 BalanceGridRCB(theMG,j);
382 /*
383 TransferGrid(theMG,fromlevel,tolevel);
384 */
385 }
386 else
387 {
388 UserWriteF(PFMT "lbs(): ERROR fromlevel=%d "
389 "tolevel=%d\n",me,fromlevel,tolevel);
390 }
391 break;
392
393 case (5) :
394 n = sscanf(argv,"%d %d %d %d",
395 &param,&dest,&fromlevel,&tolevel);
396 if (n < 4) tolevel = TOPLEVEL(theMG);
397 if (n < 3) fromlevel = 0;
398 if (n < 2) break;
399 CollectElementsNearSegment(theMG,fromlevel,tolevel,dest);
400 UserWriteF(PFMT "lbs() collect to proc %d\n",
401 me,dest);
402 break;
403
404 /* dies erzeugt eine regelmaessige Domain Decomposition */
405 case (6) :
406 if (sscanf(argv,"%d %d %d",&param,&hor_boxes,&vert_boxes) != 3) break;
407 ASSERT(hor_boxes*vert_boxes == procs );
408 fromlevel = TOPLEVEL(theMG);
409 CreateDD(theMG,fromlevel,hor_boxes,vert_boxes);
410 break;
411
412 case (8) :
413 {
414 SimpleSubdomainDistribution(theMG,procs,fromlevel,tolevel);
415 break;
416 }
417
418 default :
419 UserWriteF(PFMT "lbs(): strategy (%d) is not implemented!\n", mode);
420 break;
421 }
422
423 TransferGridFromLevel(theMG,fromlevel);
424
425 /* switch DDD infos off */
426 if (param>=100)
427 DDD_SetOption(theMG->dddContext(), OPT_INFO_XFER, XFER_SHOW_NONE);
428 }
429
430 END_UGDIM_NAMESPACE
431
432 #endif /* ModelP */