Debian Codegraph @

dune/uggrid/parallel/ddd/basic/notify.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: notify.c */
8 /* */
9 /* Purpose: notifies destinations for communication with globally */
10 /* unknown topology */
11 /* */
12 /* Author: Klaus Birken */
13 /* Rechenzentrum Uni Stuttgart */
14 /* Universitaet Stuttgart */
15 /* Allmandring 30 */
16 /* 70550 Stuttgart */
17 /* internet: [email protected] */
18 /* */
19 /* History: 94/01/17 kb begin */
20 /* 95/04/06 kb added SpreadNotify */
21 /* 96/07/12 kb united xxxNotify functions to one DDD_Notify() */
22 /* */
23 /* Remarks: */
24 /* */
25 /****************************************************************************/
26
27 /****************************************************************************/
28 /* */
29 /* include files */
30 /* system include files */
31 /* application include files */
32 /* */
33 /****************************************************************************/
34
35 /* standard C library */
36 #include <config.h>
37 #include <cstdlib>
38 #include <cstdio>
39
40 #include <algorithm>
41 #include <new>
42 #include <tuple>
43
44 #include <dune/common/stdstreams.hh>
45
46 #include <dune/uggrid/parallel/ddd/dddi.h>
47 #include "notify.h"
48
49 #include <dune/uggrid/parallel/ddd/dddcontext.hh>
50 #include <dune/uggrid/parallel/ppif/ppifcontext.hh>
51
52 USING_UG_NAMESPACES
53
54 /* PPIF namespace: */
55 using namespace PPIF;
56
57 #define DebugNotify 10 /* 0 is all, 10 is off */
58
59 #define PROC_INVALID_TEMP -1
60
61 /****************************************************************************/
62 /* */
63 /* routines */
64 /* */
65 /****************************************************************************/
66
67 namespace DDD {
68
69 using namespace DDD::Basic;
70
71 static int MAX_INFOS(int procs)
72 {
73 return procs * std::max(1+procs, 10);
74 }
75
76 void NotifyInit(DDD::DDDContext& context)
77 {
78 auto& ctx = context.notifyContext();
79 const auto procs = context.procs();
80
81 /* allocate memory */
82 ctx.theRouting.resize(procs);
83
84 ctx.maxInfos = MAX_INFOS(procs); /* TODO maximum value, just for testing */
85
86 /* init local array for all Info records */
87 ctx.allInfoBuffer.resize(ctx.maxInfos);
88
89 /* allocate array of NOTIFY_DESCs */
90 ctx.theDescs.resize(procs-1);
91 }
92
93
94 void NotifyExit(DDD::DDDContext& context)
95 {
96 auto& ctx = context.notifyContext();
97
98 /* free memory */
99 ctx.theRouting.clear();
100 ctx.allInfoBuffer.clear();
101 ctx.theDescs.clear();
102 }
103
104
105 /****************************************************************************/
106
107 static bool sort_XferInfos(const NOTIFY_INFO& a, const NOTIFY_INFO& b)
108 {
109 return std::tie(a.to, a.from) < std::tie(b.to, b.from);
110 }
111
112 static bool sort_XferFlags(const NOTIFY_INFO& a, const NOTIFY_INFO& b)
113 {
114 return a.flag < b.flag;
115 }
116
117 static
118 NOTIFY_INFO *NotifyPrepare (DDD::DDDContext& context)
119 {
120 auto& ctx = context.notifyContext();
121 const auto& me = context.me();
122
123 #if DebugNotify<=4
124 printf("%4d: NotifyPrepare\n", me);
125 fflush(stdout);
126 #endif
127
128 /* init local array for all Info records */
129 NOTIFY_INFO* allInfos = ctx.allInfoBuffer.data();
130
131
132 /* init local routing array */
133 ctx.theRouting[me] = -1;
134
135
136 /* dummy Info if there is no message to be send */
137 allInfos[0].from = me;
138 allInfos[0].to = PROC_INVALID_TEMP;
139 allInfos[0].size = 0;
140 allInfos[0].flag = NotifyTypes::DUMMY;
141 ctx.lastInfo = 1;
142
143 return allInfos;
144 }
145
146
147
148 /****************************************************************************/
149
150 /*
151 If the parameter 'exception' is !=0, this processor invokes a
152 global exception, which will cause all processors to abort this
153 notify procedure and return the exception code with flipped sign.
154 If more processors issue exception codes, the maximum will be
155 communicated.
156 */
157
158 static
159 int NotifyTwoWave(DDD::DDDContext& context, NOTIFY_INFO *allInfos, int lastInfo, int exception)
160 {
161 auto& ctx = context.notifyContext();
162 const auto& me = context.me();
163 const auto& degree = context.ppifContext().degree();
164
165 NOTIFY_INFO *newInfos;
166 int l, i, j, n, unknownInfos, myInfos;
167 int local_exception = exception;
168
169 #if DebugNotify<=4
170 printf("%4d: NotifyTwoWave, lastInfo=%d\n", me, lastInfo);
171 fflush(stdout);
172 #endif
173
174 /* BOTTOM->TOP WAVE */
175 /* get local Info lists from downtree */
176 for(l=degree-1; l>=0; l--)
177 {
178 GetConcentrate(context.ppifContext(), l, &n, sizeof(int));
179
180 if (n<0)
181 {
182 /* exception from downtree, propagate */
183 if (-n > local_exception)
184 local_exception = -n;
185 }
186
187 if (lastInfo+n >= ctx.maxInfos) {
188 DDD_PrintError('E', 6321, "msg-info array overflow in NotifyTwoWave");
189 local_exception = EXCEPTION_NOTIFY;
190
191 /* receive data, but put it onto dummy position */
192 GetConcentrate(context.ppifContext(), l, allInfos, n*sizeof(NOTIFY_INFO));
193 }
194 else
195 {
196 if (n>0)
197 GetConcentrate(context.ppifContext(), l, &(allInfos[lastInfo]), n*sizeof(NOTIFY_INFO));
198 }
199
200 /* construct routing table */
201 for(i=0; i<n; i++)
202 ctx.theRouting[allInfos[lastInfo+i].from] = l;
203
204 if (n>0)
205 lastInfo += n;
206 }
207
208
209 if (! local_exception)
210 {
211 /* determine target direction in tree */
212 /* TODO: eventually extra solution for root node!
213 (it knows all flags are MYSELF or KNOWN!) */
214 std::sort(allInfos, allInfos + lastInfo, sort_XferInfos);
215 i = j = 0;
216 unknownInfos = lastInfo;
217 myInfos = 0;
218 while (i<lastInfo && allInfos[j].to==PROC_INVALID_TEMP)
219 {
220 if (allInfos[j].from==allInfos[i].to)
221 {
222 allInfos[i].flag = (allInfos[i].to==me) ? NotifyTypes::MYSELF : NotifyTypes::KNOWN;
223 unknownInfos--;
224 if (allInfos[i].to==me)
225 myInfos++;
226 i++;
227 } else {
228 if (allInfos[j].from<allInfos[i].to)
229 j++;
230 else
231 i++;
232 }
233 }
234 std::sort(allInfos, allInfos + lastInfo, sort_XferFlags);
235
236 /* send local Info list uptree, but only unknown Infos */
237 newInfos = &allInfos[lastInfo-unknownInfos];
238 Concentrate(context.ppifContext(), &unknownInfos, sizeof(int));
239 Concentrate(context.ppifContext(), newInfos, unknownInfos*sizeof(NOTIFY_INFO));
240 lastInfo -= unknownInfos;
241
242 #if DebugNotify<=1
243 for(i=0; i<unknownInfos; i++)
244 {
245 printf("%4d: NotifyTwoWave, "
246 "send uptree unknown %d/%d (%d|%d;%d)\n",
247 me, i, unknownInfos,
248 newInfos[i].to, newInfos[i].from, newInfos[i].size);
249 }
250 #endif
251
252 }
253 else
254 {
255 /* we have an exception somewhere in the processor tree */
256 /* propagate it */
257 int neg_exception = -local_exception;
258 Concentrate(context.ppifContext(), &neg_exception, sizeof(int));
259 /* don't need to send data now */
260 }
261
262 #if DebugNotify<=3
263 printf("%4d: NotifyTwoWave, wave 1 ready\n", me);
264 fflush(stdout);
265 #endif
266
267
268
269 /* TOP->BOTTOM WAVE */
270
271 /* get Infos local to my subtree from uptree */
272 unknownInfos = 0;
273 GetSpread(context.ppifContext(), &unknownInfos, sizeof(int));
274 if (unknownInfos<0)
275 {
276 /* exception from downtree, propagate */
277 if (-unknownInfos > local_exception)
278 local_exception = -unknownInfos;
279 }
280
281 if (unknownInfos>0)
282 {
283 GetSpread(context.ppifContext(), newInfos, unknownInfos*sizeof(NOTIFY_INFO));
284 lastInfo += unknownInfos;
285 }
286
287 if (! local_exception)
288 {
289 /* sort Infos according to routing */
290 std::sort(
291 allInfos, allInfos + lastInfo,
292 [&ctx](const NOTIFY_INFO& a, const NOTIFY_INFO& b) {
293 return ctx.theRouting[a.to] < ctx.theRouting[b.to];
294 });
295
296 #if DebugNotify<=1
297 for(i=0; i<lastInfo; i++)
298 {
299 printf("%4d: NotifyTwoWave, "
300 "sorted for routing %d/%d (%d|%d;%d)\n",
301 me, i, lastInfo,
302 allInfos[i].to, allInfos[i].from, allInfos[i].size);
303 }
304 #endif
305
306 /* send relevant Infos downtree */
307 i = 0;
308 unknownInfos = lastInfo;
309 while ((i<unknownInfos)&&(allInfos[i].to==me)) i++;
310 lastInfo = i;
311 for(l=0; l<degree; l++)
312 {
313 j = i;
314 while ((i<unknownInfos)&&(ctx.theRouting[allInfos[i].to]==l)) i++;
315 j = i-j;
316
317 Spread(context.ppifContext(), l, &j, sizeof(int));
318 if (j>0)
319 Spread(context.ppifContext(), l, &allInfos[i-j], j*sizeof(NOTIFY_INFO));
320 }
321
322
323 /* reuse theDescs-array for registering messages to be received */
324 for(i=0; i<lastInfo; i++)
325 {
326 ctx.theDescs[i].proc = allInfos[i].from;
327 ctx.theDescs[i].size = allInfos[i].size;
328 }
329
330 #if DebugNotify<=3
331 printf("%4d: NotifyTwoWave, "
332 "wave 2 ready, nRecv=%d\n", me, lastInfo);
333 fflush(stdout);
334 #endif
335 }
336 else
337 {
338 /* we received an exception from uptree, propagate it */
339 for(l=0; l<degree; l++)
340 {
341 int neg_exception = -local_exception;
342 Spread(context.ppifContext(), l, &neg_exception, sizeof(int));
343 /* dont send any data */
344 }
345
346 #if DebugNotify<=3
347 printf("%4d: NotifyTwoWave, "
348 "wave 2 ready, Exception=%d\n", me, local_exception);
349 fflush(stdout);
350 #endif
351
352 return(-local_exception);
353 }
354
355 return(lastInfo);
356 }
357
358
359
360 /****************************************************************************/
361
362
363 NOTIFY_DESC *DDD_NotifyBegin(DDD::DDDContext& context, int n)
364 {
365 auto& ctx = context.notifyContext();
366
367 ctx.nSendDescs = n;
368
369 /* allocation of theDescs is done in NotifyInit() */
370
371 if (n > context.procs()-1)
372 {
373 DDD_PrintError('E', 6340,
374 "more send-messages than other processors in DDD_NotifyBegin");
375 return nullptr;
376 }
377
378 return ctx.theDescs.data();
379 }
380
381
382 void DDD_NotifyEnd(DDD::DDDContext&)
383 {
384 /* free'ing of theDescs is done in NotifyExit() */
385 }
386
387
388 int DDD_Notify(DDD::DDDContext& context)
389 {
390 auto& ctx = context.notifyContext();
391 int i, nRecvMsgs;
392
393 const auto me = context.me();
394 const auto procs = context.procs();
395
396 /* get storage for local info list */
397 NOTIFY_INFO* allInfos = NotifyPrepare(context);
398 if (allInfos == nullptr) return(ERROR);
399
400 if (ctx.nSendDescs<0)
401 {
402 /* this processor is trying to send a global notification
403 message. this is necessary for communicating fatal error
404 conditions to all other processors. */
405
406 Dune::dwarn
407 << "DDD_Notify: proc " << me
408 << " is sending global exception #" << (-ctx.nSendDescs) << "\n";
409
410 /* notify partners */
411 nRecvMsgs = NotifyTwoWave(context, allInfos, ctx.lastInfo, -ctx.nSendDescs);
412 }
413 else
414 {
415 /* convert message list to local Info list */
416 for(i=0; i<ctx.nSendDescs; i++)
417 {
418 #if DebugNotify<=4
419 printf("%4d: Notify send msg #%02d to %3d size=%d\n", me,
420 ctx.lastInfo, ctx.theDescs[i].proc, ctx.theDescs[i].size);
421 #endif
422
423 if (ctx.theDescs[i].proc==me) {
424 Dune::dwarn << "DDD_Notify: proc " << me
425 << " is trying to send message to itself\n";
426 return(ERROR);
427 }
428 if (ctx.theDescs[i].proc>=procs) {
429 Dune::dwarn
430 << "DDD_Notify: proc " << me << " is trying to send message to proc "
431 << ctx.theDescs[i].proc << "\n";
432 return(ERROR);
433 }
434
435 allInfos[ctx.lastInfo].from = me;
436 allInfos[ctx.lastInfo].to = ctx.theDescs[i].proc;
437 allInfos[ctx.lastInfo].size = ctx.theDescs[i].size;
438 allInfos[ctx.lastInfo].flag = NotifyTypes::UNKNOWN;
439 ctx.lastInfo++;
440 }
441
442 /* notify partners */
443 nRecvMsgs = NotifyTwoWave(context, allInfos, ctx.lastInfo, 0);
444 }
445
446
447 # if DebugNotify<=4
448 for(i=0; i<nRecvMsgs; i++)
449 {
450 printf("%4d: Notify recv msg #%02d from %3d size=%d\n", me,
451 ctx.lastInfo, ctx.theDescs[i].proc, ctx.theDescs[i].size);
452 }
453 # endif
454
455
456 return(nRecvMsgs);
457 }
458
459 /****************************************************************************/
460
461 } /* namespace DDD */