| 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: ifcreate.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: routines concerning interfaces between processors */ |
| 10 |
/* part 1: creating and maintaining 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 |
/* 96/01/08 kb renamed range to attr */ |
| 25 |
/* 96/01/16 kb added DDD_OBJ shortcut to avoid indirect addr. */ |
| 26 |
/* 96/05/07 kb removed need for global const MAX_COUPLING */ |
| 27 |
/* */ |
| 28 |
/* Remarks: */ |
| 29 |
/* */ |
| 30 |
/****************************************************************************/ |
| 31 |
|
| 32 |
/****************************************************************************/ |
| 33 |
/* */ |
| 34 |
/* include files */ |
| 35 |
/* system include files */ |
| 36 |
/* application include files */ |
| 37 |
/* */ |
| 38 |
/****************************************************************************/ |
| 39 |
|
| 40 |
/* standard C library */ |
| 41 |
#include <config.h> |
| 42 |
#include <cstdlib> |
| 43 |
#include <cstdio> |
| 44 |
#include <cstring> |
| 45 |
#include <cassert> |
| 46 |
|
| 47 |
#include <algorithm> |
| 48 |
#include <iomanip> |
| 49 |
#include <iostream> |
| 50 |
#include <tuple> |
| 51 |
|
| 52 |
#include <dune/common/exceptions.hh> |
| 53 |
#include <dune/common/stdstreams.hh> |
| 54 |
|
| 55 |
#include <dune/uggrid/parallel/ddd/dddi.h> |
| 56 |
#include "if.h" |
| 57 |
|
| 58 |
USING_UG_NAMESPACE |
| 59 |
|
| 60 |
/* PPIF namespace: */ |
| 61 |
using namespace PPIF; |
| 62 |
|
| 63 |
START_UGDIM_NAMESPACE |
| 64 |
|
| 65 |
/****************************************************************************/ |
| 66 |
/* */ |
| 67 |
/* routines */ |
| 68 |
/* */ |
| 69 |
/****************************************************************************/ |
| 70 |
|
| 71 |
|
| 72 |
/****************************************************************************/ |
| 73 |
/* */ |
| 74 |
/* Function: sort_IFCouplings */ |
| 75 |
/* */ |
| 76 |
/* Purpose: qsort procedure for sorting interface couplings. */ |
| 77 |
/* coupling list will be ordered according to: */ |
| 78 |
/* 1. processor number of represented object copy */ |
| 79 |
/* (increasing order) */ |
| 80 |
/* 2. direction of interface according to priorities */ |
| 81 |
/* (increasing order) */ |
| 82 |
/* 3. attr property of objects */ |
| 83 |
/* (decreasing order) */ |
| 84 |
/* 4. global ids of objects */ |
| 85 |
/* (increasing order) */ |
| 86 |
/* */ |
| 87 |
/* Input: two couplings `a`, `b` */ |
| 88 |
/* */ |
| 89 |
/* Output: `a < b` */ |
| 90 |
/* */ |
| 91 |
/****************************************************************************/ |
| 92 |
|
| 93 |
static bool sort_IFCouplings (const COUPLING* a, const COUPLING* b) |
| 94 |
{ |
| 95 |
const auto aDir = CPLDIR(a), bDir = CPLDIR(b); |
| 96 |
// a, b switched in the third component as decreasing order is used for attr |
| 97 |
return std::tie(CPL_PROC(a), aDir, OBJ_ATTR(b->obj), OBJ_GID(a->obj)) |
| 98 |
< std::tie(CPL_PROC(b), bDir, OBJ_ATTR(a->obj), OBJ_GID(b->obj)); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static void IFDeleteAll(DDD::DDDContext& context, DDD_IF ifId) |
| 104 |
{ |
| 105 |
auto& theIF = context.ifCreateContext().theIf; |
| 106 |
|
| 107 |
IF_PROC *ifh, *ifhNext; |
| 108 |
IF_ATTR *ifr, *ifrNext; |
| 109 |
|
| 110 |
/* free IF_PROC memory */ |
| 111 |
ifh=theIF[ifId].ifHead; |
| 112 |
while (ifh!=NULL) |
| 113 |
{ |
| 114 |
ifhNext = ifh->next; |
| 115 |
|
| 116 |
/* free IF_ATTR memory */ |
| 117 |
ifr=ifh->ifAttr; |
| 118 |
while (ifr!=NULL) |
| 119 |
{ |
| 120 |
ifrNext = ifr->next; |
| 121 |
delete ifr; |
| 122 |
ifr = ifrNext; |
| 123 |
} |
| 124 |
|
| 125 |
delete ifh; |
| 126 |
|
| 127 |
ifh = ifhNext; |
| 128 |
} |
| 129 |
|
| 130 |
/* free memory for coupling table */ |
| 131 |
if (theIF[ifId].cpl!=NULL) |
| 132 |
{ |
| 133 |
FreeIF(theIF[ifId].cpl); |
| 134 |
theIF[ifId].cpl=NULL; |
| 135 |
} |
| 136 |
|
| 137 |
/* free memory for shortcut object table */ |
| 138 |
if (theIF[ifId].obj!=NULL) |
| 139 |
{ |
| 140 |
FreeIF(theIF[ifId].obj); |
| 141 |
theIF[ifId].obj=NULL; |
| 142 |
} |
| 143 |
|
| 144 |
/* reset pointers */ |
| 145 |
theIF[ifId].ifHead = NULL; |
| 146 |
theIF[ifId].nIfHeads = 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
/* TODO el-set relation, VERY inefficient! */ |
| 153 |
static bool is_elem (DDD_PRIO el, int n, const DDD_PRIO *set) |
| 154 |
{ |
| 155 |
for(int i=0; i<n; i++) |
| 156 |
if (set[i]==el) |
| 157 |
return(true); |
| 158 |
|
| 159 |
return(false); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static RETCODE update_channels(DDD::DDDContext& context, DDD_IF ifId) |
| 165 |
{ |
| 166 |
auto& theIF = context.ifCreateContext().theIf; |
| 167 |
|
| 168 |
IF_PROC *ifh; |
| 169 |
int i; |
| 170 |
DDD_PROC *partners = DDD_ProcArray(context); |
| 171 |
|
| 172 |
if (theIF[ifId].nIfHeads==0) |
| 173 |
{ |
| 174 |
RET_ON_OK; |
| 175 |
} |
| 176 |
|
| 177 |
/* MarkHeap(); */ |
| 178 |
|
| 179 |
for(i=0, ifh=theIF[ifId].ifHead; ifh!=NULL; i++, ifh=ifh->next) |
| 180 |
{ |
| 181 |
partners[i] = ifh->proc; |
| 182 |
} |
| 183 |
|
| 184 |
if (! IS_OK(DDD_GetChannels(context, theIF[ifId].nIfHeads))) |
| 185 |
{ |
| 186 |
RET_ON_ERROR; |
| 187 |
} |
| 188 |
|
| 189 |
for(ifh=theIF[ifId].ifHead; ifh!=NULL; ifh=ifh->next) { |
| 190 |
ifh->vc = VCHAN_TO(context, ifh->proc); |
| 191 |
} |
| 192 |
|
| 193 |
/* ReleaseHeap(); */ |
| 194 |
|
| 195 |
|
| 196 |
RET_ON_OK; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/****************************************************************************/ |
| 201 |
|
| 202 |
/* collect couplings into interface array, for standard interface */ |
| 203 |
|
| 204 |
static COUPLING ** IFCollectStdCouplings(DDD::DDDContext& context) |
| 205 |
{ |
| 206 |
const auto& nCplItems = context.couplingContext().nCplItems; |
| 207 |
|
| 208 |
if (nCplItems == 0) |
| 209 |
return nullptr; |
| 210 |
|
| 211 |
/* get memory for couplings inside STD_IF */ |
| 212 |
COUPLING** cplarray = (COUPLING **) AllocIF(sizeof(COUPLING *)*nCplItems); |
| 213 |
if (cplarray==NULL) |
| 214 |
throw std::bad_alloc(); |
| 215 |
|
| 216 |
/* collect couplings */ |
| 217 |
int n = 0; |
| 218 |
const auto& nCpls = context.couplingContext().nCpls; |
| 219 |
for(int index=0; index < nCpls; index++) |
| 220 |
{ |
| 221 |
for(COUPLING* cpl=IdxCplList(context, index); cpl!=NULL; cpl=CPL_NEXT(cpl)) |
| 222 |
{ |
| 223 |
cplarray[n] = cpl; |
| 224 |
SETCPLDIR(cpl,0); |
| 225 |
n++; |
| 226 |
} |
| 227 |
} |
| 228 |
/* |
| 229 |
printf("%04d: n=%d, nCplItems=%d\n",context.me(),n,nCplItems); |
| 230 |
*/ |
| 231 |
assert(n==nCplItems); |
| 232 |
return cplarray; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/****************************************************************************/ |
| 237 |
|
| 238 |
static RETCODE IFCreateFromScratch(DDD::DDDContext& context, COUPLING **tmpcpl, DDD_IF ifId) |
| 239 |
{ |
| 240 |
auto& theIF = context.ifCreateContext().theIf; |
| 241 |
|
| 242 |
IF_PROC *ifHead = nullptr, *lastIfHead; |
| 243 |
IF_ATTR *ifAttr = nullptr, *lastIfAttr = nullptr; |
| 244 |
int n, i; |
| 245 |
DDD_PROC lastproc; |
| 246 |
[[maybe_unused]] int STAT_MOD; |
| 247 |
|
| 248 |
const auto& objTable = context.objTable(); |
| 249 |
|
| 250 |
STAT_GET_MODULE(STAT_MOD); |
| 251 |
STAT_SET_MODULE(DDD_MODULE_IF); |
| 252 |
|
| 253 |
/* first delete possible old interface */ |
| 254 |
IFDeleteAll(context, ifId); |
| 255 |
|
| 256 |
STAT_RESET1; |
| 257 |
if (ifId==STD_INTERFACE) |
| 258 |
{ |
| 259 |
theIF[ifId].cpl = IFCollectStdCouplings(context); |
| 260 |
n = context.couplingContext().nCplItems; |
| 261 |
} |
| 262 |
else |
| 263 |
{ |
| 264 |
int index; |
| 265 |
|
| 266 |
|
| 267 |
/* collect relevant couplings into tmpcpl array */ |
| 268 |
n=0; |
| 269 |
const auto& nCpls = context.couplingContext().nCpls; |
| 270 |
for(index=0; index < nCpls; index++) |
| 271 |
{ |
| 272 |
/* determine whether object belongs to IF */ |
| 273 |
if ((1<<OBJ_TYPE(objTable[index])) & theIF[ifId].maskO) |
| 274 |
{ |
| 275 |
const bool objInA = is_elem(OBJ_PRIO(objTable[index]), |
| 276 |
theIF[ifId].nPrioA, theIF[ifId].A); |
| 277 |
const bool objInB = is_elem(OBJ_PRIO(objTable[index]), |
| 278 |
theIF[ifId].nPrioB, theIF[ifId].B); |
| 279 |
|
| 280 |
if (objInA || objInB) |
| 281 |
{ |
| 282 |
COUPLING *cpl; |
| 283 |
|
| 284 |
/* test coupling list */ |
| 285 |
for(cpl=IdxCplList(context, index); cpl!=NULL; cpl=CPL_NEXT(cpl)) |
| 286 |
{ |
| 287 |
const bool cplInA = is_elem(cpl->prio, |
| 288 |
theIF[ifId].nPrioA, theIF[ifId].A); |
| 289 |
const bool cplInB = is_elem(cpl->prio, |
| 290 |
theIF[ifId].nPrioB, theIF[ifId].B); |
| 291 |
|
| 292 |
/* compute possible IF directions */ |
| 293 |
const int dir = ((objInA&&cplInB) ? DirAB : 0) | |
| 294 |
((objInB&&cplInA) ? DirBA : 0); |
| 295 |
|
| 296 |
if (dir != 0) |
| 297 |
{ |
| 298 |
SETCPLDIR(cpl,dir); |
| 299 |
tmpcpl[n] = cpl; |
| 300 |
n++; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if (n>0) |
| 308 |
{ |
| 309 |
/* re-alloc cpllist, now with correct size */ |
| 310 |
theIF[ifId].cpl = (COUPLING **) AllocIF(sizeof(COUPLING *)*n); |
| 311 |
if (theIF[ifId].cpl==NULL) |
| 312 |
{ |
| 313 |
Dune::dwarn << "IFCreateFromScratch: " STR_NOMEM " for IF " |
| 314 |
<< std::setw(2) << ifId << "\n"; |
| 315 |
RET_ON_ERROR; |
| 316 |
} |
| 317 |
|
| 318 |
/* copy data from temporary array */ |
| 319 |
memcpy((void *)theIF[ifId].cpl, (void *)tmpcpl, sizeof(COUPLING *)*n); |
| 320 |
} |
| 321 |
else |
| 322 |
{ |
| 323 |
theIF[ifId].cpl = NULL; |
| 324 |
} |
| 325 |
} |
| 326 |
STAT_TIMER1(T_CREATE_COLLECT); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
/* sort IF couplings */ |
| 331 |
STAT_RESET1; |
| 332 |
if (n>1) |
| 333 |
std::sort(theIF[ifId].cpl, theIF[ifId].cpl + n, sort_IFCouplings); |
| 334 |
STAT_TIMER1(T_CREATE_SORT); |
| 335 |
|
| 336 |
|
| 337 |
/* create IF_PROCs */ |
| 338 |
STAT_RESET1; |
| 339 |
lastproc = PROC_INVALID; |
| 340 |
lastIfHead = NULL; |
| 341 |
theIF[ifId].nIfHeads = 0; |
| 342 |
for(i=0; i<n; i++) |
| 343 |
{ |
| 344 |
COUPLING **cplp = &(theIF[ifId].cpl[i]); |
| 345 |
COUPLING *cpl = *cplp; |
| 346 |
DDD_ATTR attr = OBJ_ATTR(cpl->obj); |
| 347 |
|
| 348 |
if (CPL_PROC(cpl) != lastproc) |
| 349 |
{ |
| 350 |
/* create new IfHead */ |
| 351 |
theIF[ifId].nIfHeads++; |
| 352 |
ifHead = new IF_PROC; |
| 353 |
ifHead->cpl = cplp; |
| 354 |
ifHead->proc = CPL_PROC(cpl); |
| 355 |
ifHead->next = lastIfHead; |
| 356 |
lastIfHead = ifHead; |
| 357 |
lastproc = ifHead->proc; |
| 358 |
|
| 359 |
ifHead->nAttrs = 1; |
| 360 |
ifHead->ifAttr = ifAttr = new IF_ATTR(attr); |
| 361 |
lastIfAttr = ifAttr; |
| 362 |
} |
| 363 |
|
| 364 |
/* count #items per processor */ |
| 365 |
ifHead->nItems++; |
| 366 |
|
| 367 |
|
| 368 |
/* keep current ifAttr or find new one? */ |
| 369 |
if (attr!=ifAttr->attr) |
| 370 |
{ |
| 371 |
IF_ATTR *ifR; |
| 372 |
|
| 373 |
/* does ifAttr already exist? */ |
| 374 |
ifR = ifHead->ifAttr; |
| 375 |
while ((ifR!=NULL) && (ifR->attr!=attr)) |
| 376 |
{ |
| 377 |
ifR=ifR->next; |
| 378 |
} |
| 379 |
|
| 380 |
if (ifR!=NULL) |
| 381 |
{ |
| 382 |
/* reuse existing ifAttr */ |
| 383 |
ifAttr = ifR; |
| 384 |
} |
| 385 |
else |
| 386 |
{ |
| 387 |
/* create new ifAttr */ |
| 388 |
ifHead->nAttrs++; |
| 389 |
ifAttr = new IF_ATTR(attr); |
| 390 |
lastIfAttr->next = ifAttr; |
| 391 |
lastIfAttr = ifAttr; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
/* count #items per processor and attr */ |
| 397 |
ifAttr->nItems++; |
| 398 |
|
| 399 |
|
| 400 |
/* count #items per directions AB, BA or ABA |
| 401 |
and set beginnings of AB/BA/ABA subarrays */ |
| 402 |
if (ifId!=STD_INTERFACE) |
| 403 |
{ |
| 404 |
switch (CPLDIR(cpl)) |
| 405 |
{ |
| 406 |
case DirAB : |
| 407 |
ifHead->nAB++; |
| 408 |
if (ifHead->cplAB==0) ifHead->cplAB = cplp; |
| 409 |
ifAttr->nAB++; |
| 410 |
if (ifAttr->cplAB==0) ifAttr->cplAB = cplp; |
| 411 |
break; |
| 412 |
|
| 413 |
case DirBA : |
| 414 |
ifHead->nBA++; |
| 415 |
if (ifHead->cplBA==0) ifHead->cplBA = cplp; |
| 416 |
ifAttr->nBA++; |
| 417 |
if (ifAttr->cplBA==0) ifAttr->cplBA = cplp; |
| 418 |
break; |
| 419 |
|
| 420 |
case DirABA : |
| 421 |
ifHead->nABA++; |
| 422 |
if (ifHead->cplABA==0) ifHead->cplABA = cplp; |
| 423 |
ifAttr->nABA++; |
| 424 |
if (ifAttr->cplABA==0) ifAttr->cplABA = cplp; |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
STAT_TIMER1(T_CREATE_BUILD); |
| 430 |
|
| 431 |
/* remember anchor of ifHead list */ |
| 432 |
if (theIF[ifId].nIfHeads>0) { |
| 433 |
theIF[ifId].ifHead = ifHead; |
| 434 |
} |
| 435 |
|
| 436 |
/* store overall number of coupling items */ |
| 437 |
theIF[ifId].nItems = n; |
| 438 |
|
| 439 |
|
| 440 |
/* establish obj-table as an addressing shortcut */ |
| 441 |
STAT_RESET1; |
| 442 |
IFCreateObjShortcut(context, ifId); |
| 443 |
STAT_TIMER1(T_CREATE_SHORTCUT); |
| 444 |
|
| 445 |
|
| 446 |
STAT_RESET1; |
| 447 |
if (! IS_OK(update_channels(context, ifId))) |
| 448 |
{ |
| 449 |
DDD_PrintError('E', 4003, "couldn't create communication channels"); |
| 450 |
RET_ON_ERROR; |
| 451 |
} |
| 452 |
STAT_TIMER1(T_CREATE_COMM); |
| 453 |
|
| 454 |
/* TODO das handling der VCs muss noch erheblich verbessert werden */ |
| 455 |
/* TODO Still very inefficient because we have to search for is_elem */ |
| 456 |
|
| 457 |
STAT_SET_MODULE(STAT_MOD); |
| 458 |
|
| 459 |
RET_ON_OK; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
/****************************************************************************/ |
| 464 |
/* */ |
| 465 |
/* DDD_IFDefine */ |
| 466 |
/* */ |
| 467 |
/****************************************************************************/ |
| 468 |
|
| 469 |
/** |
| 470 |
Definition of a DDD Interface. |
| 471 |
|
| 472 |
This function defines a new \ddd{interface}. Its argument list contains |
| 473 |
three arrays: the first one specifies a subset of the global DDD object set, |
| 474 |
the second and third ones specify a subset of all DDD priorities. |
| 475 |
After the initial creation of the new interface its ID is returned. |
| 476 |
|
| 477 |
During all following DDD operations ({\bf Identify}- as well as |
| 478 |
{\bf Transfer}-operations) the interface will be kept consistent, and can |
| 479 |
be used for communication via \funk{IFExchange} and |
| 480 |
\funk{IFOneway} and analogous functions. |
| 481 |
|
| 482 |
@param nO number of entries in array {\it O[\ ]} (as specified with second argument). |
| 483 |
@param O array of DDD type IDs; the new interface will only contain objects with one of these types. |
| 484 |
@param nA number of entries in array {\it A[\ ]} (as specified with forth argument). |
| 485 |
@param A first array of DDD priorities; the object set A from the new interface will only contain objects with one of these priorities. |
| 486 |
@param nB number of entries in array {\it B[\ ]} (as specified with sixth argument). |
| 487 |
@param B second array of DDD priorities; the object set B from the new interface will only contain objects with one of these priorities. |
| 488 |
*/ |
| 489 |
|
| 490 |
DDD_IF DDD_IFDefine ( |
| 491 |
DDD::DDDContext& context, |
| 492 |
int nO, DDD_TYPE O[], |
| 493 |
int nA, DDD_PRIO A[], |
| 494 |
int nB, DDD_PRIO B[]) |
| 495 |
{ |
| 496 |
auto& ctx = context.ifCreateContext(); |
| 497 |
auto& theIF = ctx.theIf; |
| 498 |
auto& nIFs = ctx.nIfs; |
| 499 |
|
| 500 |
int i; |
| 501 |
|
| 502 |
if (nIFs==MAX_IF) { |
| 503 |
DDD_PrintError('E', 4100, "no more interfaces in DDD_IFDefine"); |
| 504 |
return(0); |
| 505 |
} |
| 506 |
|
| 507 |
/* construct interface definition */ |
| 508 |
theIF[nIFs].nObjStruct = nO; |
| 509 |
theIF[nIFs].nPrioA = nA; |
| 510 |
theIF[nIFs].nPrioB = nB; |
| 511 |
memcpy(theIF[nIFs].O, O, nO*sizeof(DDD_TYPE)); |
| 512 |
memcpy(theIF[nIFs].A, A, nA*sizeof(DDD_PRIO)); |
| 513 |
memcpy(theIF[nIFs].B, B, nB*sizeof(DDD_PRIO)); |
| 514 |
if (nO>1) std::sort(theIF[nIFs].O, theIF[nIFs].O + nO); |
| 515 |
if (nA>1) std::sort(theIF[nIFs].A, theIF[nIFs].A + nA); |
| 516 |
if (nB>1) std::sort(theIF[nIFs].B, theIF[nIFs].B + nB); |
| 517 |
|
| 518 |
/* reset name string */ |
| 519 |
theIF[nIFs].name[0] = 0; |
| 520 |
|
| 521 |
/* compute hash tables for fast access */ |
| 522 |
theIF[nIFs].maskO = 0; |
| 523 |
for(i=0; i<nO; i++) |
| 524 |
theIF[nIFs].maskO |= (1<<(unsigned int)O[i]); |
| 525 |
|
| 526 |
|
| 527 |
/* create initial interface state */ |
| 528 |
theIF[nIFs].ifHead = NULL; |
| 529 |
const auto& nCplItems = context.couplingContext().nCplItems; |
| 530 |
if (nCplItems > 0) |
| 531 |
{ |
| 532 |
/* allocate temporary cpl-list, this will be too large for |
| 533 |
average interfaces. */ |
| 534 |
std::vector<COUPLING*> tmpcpl(nCplItems); |
| 535 |
|
| 536 |
if (! IS_OK(IFCreateFromScratch(context, tmpcpl.data(), nIFs))) |
| 537 |
{ |
| 538 |
DDD_PrintError('E', 4101, "cannot create interface in DDD_IFDefine"); |
| 539 |
return(0); |
| 540 |
} |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
if (! IS_OK(IFCreateFromScratch(context, NULL, nIFs))) |
| 545 |
{ |
| 546 |
DDD_PrintError('E', 4102, "cannot create interface in DDD_IFDefine"); |
| 547 |
return(0); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
nIFs++; |
| 553 |
|
| 554 |
return(nIFs-1); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static void StdIFDefine(DDD::DDDContext& context) |
| 560 |
{ |
| 561 |
auto& theIF = context.ifCreateContext().theIf; |
| 562 |
|
| 563 |
/* exception: no OBJSTRUCT or priority entries */ |
| 564 |
theIF[STD_INTERFACE].nObjStruct = 0; |
| 565 |
theIF[STD_INTERFACE].nPrioA = 0; |
| 566 |
theIF[STD_INTERFACE].nPrioB = 0; |
| 567 |
|
| 568 |
theIF[STD_INTERFACE].maskO = 0xffff; |
| 569 |
|
| 570 |
|
| 571 |
/* reset name string */ |
| 572 |
theIF[STD_INTERFACE].name[0] = '\0'; |
| 573 |
|
| 574 |
|
| 575 |
/* create initial interface state */ |
| 576 |
theIF[STD_INTERFACE].ifHead = NULL; |
| 577 |
if (! IS_OK(IFCreateFromScratch(context, NULL, STD_INTERFACE))) |
| 578 |
DUNE_THROW(Dune::Exception, |
| 579 |
"cannot create standard interface during IF initialization"); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
void DDD_IFSetName(DDD::DDDContext& context, DDD_IF ifId, const char *name) |
| 585 |
{ |
| 586 |
auto& theIF = context.ifCreateContext().theIf; |
| 587 |
|
| 588 |
/* copy name string */ |
| 589 |
strncpy(theIF[ifId].name, name, IF_NAMELEN-1); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
/****************************************************************************/ |
| 594 |
|
| 595 |
static void writeCoupling(const DDD::DDDContext& context, const IF_PROC& ifh, const COUPLING& cpl, const char* obj, std::ostream& out) |
| 596 |
{ |
| 597 |
using std::setw; |
| 598 |
|
| 599 |
out << "| gid=" << OBJ_GID(cpl.obj) << " proc=" << setw(4) << CPL_PROC(&cpl) |
| 600 |
<< " prio=" << setw(2) << cpl.prio |
| 601 |
<< "osc=" << obj << "/" << OBJ_OBJ(context, cpl.obj) << "\n"; |
| 602 |
} |
| 603 |
|
| 604 |
void DDD_InfoIFImpl(DDD::DDDContext& context, DDD_IF ifId) |
| 605 |
{ |
| 606 |
using std::setw; |
| 607 |
|
| 608 |
std::ostream& out = std::cout; |
| 609 |
auto& theIF = context.ifCreateContext().theIf; |
| 610 |
|
| 611 |
out << "|\n| DDD_IFInfoImpl for proc=" << context.me() << ", IF " << ifId << "\n"; |
| 612 |
|
| 613 |
out << "| cpl=" << theIF[ifId].cpl << " nIfHeads=" |
| 614 |
<< theIF[ifId].nIfHeads << " first=" << theIF[ifId].ifHead << "\n"; |
| 615 |
|
| 616 |
for(const IF_PROC* ifh=theIF[ifId].ifHead; ifh!=nullptr; ifh=ifh->next) |
| 617 |
{ |
| 618 |
out << "| head=" << ifh << " cpl=" << ifh->cpl |
| 619 |
<< " p=" << setw(3) << ifh->proc << " nItems=" << setw(5) << ifh->nItems |
| 620 |
<< " nAttrs=" << setw(3) << ifh->nAttrs << "\n"; |
| 621 |
|
| 622 |
out << "| nAB= " << setw(5) << ifh->nAB << "\n"; |
| 623 |
for(int i = 0; i < ifh->nAB; ++i) |
| 624 |
writeCoupling(context, *ifh, *ifh->cplAB[i], ifh->objAB[i], out); |
| 625 |
|
| 626 |
out << "| nBA= " << setw(5) << ifh->nBA << "\n"; |
| 627 |
for(int i = 0; i < ifh->nBA; ++i) |
| 628 |
writeCoupling(context, *ifh, *ifh->cplBA[i], ifh->objBA[i], out); |
| 629 |
|
| 630 |
out << "| nABA=" << setw(5) << ifh->nABA << "\n"; |
| 631 |
for(int i = 0; i < ifh->nABA; ++i) |
| 632 |
writeCoupling(context, *ifh, *ifh->cplABA[i], ifh->objABA[i], out); |
| 633 |
} |
| 634 |
|
| 635 |
out << "|\n"; |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
static void IFDisplay (const DDD::DDDContext& context, DDD_IF i) |
| 641 |
{ |
| 642 |
using std::setw; |
| 643 |
|
| 644 |
std::ostream& out = std::cout; |
| 645 |
const auto& ctx = context.ifCreateContext(); |
| 646 |
const auto& theIF = ctx.theIf; |
| 647 |
|
| 648 |
out << "| IF " << i << " "; |
| 649 |
if (i==STD_INTERFACE) |
| 650 |
{ |
| 651 |
out << "including all (" << setw(8) << std::hex << theIF[i].maskO << std::dec << ")\n" |
| 652 |
<< "| prio all to all\n"; |
| 653 |
} |
| 654 |
else |
| 655 |
{ |
| 656 |
out << "including "; |
| 657 |
for(int j=0; j<theIF[i].nObjStruct; j++) |
| 658 |
out << context.typeDefs()[theIF[i].O[j]].name << " "; |
| 659 |
out << "(" << setw(8) << std::hex << theIF[i].maskO << std::dec << ")\n" |
| 660 |
<< "| prio "; |
| 661 |
for(int j=0; j<theIF[i].nPrioA; j++) |
| 662 |
out << theIF[i].A[j] << " "; |
| 663 |
out << "to "; |
| 664 |
for(int j=0; j<theIF[i].nPrioB; j++) |
| 665 |
out << theIF[i].B[j] << " "; |
| 666 |
out << "\n"; |
| 667 |
} |
| 668 |
|
| 669 |
if (theIF[i].name[0]!=0) |
| 670 |
out << "| '" << theIF[i].name << "'\n"; |
| 671 |
|
| 672 |
for(const IF_PROC* ifh=theIF[i].ifHead; ifh!=NULL; ifh=ifh->next) |
| 673 |
{ |
| 674 |
if (DDD_GetOption(context, OPT_INFO_IF_WITH_ATTR)==OPT_OFF) |
| 675 |
out << "| " << setw(3) << ifh->nItems << "=" |
| 676 |
<< setw(3) << ifh->nAB << "," << setw(3) << ifh->nBA << "," << setw(3) << ifh->nABA |
| 677 |
<< " - " << setw(2) << ifh->proc << "\n"; |
| 678 |
else |
| 679 |
{ |
| 680 |
out << "| " << setw(3) << ifh->nItems |
| 681 |
<< "=" << setw(3) << ifh->nAB << "," << setw(3) << ifh->nBA << "," << setw(3) << ifh->nABA |
| 682 |
<< " - " << setw(2) << ifh->proc << " - #a=" << setw(5) << ifh->nAttrs << "\n"; |
| 683 |
|
| 684 |
for (const IF_ATTR* ifr=ifh->ifAttr; ifr!=NULL; ifr=ifr->next) |
| 685 |
{ |
| 686 |
out << "| a " << setw(3) << ifr->nItems << "=" |
| 687 |
<< setw(3) << ifr->nAB << "," << setw(3) << ifr->nBA << "," << setw(3) << ifr->nABA |
| 688 |
<< " - " << setw(4) << ifr->attr << "\n"; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
/****************************************************************************/ |
| 696 |
/* */ |
| 697 |
/* DDD_IFDisplay */ |
| 698 |
/* */ |
| 699 |
/****************************************************************************/ |
| 700 |
|
| 701 |
/** |
| 702 |
Display overview of single DDD interface. |
| 703 |
This function displays an overview table for one DDD-interface, |
| 704 |
its definition parameters and the current number of constituing objects |
| 705 |
on the calling processor. |
| 706 |
|
| 707 |
For each neighbour processor corresponding |
| 708 |
to that interface a relation line is displayed, which contains |
| 709 |
the overall number of objects inside the interface, the number of |
| 710 |
oneway relations outwards, the number of oneway relations inwards, |
| 711 |
the number of exchange relations and the neighbour processor number. |
| 712 |
|
| 713 |
@param aIF the \ddd{interface} ID. |
| 714 |
*/ |
| 715 |
|
| 716 |
void DDD_IFDisplay(const DDD::DDDContext& context, DDD_IF aIF) |
| 717 |
{ |
| 718 |
if (aIF >= context.ifCreateContext().nIfs) |
| 719 |
{ |
| 720 |
Dune::dwarn << "DDD_IFDisplay: invalid IF " << std::setw(2) << aIF << "\n"; |
| 721 |
return; |
| 722 |
} |
| 723 |
|
| 724 |
std::cout << "|\n| DDD_IF-Info for proc=" << context.me() << "\n"; |
| 725 |
|
| 726 |
IFDisplay(context, aIF); |
| 727 |
|
| 728 |
std::cout << "|\n"; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
/****************************************************************************/ |
| 734 |
/* */ |
| 735 |
/* DDD_IFDisplayAll */ |
| 736 |
/* */ |
| 737 |
/****************************************************************************/ |
| 738 |
|
| 739 |
/** |
| 740 |
Display overview of all DDD interfaces. |
| 741 |
This function displays an overview table containing all DDD-interfaces, |
| 742 |
their definition parameters and the current number of constituing objects |
| 743 |
on the calling processor. |
| 744 |
|
| 745 |
For each interface and each neighbour processor corresponding |
| 746 |
to that interface a relation line is displayed, which contains |
| 747 |
the overall number of objects inside the interface, the number of |
| 748 |
oneway relations outwards, the number of oneway relations inwards, |
| 749 |
the number of exchange relations and the neighbour processor number. |
| 750 |
*/ |
| 751 |
|
| 752 |
void DDD_IFDisplayAll(const DDD::DDDContext& context) |
| 753 |
{ |
| 754 |
std::cout << "|\n| DDD_IF-Info for proc=" << context.me() << " (all)\n"; |
| 755 |
|
| 756 |
const auto& nIFs = context.ifCreateContext().nIfs; |
| 757 |
for(int i=0; i < nIFs; ++i) |
| 758 |
{ |
| 759 |
IFDisplay(context, i); |
| 760 |
} |
| 761 |
|
| 762 |
std::cout << "|\n"; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
static void IFRebuildAll(DDD::DDDContext& context) |
| 768 |
{ |
| 769 |
/* create standard interface */ |
| 770 |
if (! IS_OK(IFCreateFromScratch(context, NULL, STD_INTERFACE))) |
| 771 |
DUNE_THROW(Dune::Exception, |
| 772 |
"cannot create standard interface in IFRebuildAll"); |
| 773 |
|
| 774 |
|
| 775 |
const auto& nIFs = context.ifCreateContext().nIfs; |
| 776 |
if (nIFs>1) |
| 777 |
{ |
| 778 |
int i; |
| 779 |
|
| 780 |
const auto& nCplItems = context.couplingContext().nCplItems; |
| 781 |
if (nCplItems > 0) |
| 782 |
{ |
| 783 |
/* allocate temporary cpl-list, this will be too large for |
| 784 |
average interfaces. */ |
| 785 |
std::vector<COUPLING*> tmpcpl(nCplItems); |
| 786 |
|
| 787 |
/* TODO: Exploit that STD_IF is a superset of all interfaces */ |
| 788 |
for(i=1; i<nIFs; i++) |
| 789 |
{ |
| 790 |
if (! IS_OK(IFCreateFromScratch(context, tmpcpl.data(), i))) |
| 791 |
DUNE_THROW(Dune::Exception, |
| 792 |
"cannot create interface " << i); |
| 793 |
/* |
| 794 |
DDD_InfoIFImpl(context, i); |
| 795 |
*/ |
| 796 |
} |
| 797 |
} |
| 798 |
else |
| 799 |
{ |
| 800 |
/* delete old interface structures */ |
| 801 |
for(i=1; i<nIFs; i++) |
| 802 |
{ |
| 803 |
/* delete possible old interface */ |
| 804 |
IFDeleteAll(context, i); |
| 805 |
} |
| 806 |
} |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
void IFAllFromScratch(DDD::DDDContext& context) |
| 812 |
{ |
| 813 |
if (DDD_GetOption(context, OPT_IF_CREATE_EXPLICIT)==OPT_ON) |
| 814 |
{ |
| 815 |
/* interfaces must be created explicitly by calling |
| 816 |
DDD_IFRefreshAll(). This is for doing timings from |
| 817 |
application level. */ |
| 818 |
return; |
| 819 |
} |
| 820 |
|
| 821 |
IFRebuildAll(context); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
void DDD_IFRefreshAll(DDD::DDDContext& context) |
| 827 |
{ |
| 828 |
if (DDD_GetOption(context, OPT_IF_CREATE_EXPLICIT)==OPT_OFF) |
| 829 |
{ |
| 830 |
/* if interfaces are not created explicit, then they |
| 831 |
are always kept consistent automatically. therefore, |
| 832 |
this function is senseless. */ |
| 833 |
/* return; nevertheless, dont return, create interfaces |
| 834 |
once more. just to be sure. */ |
| 835 |
} |
| 836 |
|
| 837 |
IFRebuildAll(context); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
/****************************************************************************/ |
| 842 |
|
| 843 |
void ddd_IFInit(DDD::DDDContext& context) |
| 844 |
{ |
| 845 |
auto& ctx = context.ifCreateContext(); |
| 846 |
|
| 847 |
/* init lists of unused items */ |
| 848 |
ctx.theIf[0].ifHead = nullptr; |
| 849 |
ctx.theIf[0].cpl = nullptr; |
| 850 |
|
| 851 |
/* init standard interface */ |
| 852 |
StdIFDefine(context); |
| 853 |
|
| 854 |
/* no other interfaces yet */ |
| 855 |
ctx.nIfs = 1; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
void ddd_IFExit(DDD::DDDContext& context) |
| 860 |
{ |
| 861 |
const auto& nIFs = context.ifCreateContext().nIfs; |
| 862 |
for(int i=0; i < nIFs; i++) |
| 863 |
IFDeleteAll(context, i); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
/****************************************************************************/ |
| 868 |
|
| 869 |
|
| 870 |
static size_t IFInfoMemory(const DDD::DDDContext& context, DDD_IF ifId) |
| 871 |
{ |
| 872 |
const auto& theIF = context.ifCreateContext().theIf; |
| 873 |
|
| 874 |
size_t sum=0; |
| 875 |
|
| 876 |
sum += sizeof(IF_PROC) * theIF[ifId].nIfHeads; /* component ifHead */ |
| 877 |
sum += sizeof(COUPLING *) * theIF[ifId].nItems; /* component cpl */ |
| 878 |
sum += sizeof(IFObjPtr) * theIF[ifId].nItems; /* component obj */ |
| 879 |
|
| 880 |
for(const IF_PROC* ifp=theIF[ifId].ifHead; ifp != nullptr; ifp=ifp->next) |
| 881 |
{ |
| 882 |
sum += sizeof(IF_ATTR) * ifp->nAttrs; /* component ifAttr */ |
| 883 |
} |
| 884 |
|
| 885 |
return(sum); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
size_t DDD_IFInfoMemory(const DDD::DDDContext& context, DDD_IF ifId) |
| 891 |
{ |
| 892 |
if (ifId >= context.ifCreateContext().nIfs) |
| 893 |
DUNE_THROW(Dune::Exception, "invalid interface " << ifId); |
| 894 |
|
| 895 |
return IFInfoMemory(context, ifId); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
size_t DDD_IFInfoMemoryAll(const DDD::DDDContext& context) |
| 900 |
{ |
| 901 |
const auto& nIFs = context.ifCreateContext().nIfs; |
| 902 |
|
| 903 |
size_t sum = 0; |
| 904 |
for(int i = 0; i < nIFs; ++i) |
| 905 |
{ |
| 906 |
sum += IFInfoMemory(context, i); |
| 907 |
} |
| 908 |
|
| 909 |
return(sum); |
| 910 |
} |
| 911 |
|
| 912 |
END_UGDIM_NAMESPACE |
| 913 |
|
| 914 |
/****************************************************************************/ |