| 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: cmdmsg.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: ddd command transfer: */ |
| 10 |
/* send messages with commands to owners of other local */ |
| 11 |
/* object copies. this is used for sending XferCopy commands */ |
| 12 |
/* to owners of local copies, in order to prevent deletion */ |
| 13 |
/* and creation of the same object copy during xfer. */ |
| 14 |
/* (used only if OPT_XFER_PRUNE_DELETE is set to OPT_ON) */ |
| 15 |
/* */ |
| 16 |
/* Author: Klaus Birken */ |
| 17 |
/* Institut fuer Computeranwendungen III */ |
| 18 |
/* Universitaet Stuttgart */ |
| 19 |
/* Pfaffenwaldring 27 */ |
| 20 |
/* 70569 Stuttgart */ |
| 21 |
/* internet: [email protected] */ |
| 22 |
/* */ |
| 23 |
/* History: 970411 kb created */ |
| 24 |
/* */ |
| 25 |
/* Remarks: */ |
| 26 |
/* */ |
| 27 |
/****************************************************************************/ |
| 28 |
|
| 29 |
/****************************************************************************/ |
| 30 |
/* */ |
| 31 |
/* include files */ |
| 32 |
/* system include files */ |
| 33 |
/* application include files */ |
| 34 |
/* */ |
| 35 |
/****************************************************************************/ |
| 36 |
|
| 37 |
/* standard C library */ |
| 38 |
#include <config.h> |
| 39 |
#include <cstdlib> |
| 40 |
#include <cstdio> |
| 41 |
#include <cstring> |
| 42 |
#include <cassert> |
| 43 |
|
| 44 |
#include <algorithm> |
| 45 |
#include <forward_list> |
| 46 |
#include <iomanip> |
| 47 |
#include <iostream> |
| 48 |
#include <sstream> |
| 49 |
#include <vector> |
| 50 |
|
| 51 |
#include <dune/common/stdstreams.hh> |
| 52 |
|
| 53 |
#include <dune/uggrid/parallel/ddd/dddi.h> |
| 54 |
#include "xfer.h" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
USING_UG_NAMESPACE |
| 59 |
using namespace PPIF; |
| 60 |
|
| 61 |
START_UGDIM_NAMESPACE |
| 62 |
|
| 63 |
/****************************************************************************/ |
| 64 |
/* */ |
| 65 |
/* data structures */ |
| 66 |
/* */ |
| 67 |
/****************************************************************************/ |
| 68 |
|
| 69 |
/* CMDMSG: complete description of message on sender side */ |
| 70 |
|
| 71 |
struct CMDMSG |
| 72 |
{ |
| 73 |
DDD_PROC proc; |
| 74 |
|
| 75 |
DDD_GID *aUnDelete = nullptr; |
| 76 |
int nUnDelete = 0; |
| 77 |
|
| 78 |
/* lowcomm message handle */ |
| 79 |
LC_MSGHANDLE msg_h; |
| 80 |
|
| 81 |
CMDMSG(DDD_PROC dest) |
| 82 |
: proc(dest) |
| 83 |
{} |
| 84 |
}; |
| 85 |
|
| 86 |
using CmdmsgList = std::forward_list<CMDMSG>; |
| 87 |
|
| 88 |
|
| 89 |
/****************************************************************************/ |
| 90 |
/* */ |
| 91 |
/* routines */ |
| 92 |
/* */ |
| 93 |
/****************************************************************************/ |
| 94 |
|
| 95 |
|
| 96 |
void CmdMsgInit(DDD::DDDContext& context) |
| 97 |
{ |
| 98 |
auto& ctx = context.cmdmsgContext(); |
| 99 |
|
| 100 |
ctx.cmdmsg_t = LC_NewMsgType(context, "CmdMsg"); |
| 101 |
ctx.undelete_id = LC_NewMsgTable("UndelTab", ctx.cmdmsg_t, sizeof(DDD_GID)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
void CmdMsgExit(DDD::DDDContext& context) |
| 106 |
{} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
/****************************************************************************/ |
| 111 |
|
| 112 |
static |
| 113 |
std::pair<int, CmdmsgList> |
| 114 |
PrepareCmdMsgs (DDD::DDDContext& context, const std::vector<XICopyObj*>& arrayCO) |
| 115 |
{ |
| 116 |
auto& ctx = context.cmdmsgContext(); |
| 117 |
|
| 118 |
if (arrayCO.empty()) |
| 119 |
return {0, CmdmsgList()}; |
| 120 |
|
| 121 |
# if DebugCmdMsg<=3 |
| 122 |
Dune::dvverb << "PreparePrune, nCopyObj=" << arrayCO.size() << "\n"; |
| 123 |
# endif |
| 124 |
|
| 125 |
|
| 126 |
/* |
| 127 |
run through CopyObj table, mark all entries that have |
| 128 |
a coupling with same proc as destination. |
| 129 |
*/ |
| 130 |
|
| 131 |
int markedCO = 0; |
| 132 |
for(auto co : arrayCO) |
| 133 |
{ |
| 134 |
DDD_PROC pCO = co->dest; |
| 135 |
COUPLING *cpl; |
| 136 |
|
| 137 |
/* run through coupling list of corresponding obj, |
| 138 |
find coupling to destination of XferCopyObj command */ |
| 139 |
cpl = ObjCplList(context, co->hdr); |
| 140 |
while (cpl!=NULL && CPL_PROC(cpl)!=pCO) |
| 141 |
cpl=CPL_NEXT(cpl); |
| 142 |
|
| 143 |
if (cpl!=NULL) |
| 144 |
{ |
| 145 |
/* found coupling -> mark CopyObj */ |
| 146 |
SET_CO_SELF(co, 1); |
| 147 |
markedCO++; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
SET_CO_SELF(co, 0); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
if (markedCO==0) |
| 157 |
return {0, CmdmsgList()}; |
| 158 |
|
| 159 |
std::vector<DDD_GID> gids(markedCO); |
| 160 |
CmdmsgList msgs; |
| 161 |
int nMsgs = 0; |
| 162 |
|
| 163 |
/* |
| 164 |
run through CopyObj table again, consider only marked items, |
| 165 |
each time a new proc-nr is encountered in one of these |
| 166 |
tables, create a new CMDMSG item. |
| 167 |
|
| 168 |
(the lists have been sorted according to proc-nr previously.) |
| 169 |
*/ |
| 170 |
int j=0; |
| 171 |
for(auto co : arrayCO) |
| 172 |
{ |
| 173 |
if (CO_SELF(co)) |
| 174 |
{ |
| 175 |
gids[j] = co->gid; |
| 176 |
|
| 177 |
if (msgs.empty() or msgs.front().proc != co->dest) |
| 178 |
{ |
| 179 |
msgs.emplace_front(co->dest); |
| 180 |
nMsgs++; |
| 181 |
|
| 182 |
msgs.front().aUnDelete = &gids[j]; |
| 183 |
} |
| 184 |
|
| 185 |
msgs.front().nUnDelete++; |
| 186 |
j++; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* initiate send messages */ |
| 191 |
for(auto& xm : msgs) |
| 192 |
{ |
| 193 |
/* create new send message */ |
| 194 |
xm.msg_h = LC_NewSendMsg(context, ctx.cmdmsg_t, xm.proc); |
| 195 |
|
| 196 |
/* init tables inside message */ |
| 197 |
LC_SetTableSize(xm.msg_h, ctx.undelete_id, xm.nUnDelete); |
| 198 |
|
| 199 |
/* prepare message for sending away */ |
| 200 |
LC_MsgPrepareSend(context, xm.msg_h); |
| 201 |
|
| 202 |
DDD_GID* array = (DDD_GID *)LC_GetPtr(xm.msg_h, ctx.undelete_id); |
| 203 |
memcpy((char *)array, |
| 204 |
(char *)xm.aUnDelete, |
| 205 |
sizeof(DDD_GID)*xm.nUnDelete); |
| 206 |
} |
| 207 |
|
| 208 |
return {nMsgs, std::move(msgs)}; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
static void CmdMsgSend(DDD::DDDContext& context, const CmdmsgList& msgs) |
| 213 |
{ |
| 214 |
for(const auto& msg : msgs) { |
| 215 |
/* schedule message for send */ |
| 216 |
LC_MsgSend(context, msg.msg_h); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
/****************************************************************************/ |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static int CmdMsgUnpack (DDD::DDDContext& context, |
| 227 |
LC_MSGHANDLE *theMsgs, int nRecvMsgs, |
| 228 |
XIDelCmd **itemsDC, int nDC) |
| 229 |
{ |
| 230 |
auto& ctx = context.cmdmsgContext(); |
| 231 |
|
| 232 |
int i, k, jDC, iDC, pos, nPruned; |
| 233 |
|
| 234 |
int lenGidTab = 0; |
| 235 |
for(i=0; i<nRecvMsgs; i++) |
| 236 |
{ |
| 237 |
LC_MSGHANDLE xm = theMsgs[i]; |
| 238 |
lenGidTab += (int)LC_GetTableLen(xm, ctx.undelete_id); |
| 239 |
} |
| 240 |
|
| 241 |
if (lenGidTab==0) |
| 242 |
return(0); |
| 243 |
|
| 244 |
std::vector<DDD_GID> unionGidTab(lenGidTab); |
| 245 |
|
| 246 |
for(i=0, pos=0; i<nRecvMsgs; i++) |
| 247 |
{ |
| 248 |
LC_MSGHANDLE xm = theMsgs[i]; |
| 249 |
int len = LC_GetTableLen(xm, ctx.undelete_id); |
| 250 |
|
| 251 |
if (len>0) |
| 252 |
{ |
| 253 |
memcpy((char *) (unionGidTab.data()+pos), |
| 254 |
(char *) LC_GetPtr(xm, ctx.undelete_id), |
| 255 |
sizeof(DDD_GID) * len); |
| 256 |
pos += len; |
| 257 |
} |
| 258 |
} |
| 259 |
assert(pos==lenGidTab); |
| 260 |
|
| 261 |
/* sort GidTab */ |
| 262 |
std::sort(unionGidTab.begin(), unionGidTab.end()); |
| 263 |
|
| 264 |
#ifdef SUPPORT_RESENT_FLAG |
| 265 |
{ |
| 266 |
int iLCO, nLCO = context.couplingContext().nCpls; |
| 267 |
std::vector<DDD_HDR> localCplObjs = LocalCoupledObjectsList(context); |
| 268 |
|
| 269 |
/* set RESENT flag for objects which will receive another copy */ |
| 270 |
iLCO=0; |
| 271 |
for(k=0; k<lenGidTab; k++) |
| 272 |
{ |
| 273 |
while (iLCO<nLCO && |
| 274 |
(OBJ_GID(localCplObjs[iLCO]) < unionGidTab[k])) |
| 275 |
{ |
| 276 |
SET_OBJ_RESENT(localCplObjs[iLCO], 0); |
| 277 |
|
| 278 |
# if DebugCmdMsg<=0 |
| 279 |
Dune::dvverb << "PruneDelCmds. " << OBJ_GID(localCplObjs[iLCO]) << " without resent1.\n"; |
| 280 |
# endif |
| 281 |
|
| 282 |
iLCO++; |
| 283 |
} |
| 284 |
|
| 285 |
if (iLCO<nLCO && (OBJ_GID(localCplObjs[iLCO]) == unionGidTab[k])) |
| 286 |
{ |
| 287 |
SET_OBJ_RESENT(localCplObjs[iLCO], 1); |
| 288 |
|
| 289 |
# if DebugCmdMsg<=1 |
| 290 |
Dune::dvverb << "PruneDelCmds. " << OBJ_GID(localCplObjs[iLCO]) << " will be resent.\n"; |
| 291 |
# endif |
| 292 |
|
| 293 |
iLCO++; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* reset remaining objects' flags */ |
| 298 |
while (iLCO<nLCO) |
| 299 |
{ |
| 300 |
SET_OBJ_RESENT(localCplObjs[iLCO], 0); |
| 301 |
|
| 302 |
# if DebugCmdMsg<=0 |
| 303 |
Dune::dvverb << "PruneDelCmds. " << OBJ_GID(localCplObjs[iLCO]) << " without resent2.\n"; |
| 304 |
# endif |
| 305 |
|
| 306 |
iLCO++; |
| 307 |
} |
| 308 |
} |
| 309 |
#endif |
| 310 |
|
| 311 |
|
| 312 |
k=0; jDC=0; |
| 313 |
for(iDC=0; iDC<nDC; iDC++) |
| 314 |
{ |
| 315 |
DDD_GID gidDC = OBJ_GID(itemsDC[iDC]->hdr); |
| 316 |
|
| 317 |
while (k<lenGidTab && (unionGidTab[k]<gidDC)) |
| 318 |
k++; |
| 319 |
|
| 320 |
if (k<lenGidTab && (unionGidTab[k]==gidDC)) |
| 321 |
{ |
| 322 |
/* found a DelCmd-item to prune */ |
| 323 |
SET_OBJ_PRUNED(itemsDC[iDC]->hdr, 1); |
| 324 |
|
| 325 |
# if DebugCmdMsg<=1 |
| 326 |
Dune::dvverb << "PruneDelCmds. pruned " << gidDC << "\n"; |
| 327 |
# endif |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
itemsDC[jDC] = itemsDC[iDC]; |
| 332 |
jDC++; |
| 333 |
} |
| 334 |
} |
| 335 |
nPruned = nDC-jDC; |
| 336 |
|
| 337 |
# if DebugCmdMsg<=3 |
| 338 |
Dune::dvverb << "PruneDelCmds. nPruned=" << nPruned << "/" << nDC << "\n"; |
| 339 |
# endif |
| 340 |
|
| 341 |
return(nPruned); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
/****************************************************************************/ |
| 346 |
|
| 347 |
|
| 348 |
static void CmdMsgDisplay(DDD::DDDContext& context, const char *comment, LC_MSGHANDLE xm) |
| 349 |
{ |
| 350 |
auto& ctx = context.cmdmsgContext(); |
| 351 |
|
| 352 |
using std::setw; |
| 353 |
std::ostream& out = std::cout; |
| 354 |
|
| 355 |
DDD_GID *theGid; |
| 356 |
int proc = LC_MsgGetProc(xm); |
| 357 |
int lenGid = (int) LC_GetTableLen(xm, ctx.undelete_id); |
| 358 |
|
| 359 |
std::ostringstream prefixStream; |
| 360 |
prefixStream << setw(3) << context.me() << "-" << comment << setw(3) << proc << " "; |
| 361 |
const std::string& prefix = prefixStream.str(); |
| 362 |
|
| 363 |
/* get table addresses inside message */ |
| 364 |
theGid = (DDD_GID *) LC_GetPtr(xm, ctx.undelete_id); |
| 365 |
|
| 366 |
out << prefix << " 04 Gid.size=" << setw(5) << lenGid << "\n"; |
| 367 |
|
| 368 |
for(int i=0; i<lenGid; i++) |
| 369 |
out << prefix << " 14 gid " << setw(4) << i << " - " |
| 370 |
<< DDD_GID_TO_INT(theGid[i]) << "\n"; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
/****************************************************************************/ |
| 375 |
|
| 376 |
|
| 377 |
/* |
| 378 |
prune superfluous DelCmds. DelCmds are regarded superfluous, |
| 379 |
if there exists another processor which also owns an object copy |
| 380 |
and sends it to destination=me. |
| 381 |
|
| 382 |
this is implemented by sending the GID of all objects with an |
| 383 |
XferCopyObj-command with destination=p to p, iff p does already |
| 384 |
own a copy of the object. after receiving the messages, |
| 385 |
each processor prunes DelCmds for the GIDs in the message. |
| 386 |
|
| 387 |
returns: number of pruned DelCmds. ( < nDC) |
| 388 |
*/ |
| 389 |
|
| 390 |
int PruneXIDelCmd ( |
| 391 |
DDD::DDDContext& context, |
| 392 |
XIDelCmd **itemsDC, int nDC, |
| 393 |
const std::vector<XICopyObj*>& arrayCO) |
| 394 |
{ |
| 395 |
auto& ctx = context.cmdmsgContext(); |
| 396 |
|
| 397 |
/* accumulate messages (one for each partner) */ |
| 398 |
int nSendMsgs; |
| 399 |
CmdmsgList sendMsgs; |
| 400 |
std::tie(nSendMsgs, sendMsgs) = PrepareCmdMsgs(context, arrayCO); |
| 401 |
|
| 402 |
#if DebugCmdMsg>2 |
| 403 |
if (DDD_GetOption(context, OPT_DEBUG_XFERMESGS)==OPT_ON) |
| 404 |
#endif |
| 405 |
{ |
| 406 |
for (const auto& msg : sendMsgs) |
| 407 |
CmdMsgDisplay(context, "PS", msg.msg_h); |
| 408 |
} |
| 409 |
|
| 410 |
/* init communication topology */ |
| 411 |
int nRecvMsgs = LC_Connect(context, ctx.cmdmsg_t); |
| 412 |
|
| 413 |
/* build and send messages */ |
| 414 |
CmdMsgSend(context, sendMsgs); |
| 415 |
|
| 416 |
/* communicate set of messages (send AND receive) */ |
| 417 |
LC_MSGHANDLE* recvMsgs = LC_Communicate(context); |
| 418 |
|
| 419 |
|
| 420 |
int nPruned = CmdMsgUnpack(context, recvMsgs, nRecvMsgs, itemsDC, nDC); |
| 421 |
|
| 422 |
|
| 423 |
/* |
| 424 |
for(int i=0; i<nRecvMsgs; i++) |
| 425 |
{ |
| 426 |
#if DebugCmdMsg>=2 |
| 427 |
if (DDD_GetOption(context, OPT_DEBUG_XFERMESGS)==OPT_ON) |
| 428 |
#endif |
| 429 |
CmdMsgDisplay(context, "PR", recvMsgs[i]); |
| 430 |
} |
| 431 |
*/ |
| 432 |
|
| 433 |
|
| 434 |
/* cleanup low-comm layer */ |
| 435 |
LC_Cleanup(context); |
| 436 |
|
| 437 |
return(nPruned); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
/****************************************************************************/ |
| 442 |
|
| 443 |
END_UGDIM_NAMESPACE |