| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
/****************************************************************************/ |
| 4 |
/* */ |
| 5 |
/* File: ifcmd.ct */ |
| 6 |
/* */ |
| 7 |
/* Purpose: template routines for interface functions */ |
| 8 |
/* */ |
| 9 |
/* Author: Klaus Birken */ |
| 10 |
/* Institut fuer Computeranwendungen III */ |
| 11 |
/* Universitaet Stuttgart */ |
| 12 |
/* Pfaffenwaldring 27 */ |
| 13 |
/* 70569 Stuttgart */ |
| 14 |
/* internet: [email protected] */ |
| 15 |
/* */ |
| 16 |
/* History: 970110 kb begin */ |
| 17 |
/* */ |
| 18 |
/* Remarks: */ |
| 19 |
/* */ |
| 20 |
/****************************************************************************/ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
/****************************************************************************/ |
| 25 |
/* */ |
| 26 |
/* derived macros */ |
| 27 |
/* */ |
| 28 |
/****************************************************************************/ |
| 29 |
|
| 30 |
#define _CAT(a,b) a ## b |
| 31 |
#define CAT(a,b) _CAT(a,b) |
| 32 |
|
| 33 |
|
| 34 |
#ifdef IF_WITH_ATTR |
| 35 |
#define PART ifAttr |
| 36 |
#else |
| 37 |
#define PART ifHead |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#ifdef IF_WITH_XARGS |
| 42 |
#define EXEC_LOOP(context, a,b,c) NS_DIM_PREFIX IFExecLoopCplX(context, a,b,c) |
| 43 |
#define COMM_LOOP(context, a,b,c,d,e) NS_DIM_PREFIX IFCommLoopCplX(context, a,b,c,d,e) |
| 44 |
#define D_AB(p) (p->cplAB) |
| 45 |
#define D_BA(p) (p->cplBA) |
| 46 |
#define D_ABA(p) (p->cplABA) |
| 47 |
#else |
| 48 |
#define EXEC_LOOP(context, a,b,c) NS_DIM_PREFIX IFExecLoopObj(context, a,b,c) |
| 49 |
#define COMM_LOOP(context, a,b,c,d,e) NS_DIM_PREFIX IFCommLoopObj(context, a,b,c,d,e) |
| 50 |
#define D_AB(p) (p->objAB) |
| 51 |
#define D_BA(p) (p->objBA) |
| 52 |
#define D_ABA(p) (p->objABA) |
| 53 |
#endif |
| 54 |
|
| 55 |
#define IF_FUNCNAME CAT(DDD_IF,IF_NAME) |
| 56 |
|
| 57 |
/****************************************************************************/ |
| 58 |
/* */ |
| 59 |
/* auxiliary macros */ |
| 60 |
/* */ |
| 61 |
/****************************************************************************/ |
| 62 |
|
| 63 |
#ifndef IF_AUX_MACROS |
| 64 |
#define IF_AUX_MACROS |
| 65 |
|
| 66 |
#define CONCAT(txt,fct) txt FUNCNAME_STR(fct) |
| 67 |
#define FUNCNAME_STR(f) #f |
| 68 |
|
| 69 |
#endif |
| 70 |
|
| 71 |
|
| 72 |
/****************************************************************************/ |
| 73 |
/* */ |
| 74 |
/* routines */ |
| 75 |
/* */ |
| 76 |
/****************************************************************************/ |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
/****************************************************************************/ |
| 81 |
|
| 82 |
|
| 83 |
#ifdef IF_EXECLOCAL |
| 84 |
/** |
| 85 |
Local execution on \ddd{Interface}. |
| 86 |
This function executes a given handler for each object inside |
| 87 |
a \ddd{Interface}. |
| 88 |
|
| 89 |
}*/ |
| 90 |
#else |
| 91 |
#if defined(IF_ONEWAY) |
| 92 |
/** |
| 93 |
Oneway-communication across a \ddd{Interface}. |
| 94 |
This function initiates a communication across a previously |
| 95 |
defined DDD interface. It has to be issued on all processors. |
| 96 |
|
| 97 |
The communication is carried out one-directional, dependent |
| 98 |
on parameter {\em aDir}. In case of #IF_FORWARD# information |
| 99 |
will flow from object set A to object set B, in case of #IF_BACKWARD# |
| 100 |
it will be vice versa (see \funk{IFDefine}). |
| 101 |
}*/ |
| 102 |
#else |
| 103 |
/** |
| 104 |
Bidirectional communication across a \ddd{Interface}. |
| 105 |
This function initiates a communication across a previously |
| 106 |
defined DDD interface. It has to be issued on all processors. |
| 107 |
|
| 108 |
The communication is carried out in both directions, i.e.~from |
| 109 |
object set A to object set B and vice versa (see \funk{IFDefine}). |
| 110 |
}*/ |
| 111 |
#endif |
| 112 |
|
| 113 |
/*{ |
| 114 |
The user-defined function {\em gather\_proc} is called for each object |
| 115 |
inside the interface. It has to pack all necessary information into |
| 116 |
a special memory location. Afterwards the actual communication based |
| 117 |
on the current processor topology happens. After all data had been |
| 118 |
scheduled onto the destination processors, it has to be |
| 119 |
incorporated into the destination part of the DDD interface via |
| 120 |
the user-defined function {\em scatter\_proc}. |
| 121 |
|
| 122 |
Both communication procedures should have the |
| 123 |
following declaration: |
| 124 |
|
| 125 |
#int (*ComProcPtr) (DDD_OBJ obj, void *data)# |
| 126 |
}*/ |
| 127 |
#endif |
| 128 |
|
| 129 |
/*{ |
| 130 |
\todo{further description} |
| 131 |
}*/ |
| 132 |
#ifdef IF_WITH_XARGS |
| 133 |
/*{ |
| 134 |
\todo{Doku extended args} |
| 135 |
}*/ |
| 136 |
#endif |
| 137 |
|
| 138 |
/*{ |
| 139 |
@param aIF the \ddd{Interface} to be used. |
| 140 |
}*/ |
| 141 |
#ifdef IF_WITH_ATTR |
| 142 |
/*{ |
| 143 |
@param aAttr the \ddd{Attribute} which specifies the sub-interface. |
| 144 |
}*/ |
| 145 |
#endif |
| 146 |
#ifdef IF_EXECLOCAL |
| 147 |
/*{ |
| 148 |
@param ExecProc handler which is executed for each object in the interface. |
| 149 |
}*/ |
| 150 |
#else |
| 151 |
#ifdef IF_ONEWAY |
| 152 |
/*{ |
| 153 |
@param aDir the communication direction (#IF_FORWARD# or #IF_BACKWARD#) |
| 154 |
}*/ |
| 155 |
#endif |
| 156 |
/*{ |
| 157 |
@param aSize amount of data for each object in the interface (number of bytes) |
| 158 |
}*/ |
| 159 |
#endif |
| 160 |
/*{ |
| 161 |
*/ |
| 162 |
|
| 163 |
|
| 164 |
void IF_FUNCNAME ( |
| 165 |
DDD::DDDContext& context, |
| 166 |
NS_DIM_PREFIX DDD_IF aIF, |
| 167 |
#ifdef IF_WITH_ATTR |
| 168 |
NS_DIM_PREFIX DDD_ATTR aAttr, |
| 169 |
#endif |
| 170 |
#ifdef IF_EXECLOCAL |
| 171 |
#ifdef IF_WITH_XARGS |
| 172 |
NS_DIM_PREFIX ExecProcXPtr ExecProc) |
| 173 |
#else |
| 174 |
NS_DIM_PREFIX ExecProcPtr ExecProc) |
| 175 |
#endif |
| 176 |
#else |
| 177 |
#ifdef IF_ONEWAY |
| 178 |
NS_DIM_PREFIX DDD_IF_DIR aDir, |
| 179 |
#endif |
| 180 |
size_t aSize, |
| 181 |
#ifdef IF_WITH_XARGS |
| 182 |
NS_DIM_PREFIX ComProcXPtr Gather, NS_DIM_PREFIX ComProcXPtr Scatter) |
| 183 |
#else |
| 184 |
NS_DIM_PREFIX ComProcPtr2 Gather, NS_DIM_PREFIX ComProcPtr2 Scatter) |
| 185 |
#endif |
| 186 |
#endif |
| 187 |
{ |
| 188 |
using std::setw; |
| 189 |
|
| 190 |
NS_DIM_PREFIX IF_PROC *ifHead; |
| 191 |
|
| 192 |
|
| 193 |
/* prohibit using standard interface (IF0) */ |
| 194 |
if (aIF==NS_DIM_PREFIX STD_INTERFACE) |
| 195 |
DUNE_THROW(Dune::Exception, "cannot use standard interface"); |
| 196 |
|
| 197 |
|
| 198 |
/* shortcuts can only be used without extended handler arguments */ |
| 199 |
#ifndef IF_WITH_XARGS |
| 200 |
/* if shortcut tables are invalid -> recompute */ |
| 201 |
NS_DIM_PREFIX IFCheckShortcuts(context, aIF); |
| 202 |
#endif |
| 203 |
|
| 204 |
|
| 205 |
#ifdef IF_EXECLOCAL |
| 206 |
|
| 207 |
ForIF(context, aIF, ifHead) |
| 208 |
{ |
| 209 |
#ifdef IF_WITH_ATTR |
| 210 |
NS_DIM_PREFIX IF_ATTR *ifAttr = ifHead->ifAttr; |
| 211 |
while ((ifAttr!=NULL) && (ifAttr->attr!=aAttr)) |
| 212 |
ifAttr = ifAttr->next; |
| 213 |
|
| 214 |
if (ifAttr==NULL) continue; |
| 215 |
#endif |
| 216 |
|
| 217 |
EXEC_LOOP(context, ExecProc, D_BA(PART), PART->nBA); |
| 218 |
EXEC_LOOP(context, ExecProc, D_AB(PART), PART->nAB); |
| 219 |
EXEC_LOOP(context, ExecProc, D_ABA(PART), PART->nABA); |
| 220 |
} |
| 221 |
|
| 222 |
#else /* ! IF_EXECLOCAL */ |
| 223 |
|
| 224 |
/* |
| 225 |
STAT_ZEROTIMER; |
| 226 |
STAT_RESET1; |
| 227 |
STAT_SETCOUNT(20,0); |
| 228 |
STAT_SETCOUNT(21,0); |
| 229 |
STAT_SETCOUNT(22,0); |
| 230 |
*/ |
| 231 |
|
| 232 |
/* allocate storage for in and out buffers */ |
| 233 |
ForIF(context, aIF, ifHead) |
| 234 |
{ |
| 235 |
#ifdef IF_ONEWAY |
| 236 |
int nOut, nIn; |
| 237 |
#endif |
| 238 |
|
| 239 |
#ifdef IF_WITH_ATTR |
| 240 |
/* reset buffers */ |
| 241 |
ifHead->bufIn.clear(); |
| 242 |
ifHead->bufOut.clear(); |
| 243 |
/* if no ATTR can be found, buffers will be empty */ |
| 244 |
NS_DIM_PREFIX IF_ATTR *ifAttr = ifHead->ifAttr; |
| 245 |
while ((ifAttr!=NULL) && (ifAttr->attr!=aAttr)) |
| 246 |
ifAttr = ifAttr->next; |
| 247 |
|
| 248 |
if (ifAttr==NULL) continue; |
| 249 |
#endif |
| 250 |
|
| 251 |
#ifdef IF_EXCHANGE |
| 252 |
NS_DIM_PREFIX IFGetMem(ifHead, aSize, PART->nItems, PART->nItems); |
| 253 |
#endif |
| 254 |
|
| 255 |
#ifdef IF_ONEWAY |
| 256 |
if (aDir==NS_DIM_PREFIX IF_FORWARD) { |
| 257 |
nOut = PART->nAB; nIn = PART->nBA; |
| 258 |
} |
| 259 |
else { |
| 260 |
nOut = PART->nBA; nIn = PART->nAB; |
| 261 |
} |
| 262 |
IFGetMem(ifHead, aSize, nIn+PART->nABA, nOut+PART->nABA); |
| 263 |
#endif |
| 264 |
|
| 265 |
/* STAT_SETCOUNT(20,STAT_GETCOUNT(20)+ifHead->nItems); |
| 266 |
STAT_SETCOUNT(21,STAT_GETCOUNT(21)+2); |
| 267 |
STAT_SETCOUNT(22,STAT_GETCOUNT(22)+ |
| 268 |
ifHead->bufIn.size() + ifHead->bufOut.size()); |
| 269 |
*/ |
| 270 |
} |
| 271 |
/* |
| 272 |
STAT_TIMER1(63); |
| 273 |
*/ |
| 274 |
|
| 275 |
|
| 276 |
/* init communication, initiate receives */ |
| 277 |
int recv_mesgs = NS_DIM_PREFIX IFInitComm(context, aIF); |
| 278 |
|
| 279 |
|
| 280 |
/* build messages using gather-handler and send them away */ |
| 281 |
ForIF(context, aIF, ifHead) |
| 282 |
{ |
| 283 |
char *buffer; |
| 284 |
#ifdef IF_ONEWAY |
| 285 |
int nOut; |
| 286 |
#ifdef IF_WITH_XARGS |
| 287 |
NS_DIM_PREFIX COUPLING **datOut; |
| 288 |
#else |
| 289 |
NS_DIM_PREFIX IFObjPtr *datOut; |
| 290 |
#endif |
| 291 |
#endif |
| 292 |
|
| 293 |
#ifdef IF_WITH_ATTR |
| 294 |
NS_DIM_PREFIX IF_ATTR *ifAttr = ifHead->ifAttr; |
| 295 |
while ((ifAttr!=NULL) && (ifAttr->attr!=aAttr)) |
| 296 |
ifAttr = ifAttr->next; |
| 297 |
|
| 298 |
if (ifAttr==NULL) continue; |
| 299 |
#endif |
| 300 |
|
| 301 |
buffer = ifHead->bufOut.data(); |
| 302 |
|
| 303 |
#ifdef IF_EXCHANGE |
| 304 |
/* exchange BA and AB during send */ |
| 305 |
buffer= COMM_LOOP(context, Gather, D_BA(PART), buffer, aSize, PART->nBA); |
| 306 |
buffer= COMM_LOOP(context, Gather, D_AB(PART), buffer, aSize, PART->nAB); |
| 307 |
#endif |
| 308 |
|
| 309 |
#ifdef IF_ONEWAY |
| 310 |
if (aDir==NS_DIM_PREFIX IF_FORWARD) { |
| 311 |
nOut = PART->nAB; datOut = D_AB(PART); |
| 312 |
} |
| 313 |
else { |
| 314 |
nOut = PART->nBA; datOut = D_BA(PART); |
| 315 |
} |
| 316 |
|
| 317 |
buffer= COMM_LOOP(context, Gather, datOut, buffer, aSize, nOut); |
| 318 |
#endif |
| 319 |
|
| 320 |
buffer= COMM_LOOP(context, Gather, D_ABA(PART), buffer, aSize, PART->nABA); |
| 321 |
|
| 322 |
NS_DIM_PREFIX IFInitSend(context, ifHead); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
/* poll receives and scatter data */ |
| 328 |
unsigned long tries; |
| 329 |
for(tries=0; tries<MAX_TRIES && recv_mesgs>0; tries++) |
| 330 |
{ |
| 331 |
/* poll receive calls */ |
| 332 |
ForIF(context, aIF, ifHead) |
| 333 |
{ |
| 334 |
if (not ifHead->bufIn.empty() && ifHead->msgIn!=NO_MSGID) |
| 335 |
{ |
| 336 |
int error = InfoARecv(context.ppifContext(), ifHead->vc, ifHead->msgIn); |
| 337 |
if (error==-1) |
| 338 |
DUNE_THROW(Dune::Exception, |
| 339 |
"InfoARecv failed for recv to proc=" << ifHead->proc); |
| 340 |
|
| 341 |
if (error==1) |
| 342 |
{ |
| 343 |
char *buffer; |
| 344 |
#ifdef IF_ONEWAY |
| 345 |
int nIn; |
| 346 |
#ifdef IF_WITH_XARGS |
| 347 |
NS_DIM_PREFIX COUPLING **datIn; |
| 348 |
#else |
| 349 |
NS_DIM_PREFIX IFObjPtr *datIn; |
| 350 |
#endif |
| 351 |
#endif |
| 352 |
|
| 353 |
#ifdef CtrlTimeoutsDetailed |
| 354 |
printf("%4d: IFCTRL %02d received msg from " |
| 355 |
"%4d after %10ld, size %ld\n", |
| 356 |
context.me(), aIF, ifHead->proc, |
| 357 |
(unsigned long)tries, |
| 358 |
(unsigned long)ifHead->bufIn.size()); |
| 359 |
#endif |
| 360 |
|
| 361 |
recv_mesgs--; |
| 362 |
ifHead->msgIn=NO_MSGID; |
| 363 |
|
| 364 |
#ifdef IF_WITH_ATTR |
| 365 |
NS_DIM_PREFIX IF_ATTR *ifAttr = ifHead->ifAttr; |
| 366 |
while ((ifAttr!=NULL) && (ifAttr->attr!=aAttr)) |
| 367 |
ifAttr = ifAttr->next; |
| 368 |
|
| 369 |
if (ifAttr==nullptr) continue; |
| 370 |
#endif |
| 371 |
|
| 372 |
buffer = ifHead->bufIn.data(); |
| 373 |
|
| 374 |
/* get data using scatter-handler */ |
| 375 |
#ifdef IF_EXCHANGE |
| 376 |
buffer = COMM_LOOP(context, Scatter, |
| 377 |
D_AB(PART), buffer, aSize, PART->nAB); |
| 378 |
buffer = COMM_LOOP(context, Scatter, |
| 379 |
D_BA(PART), buffer, aSize, PART->nBA); |
| 380 |
#endif |
| 381 |
|
| 382 |
#ifdef IF_ONEWAY |
| 383 |
if (aDir==NS_DIM_PREFIX IF_FORWARD) { |
| 384 |
nIn = PART->nBA; datIn = D_BA(PART); |
| 385 |
} |
| 386 |
else { |
| 387 |
nIn = PART->nAB; datIn = D_AB(PART); |
| 388 |
} |
| 389 |
|
| 390 |
buffer = COMM_LOOP(context, Scatter, datIn, buffer, aSize, nIn); |
| 391 |
#endif |
| 392 |
|
| 393 |
buffer = COMM_LOOP(context, Scatter, |
| 394 |
D_ABA(PART), buffer, aSize, PART->nABA); |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/* finally poll send completion */ |
| 401 |
if (recv_mesgs>0) |
| 402 |
{ |
| 403 |
Dune::dwarn << FUNCNAME_STR(IF_FUNCNAME) ": receive-timeout for IF " << setw(2) << aIF << "\n"; |
| 404 |
|
| 405 |
ForIF(context, aIF, ifHead) |
| 406 |
{ |
| 407 |
if (not ifHead->bufIn.empty() && ifHead->msgIn!=NO_MSGID) |
| 408 |
{ |
| 409 |
Dune::dwarn |
| 410 |
<< " waiting for message (from proc " << ifHead->proc |
| 411 |
<< ", size " << ifHead->bufIn.size() << ")\n"; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
else |
| 416 |
{ |
| 417 |
#ifdef CtrlTimeouts |
| 418 |
printf("%4d: IFCTRL %02d received msg all after %10ld tries\n", |
| 419 |
context.me(), aIF, (unsigned long)tries); |
| 420 |
#endif |
| 421 |
|
| 422 |
if (! NS_DIM_PREFIX IFPollSend(context, aIF)) |
| 423 |
{ |
| 424 |
Dune::dwarn << FUNCNAME_STR(IF_FUNCNAME) ": send-timeout for IF " << setw(2) << aIF << "\n"; |
| 425 |
|
| 426 |
ForIF(context, aIF, ifHead) |
| 427 |
{ |
| 428 |
if (not ifHead->bufOut.empty() && ifHead->msgOut!=NO_MSGID) |
| 429 |
{ |
| 430 |
Dune::dwarn |
| 431 |
<< " waiting for send completion (to proc " << ifHead->proc |
| 432 |
<< ", size " << ifHead->bufOut.size() << ")\n"; |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
/* free memory */ |
| 440 |
NS_DIM_PREFIX IFExitComm(context, aIF); |
| 441 |
|
| 442 |
/*STAT_TIMER1(60);*/ |
| 443 |
|
| 444 |
#endif /* IF_EXECLOCAL */ |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
/****************************************************************************/ |
| 449 |
|
| 450 |
/* undefs for all input macros and defines */ |
| 451 |
|
| 452 |
#undef IF_NAME |
| 453 |
#undef IF_FUNCNAME |
| 454 |
|
| 455 |
#ifdef IF_ONEWAY |
| 456 |
#undef IF_ONEWAY |
| 457 |
#endif |
| 458 |
|
| 459 |
#ifdef IF_EXCHANGE |
| 460 |
#undef IF_EXCHANGE |
| 461 |
#endif |
| 462 |
|
| 463 |
#ifdef IF_EXECLOCAL |
| 464 |
#undef IF_EXECLOCAL |
| 465 |
#endif |
| 466 |
|
| 467 |
|
| 468 |
#ifdef IF_WITH_ATTR |
| 469 |
#undef IF_WITH_ATTR |
| 470 |
#endif |
| 471 |
|
| 472 |
#ifdef IF_WITH_XARGS |
| 473 |
#undef IF_WITH_XARGS |
| 474 |
#endif |
| 475 |
|
| 476 |
#ifdef IF_CBR |
| 477 |
#undef IF_CBR |
| 478 |
#endif |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
/* undefs for derived macros */ |
| 483 |
|
| 484 |
#undef PART |
| 485 |
#undef EXEC_LOOP |
| 486 |
#undef COMM_LOOP |
| 487 |
#undef D_AB |
| 488 |
#undef D_BA |
| 489 |
#undef D_ABA |
| 490 |
|
| 491 |
#undef _CAT |
| 492 |
#undef CAT |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
/****************************************************************************/ |
| 497 |
|