| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2007-2012, 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 |
//#define MUSIC_DEBUG |
| 20 |
//#define MUSIC_AFTER_RUNTIME_CONSTRUCTOR_REPORT |
| 21 |
#include "music/runtime.hh" |
| 22 |
|
| 23 |
#if MUSIC_USE_MPI |
| 24 |
|
| 25 |
#include <mpi.h> |
| 26 |
|
| 27 |
#include "music/temporal.hh" |
| 28 |
#include "music/error.hh" |
| 29 |
#include "music/connection.hh" |
| 30 |
#include "music/memory.hh" |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
#include <iostream> |
| 34 |
#include <set> |
| 35 |
#include <cassert> |
| 36 |
|
| 37 |
namespace MUSIC |
| 38 |
{ |
| 39 |
|
| 40 |
bool Runtime::isInstantiated_ = false; |
| 41 |
|
| 42 |
|
| 43 |
Runtime::Runtime (Setup* s, double h) |
| 44 |
: mAgent (0) |
| 45 |
{ |
| 46 |
checkInstantiatedOnce (isInstantiated_, "Runtime"); |
| 47 |
s->maybePostponedSetup (); |
| 48 |
|
| 49 |
app_name = s->applicationName (); |
| 50 |
leader_ = s->leader (); |
| 51 |
|
| 52 |
/* remedius |
| 53 |
* new type of subconnectors for collective communication was created. |
| 54 |
*/ |
| 55 |
//CollectiveSubconnectors collectiveSubconnectors; |
| 56 |
// Setup the MUSIC clock |
| 57 |
localTime = Clock (s->timebase (), h); |
| 58 |
|
| 59 |
comm = s->communicator (); |
| 60 |
|
| 61 |
/* remedius |
| 62 |
* copy ports in order to delete them afterwards (it was probably a memory leak in previous revision) |
| 63 |
*/ |
| 64 |
for (std::vector<Port *>::iterator it = s->ports ()->begin (); |
| 65 |
it < s->ports ()->end (); it++) |
| 66 |
ports.push_back ( (*it)); |
| 67 |
|
| 68 |
Connections* connections = s->connections (); |
| 69 |
|
| 70 |
scheduler = new Scheduler (comm, leader_); |
| 71 |
if (s->launchedByMusic ()) |
| 72 |
{ |
| 73 |
takeTickingPorts (s); |
| 74 |
|
| 75 |
// create a total order for connectors and |
| 76 |
// establish connection to peers |
| 77 |
connectToPeers (connections); |
| 78 |
|
| 79 |
// specialize connectors and fill up connectors vector |
| 80 |
specializeConnectors (connections); |
| 81 |
// from here we can start using the vector `connectors' |
| 82 |
// negotiate where to route data and fill up subconnector vectors |
| 83 |
spatialNegotiation (); |
| 84 |
|
| 85 |
// build data routing tables |
| 86 |
buildTables (s); |
| 87 |
takePreCommunicators (); |
| 88 |
takePostCommunicators (); |
| 89 |
// negotiate timing constraints for synchronizers |
| 90 |
temporalNegotiation (s, connections); |
| 91 |
#if 0 |
| 92 |
if (needsMultiCommunication ()) |
| 93 |
{ |
| 94 |
#endif |
| 95 |
sAgents.push_back (mAgent = new MulticommAgent (scheduler)); |
| 96 |
scheduler->setAgent (mAgent); |
| 97 |
#if 0 |
| 98 |
} |
| 99 |
#endif |
| 100 |
sAgents.push_back (new UnicommAgent (scheduler)); |
| 101 |
scheduler->setAgent (sAgents[sAgents.size () - 1]); |
| 102 |
|
| 103 |
// final initialization before simulation starts |
| 104 |
initialize (s); |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
sAgents.push_back (new DummyAgent (scheduler)); |
| 109 |
scheduler->setAgent (sAgents[0]); |
| 110 |
scheduler->initializeAgentState (); |
| 111 |
} |
| 112 |
|
| 113 |
delete s; |
| 114 |
#ifdef MUSIC_AFTER_RUNTIME_CONSTRUCTOR_REPORT |
| 115 |
if (mpi_get_rank (MPI_COMM_WORLD) == 0) |
| 116 |
reportMem (); |
| 117 |
#if 0 |
| 118 |
// Code used for debugging and analysis |
| 119 |
for (std::vector<Connector*>::iterator connector = connectors.begin (); |
| 120 |
connector != connectors.end (); |
| 121 |
++connector) |
| 122 |
(*connector)->report (); |
| 123 |
#endif |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
Runtime::~Runtime () |
| 129 |
{ |
| 130 |
// delete connectors |
| 131 |
for (std::vector<Connector*>::iterator connector = connectors.begin (); |
| 132 |
connector != connectors.end (); ++connector) |
| 133 |
delete *connector; |
| 134 |
#if 0 |
| 135 |
/* MDJ 2012-08-07 |
| 136 |
Some applications assume that they need to delete the ports. |
| 137 |
Until we have made a decision what is the proper usage pattern, |
| 138 |
this code is disabled. */ |
| 139 |
|
| 140 |
// delete ports |
| 141 |
for (std::vector<Port *>::iterator it=ports.begin(); it < ports.end(); it++ ) |
| 142 |
delete (*it); |
| 143 |
#endif |
| 144 |
for (std::vector<SchedulerAgent *>::iterator it = sAgents.begin (); |
| 145 |
it != sAgents.end (); it++) |
| 146 |
delete (*it); |
| 147 |
delete scheduler; |
| 148 |
isInstantiated_ = false; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
bool |
| 153 |
Runtime::needsMultiCommunication () |
| 154 |
{ |
| 155 |
std::vector<Connector*>::iterator c; |
| 156 |
for (c = connectors.begin (); c != connectors.end (); ++c) |
| 157 |
if ((*c)->needsMultiCommunication ()) |
| 158 |
return true; |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
void |
| 163 |
Runtime::takeTickingPorts (Setup* s) |
| 164 |
{ |
| 165 |
std::vector<Port*>::iterator p; |
| 166 |
for (p = ports.begin (); p != ports.end (); ++p) |
| 167 |
{ |
| 168 |
TickingPort* tp = dynamic_cast<TickingPort*> (*p); |
| 169 |
if (tp != NULL) |
| 170 |
tickingPorts.push_back (tp); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
void |
| 176 |
Runtime::takePreCommunicators () |
| 177 |
{ |
| 178 |
std::vector<Connector*>::iterator c; |
| 179 |
for (c = connectors.begin (); c != connectors.end (); ++c) |
| 180 |
{ |
| 181 |
PreCommunicationConnector* preCommunicationConnector = |
| 182 |
dynamic_cast<PreCommunicationConnector*> (*c); |
| 183 |
if (preCommunicationConnector != NULL) |
| 184 |
preCommunication.push_back (preCommunicationConnector); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
void |
| 190 |
Runtime::takePostCommunicators () |
| 191 |
{ |
| 192 |
std::vector<Connector*>::iterator c; |
| 193 |
for (c = connectors.begin (); c != connectors.end (); ++c) |
| 194 |
{ |
| 195 |
PostCommunicationConnector* postCommunicationConnector = |
| 196 |
dynamic_cast<PostCommunicationConnector*> (*c); |
| 197 |
if (postCommunicationConnector != NULL) |
| 198 |
postCommunication.push_back (postCommunicationConnector); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// This predicate gives a total order for connectors which is the |
| 203 |
// same on the sender and receiver sides. It belongs here rather |
| 204 |
// than in connector.hh or connector.cc since it is connected to the |
| 205 |
// connect algorithm. |
| 206 |
bool |
| 207 |
lessConnection (const Connection* cn1, const Connection* cn2) |
| 208 |
{ |
| 209 |
Connector* c1 = cn1->connector (); |
| 210 |
Connector* c2 = cn2->connector (); |
| 211 |
return (c1->receiverAppName () < c2->receiverAppName () |
| 212 |
|| (c1->receiverAppName () == c2->receiverAppName () |
| 213 |
&& c1->receiverPortName () < c2->receiverPortName ())); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
void |
| 218 |
Runtime::connectToPeers (Connections* connections) |
| 219 |
{ |
| 220 |
// This ordering is necessary so that both sender and receiver |
| 221 |
// in each pair sets up communication at the same point in time |
| 222 |
// |
| 223 |
// Note that we don't need to use the dead-lock scheme used in |
| 224 |
// build_schedule () here. |
| 225 |
// |
| 226 |
sort (connections->begin (), connections->end (), lessConnection); |
| 227 |
|
| 228 |
for (Connections::iterator c = connections->begin (); |
| 229 |
c != connections->end (); ++c) |
| 230 |
(*c)->connector ()->createIntercomm (); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
void |
| 235 |
Runtime::specializeConnectors (Connections* connections) |
| 236 |
{ |
| 237 |
for (Connections::iterator c = connections->begin (); |
| 238 |
c != connections->end (); ++c) |
| 239 |
{ |
| 240 |
Connector *connector = (*c)->connector (); |
| 241 |
connector->specialize (localTime); |
| 242 |
//(*c)->setConnector (connector); |
| 243 |
connectors.push_back (connector); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
void |
| 249 |
Runtime::spatialNegotiation () |
| 250 |
{ |
| 251 |
// Let each connector pair setup their inter-communicators |
| 252 |
// and create all required subconnectors. |
| 253 |
unsigned int multiId = 0; |
| 254 |
for (std::vector<Connector*>::iterator c = connectors.begin (); |
| 255 |
c != connectors.end (); ++c) |
| 256 |
{ |
| 257 |
// negotiate and fill up vectors passed as arguments |
| 258 |
(*c)->spatialNegotiation (); |
| 259 |
multiId |= (*c)->idFlag (); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
void |
| 265 |
Runtime::buildTables (Setup* s) |
| 266 |
{ |
| 267 |
for (std::vector<Port*>::iterator p = ports.begin (); p != ports.end (); |
| 268 |
++p) |
| 269 |
(*p)->buildTable (); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
void |
| 274 |
Runtime::temporalNegotiation (Setup* s, Connections* connections) |
| 275 |
{ |
| 276 |
// Temporal negotiation is done globally by a serial algorithm |
| 277 |
// which yields the same result in each process |
| 278 |
s->temporalNegotiator ()->negotiate (localTime, connections); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
MPI_Comm |
| 283 |
Runtime::communicator () |
| 284 |
{ |
| 285 |
return comm; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
void |
| 290 |
Runtime::initialize (Setup* s) |
| 291 |
{ |
| 292 |
scheduler->initialize (s->temporalNegotiator ()->applicationGraph (), |
| 293 |
connectors); |
| 294 |
#if 0 |
| 295 |
if (mAgent) |
| 296 |
mAgent->initialize (localTime, comm, leader_, connectors); |
| 297 |
#endif |
| 298 |
// scheduler->nextCommunication (localTime, schedule); |
| 299 |
scheduler->tick (localTime); |
| 300 |
// compensate for first localTime.tick () in Runtime::tick () |
| 301 |
localTime.ticks (-1); |
| 302 |
// the time zero tick () (where we may or may not communicate) |
| 303 |
tick (); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
void |
| 308 |
Runtime::finalize () |
| 309 |
{ |
| 310 |
#if 1 |
| 311 |
scheduler->finalize (localTime, connectors); |
| 312 |
#else |
| 313 |
/* remedius |
| 314 |
* set of receiver port codes that still has to be finalized |
| 315 |
*/ |
| 316 |
std::set<int> cnn_ports; |
| 317 |
for (std::vector<Connector*>::iterator c = connectors.begin (); |
| 318 |
c != connectors.end (); |
| 319 |
++c) |
| 320 |
cnn_ports.insert ((*c)->receiverPortCode ()); |
| 321 |
|
| 322 |
/* |
| 323 |
* finalize communication |
| 324 |
* |
| 325 |
* For ordinary connectors, Connector::finalize () is called |
| 326 |
* repeatedly until Connector::isFinalized () returns true. |
| 327 |
* |
| 328 |
* For connectors participating in multiConnectors, |
| 329 |
* Connector::finalize () is called, followed by a multi-tick |
| 330 |
* until Connector::isFInalized () returns true. |
| 331 |
*/ |
| 332 |
|
| 333 |
std::vector<Connector*> cCache; |
| 334 |
|
| 335 |
if (!schedule.empty ()) |
| 336 |
do |
| 337 |
{ |
| 338 |
std::vector<std::pair<double, Connector*> >::iterator comm; |
| 339 |
for (comm = schedule.begin (); comm != schedule.end (); ++comm) |
| 340 |
{ |
| 341 |
Connector* connector = comm->second; |
| 342 |
if (connector == NULL |
| 343 |
|| (cnn_ports.find (connector->receiverPortCode ()) |
| 344 |
== cnn_ports.end ())) |
| 345 |
continue; // already finalized |
| 346 |
|
| 347 |
if (!connector->idFlag ()) |
| 348 |
{ |
| 349 |
if (connector->isFinalized ()) |
| 350 |
// an output port was finalized in tick () |
| 351 |
{ |
| 352 |
cnn_ports.erase (connector->receiverPortCode ()); |
| 353 |
continue; |
| 354 |
} |
| 355 |
// finalize () needs to come after isFinalized check |
| 356 |
// since it can itself set finalized state |
| 357 |
connector->finalize (); |
| 358 |
continue; |
| 359 |
} |
| 360 |
|
| 361 |
// all multiconnector code should go into scheduler |
| 362 |
// this is a temporary solution |
| 363 |
int remoteLeader = connector->remoteLeader (); |
| 364 |
bool direction = connector->isInput (); |
| 365 |
unsigned int multiId = connector->idFlag (); |
| 366 |
cCache.clear (); |
| 367 |
cCache.push_back (connector); |
| 368 |
// yes, horrible quadratic algorithm for now |
| 369 |
for (std::vector< std::pair<double, Connector*> >::iterator c |
| 370 |
= comm + 1; |
| 371 |
c != schedule.end (); |
| 372 |
++c) |
| 373 |
{ |
| 374 |
if (c->second != NULL |
| 375 |
&& c->second->idFlag () |
| 376 |
&& c->second->remoteLeader () == remoteLeader |
| 377 |
&& c->second->isInput () == direction) |
| 378 |
{ |
| 379 |
cCache.push_back (c->second); |
| 380 |
multiId |= c->second->idFlag (); |
| 381 |
c->second = NULL; // taken |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
bool isFinalized = true; |
| 386 |
for (std::vector<Connector*>::iterator c = cCache.begin (); |
| 387 |
c != cCache.end (); |
| 388 |
++c) |
| 389 |
{ |
| 390 |
if (!(*c)->isFinalized ()) |
| 391 |
isFinalized = false; |
| 392 |
} |
| 393 |
|
| 394 |
if (isFinalized) |
| 395 |
{ |
| 396 |
for (std::vector<Connector*>::iterator c = cCache.begin (); |
| 397 |
c != cCache.end (); |
| 398 |
++c) |
| 399 |
cnn_ports.erase ((*c)->receiverPortCode ()); |
| 400 |
continue; |
| 401 |
} |
| 402 |
|
| 403 |
for (std::vector<Connector*>::iterator c = cCache.begin (); |
| 404 |
c != cCache.end (); |
| 405 |
++c) |
| 406 |
if (!(*c)->isFinalized ()) |
| 407 |
(*c)->finalize (); |
| 408 |
|
| 409 |
if (multiConnectors[multiId] == NULL) |
| 410 |
{ |
| 411 |
std::cout << "Rank " << mpi_get_rank (MPI_COMM_WORLD) |
| 412 |
<< " aborting on multiId " << multiId |
| 413 |
<< " remote leader = " << connector->remoteLeader () |
| 414 |
<< std::endl; |
| 415 |
assert (0); |
| 416 |
} |
| 417 |
multiConnectors[multiId]->tick (); |
| 418 |
//cCache.clear (); |
| 419 |
} |
| 420 |
|
| 421 |
schedule.clear (); |
| 422 |
scheduler->nextCommunication (localTime, schedule); |
| 423 |
//scheduler->tick(localTime); |
| 424 |
for (std::vector<PostCommunicationConnector*>::iterator c = |
| 425 |
postCommunication.begin (); |
| 426 |
c != postCommunication.end (); |
| 427 |
++c) |
| 428 |
(*c)->postCommunication (); |
| 429 |
} |
| 430 |
while (!cnn_ports.empty ()); |
| 431 |
#endif |
| 432 |
|
| 433 |
#if defined (OPEN_MPI) && MPI_VERSION <= 2 |
| 434 |
// This is needed in OpenMPI version <= 1.2 for the freeing of the |
| 435 |
// intercommunicators to go well |
| 436 |
MPI_Barrier (MPI_COMM_WORLD); |
| 437 |
#endif |
| 438 |
for (std::vector<Connector*>::iterator connector = connectors.begin (); |
| 439 |
connector != connectors.end (); ++connector) |
| 440 |
(*connector)->freeIntercomm (); |
| 441 |
|
| 442 |
MPI_Finalize (); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
void |
| 447 |
Runtime::tick () |
| 448 |
{ |
| 449 |
|
| 450 |
// Update local time |
| 451 |
localTime.tick (); |
| 452 |
// ContPorts do some per-tick initialization here |
| 453 |
std::vector<TickingPort*>::iterator p; |
| 454 |
for (p = tickingPorts.begin (); p != tickingPorts.end (); ++p) |
| 455 |
(*p)->tick (); |
| 456 |
// ContOutputConnectors sample data |
| 457 |
for (std::vector<PreCommunicationConnector*>::iterator c = |
| 458 |
preCommunication.begin (); c != preCommunication.end (); ++c) |
| 459 |
(*c)->preCommunication (); |
| 460 |
|
| 461 |
#if 1 |
| 462 |
scheduler->tick (localTime); |
| 463 |
#else |
| 464 |
if (!schedule.empty ()) |
| 465 |
while (schedule[0].first <= localTime.time()) |
| 466 |
{ |
| 467 |
std::vector< std::pair<double, Connector*> >::iterator comm; |
| 468 |
for (comm = schedule.begin (); |
| 469 |
comm != schedule.end() && comm->first <= localTime.time (); |
| 470 |
++comm) |
| 471 |
{ |
| 472 |
if (comm->second == NULL || comm->second->isFinalized ()) |
| 473 |
continue; |
| 474 |
|
| 475 |
Connector* connector = comm->second; |
| 476 |
|
| 477 |
if (!connector->idFlag ()) |
| 478 |
{ |
| 479 |
// here standard connectors are handled |
| 480 |
connector->tick (); |
| 481 |
continue; |
| 482 |
} |
| 483 |
|
| 484 |
// all multiconnector code should go into scheduler |
| 485 |
// this is a temporary solution |
| 486 |
int remoteLeader = connector->remoteLeader (); |
| 487 |
bool direction = connector->isInput (); |
| 488 |
unsigned int multiId = connector->idFlag (); |
| 489 |
//cCache.push_back (connector); |
| 490 |
// yes, horrible quadratic algorithm for now |
| 491 |
for (std::vector< std::pair<double, Connector*> >::iterator c |
| 492 |
= comm + 1; |
| 493 |
c != schedule.end () && c->first <= localTime.time(); |
| 494 |
++c) |
| 495 |
{ |
| 496 |
if (c->second != NULL |
| 497 |
&& c->second->idFlag () |
| 498 |
&& c->second->remoteLeader () == remoteLeader |
| 499 |
&& c->second->isInput () == direction) |
| 500 |
{ |
| 501 |
//cCache.push_back (c->second); |
| 502 |
multiId |= c->second->idFlag (); |
| 503 |
c->second = NULL;// taken |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
//if (mpi_get_rank (MPI_COMM_WORLD) == 2) |
| 508 |
// std::cout << "multiId = " << multiId << std::endl; |
| 509 |
if (multiConnectors[multiId] == NULL) |
| 510 |
{ |
| 511 |
assert (0); |
| 512 |
} |
| 513 |
multiConnectors[multiId]->tick (); |
| 514 |
//cCache.clear (); |
| 515 |
} |
| 516 |
schedule.erase (schedule.begin (), comm); |
| 517 |
scheduler->nextCommunication (localTime, schedule); |
| 518 |
}*/ |
| 519 |
#endif |
| 520 |
|
| 521 |
// ContInputConnectors write data to application here |
| 522 |
for (std::vector<PostCommunicationConnector*>::iterator c = |
| 523 |
postCommunication.begin (); c != postCommunication.end (); ++c) |
| 524 |
(*c)->postCommunication (); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
double |
| 529 |
Runtime::time () |
| 530 |
{ |
| 531 |
return localTime.time (); |
| 532 |
} |
| 533 |
|
| 534 |
} |
| 535 |
#endif |