Debian Codegraph @

dune/uggrid/parallel/ddd/mgr/prio.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: prio.c */
8 /* */
9 /* Purpose: priority management for ddd */
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/04/25 kb begin */
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 <array>
35 #include <cstdlib>
36 #include <cstdio>
37 #include <cstring>
38
39 #include <iomanip>
40 #include <iostream>
41
42 #include <dune/common/exceptions.hh>
43 #include <dune/common/stdstreams.hh>
44
45 #include <dune/uggrid/parallel/ddd/dddi.h>
46
47 USING_UG_NAMESPACE
48
49 /* PPIF namespace: */
50 using namespace PPIF;
51
52 START_UGDIM_NAMESPACE
53
54 /****************************************************************************/
55 /* */
56 /* defines in the following order */
57 /* */
58 /* compile time constants defining static data size (i.e. arrays) */
59 /* other constants */
60 /* macros */
61 /* */
62 /****************************************************************************/
63
64
65 /* get index for one-dimensional storage of twodimensional symm. matrix,
66 which is stored as lower triangle and diagonal
67
68 col must be <= row !
69 */
70 #define PM_ENTRY(pm,row,col) (pm[((((row)+1)*(row))/2)+(col)])
71 #define PM_SIZE ((MAX_PRIO*(MAX_PRIO+1))/2)
72
73
74 /* get priority-merge value for given default mode */
75 #define PM_GETDEFAULT(mm,p1,p2,pres) \
76 switch (mm) { \
77 case PRIOMERGE_MAXIMUM : pres = MAX(p1,p2); break; \
78 case PRIOMERGE_MINIMUM : pres = MIN(p1,p2); break; \
79 default : pres = 0; break; \
80 }
81
82
83 /****************************************************************************/
84 /* */
85 /* data structures */
86 /* */
87 /****************************************************************************/
88
89
90
91 /****************************************************************************/
92 /* */
93 /* definition of static variables */
94 /* */
95 /****************************************************************************/
96
97
98
99
100
101 /****************************************************************************/
102 /* */
103 /* routines */
104 /* */
105 /****************************************************************************/
106
107
108
109 void DDD_PrioritySet(DDD::DDDContext& context, DDD_HDR hdr, DDD_PRIO prio)
110 {
111 /* check input parameters */
112 if (prio>=MAX_PRIO)
113 DUNE_THROW(Dune::Exception, "priority must be less than " << MAX_PRIO);
114
115 # ifdef LogObjects
116 Dune::dinfo << "LOG DDD_PrioritySet " << OBJ_GID(hdr)
117 << " old=" << OBJ_PRIO(hdr) << " new=" << prio << "\n";
118 # endif
119
120 if (ddd_XferActive(context))
121 {
122 /* we are during Xfer, therefore initiate PrioChange operation */
123 DDD_XferPrioChange(context, hdr, prio);
124 }
125 else if(ddd_PrioActive(context))
126 {
127 /* we are in a Prio-environment, therefore initiate consistent PrioChange operation */
128 DDD_PrioChange(context, hdr, prio);
129 }
130 else
131 {
132 if (! ObjHasCpl(context, hdr))
133 {
134 /* just one local object, we can simply change its priority */
135 OBJ_PRIO(hdr) = prio;
136 }
137 else
138 {
139 /* distributed object will get inconsistent here. issue warning. */
140 if (DDD_GetOption(context, OPT_WARNING_PRIOCHANGE)==OPT_ON)
141 Dune::dwarn << "DDD_PrioritySet: creating inconsistency for gid="
142 << OBJ_GID(hdr) << "\n";
143
144 /* change priority, nevertheless */
145 OBJ_PRIO(hdr) = prio;
146 }
147 }
148 }
149
150
151
152
153
154 /****************************************************************************/
155
156 /*
157 compute the result of merging two priorities.
158
159 this function merges two priorities p1 and p2 for
160 object of DDD_TYPE desc. the default merge operation
161 is used (PRIOMERGE_DEFAULT). if a merge-matrix has been
162 specified, this matrix will be used.
163
164 on return, *pres will contain the resulting priority.
165 the return value is:
166
167 PRIO_UNKNOWN can't decide which priority wins.
168 PRIO_FIRST first priority wins.
169 PRIO_SECOND second priority wins.
170 PRIO_ERROR an error has occurred.
171 */
172
173 enum PrioMergeVals PriorityMerge(const TYPE_DESC* desc, DDD_PRIO p1, DDD_PRIO p2, DDD_PRIO *pres)
174 {
175 /* check if matrix is available */
176 if (desc->prioMatrix == nullptr)
177 {
178 PM_GETDEFAULT(desc->prioDefault, p1, p2, *pres);
179 if (*pres==MAX_PRIO) return PRIO_ERROR;
180 }
181 else
182 {
183 if (p2<=p1)
184 {
185 *pres = PM_ENTRY(desc->prioMatrix,p1,p2);
186 }
187 else
188 {
189 *pres = PM_ENTRY(desc->prioMatrix,p2,p1);
190 }
191
192 }
193
194 if (*pres==p1 || *pres!=p2) return(PRIO_FIRST);
195 if (*pres==p2 || *pres!=p1) return(PRIO_SECOND);
196 return(PRIO_UNKNOWN);
197 }
198
199
200 /****************************************************************************/
201
202 /*
203 allocate prioMatrix and set default entries
204 */
205
206 static int SetPrioMatrix (TYPE_DESC *desc, int priomerge_mode)
207 {
208 int r, c;
209
210 if (desc->prioMatrix==nullptr)
211 {
212 /* prioMatrix has not been allocated before */
213 desc->prioMatrix = std::make_unique<DDD_PRIO[]>(PM_SIZE);
214 }
215
216 for(r=0; r<MAX_PRIO; r++)
217 {
218 for(c=0; c<=r; c++)
219 {
220 DDD_PRIO pres;
221
222 PM_GETDEFAULT(priomerge_mode, r, c, pres);
223 if (pres==MAX_PRIO) return false;
224
225 PM_ENTRY(desc->prioMatrix, r, c) = pres;
226 }
227 }
228
229 /* remember default setting */
230 desc->prioDefault = priomerge_mode;
231
232 return true;
233 }
234
235
236 /****************************************************************************/
237
238 /*
239 check prioMatrix
240 */
241
242 static int CheckPrioMatrix (TYPE_DESC *desc)
243 {
244 int r, c;
245
246 if (desc->prioMatrix==nullptr)
247 /* no prioMatrix defined, ok */
248 return true;
249
250 /* check: entries i must be 0<=i<MAX_PRIO */
251 for(r=0; r<MAX_PRIO; r++)
252 {
253 for(c=0; c<=r; c++)
254 {
255 DDD_PRIO p = PM_ENTRY(desc->prioMatrix,r,c);
256
257 if (p>=MAX_PRIO)
258 DUNE_THROW(Dune::Exception,
259 "PriorityMerge(" << r << "," << c << ") yields"
260 << p << " larger than " << (MAX_PRIO-1));
261 }
262 }
263
264
265 /* TODO: check for associativity */
266
267
268 return true;
269 }
270
271
272
273 /****************************************************************************/
274
275
276 void DDD_PrioMergeDefault (DDD::DDDContext& context, DDD_TYPE type_id, int priomerge_mode)
277 {
278 if (! SetPrioMatrix(&context.typeDefs()[type_id], priomerge_mode))
279 DUNE_THROW(Dune::Exception, "unknown defaultprio-mergemode in DDD_TYPE " << type_id);
280 }
281
282
283
284 void DDD_PrioMergeDefine (DDD::DDDContext& context, DDD_TYPE type_id,
285 DDD_PRIO p1, DDD_PRIO p2, DDD_PRIO pres)
286 {
287 TYPE_DESC *desc = &context.typeDefs()[type_id];
288
289 /* check for correct type */
290 if (! ddd_TypeDefined(desc))
291 DUNE_THROW(Dune::Exception, "undefined DDD_TYPE");
292
293
294 /* create prioMatrix on demand */
295 if (desc->prioMatrix == nullptr)
296 {
297 if (! SetPrioMatrix(desc, PRIOMERGE_DEFAULT))
298 DUNE_THROW(Dune::Exception, "error for DDD_TYPE " << type_id);
299 }
300
301
302 /* check input priorities */
303
304 if (p1>=MAX_PRIO)
305 DUNE_THROW(Dune::Exception, "invalid priority p1=" << p1);
306 if (p2>=MAX_PRIO)
307 DUNE_THROW(Dune::Exception, "invalid priority p2=" << p2);
308 if (pres>=MAX_PRIO)
309 DUNE_THROW(Dune::Exception, "invalid priority pres=" << pres);
310
311
312 /* set prioMatrix entry */
313 if (p2<=p1)
314 {
315 PM_ENTRY(desc->prioMatrix,p1,p2) = pres;
316 }
317 else
318 {
319 PM_ENTRY(desc->prioMatrix,p2,p1) = pres;
320 }
321
322
323 /* finally always check prioMatrix, just to be sure */
324 if (!CheckPrioMatrix(desc))
325 DUNE_THROW(Dune::Exception,
326 "error(s) in merge-check for DDD_TYPE " << type_id);
327 }
328
329
330 /****************************************************************************/
331
332
333 /*
334 call merge operation from application program
335 */
336 DDD_PRIO DDD_PrioMerge (DDD::DDDContext& context, DDD_TYPE type_id, DDD_PRIO p1, DDD_PRIO p2)
337 {
338 TYPE_DESC *desc = &context.typeDefs()[type_id];
339 DDD_PRIO newprio;
340
341 /* check for correct type */
342 if (! ddd_TypeDefined(desc))
343 DUNE_THROW(Dune::Exception, "undefined DDD_TYPE");
344
345 if (p1>=MAX_PRIO)
346 DUNE_THROW(Dune::Exception, "invalid priority p1=" << p1);
347 if (p2>=MAX_PRIO)
348 DUNE_THROW(Dune::Exception, "invalid priority p2=" << p2);
349
350 if (PriorityMerge(desc, p1, p2, &newprio) == PRIO_ERROR)
351 DUNE_THROW(Dune::Exception, "cannot merge priorities");
352
353 return newprio;
354 }
355
356
357 /****************************************************************************/
358
359 static const char* prioMergeDefaultName(int prioDefault)
360 {
361 switch (prioDefault) {
362 case PRIOMERGE_MAXIMUM:
363 return "MAX";
364 case PRIOMERGE_MINIMUM:
365 return "MIN";
366 default:
367 return "(ERROR)";
368 }
369 }
370
371 void DDD_PrioMergeDisplay (DDD::DDDContext& context, DDD_TYPE type_id)
372 {
373 std::ostream& out = std::cout;
374 TYPE_DESC* desc = &context.typeDefs()[type_id];
375 int r, c;
376
377 if (context.me() != 0)
378 return;
379
380 /* check for correct type */
381 if (! ddd_TypeDefined(desc))
382 DUNE_THROW(Dune::Exception, "undefined DDD_TYPE");
383
384 out << "/ PrioMergeDisplay for '" << desc->name << "', default mode "
385 << prioMergeDefaultName(desc->prioDefault) << "\n";
386
387 if (desc->prioMatrix == nullptr)
388 {
389 out << "\\ \t(no special cases defined)\n";
390 return;
391 }
392
393 /* find out which rows/columns we will have to print */
394 std::array<bool, MAX_PRIO> changed_rows;
395
396 for(r=0; r<MAX_PRIO; r++)
397 {
398 changed_rows[r] = false;
399
400 for(c=0; c<MAX_PRIO; c++)
401 {
402 DDD_PRIO p_dflt, p_actual;
403
404 PM_GETDEFAULT(desc->prioDefault, r, c, p_dflt);
405 PriorityMerge(desc, r, c, &p_actual);
406
407 if (p_dflt != p_actual)
408 changed_rows[r] = true;
409 }
410 }
411
412 /* print */
413 using std::setw;
414 out << "|\t ";
415 for(c=0; c<MAX_PRIO; c++)
416 {
417 if (! changed_rows[c])
418 continue;
419
420 out << " " << setw(3) << c << " ";
421 }
422 out << "\n";
423
424 for(r=0; r<MAX_PRIO; r++)
425 {
426 if (! changed_rows[r])
427 continue;
428
429 out << "|\t" << setw(2) << r << " : ";
430 for(c=0; c<MAX_PRIO; c++)
431 {
432 DDD_PRIO p_dflt, p_actual;
433
434 if (! changed_rows[c])
435 continue;
436
437 PM_GETDEFAULT(desc->prioDefault, r, c, p_dflt);
438 PriorityMerge(desc, r, c, &p_actual);
439
440 if (p_dflt != p_actual)
441 out << " " << setw(3) << p_actual << " ";
442 else
443 out << "(" << setw(3) << p_actual << ") ";
444 }
445
446 out << "\n";
447 }
448
449 out << "\\\n";
450 }
451
452 /****************************************************************************/
453
454 END_UGDIM_NAMESPACE