| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2008, 2009, 2022 INCF |
| 4 |
* |
| 5 |
* MUSIC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* MUSIC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "music/temporal.hh" // Must be included first on BG/L |
| 20 |
//#define MUSIC_DEBUG |
| 21 |
|
| 22 |
#if MUSIC_USE_MPI |
| 23 |
|
| 24 |
#include "music/setup.hh" |
| 25 |
|
| 26 |
namespace MUSIC |
| 27 |
{ |
| 28 |
|
| 29 |
TemporalNegotiator::TemporalNegotiator (Setup* setup) : |
| 30 |
setup_ (setup), negotiationBuffer (NULL) |
| 31 |
{ |
| 32 |
nApplications_ = setup_->applicationMap ()->size (); |
| 33 |
nAllConnections = 0; |
| 34 |
nodes = new TemporalNegotiatorGraph (setup_->timebase (), nApplications_, |
| 35 |
setup_->applicationColor ()); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
TemporalNegotiator::~TemporalNegotiator () |
| 40 |
{ |
| 41 |
delete nodes; |
| 42 |
freeNegotiationData (negotiationBuffer); |
| 43 |
|
| 44 |
// Free negotiation communicator in application leaders |
| 45 |
if (negotiationComm != MPI_COMM_NULL) |
| 46 |
MPI_Comm_free (&negotiationComm); |
| 47 |
|
| 48 |
MPI_Group_free (&applicationLeaders); |
| 49 |
MPI_Group_free (&groupWorld); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void |
| 54 |
TemporalNegotiator::separateConnections ( |
| 55 |
std::vector<Connection*>* connections) |
| 56 |
{ |
| 57 |
for (std::vector<Connection*>::iterator c = connections->begin (); |
| 58 |
c != connections->end (); ++c) |
| 59 |
{ |
| 60 |
if (dynamic_cast<OutputConnection*> (*c) != NULL) |
| 61 |
outputConnections.push_back (*dynamic_cast<OutputConnection*> (*c)); |
| 62 |
else |
| 63 |
inputConnections.push_back (*dynamic_cast<InputConnection*> (*c)); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
TemporalNegotiatorGraph* |
| 69 |
TemporalNegotiator::applicationGraph () |
| 70 |
{ |
| 71 |
return nodes; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
bool |
| 76 |
TemporalNegotiator::isLeader () |
| 77 |
{ |
| 78 |
return mpi_get_rank (setup_->communicator ()) == 0; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
bool |
| 83 |
TemporalNegotiator::hasPeers () |
| 84 |
{ |
| 85 |
return mpi_get_comm_size (setup_->communicator ()) > 1; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
void |
| 90 |
TemporalNegotiator::createNegotiationCommunicator () |
| 91 |
{ |
| 92 |
ApplicationMap* applicationMap = setup_->applicationMap (); |
| 93 |
int* ranks = new int[nApplications_]; |
| 94 |
|
| 95 |
for (int i = 0; i < nApplications_; ++i) |
| 96 |
ranks[i] = (*applicationMap)[i].leader (); |
| 97 |
|
| 98 |
MPI_Comm_group (MPI_COMM_WORLD, &groupWorld); |
| 99 |
MPI_Group_incl (groupWorld, nApplications_, ranks, &applicationLeaders); |
| 100 |
delete[] ranks; |
| 101 |
MPI_Comm_create (MPI_COMM_WORLD, applicationLeaders, &negotiationComm); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
int |
| 106 |
TemporalNegotiator::negotiationDataSize (int nConnections) |
| 107 |
{ |
| 108 |
return negotiationDataSize (1, nConnections); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
int |
| 113 |
TemporalNegotiator::negotiationDataSize (int nBlocks, int nConnections) |
| 114 |
{ |
| 115 |
// -1 due to definition of connection member |
| 116 |
return (nBlocks * sizeof(TemporalNegotiationData) |
| 117 |
+ (nConnections - 1) * sizeof(ConnectionDescriptor)); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
TemporalNegotiationData* |
| 122 |
TemporalNegotiator::allocNegotiationData (int nBlocks, int nConnections) |
| 123 |
{ |
| 124 |
void* memory = new char[negotiationDataSize (nBlocks, nConnections)]; |
| 125 |
return static_cast<TemporalNegotiationData*> (memory); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void |
| 130 |
TemporalNegotiator::freeNegotiationData (TemporalNegotiationData* data) |
| 131 |
{ |
| 132 |
if (data != NULL) |
| 133 |
{ |
| 134 |
delete[] static_cast<char*> (static_cast<void*> (data)); |
| 135 |
data = NULL; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
int |
| 141 |
TemporalNegotiator::computeDefaultMaxBuffered (int maxLocalWidth, |
| 142 |
int eventSize, ClockState tickInterval, double timebase) |
| 143 |
{ |
| 144 |
int res; |
| 145 |
|
| 146 |
// Do different calculation depending on the kind of port |
| 147 |
|
| 148 |
// NOTE: We should not base this choice eventSize but instead on |
| 149 |
// proper type information |
| 150 |
|
| 151 |
if (eventSize == 0) |
| 152 |
// continuous data |
| 153 |
res = DEFAULT_PACKET_SIZE / maxLocalWidth; |
| 154 |
else if (eventSize == 1) |
| 155 |
// message data |
| 156 |
res = DEFAULT_MESSAGE_MAX_BUFFERED; |
| 157 |
else |
| 158 |
// event data |
| 159 |
res = (DEFAULT_PACKET_SIZE |
| 160 |
/ (EVENT_FREQUENCY_ESTIMATE * maxLocalWidth * eventSize * timebase |
| 161 |
* tickInterval)); |
| 162 |
if (res < 1) |
| 163 |
res = 1; |
| 164 |
return res; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
int |
| 169 |
TemporalNegotiator::findNodeColor (int leader) |
| 170 |
{ |
| 171 |
int color = -1; |
| 172 |
ApplicationMap* applicationMap = setup_->applicationMap (); |
| 173 |
for (int i = 0; i < nApplications_; ++i) |
| 174 |
if (leader == (*applicationMap)[i].leader ()) |
| 175 |
color = (*applicationMap)[i].color (); |
| 176 |
assert (color != -1); |
| 177 |
return color; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void |
| 182 |
TemporalNegotiator::collectNegotiationData (ClockState ti) |
| 183 |
{ |
| 184 |
int nOut = outputConnections.size (); |
| 185 |
int nIn = inputConnections.size (); |
| 186 |
nLocalConnections = nOut + nIn; |
| 187 |
MUSIC_LOG ("nLocalConnections = " << nLocalConnections |
| 188 |
<< ", nOut = " << nOut |
| 189 |
<< ", nIn = " << nIn); |
| 190 |
negotiationData = allocNegotiationData (1, nLocalConnections); |
| 191 |
negotiationData->timebase = setup_->timebase (); |
| 192 |
negotiationData->tickInterval = ti; |
| 193 |
negotiationData->color = setup_->applicationColor (); |
| 194 |
negotiationData->leader = setup_->leader (); |
| 195 |
negotiationData->nProcs = setup_->nProcs (); |
| 196 |
negotiationData->nOutConnections = outputConnections.size (); |
| 197 |
negotiationData->nInConnections = inputConnections.size (); |
| 198 |
|
| 199 |
for (int i = 0; i < nOut; ++i) |
| 200 |
{ |
| 201 |
Connector* connector = outputConnections[i].connector (); |
| 202 |
int remote = connector->remoteLeader (); |
| 203 |
negotiationData->connection[i].remoteNode = findNodeColor (remote); |
| 204 |
negotiationData->connection[i].receiverPort = |
| 205 |
connector->receiverPortCode (); |
| 206 |
negotiationData->connection[i].multiComm = connector->idFlag (); |
| 207 |
negotiationData->connection[i].maxBuffered = |
| 208 |
outputConnections[i].maxBuffered (); |
| 209 |
negotiationData->connection[i].defaultMaxBuffered = |
| 210 |
computeDefaultMaxBuffered (connector->maxLocalWidth (), |
| 211 |
outputConnections[i].elementSize (), ti, setup_->timebase ()); |
| 212 |
|
| 213 |
negotiationData->connection[i].accLatency = 0; |
| 214 |
} |
| 215 |
for (int i = 0; i < nIn; ++i) |
| 216 |
{ |
| 217 |
int remote = inputConnections[i].connector ()->remoteLeader (); |
| 218 |
negotiationData->connection[nOut + i].remoteNode = findNodeColor ( |
| 219 |
remote); |
| 220 |
negotiationData->connection[nOut + i].receiverPort = |
| 221 |
inputConnections[i].connector ()->receiverPortCode (); |
| 222 |
negotiationData->connection[nOut + i].multiComm = |
| 223 |
inputConnections[i].connector ()->idFlag (); |
| 224 |
negotiationData->connection[nOut + i].maxBuffered = |
| 225 |
inputConnections[i].maxBuffered (); |
| 226 |
negotiationData->connection[nOut + i].defaultMaxBuffered = 0; |
| 227 |
|
| 228 |
negotiationData->connection[nOut + i].accLatency = |
| 229 |
inputConnections[i].accLatency (); |
| 230 |
MUSIC_LOGR ("port " << inputConnections[i].connector ()->receiverPortName () << ": " << inputConnections[i].accLatency ()); |
| 231 |
negotiationData->connection[nOut + i].interpolate = |
| 232 |
inputConnections[i].interpolate (); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
void |
| 238 |
TemporalNegotiator::fillTemporalNegotiatorGraph () |
| 239 |
{ |
| 240 |
nodes->setNConnections (nAllConnections / 2); |
| 241 |
char* memory = static_cast<char*> (static_cast<void*> (negotiationBuffer)); |
| 242 |
TemporalNegotiationData* data; |
| 243 |
for (int i = 0; i < nApplications_; ++i) |
| 244 |
{ |
| 245 |
|
| 246 |
data = |
| 247 |
static_cast<TemporalNegotiationData*> (static_cast<void*> (memory)); |
| 248 |
int nOut = data->nOutConnections; |
| 249 |
int nIn = data->nInConnections; |
| 250 |
nodes->addNode ( |
| 251 |
ANode<TemporalNegotiationData, ConnectionDescriptor> (nOut, nIn, |
| 252 |
*data), data->color); |
| 253 |
memory += negotiationDataSize (nOut + nIn); |
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
void |
| 260 |
TemporalNegotiator::communicateNegotiationData () |
| 261 |
{ |
| 262 |
// First talk to others about how many connections each node has |
| 263 |
int* nConnections = new int[nApplications_]; |
| 264 |
MPI_Allgather (&nLocalConnections, 1, MPI_INT, nConnections, 1, |
| 265 |
MPI_INT, negotiationComm); |
| 266 |
for (int i = 0; i < nApplications_; ++i) |
| 267 |
nAllConnections += nConnections[i]; |
| 268 |
negotiationBuffer = allocNegotiationData (nApplications_, nAllConnections); |
| 269 |
int* receiveSizes = new int[nApplications_]; |
| 270 |
int* displacements = new int[nApplications_]; |
| 271 |
int displacement = 0; |
| 272 |
for (int i = 0; i < nApplications_; ++i) |
| 273 |
{ |
| 274 |
int receiveSize = negotiationDataSize (nConnections[i]); |
| 275 |
receiveSizes[i] = receiveSize; |
| 276 |
displacements[i] = displacement; |
| 277 |
|
| 278 |
displacement += receiveSize; |
| 279 |
} |
| 280 |
delete[] nConnections; |
| 281 |
int sendSize = negotiationDataSize (nLocalConnections); |
| 282 |
MPI_Allgatherv (negotiationData, sendSize, MPI_BYTE, |
| 283 |
negotiationBuffer, receiveSizes, displacements, MPI_BYTE, |
| 284 |
negotiationComm); |
| 285 |
delete[] displacements; |
| 286 |
delete[] receiveSizes; |
| 287 |
freeNegotiationData (negotiationData); |
| 288 |
fillTemporalNegotiatorGraph (); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
ConnectionDescriptor* |
| 293 |
TemporalNegotiator::findInputConnection (int node, int port) |
| 294 |
{ |
| 295 |
int nOut = nodes->at (node).data ().nOutConnections; |
| 296 |
// Get address of first input ConnectionDescriptor |
| 297 |
ConnectionDescriptor* inputDescriptors = |
| 298 |
&nodes->at (node).data ().connection[nOut]; |
| 299 |
for (int i = 0; i < nodes->at (node).data ().nInConnections; ++i) |
| 300 |
if (inputDescriptors[i].receiverPort == port) |
| 301 |
return &inputDescriptors[i]; |
| 302 |
error ("internal error in TemporalNegotiator::findInputConnection"); |
| 303 |
return 0; // never reached |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
void |
| 308 |
TemporalNegotiator::combineParameters () |
| 309 |
{ |
| 310 |
double timebase = nodes->at (0).data ().timebase; |
| 311 |
|
| 312 |
for (int o = 0; o < nApplications_; ++o) |
| 313 |
{ |
| 314 |
// check timebase |
| 315 |
if (nodes->at (o).data ().timebase != timebase) |
| 316 |
error0 ("applications don't use same timebase"); |
| 317 |
|
| 318 |
for (int c = 0; c < nodes->at (o).data ().nOutConnections; ++c) |
| 319 |
{ |
| 320 |
ConnectionDescriptor* out = &nodes->at (o).data ().connection[c]; |
| 321 |
int i = out->remoteNode; |
| 322 |
ConnectionDescriptor* in = findInputConnection (i, |
| 323 |
out->receiverPort); |
| 324 |
|
| 325 |
// maxBuffered |
| 326 |
|
| 327 |
// check defaults |
| 328 |
if (out->maxBuffered == MAX_BUFFERED_NO_VALUE |
| 329 |
&& in->maxBuffered == MAX_BUFFERED_NO_VALUE) |
| 330 |
out->maxBuffered = out->defaultMaxBuffered; |
| 331 |
else if (in->maxBuffered != MAX_BUFFERED_NO_VALUE) |
| 332 |
{ |
| 333 |
// convert to sender side ticks |
| 334 |
ClockState inMaxBufferedTime = in->maxBuffered |
| 335 |
* nodes->at (i).data ().tickInterval; |
| 336 |
int inMaxBuffered = (inMaxBufferedTime |
| 337 |
/ nodes->at (o).data ().tickInterval); |
| 338 |
// take min maxBuffered |
| 339 |
if (out->maxBuffered == MAX_BUFFERED_NO_VALUE |
| 340 |
|| inMaxBuffered < out->maxBuffered) |
| 341 |
out->maxBuffered = inMaxBuffered; |
| 342 |
} |
| 343 |
// store maxBuffered in sender units |
| 344 |
in->maxBuffered = out->maxBuffered; |
| 345 |
MUSIC_LOG0("Max Buffered:"<<in->maxBuffered); |
| 346 |
|
| 347 |
// accLatency |
| 348 |
out->accLatency = in->accLatency; |
| 349 |
|
| 350 |
// interpolate |
| 351 |
out->interpolate = in->interpolate; |
| 352 |
|
| 353 |
// remoteTickInterval |
| 354 |
out->remoteTickInterval = nodes->at (i).data ().tickInterval; |
| 355 |
in->remoteTickInterval = nodes->at (o).data ().tickInterval; |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
void |
| 362 |
TemporalNegotiator::distributeParameters () |
| 363 |
{ |
| 364 |
for (int o = 0; o < nApplications_; ++o) |
| 365 |
{ |
| 366 |
for (int c = 0; c < nodes->at (o).data ().nOutConnections; ++c) |
| 367 |
{ |
| 368 |
ConnectionDescriptor* out = &nodes->at (o).data ().connection[c]; |
| 369 |
int i = out->remoteNode; |
| 370 |
ConnectionDescriptor* in = findInputConnection (i, |
| 371 |
out->receiverPort); |
| 372 |
|
| 373 |
// store maxBuffered in sender units |
| 374 |
in->maxBuffered = out->maxBuffered; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
void |
| 381 |
TemporalNegotiator::loopAlgorithm () |
| 382 |
{ |
| 383 |
std::vector<AEdge<TemporalNegotiationData, ConnectionDescriptor> > path; |
| 384 |
TemporalNegotiatorGraph::iterator it; |
| 385 |
for (it = nodes->begin (); it < nodes->end (); ++it) |
| 386 |
if (!it->visited) |
| 387 |
nodes->depthFirst (*it, path); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
void |
| 392 |
TemporalNegotiator::broadcastNegotiationData () |
| 393 |
{ |
| 394 |
MPI_Comm comm = setup_->communicator (); |
| 395 |
|
| 396 |
if (hasPeers ()) |
| 397 |
{ |
| 398 |
MPI_Bcast (&nAllConnections, 1, MPI_INT, 0, comm); |
| 399 |
char* memory = |
| 400 |
static_cast<char*> (static_cast<void*> (negotiationBuffer)); |
| 401 |
MPI_Bcast (memory, negotiationDataSize (nApplications_, nAllConnections), MPI_BYTE, |
| 402 |
0, comm); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
void |
| 408 |
TemporalNegotiator::receiveNegotiationData () |
| 409 |
{ |
| 410 |
MPI_Comm comm = setup_->communicator (); |
| 411 |
MPI_Bcast (&nAllConnections, 1, MPI_INT, 0, comm); |
| 412 |
negotiationBuffer = allocNegotiationData (nApplications_, nAllConnections); |
| 413 |
char* memory = static_cast<char*> (static_cast<void*> (negotiationBuffer)); |
| 414 |
MPI_Bcast (memory, negotiationDataSize (nApplications_, nAllConnections), |
| 415 |
MPI_BYTE, 0, |
| 416 |
comm); |
| 417 |
fillTemporalNegotiatorGraph (); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
void |
| 422 |
TemporalNegotiator::negotiate (Clock& localTime, |
| 423 |
std::vector<Connection*>* connections) |
| 424 |
{ |
| 425 |
separateConnections (connections); |
| 426 |
|
| 427 |
//MUSIC_LOGR (printconns ("NOut: ", outputConnections)); |
| 428 |
//MUSIC_LOGR (printconns ("NIn: ", inputConnections)); |
| 429 |
createNegotiationCommunicator (); |
| 430 |
|
| 431 |
if (isLeader ()) |
| 432 |
{ |
| 433 |
collectNegotiationData (localTime.tickInterval ()); |
| 434 |
communicateNegotiationData (); |
| 435 |
combineParameters (); |
| 436 |
loopAlgorithm (); |
| 437 |
distributeParameters (); |
| 438 |
broadcastNegotiationData (); |
| 439 |
} |
| 440 |
else |
| 441 |
receiveNegotiationData (); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
void |
| 446 |
TemporalNegotiatorGraph::handleLoop ( |
| 447 |
ANode<TemporalNegotiationData, ConnectionDescriptor> &x, |
| 448 |
std::vector<AEdge<TemporalNegotiationData, ConnectionDescriptor> > & path) |
| 449 |
{ |
| 450 |
// Search path to detect beginning of loop |
| 451 |
int loop; |
| 452 |
for (loop = 0; &path[loop].pre () != &x; ++loop) |
| 453 |
; |
| 454 |
// Compute how much headroom we have for buffering |
| 455 |
ClockState totalDelay = 0; |
| 456 |
for (unsigned int c = loop; c < path.size (); ++c) |
| 457 |
{ |
| 458 |
|
| 459 |
MUSIC_LOGR ("latency = " << path[c].data().accLatency << ", ti = " |
| 460 |
<< path[c].pre ().data().tickInterval); |
| 461 |
totalDelay += path[c].data ().accLatency |
| 462 |
- path[c].pre ().data ().tickInterval; |
| 463 |
} |
| 464 |
|
| 465 |
// If negative we will not be able to make it in time |
| 466 |
// around the loop even without any extra buffering |
| 467 |
if (totalDelay < 0) |
| 468 |
{ |
| 469 |
std::ostringstream ostr; |
| 470 |
ostr << "too short latency (" << -timebase_ * totalDelay |
| 471 |
<< " s) around loop: "; // << path[loop].pre().name(); |
| 472 |
for (unsigned int c = loop + 1; c < path.size (); ++c) |
| 473 |
ostr << ", "; // << path[c].pre().name(); |
| 474 |
error0 (ostr.str ()); |
| 475 |
} |
| 476 |
|
| 477 |
// Distribute totalDelay as allowed buffering uniformly over loop |
| 478 |
// (we could do better by considering constraints form other loops) |
| 479 |
int loopLength = path.size () - loop; |
| 480 |
ClockState bufDelay = totalDelay / loopLength; |
| 481 |
for (unsigned int c = loop; c < path.size (); ++c) |
| 482 |
{ |
| 483 |
int allowedTicks = bufDelay / path[c].pre ().data ().tickInterval; |
| 484 |
path[c].data ().maxBuffered = std::min (path[c].data ().maxBuffered, |
| 485 |
allowedTicks); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
void |
| 491 |
TemporalNegotiatorGraph::setNConnections (int nConns) |
| 492 |
{ |
| 493 |
nConnections_ = nConns; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
int |
| 498 |
TemporalNegotiatorGraph::getNConnections () |
| 499 |
{ |
| 500 |
return nConnections_; |
| 501 |
} |
| 502 |
} |
| 503 |
#endif |