Debian Codegraph @

dune/uggrid/parallel/ddd/prio/pcmds.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: pcmds.c */
8 /* */
9 /* Purpose: DDD-commands for Prio Environment */
10 /* */
11 /* Author: Klaus Birken */
12 /* Institut fuer Computeranwendungen III */
13 /* Universitaet Stuttgart */
14 /* Pfaffenwaldring 27 */
15 /* 70569 Stuttgart */
16 /* email: [email protected] */
17 /* phone: 0049-(0)711-685-7007 */
18 /* fax : 0049-(0)711-685-7000 */
19 /* */
20 /* History: 980720 kb begin */
21 /* */
22 /* Remarks: */
23 /* */
24 /****************************************************************************/
25
26 /****************************************************************************/
27 /* */
28 /* include files */
29 /* system include files */
30 /* application include files */
31 /* */
32 /****************************************************************************/
33
34 /* standard C library */
35 #include <config.h>
36 #include <cstdlib>
37 #include <cstdio>
38 #include <cstring>
39
40 #include <dune/common/exceptions.hh>
41 #include <dune/common/stdstreams.hh>
42
43 #include <dune/uggrid/parallel/ddd/dddi.h>
44
45 #define DebugPrio 10 /* 0 is all, 10 is off */
46
47
48 /****************************************************************************/
49 /* */
50 /* defines in the following order */
51 /* */
52 /* compile time constants defining static data size (i.e. arrays) */
53 /* other constants */
54 /* macros */
55 /* */
56 /****************************************************************************/
57
58
59 namespace DDD {
60 namespace Prio {
61
62 /* overall mode of prio-environment */
63 enum class PrioMode : unsigned char {
64 PMODE_IDLE = 0, /* waiting for next DDD_PrioBegin() */
65 PMODE_CMDS, /* after DDD_PrioBegin(), before DDD_PrioEnd() */
66 PMODE_BUSY /* during DDD_PrioEnd() */
67 };
68
69 } /* namespace Prio */
70 } /* namespace DDD */
71
72 START_UGDIM_NAMESPACE
73
74 using DDD::Prio::PrioMode;
75
76 /*
77 management functions for PrioMode.
78
79 these functions control the mode the prio-module is
80 currently in. this is used for error detection, but
81 also for correct detection of coupling inconsistencies
82 and recovery.
83 */
84
85 static const char* PrioModeName (PrioMode mode)
86 {
87 switch(mode)
88 {
89 case PrioMode::PMODE_IDLE : return "idle-mode";
90 case PrioMode::PMODE_CMDS : return "commands-mode";
91 case PrioMode::PMODE_BUSY : return "busy-mode";
92 }
93 return "unknown-mode";
94 }
95
96
97 static void PrioSetMode (DDD::DDDContext& context, PrioMode mode)
98 {
99 auto& ctx = context.prioContext();
100 ctx.prioMode = mode;
101
102 # if DebugPrio<=8
103 Dune::dinfo << "PrioMode=" << PrioModeName(ctx.prioMode) << "\n";
104 # endif
105 }
106
107
108 static PrioMode PrioSuccMode (PrioMode mode)
109 {
110 switch(mode)
111 {
112 case PrioMode::PMODE_IDLE: return PrioMode::PMODE_CMDS;
113 case PrioMode::PMODE_CMDS: return PrioMode::PMODE_BUSY;
114 case PrioMode::PMODE_BUSY: return PrioMode::PMODE_IDLE;
115 default: std::abort();
116 }
117 }
118
119
120 bool ddd_PrioActive (const DDD::DDDContext& context)
121 {
122 return context.prioContext().prioMode != PrioMode::PMODE_IDLE;
123 }
124
125
126 static bool PrioStepMode(DDD::DDDContext& context, PrioMode old)
127 {
128 auto& ctx = context.prioContext();
129 if (ctx.prioMode!=old)
130 {
131 Dune::dwarn
132 << "wrong prio-mode (currently in " << PrioModeName(ctx.prioMode)
133 << ", expected " << PrioModeName(old) << ")\n";
134 return false;
135 }
136
137 PrioSetMode(context, PrioSuccMode(ctx.prioMode));
138 return true;
139 }
140
141
142 /****************************************************************************/
143
144
145 void ddd_PrioInit(DDD::DDDContext& context)
146 {
147 PrioSetMode(context, PrioMode::PMODE_IDLE);
148 }
149
150
151 void ddd_PrioExit(DDD::DDDContext&)
152 {}
153
154
155
156 /****************************************************************************/
157 /* */
158 /* Function: DDD_PrioChange */
159 /* */
160 /****************************************************************************/
161
162 /**
163 Consistent change of a local object's priority during DDD Prio Environment.
164 Local objects which are part of a distributed object must notify
165 other copies about local priority changes.
166 DDD will send appropriate messages to the owner processors of
167 the other copies.
168
169 This function is regarded as a {\bf Prio}-operation due
170 to its influence on DDD management information on neighbouring
171 processors. Therefore the function has to be issued between
172 a starting \funk{PrioBegin} and a final \funk{PrioEnd} call.
173
174 @param hdr DDD local object whose priority should be changed.
175 @param prio new priority for this local object.
176 */
177
178 void DDD_PrioChange (const DDD::DDDContext& context, DDD_HDR hdr, DDD_PRIO prio)
179 {
180 #if DebugPrio<=2
181 DDD_PRIO old_prio = OBJ_PRIO(hdr);
182 #endif
183
184 if (!ddd_PrioActive(context))
185 DUNE_THROW(Dune::Exception, "Missing DDD_PrioBegin()");
186
187
188 /* change priority of object directly, for local objects this
189 is all what we need. */
190 {
191 /*
192 DDD_PRIO newprio;
193 PriorityMerge(&context.typeDefs()[OBJ_TYPE(hdr)],
194 OBJ_PRIO(hdr), prio, &newprio);
195 OBJ_PRIO(hdr) = newprio;
196 */
197 OBJ_PRIO(hdr) = prio;
198 }
199
200 /* handle distributed objects
201 if (ObjHasCpl(context, hdr))
202 {
203 nothing to do here:
204 for distributed objects, we will communicate the prio
205 via standard interface later.
206 }
207 */
208
209
210 # if DebugPrio<=2
211 Dune::dvverb
212 << "DDD_PrioChange " << OBJ_GID(hdr)
213 << ", old_prio=" << old_prio << ", new_prio=" << OBJ_PRIO(hdr) << "\n";
214 # endif
215 }
216
217
218
219 /****************************************************************************/
220 /* */
221 /* Function: DDD_PrioEnd */
222 /* */
223 /****************************************************************************/
224
225 static int GatherPrio (DDD::DDDContext&, DDD_HDR obj, void *data, DDD_PROC proc, DDD_PRIO prio)
226 {
227 # if DebugPrio<=1
228 Dune::dvverb
229 << "DDD_PrioEnd/GatherPrio " << OBJ_GID(obj) << ", prio=" << OBJ_PRIO(obj)
230 << ". Send to copy on proc " << proc << "/p" << prio << "\n";
231 # endif
232
233 *((DDD_PRIO *)data) = OBJ_PRIO(obj);
234 return(0);
235 }
236
237 static int ScatterPrio (DDD::DDDContext& context, DDD_HDR obj, void *data, DDD_PROC proc, DDD_PRIO prio)
238 {
239 DDD_PRIO real_prio = *((DDD_PRIO *)data);
240
241 /* if prio on other processor has been changed, we adapt it here. */
242 if (real_prio!=prio)
243 {
244 # if DebugPrio<=1
245 Dune::dvverb
246 << "DDD_PrioEnd/ScatterPrio " << OBJ_GID(obj) << "/" << OBJ_PRIO(obj)
247 << ", copy on proc " << proc << "/p" << prio
248 << " changed prio " << prio << " -> " << real_prio << "\n";
249 # endif
250
251 ModCoupling(context, obj, proc, real_prio);
252 }
253 # if DebugPrio<=1
254 else
255 {
256 Dune::dvverb
257 << "DDD_PrioEnd/ScatterPrio " << OBJ_GID(obj) << "/" << OBJ_PRIO(obj)
258 << ", copy on proc " << proc << "/p" << prio
259 << " keeps prio " << prio << "\n";
260 }
261 # endif
262
263 return(0);
264 }
265
266
267 /**
268 End of PrioEnvironment.
269 This function starts the actual process of changing priorities.
270 After a call to this function (on all processors) all
271 \funk{PrioChange}-commands since the last call to \funk{PrioBegin}
272 are executed. This involves a set of interface communications
273 between the processors.
274 */
275
276 DDD_RET DDD_PrioEnd(DDD::DDDContext& context)
277 {
278 /* step mode and check whether call to PrioEnd is valid */
279 if (!PrioStepMode(context, PrioMode::PMODE_CMDS))
280 DUNE_THROW(Dune::Exception, "DDD_PrioEnd() aborted");
281
282
283 ddd_StdIFExchangeX(context, sizeof(DDD_PRIO), GatherPrio, ScatterPrio);
284
285 /*
286 free temporary storage
287 */
288 STAT_RESET;
289 IFAllFromScratch(context);
290 STAT_TIMER(T_PRIO_BUILD_IF);
291
292
293 PrioStepMode(context, PrioMode::PMODE_BUSY);
294
295 return(DDD_RET_OK);
296 }
297
298
299
300
301 /****************************************************************************/
302 /* */
303 /* Function: DDD_PrioBegin */
304 /* */
305 /****************************************************************************/
306
307 /**
308 Starts a PrioEnvironment.
309 A call to this function establishes a global operation of changing
310 priorities. It must be issued on all processors. After this call an
311 arbitrary series of \funk{PrioChange}-commands may be issued. The
312 global transfer operation is carried out via a \funk{PrioEnd} call on
313 each processor.
314 */
315
316 void DDD_PrioBegin(DDD::DDDContext& context)
317 {
318 /* step mode and check whether call to JoinBegin is valid */
319 if (!PrioStepMode(context, PrioMode::PMODE_IDLE))
320 DUNE_THROW(Dune::Exception, "DDD_PrioBegin() aborted");
321 }
322
323
324
325 END_UGDIM_NAMESPACE