| 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: supp.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: support routines for Transfer Module */ |
| 10 |
/* */ |
| 11 |
/* Author: Klaus Birken */ |
| 12 |
/* Rechenzentrum Uni Stuttgart */ |
| 13 |
/* Universitaet Stuttgart */ |
| 14 |
/* Allmandring 30 */ |
| 15 |
/* 70550 Stuttgart */ |
| 16 |
/* internet: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 93/11/30 kb begin (xfer.c) */ |
| 19 |
/* 95/03/21 kb added variable sized objects (XferCopyObjX) */ |
| 20 |
/* 95/04/05 kb V1.3: extracted from xfer.c */ |
| 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 <cassert> |
| 39 |
|
| 40 |
#include <new> |
| 41 |
|
| 42 |
#include <dune/uggrid/parallel/ddd/dddi.h> |
| 43 |
|
| 44 |
USING_UG_NAMESPACE |
| 45 |
|
| 46 |
/* |
| 47 |
NOTE: all container-classes from ooppcc.h are implemented in this |
| 48 |
source file by setting the following define. |
| 49 |
*/ |
| 50 |
#define ContainerImplementation |
| 51 |
/* this is the hardliner version, for debugging only |
| 52 |
#define _CHECKALLOC(ptr) assert(ptr!=NULL) |
| 53 |
*/ |
| 54 |
#define _CHECKALLOC(ptr) if (ptr==NULL) return (NULL) |
| 55 |
|
| 56 |
#include "xfer.h" |
| 57 |
|
| 58 |
/****************************************************************************/ |
| 59 |
/* */ |
| 60 |
/* definition of constants, macros */ |
| 61 |
/* */ |
| 62 |
/****************************************************************************/ |
| 63 |
|
| 64 |
#define ADDDATASEGM_SIZE 256 |
| 65 |
#define SIZESSEGM_SIZE 2048 |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
/****************************************************************************/ |
| 71 |
/* */ |
| 72 |
/* data types */ |
| 73 |
/* */ |
| 74 |
/****************************************************************************/ |
| 75 |
|
| 76 |
namespace DDD { |
| 77 |
namespace Xfer { |
| 78 |
|
| 79 |
/* segment of AddDatas */ |
| 80 |
struct AddDataSegm |
| 81 |
{ |
| 82 |
AddDataSegm *next; |
| 83 |
int nItems; |
| 84 |
|
| 85 |
XFERADDDATA item[ADDDATASEGM_SIZE]; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
/* segment of AddData-Sizes */ |
| 90 |
struct SizesSegm |
| 91 |
{ |
| 92 |
SizesSegm *next; |
| 93 |
int current; |
| 94 |
|
| 95 |
int data[SIZESSEGM_SIZE]; |
| 96 |
}; |
| 97 |
|
| 98 |
} /* namespace Xfer */ |
| 99 |
} /* namespace DDD */ |
| 100 |
START_UGDIM_NAMESPACE |
| 101 |
|
| 102 |
using namespace DDD::Xfer; |
| 103 |
|
| 104 |
/****************************************************************************/ |
| 105 |
/* */ |
| 106 |
/* class member function implementations */ |
| 107 |
/* */ |
| 108 |
/****************************************************************************/ |
| 109 |
|
| 110 |
#define ClassName XICopyObj |
| 111 |
|
| 112 |
/* |
| 113 |
compare-method in order to eliminate double XICopyObj-items. |
| 114 |
merge priorities from similar XICopyObj-items. |
| 115 |
|
| 116 |
the items are sorted according to key (dest,gid), |
| 117 |
all in ascending order. if dest and gid are equal, |
| 118 |
we merge priorities and get a new priority together with |
| 119 |
the information whether first item wins over second. |
| 120 |
in both cases, we use the new priority for next comparison. |
| 121 |
|
| 122 |
this implements rule XFER-C1. |
| 123 |
*/ |
| 124 |
int Method(Compare) (ClassPtr item1, ClassPtr item2, const DDD::DDDContext* context) |
| 125 |
{ |
| 126 |
DDD_PRIO newprio; |
| 127 |
int ret; |
| 128 |
|
| 129 |
if (item1->dest < item2->dest) return(-1); |
| 130 |
if (item1->dest > item2->dest) return(1); |
| 131 |
|
| 132 |
if (item1->gid < item2->gid) return(-1); |
| 133 |
if (item1->gid > item2->gid) return(1); |
| 134 |
|
| 135 |
|
| 136 |
/* items have equal gid and dest, so they are considered as equal. */ |
| 137 |
/* however, we must check priority, and patch both items with |
| 138 |
the new priority after merge. */ |
| 139 |
ret = PriorityMerge(&context->typeDefs()[OBJ_TYPE(item1->hdr)], |
| 140 |
item1->prio, item2->prio, &newprio); |
| 141 |
|
| 142 |
item1->prio = newprio; |
| 143 |
|
| 144 |
if (ret==PRIO_FIRST || ret==PRIO_UNKNOWN) |
| 145 |
{ |
| 146 |
/* tell XferInitCopyInfo() that item is rejected */ |
| 147 |
item2->prio = PRIO_INVALID; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
/* communicate new priority to XferInitCopyInfo() */ |
| 152 |
item2->prio = newprio; |
| 153 |
} |
| 154 |
|
| 155 |
return(0); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 160 |
{ |
| 161 |
fprintf(fp, "XICopyObj dest=%d gid=" DDD_GID_FMT " prio=%d\n", |
| 162 |
This->dest, This->gid, This->prio); |
| 163 |
} |
| 164 |
|
| 165 |
#undef ClassName |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
/****************************************************************************/ |
| 170 |
|
| 171 |
#define ClassName XISetPrio |
| 172 |
|
| 173 |
/* |
| 174 |
compare-method in order to eliminate double XISetPrio-items. |
| 175 |
merge priorities from similar XISetPrio-items. |
| 176 |
|
| 177 |
the items are sorted according to key (gid), |
| 178 |
all in ascending order. if both gids are equal, |
| 179 |
we merge priorities and get a new priority together with |
| 180 |
the information whether first item wins over second. |
| 181 |
in both cases, we use the new priority for next comparison. |
| 182 |
|
| 183 |
this implements rule XFER-P1. |
| 184 |
*/ |
| 185 |
int Method(Compare) (ClassPtr item1, ClassPtr item2, const DDD::DDDContext* context) |
| 186 |
{ |
| 187 |
DDD_PRIO newprio; |
| 188 |
int ret; |
| 189 |
|
| 190 |
/* ascending GID is needed for ExecLocalXI___ */ |
| 191 |
if (item1->gid < item2->gid) return(-1); |
| 192 |
if (item1->gid > item2->gid) return(1); |
| 193 |
|
| 194 |
|
| 195 |
/* items have equal gid and dest, so they are considered as equal. */ |
| 196 |
/* however, we must check priority, and patch both items with |
| 197 |
the new priority after merge. */ |
| 198 |
ret = PriorityMerge(&context->typeDefs()[OBJ_TYPE(item1->hdr)], |
| 199 |
item1->prio, item2->prio, &newprio); |
| 200 |
|
| 201 |
item1->prio = item2->prio = newprio; |
| 202 |
|
| 203 |
|
| 204 |
if (ret==PRIO_FIRST || ret==PRIO_UNKNOWN) |
| 205 |
{ |
| 206 |
/* tell XferInitCopyInfo() that item is rejected */ |
| 207 |
item2->prio = PRIO_INVALID; |
| 208 |
} |
| 209 |
else |
| 210 |
{ |
| 211 |
/* communicate new priority to XferInitCopyInfo() */ |
| 212 |
item2->prio = newprio; |
| 213 |
} |
| 214 |
|
| 215 |
return(0); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 220 |
{ |
| 221 |
fprintf(fp, "XISetPrio gid=" DDD_GID_FMT " prio=%d\n", This->gid, This->prio); |
| 222 |
} |
| 223 |
|
| 224 |
#undef ClassName |
| 225 |
|
| 226 |
|
| 227 |
/****************************************************************************/ |
| 228 |
|
| 229 |
|
| 230 |
/* |
| 231 |
include templates |
| 232 |
*/ |
| 233 |
#define T XIDelCmd |
| 234 |
#define SLL_WithOrigOrder |
| 235 |
#include "sll.ct" |
| 236 |
#undef T |
| 237 |
|
| 238 |
#define T XIDelObj |
| 239 |
#include "sll.ct" |
| 240 |
#undef T |
| 241 |
|
| 242 |
#define T XINewCpl |
| 243 |
#include "sll.ct" |
| 244 |
#undef T |
| 245 |
|
| 246 |
#define T XIOldCpl |
| 247 |
#include "sll.ct" |
| 248 |
#undef T |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
#define T XIAddCpl |
| 253 |
#include "sll.ct" |
| 254 |
#undef T |
| 255 |
|
| 256 |
#define T XIDelCpl |
| 257 |
#include "sll.ct" |
| 258 |
#undef T |
| 259 |
|
| 260 |
#define T XIModCpl |
| 261 |
#include "sll.ct" |
| 262 |
#undef T |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
/****************************************************************************/ |
| 267 |
|
| 268 |
|
| 269 |
static AddDataSegm *NewAddDataSegm(DDD::DDDContext& context) |
| 270 |
{ |
| 271 |
auto& ctx = context.xferContext(); |
| 272 |
AddDataSegm *segm; |
| 273 |
|
| 274 |
segm = (AddDataSegm *) OO_Allocate(sizeof(AddDataSegm)); |
| 275 |
if (segm==NULL) |
| 276 |
throw std::bad_alloc(); |
| 277 |
|
| 278 |
segm->next = ctx.segmAddData; |
| 279 |
ctx.segmAddData = segm; |
| 280 |
segm->nItems = 0; |
| 281 |
|
| 282 |
return(segm); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
static void FreeAddDataSegms(DDD::DDDContext& context) |
| 287 |
{ |
| 288 |
auto& ctx = context.xferContext(); |
| 289 |
AddDataSegm *segm = ctx.segmAddData; |
| 290 |
AddDataSegm *next = NULL; |
| 291 |
|
| 292 |
while (segm!=NULL) |
| 293 |
{ |
| 294 |
next = segm->next; |
| 295 |
OO_Free (segm /*,sizeof(AddDataSegm)*/); |
| 296 |
|
| 297 |
segm = next; |
| 298 |
} |
| 299 |
|
| 300 |
ctx.segmAddData = nullptr; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
/****************************************************************************/ |
| 305 |
|
| 306 |
|
| 307 |
static SizesSegm *NewSizesSegm(DDD::DDDContext& context) |
| 308 |
{ |
| 309 |
auto& ctx = context.xferContext(); |
| 310 |
SizesSegm *segm; |
| 311 |
|
| 312 |
segm = (SizesSegm *) OO_Allocate (sizeof(SizesSegm)); |
| 313 |
if (segm==NULL) |
| 314 |
throw std::bad_alloc(); |
| 315 |
|
| 316 |
segm->next = ctx.segmSizes; |
| 317 |
ctx.segmSizes = segm; |
| 318 |
segm->current = 0; |
| 319 |
|
| 320 |
return(segm); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
static void FreeSizesSegms(DDD::DDDContext& context) |
| 325 |
{ |
| 326 |
auto& ctx = context.xferContext(); |
| 327 |
SizesSegm *segm = ctx.segmSizes; |
| 328 |
SizesSegm *next = NULL; |
| 329 |
|
| 330 |
while (segm!=NULL) |
| 331 |
{ |
| 332 |
next = segm->next; |
| 333 |
OO_Free (segm /*,sizeof(SizesSegm)*/); |
| 334 |
|
| 335 |
segm = next; |
| 336 |
} |
| 337 |
|
| 338 |
ctx.segmSizes = nullptr; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
/****************************************************************************/ |
| 343 |
|
| 344 |
|
| 345 |
XFERADDDATA *NewXIAddData(DDD::DDDContext& context) |
| 346 |
{ |
| 347 |
auto& ctx = context.xferContext(); |
| 348 |
AddDataSegm *segm = ctx.segmAddData; |
| 349 |
XFERADDDATA *xa; |
| 350 |
|
| 351 |
if (segm==NULL || segm->nItems==ADDDATASEGM_SIZE) |
| 352 |
{ |
| 353 |
segm = NewAddDataSegm(context); |
| 354 |
} |
| 355 |
|
| 356 |
xa = &(segm->item[segm->nItems++]); |
| 357 |
xa->next = ctx.theXIAddData->add; |
| 358 |
ctx.theXIAddData->add = xa; |
| 359 |
|
| 360 |
return(xa); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
void FreeAllXIAddData(DDD::DDDContext& context) |
| 366 |
{ |
| 367 |
FreeAddDataSegms(context); |
| 368 |
FreeSizesSegms(context); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
/****************************************************************************/ |
| 373 |
|
| 374 |
int *AddDataAllocSizes (DDD::DDDContext& context, int cnt) |
| 375 |
{ |
| 376 |
auto& ctx = context.xferContext(); |
| 377 |
SizesSegm *segm = ctx.segmSizes; |
| 378 |
int *pos; |
| 379 |
|
| 380 |
if (segm==NULL || segm->current+cnt>=SIZESSEGM_SIZE) |
| 381 |
{ |
| 382 |
segm = NewSizesSegm(context); |
| 383 |
} |
| 384 |
|
| 385 |
pos = segm->data + segm->current; |
| 386 |
segm->current += cnt; |
| 387 |
|
| 388 |
return(pos); |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
/****************************************************************************/ |
| 393 |
|
| 394 |
|
| 395 |
/* |
| 396 |
get quantitative resource usage |
| 397 |
*/ |
| 398 |
void GetSizesXIAddData(const DDD::DDDContext& context, int *nSegms, int *nItems, size_t *alloc_mem, size_t *used_mem) |
| 399 |
{ |
| 400 |
const auto& ctx = context.xferContext(); |
| 401 |
size_t allocated=0, used=0; |
| 402 |
int ns=0, ni=0; |
| 403 |
|
| 404 |
{ |
| 405 |
for (const AddDataSegm* segm=ctx.segmAddData; segm!=NULL; segm=segm->next) |
| 406 |
{ |
| 407 |
/* count number of segments and number of items */ |
| 408 |
ns++; |
| 409 |
ni+=segm->nItems; |
| 410 |
|
| 411 |
/* compute memory usage */ |
| 412 |
allocated += sizeof(AddDataSegm); |
| 413 |
used += (sizeof(AddDataSegm) - |
| 414 |
(sizeof(XFERADDDATA)*(ADDDATASEGM_SIZE-segm->nItems))); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
/* TODO: add resources for SizesSegm */ |
| 419 |
|
| 420 |
|
| 421 |
*nSegms = ns; |
| 422 |
*nItems = ni; |
| 423 |
*alloc_mem = allocated; |
| 424 |
*used_mem = used; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
/****************************************************************************/ |
| 429 |
|
| 430 |
END_UGDIM_NAMESPACE |