Debian Codegraph @

dune/uggrid/parallel/ddd/if/ifuse.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: ifuse.c */
8 /* */
9 /* Purpose: routines concerning interfaces between processors */
10 /* part 2: usage of DDD interfaces */
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: 93/11/30 kb begin */
20 /* 94/03/03 kb complete rewrite */
21 /* 94/09/12 kb IFExchange & IFOneway rewrite, two bugs fixed */
22 /* 94/09/21 kb created from if.c */
23 /* 95/01/13 kb added range functionality */
24 /* 95/07/26 kb overlapping of gather/scatter and communication */
25 /* */
26 /* Remarks: */
27 /* */
28 /****************************************************************************/
29
30 /****************************************************************************/
31 /* */
32 /* include files */
33 /* system include files */
34 /* application include files */
35 /* */
36 /****************************************************************************/
37
38 /* standard C library */
39 #include <config.h>
40 #include <cstdlib>
41 #include <cstdio>
42 #include <cstring>
43
44 #include <dune/common/exceptions.hh>
45
46 #include <dune/uggrid/parallel/ddd/dddcontext.hh>
47
48 #include <dune/uggrid/parallel/ddd/dddi.h>
49 #include "if.h"
50
51 USING_UG_NAMESPACE
52
53 /* PPIF namespace: */
54 using namespace PPIF;
55
56 START_UGDIM_NAMESPACE
57
58 /****************************************************************************/
59 /* */
60 /* routines */
61 /* */
62 /****************************************************************************/
63
64
65 /*
66 allocate memory for message buffers,
67 one for send, one for receive
68 */
69 void IFGetMem (IF_PROC *ifHead, size_t itemSize, int lenIn, int lenOut)
70 {
71 size_t sizeIn = itemSize * lenIn;
72 size_t sizeOut = itemSize * lenOut;
73
74 /* set memory to initial value, in order to find any bugs lateron */
75 ifHead->bufIn.assign(sizeIn, 0);
76 ifHead->bufOut.assign(sizeOut, 0);
77 }
78
79
80
81
82 /*
83 initiate asynchronous receive calls,
84 return number of messages to be received
85 */
86 int IFInitComm(DDD::DDDContext& context, DDD_IF ifId)
87 {
88 IF_PROC *ifHead;
89 int error;
90 int recv_mesgs;
91
92 auto& ctx = context.ifUseContext();
93
94 /* MarkHeap(); */
95
96 recv_mesgs = 0;
97
98 /* get memory and initiate receive calls */
99 ForIF(context, ifId, ifHead)
100 {
101 if (not ifHead->bufIn.empty())
102 {
103 ifHead->msgIn =
104 RecvASync(context.ppifContext(), ifHead->vc,
105 ifHead->bufIn.data(), ifHead->bufIn.size(),
106 &error);
107 if (ifHead->msgIn==0)
108 DUNE_THROW(Dune::Exception, "RecvASync() failed");
109
110 recv_mesgs++;
111 }
112 }
113
114 ctx.send_mesgs = 0;
115
116 return recv_mesgs;
117 }
118
119
120
121 /*
122 cleanup memory
123 */
124 void IFExitComm(DDD::DDDContext& context, DDD_IF ifId)
125 {
126 if (DDD_GetOption(context, OPT_IF_REUSE_BUFFERS) == OPT_OFF)
127 {
128 IF_PROC *ifHead;
129 ForIF(context, ifId, ifHead)
130 {
131 ifHead->bufIn.clear();
132 ifHead->bufIn.shrink_to_fit();
133
134 ifHead->bufOut.clear();
135 ifHead->bufOut.shrink_to_fit();
136 }
137 }
138
139 /* ReleaseHeap(); */
140 }
141
142
143
144 /*
145 initiate single asynchronous send call
146 */
147 void IFInitSend(DDD::DDDContext& context, IF_PROC *ifHead)
148 {
149 int error;
150
151 auto& ctx = context.ifUseContext();
152
153 if (not ifHead->bufOut.empty())
154 {
155 ifHead->msgOut =
156 SendASync(context.ppifContext(), ifHead->vc,
157 ifHead->bufOut.data(), ifHead->bufOut.size(),
158 &error);
159 if (ifHead->msgOut==0)
160 DUNE_THROW(Dune::Exception, "SendASync() failed");
161
162 ctx.send_mesgs++;
163 }
164 }
165
166
167
168 /*
169 poll asynchronous send calls,
170 return if ready
171 */
172 int IFPollSend(DDD::DDDContext& context, DDD_IF ifId)
173 {
174 unsigned long tries;
175
176 auto& ctx = context.ifUseContext();
177
178 for(tries=0; tries<MAX_TRIES && ctx.send_mesgs>0; tries++)
179 {
180 IF_PROC *ifHead;
181
182 /* poll send calls */
183 ForIF(context, ifId, ifHead)
184 {
185 if (not ifHead->bufOut.empty() && ifHead->msgOut!= NO_MSGID)
186 {
187 int error = InfoASend(context.ppifContext(), ifHead->vc, ifHead->msgOut);
188 if (error==-1)
189 DUNE_THROW(Dune::Exception,
190 "InfoASend() failed for send to proc=" << ifHead->proc);
191
192 if (error==1)
193 {
194 ctx.send_mesgs--;
195 ifHead->msgOut=NO_MSGID;
196
197 #ifdef CtrlTimeoutsDetailed
198 printf("%4d: IFCTRL %02d send-completed to "
199 "%4d after %10ld, size %ld\n",
200 context.me(), ifId, ifHead->proc,
201 (unsigned long)tries,
202 (unsigned long)ifHead->bufOut.size());
203 #endif
204 }
205 }
206 }
207 }
208
209 #ifdef CtrlTimeouts
210 if (ctx.send_mesgs==0)
211 {
212 printf("%4d: IFCTRL %02d send-completed all after %10ld tries\n",
213 context.me(), ifId, (unsigned long)tries);
214 }
215 #endif
216
217 return(ctx.send_mesgs==0);
218 }
219
220
221
222 /****************************************************************************/
223
224
225 /*
226 do loop over single list of couplings,
227 copy object data from/to message buffer
228
229 fast version: uses object pointer shortcut
230 */
231 char *IFCommLoopObj (DDD::DDDContext& context,
232 ComProcPtr2 LoopProc,
233 IFObjPtr *obj,
234 char *buffer,
235 size_t itemSize,
236 int nItems)
237 {
238 for(int i=0; i<nItems; i++, buffer+=itemSize)
239 LoopProc(context, obj[i], buffer);
240
241 return(buffer);
242 }
243
244 /*
245 simple variant of above routine. dont communicate,
246 but call an application's routine.
247 */
248 void IFExecLoopObj (DDD::DDDContext& context, ExecProcPtr LoopProc, IFObjPtr *obj, int nItems)
249 {
250 for(int i=0; i<nItems; i++)
251 LoopProc(context, obj[i]);
252 }
253
254
255
256
257 /*
258 do loop over single list of couplings,
259 copy object data from/to message buffer
260
261 unnecessary indirect addressing!
262 (-> CPL -> DDD_HDR.typ -> header offset -> object address)
263 */
264 char *IFCommLoopCpl (DDD::DDDContext& context,
265 ComProcPtr2 LoopProc,
266 COUPLING **cpl,
267 char *buffer,
268 size_t itemSize,
269 int nItems)
270 {
271 for(int i=0; i<nItems; i++, buffer+=itemSize)
272 LoopProc(context, OBJ_OBJ(context, cpl[i]->obj), buffer);
273
274 return(buffer);
275 }
276
277
278
279
280 /*
281 do loop over single list of couplings,
282 copy object data from/to message buffer
283
284 extended version: call ComProc with extended parameters
285
286 (necessary) indirect addressing!
287 (-> CPL -> DDD_HDR.typ -> header offset -> object address)
288 */
289 char *IFCommLoopCplX (DDD::DDDContext& context,
290 ComProcXPtr LoopProc,
291 COUPLING **cpl,
292 char *buffer,
293 size_t itemSize,
294 int nItems)
295 {
296 for(int i=0; i<nItems; i++, buffer+=itemSize)
297 LoopProc(context, OBJ_OBJ(context, cpl[i]->obj),
298 buffer, CPL_PROC(cpl[i]), cpl[i]->prio);
299
300 return(buffer);
301 }
302
303
304
305 /*
306 simple variant of above routine. dont communicate,
307 but call an application's routine.
308 */
309 void IFExecLoopCplX (DDD::DDDContext& context, ExecProcXPtr LoopProc, COUPLING **cpl, int nItems)
310 {
311 for(int i=0; i<nItems; i++)
312 LoopProc(context, OBJ_OBJ(context, cpl[i]->obj), CPL_PROC(cpl[i]), cpl[i]->prio);
313 }
314
315
316
317 /****************************************************************************/
318
319
320 /***
321 interface loop functions for STD_INTERFACE communication.
322 (not DDD_OBJ will be passed as a parameter, but DDD_HDR
323 instead).
324 ***/
325
326
327 /*
328 do loop over single list of couplings,
329 copy object data from/to message buffer
330 */
331 char *IFCommHdrLoopCpl (DDD::DDDContext& context,
332 ComProcHdrPtr LoopProc,
333 COUPLING **cpl,
334 char *buffer,
335 size_t itemSize,
336 int nItems)
337 {
338 for(int i=0; i<nItems; i++, buffer+=itemSize)
339 LoopProc(context, cpl[i]->obj, buffer);
340
341 return(buffer);
342 }
343
344
345 /*
346 simple variant of above routine. dont communicate,
347 but call an application's routine.
348 */
349 void IFExecHdrLoopCpl (DDD::DDDContext& context, ExecProcHdrPtr LoopProc, COUPLING **cpl, int nItems)
350 {
351 for(int i=0; i<nItems; i++)
352 LoopProc(context, cpl[i]->obj);
353 }
354
355
356 /*
357 do loop over single list of couplings,
358 copy object data from/to message buffer
359
360 extended version: call ComProc with extended parameters
361 */
362 char *IFCommHdrLoopCplX (DDD::DDDContext& context,
363 ComProcHdrXPtr LoopProc,
364 COUPLING **cpl,
365 char *buffer,
366 size_t itemSize,
367 int nItems)
368 {
369 for(int i=0; i<nItems; i++, buffer+=itemSize)
370 LoopProc(context, cpl[i]->obj,
371 buffer, CPL_PROC(cpl[i]), cpl[i]->prio);
372
373 return(buffer);
374 }
375
376
377 /*
378 simple variant of above routine. dont communicate,
379 but call an application's routine.
380
381 extended version: call ExecProc with extended parameters
382 */
383 void IFExecHdrLoopCplX (DDD::DDDContext& context, ExecProcHdrXPtr LoopProc, COUPLING **cpl, int nItems)
384 {
385 for(int i=0; i<nItems; i++)
386 LoopProc(context, cpl[i]->obj, CPL_PROC(cpl[i]), cpl[i]->prio);
387 }
388
389
390
391 /****************************************************************************/
392
393 END_UGDIM_NAMESPACE