| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2008, 2009, 2010, 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/subconnector.hh" |
| 20 |
|
| 21 |
#include "music/debug.hh" |
| 22 |
|
| 23 |
#if MUSIC_USE_MPI |
| 24 |
|
| 25 |
#include "music/communication.hh" |
| 26 |
#include "music/event_router.hh" |
| 27 |
|
| 28 |
#include <cstring> |
| 29 |
#include <algorithm> |
| 30 |
#include <iostream> |
| 31 |
#include <iterator> |
| 32 |
#ifdef MUSIC_DEBUG |
| 33 |
#include <cstdlib> |
| 34 |
#endif |
| 35 |
|
| 36 |
namespace MUSIC { |
| 37 |
|
| 38 |
Subconnector::Subconnector (MPI_Datatype type, |
| 39 |
MPI_Comm intercomm_, |
| 40 |
int remoteLeader, |
| 41 |
int remoteRank, |
| 42 |
int receiverRank, |
| 43 |
int receiverPortCode) |
| 44 |
: type_ (type), |
| 45 |
intercomm (intercomm_), |
| 46 |
remoteRank_ (remoteRank), |
| 47 |
remoteWorldRank_ (remoteLeader + remoteRank), |
| 48 |
receiverRank_ (receiverRank), |
| 49 |
receiverPortCode_ (receiverPortCode), |
| 50 |
flushed(false) |
| 51 |
{ |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
Subconnector::~Subconnector () |
| 56 |
{ |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
BufferingOutputSubconnector::BufferingOutputSubconnector (int elementSize) |
| 61 |
: buffer_ (elementSize) |
| 62 |
{ |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/******************************************************************** |
| 67 |
* |
| 68 |
* Cont Subconnectors |
| 69 |
* |
| 70 |
********************************************************************/ |
| 71 |
|
| 72 |
ContOutputSubconnector::ContOutputSubconnector (//Synchronizer* synch_, |
| 73 |
MPI_Comm intercomm_, |
| 74 |
int remoteLeader, |
| 75 |
int remoteRank, |
| 76 |
int receiverPortCode_, |
| 77 |
MPI_Datatype type) |
| 78 |
: Subconnector (type, |
| 79 |
intercomm_, |
| 80 |
remoteLeader, |
| 81 |
remoteRank, |
| 82 |
remoteRank, |
| 83 |
receiverPortCode_), |
| 84 |
BufferingOutputSubconnector (0) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
void |
| 90 |
ContOutputSubconnector::initialCommunication (double param) |
| 91 |
{ |
| 92 |
send (); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void |
| 97 |
ContOutputSubconnector::maybeCommunicate () |
| 98 |
{ |
| 99 |
if(!flushed) |
| 100 |
send (); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
void |
| 105 |
ContOutputSubconnector::send () |
| 106 |
{ |
| 107 |
|
| 108 |
void* data; |
| 109 |
int size; |
| 110 |
buffer_.nextBlock (data, size); |
| 111 |
// NOTE: marshalling |
| 112 |
char* buffer = static_cast <char*> (data); |
| 113 |
|
| 114 |
while (size >= CONT_BUFFER_MAX) |
| 115 |
{ |
| 116 |
|
| 117 |
MUSIC_LOGR ("Sending to rank " << remoteRank_); |
| 118 |
MPI_Ssend (buffer, |
| 119 |
CONT_BUFFER_MAX / mpi_get_type_size (type_), |
| 120 |
type_, |
| 121 |
remoteRank_, |
| 122 |
CONT_MSG, |
| 123 |
intercomm); |
| 124 |
buffer += CONT_BUFFER_MAX; |
| 125 |
size -= CONT_BUFFER_MAX; |
| 126 |
} |
| 127 |
MUSIC_LOGR ("Last send to rank " << remoteRank_); |
| 128 |
MPI_Ssend (buffer, |
| 129 |
size / mpi_get_type_size (type_), |
| 130 |
type_, |
| 131 |
remoteRank_, |
| 132 |
CONT_MSG, |
| 133 |
intercomm); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void |
| 138 |
ContOutputSubconnector::flush (bool& dataStillFlowing) |
| 139 |
{ |
| 140 |
if (!flushed) |
| 141 |
{ |
| 142 |
if (!buffer_.isEmpty ()) |
| 143 |
{ |
| 144 |
MUSIC_LOGR ("sending data remaining in buffers"); |
| 145 |
send (); |
| 146 |
dataStillFlowing = true; |
| 147 |
} |
| 148 |
else |
| 149 |
{ |
| 150 |
char dummy; |
| 151 |
MPI_Ssend (&dummy, 0, type_, remoteRank_, FLUSH_MSG, intercomm); |
| 152 |
flushed = true; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
ContInputSubconnector::ContInputSubconnector (//Synchronizer* synch_, |
| 159 |
MPI_Comm intercomm, |
| 160 |
int remoteLeader, |
| 161 |
int remoteRank, |
| 162 |
int receiverRank, |
| 163 |
int receiverPortCode, |
| 164 |
MPI_Datatype type) |
| 165 |
: Subconnector (type, |
| 166 |
intercomm, |
| 167 |
remoteLeader, |
| 168 |
remoteRank, |
| 169 |
receiverRank, |
| 170 |
receiverPortCode), |
| 171 |
InputSubconnector () |
| 172 |
{ |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
void |
| 177 |
ContInputSubconnector::initialCommunication (double initialBufferedTicks) |
| 178 |
{ |
| 179 |
receive (); |
| 180 |
|
| 181 |
buffer_.fill (initialBufferedTicks); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void |
| 186 |
ContInputSubconnector::maybeCommunicate () |
| 187 |
{ |
| 188 |
if (!flushed) |
| 189 |
receive (); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
void |
| 194 |
ContInputSubconnector::receive () |
| 195 |
{ |
| 196 |
char* data; |
| 197 |
MPI_Status status; |
| 198 |
int size, maxCount; |
| 199 |
maxCount = CONT_BUFFER_MAX / mpi_get_type_size (type_); |
| 200 |
do |
| 201 |
{ |
| 202 |
data = static_cast<char*> (buffer_.insertBlock ()); |
| 203 |
MUSIC_LOGR ("Receiving from rank " << remoteRank_); |
| 204 |
|
| 205 |
MPI_Recv (data, |
| 206 |
maxCount, |
| 207 |
type_, |
| 208 |
remoteRank_, |
| 209 |
MPI_ANY_TAG, |
| 210 |
intercomm, |
| 211 |
&status); |
| 212 |
if (status.MPI_TAG == FLUSH_MSG) |
| 213 |
{ |
| 214 |
flushed = true; |
| 215 |
MUSIC_LOGR ("received flush message"); |
| 216 |
return; |
| 217 |
} |
| 218 |
MPI_Get_count (&status, type_, &size); |
| 219 |
buffer_.trimBlock (mpi_get_type_size (type_) * size); |
| 220 |
} |
| 221 |
while (size == maxCount); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void |
| 226 |
ContInputSubconnector::flush (bool& dataStillFlowing) |
| 227 |
{ |
| 228 |
if (!flushed) |
| 229 |
{ |
| 230 |
//MUSIC_LOGR ("receiving and throwing away data"); |
| 231 |
receive (); |
| 232 |
if (!flushed) |
| 233 |
dataStillFlowing = true; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
/******************************************************************** |
| 240 |
* |
| 241 |
* Event Subconnectors |
| 242 |
* |
| 243 |
********************************************************************/ |
| 244 |
|
| 245 |
|
| 246 |
EventOutputSubconnector::EventOutputSubconnector (//Synchronizer* synch_, |
| 247 |
MPI_Comm intercomm, |
| 248 |
int remoteLeader, |
| 249 |
int remoteRank, |
| 250 |
int receiverPortCode) |
| 251 |
: Subconnector (MPI_BYTE, |
| 252 |
intercomm, |
| 253 |
remoteLeader, |
| 254 |
remoteRank, |
| 255 |
remoteRank, |
| 256 |
receiverPortCode), |
| 257 |
BufferingOutputSubconnector (sizeof (Event)) |
| 258 |
{ |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
void |
| 263 |
EventOutputSubconnector::maybeCommunicate () |
| 264 |
{ |
| 265 |
send (); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
void |
| 270 |
EventOutputSubconnector::maybeCommunicate (std::vector<MPI_Request> &requests) |
| 271 |
{ |
| 272 |
send (requests); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
void |
| 277 |
EventOutputSubconnector::send (std::vector<MPI_Request> &requests) |
| 278 |
{ |
| 279 |
MUSIC_LOGRE ("ISend"); |
| 280 |
void* data; |
| 281 |
int size; |
| 282 |
buffer_.nextBlock (data, size); |
| 283 |
char* buffer = static_cast <char*> (data); |
| 284 |
while (size >= SPIKE_BUFFER_MAX) |
| 285 |
{ |
| 286 |
MPI_Request request; |
| 287 |
MPI_Isend (buffer, |
| 288 |
SPIKE_BUFFER_MAX, |
| 289 |
type_, |
| 290 |
remoteRank_, |
| 291 |
SPIKE_MSG, |
| 292 |
intercomm, |
| 293 |
&request); |
| 294 |
requests.push_back (request); |
| 295 |
buffer += SPIKE_BUFFER_MAX; |
| 296 |
size -= SPIKE_BUFFER_MAX; |
| 297 |
} |
| 298 |
MPI_Request request; |
| 299 |
MPI_Isend (buffer, size, type_, remoteRank_, SPIKE_MSG, intercomm, &request); |
| 300 |
requests.push_back (request); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
void |
| 305 |
EventOutputSubconnector::send () |
| 306 |
{ |
| 307 |
MUSIC_LOGRE ("Ssend"); |
| 308 |
void* data; |
| 309 |
int size; |
| 310 |
buffer_.nextBlock (data, size); |
| 311 |
char* buffer = static_cast <char*> (data); |
| 312 |
while (size >= SPIKE_BUFFER_MAX) |
| 313 |
{ |
| 314 |
MPI_Ssend (buffer, |
| 315 |
SPIKE_BUFFER_MAX, |
| 316 |
type_, |
| 317 |
remoteRank_, |
| 318 |
SPIKE_MSG, |
| 319 |
intercomm); |
| 320 |
buffer += SPIKE_BUFFER_MAX; |
| 321 |
size -= SPIKE_BUFFER_MAX; |
| 322 |
} |
| 323 |
MPI_Ssend (buffer, size, type_, remoteRank_, SPIKE_MSG, intercomm); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
void |
| 328 |
EventOutputSubconnector::flush (bool& dataStillFlowing) |
| 329 |
{ |
| 330 |
if (!flushed) |
| 331 |
{ |
| 332 |
if (!buffer_.isEmpty ()) |
| 333 |
{ |
| 334 |
MUSIC_LOGR ("sending data remaining in buffers"); |
| 335 |
send (); |
| 336 |
dataStillFlowing = true; |
| 337 |
} |
| 338 |
/* remedius |
| 339 |
* flushed flag was added since synchronous communication demands equal sends and receives |
| 340 |
*/ |
| 341 |
else |
| 342 |
{ |
| 343 |
Event* e = static_cast<Event*> (buffer_.insert ()); |
| 344 |
e->id = FLUSH_MARK; |
| 345 |
send (); |
| 346 |
flushed = true; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
EventInputSubconnector::EventInputSubconnector (//Synchronizer* synch_, |
| 353 |
MPI_Comm intercomm, |
| 354 |
int remoteLeader, |
| 355 |
int remoteRank, |
| 356 |
int receiverRank, |
| 357 |
int receiverPortCode) |
| 358 |
: Subconnector (MPI_BYTE, |
| 359 |
intercomm, |
| 360 |
remoteLeader, |
| 361 |
remoteRank, |
| 362 |
receiverRank, |
| 363 |
receiverPortCode), |
| 364 |
InputSubconnector () |
| 365 |
{ |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
EventInputSubconnectorGlobal::EventInputSubconnectorGlobal |
| 370 |
(//Synchronizer* synch_, |
| 371 |
MPI_Comm intercomm, |
| 372 |
int remoteLeader, |
| 373 |
int remoteRank, |
| 374 |
int receiverRank, |
| 375 |
int receiverPortCode, |
| 376 |
EventHandlerGlobalIndex* eh) |
| 377 |
: Subconnector (MPI_BYTE, |
| 378 |
intercomm, |
| 379 |
remoteLeader, |
| 380 |
remoteRank, |
| 381 |
receiverRank, |
| 382 |
receiverPortCode), |
| 383 |
EventInputSubconnector (//synch_, |
| 384 |
intercomm, |
| 385 |
remoteLeader, |
| 386 |
remoteRank, |
| 387 |
receiverRank, |
| 388 |
receiverPortCode), |
| 389 |
handleEvent (eh) |
| 390 |
{ |
| 391 |
|
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
//EventHandlerGlobalIndexDummy |
| 397 |
//EventInputSubconnectorGlobal::dummyHandler; |
| 398 |
|
| 399 |
|
| 400 |
EventInputSubconnectorLocal::EventInputSubconnectorLocal |
| 401 |
(//Synchronizer* synch_, |
| 402 |
MPI_Comm intercomm, |
| 403 |
int remoteLeader, |
| 404 |
int remoteRank, |
| 405 |
int receiverRank, |
| 406 |
int receiverPortCode, |
| 407 |
EventHandlerLocalIndex* eh) |
| 408 |
: Subconnector (MPI_BYTE, |
| 409 |
intercomm, |
| 410 |
remoteLeader, |
| 411 |
remoteRank, |
| 412 |
receiverRank, |
| 413 |
receiverPortCode), |
| 414 |
EventInputSubconnector ( |
| 415 |
intercomm, |
| 416 |
remoteLeader, |
| 417 |
remoteRank, |
| 418 |
receiverRank, |
| 419 |
receiverPortCode), |
| 420 |
handleEvent (eh) |
| 421 |
{ |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
//EventHandlerLocalIndexDummy |
| 426 |
//EventInputSubconnectorLocal::dummyHandler; |
| 427 |
|
| 428 |
|
| 429 |
void |
| 430 |
EventInputSubconnector::maybeCommunicate () |
| 431 |
{ |
| 432 |
if (!flushed ) |
| 433 |
receive (); |
| 434 |
} |
| 435 |
|
| 436 |
bool |
| 437 |
EventInputSubconnectorGlobal::receive (char *data, int size) |
| 438 |
{ |
| 439 |
Event* ev = (Event*) data; |
| 440 |
/* remedius |
| 441 |
* since the message can be of size 0 and contains garbage=FLUSH_MARK, |
| 442 |
* the check for the size of the message was added. |
| 443 |
*/ |
| 444 |
if (ev[0].id == FLUSH_MARK && size > 0) |
| 445 |
{ |
| 446 |
flushed = true; |
| 447 |
MUSIC_LOGR ("received flush message"); |
| 448 |
} |
| 449 |
else |
| 450 |
{ |
| 451 |
int nEvents = size / sizeof (Event); |
| 452 |
for (int i = 0; i < nEvents; ++i){ |
| 453 |
(*handleEvent) (ev[i].t, ev[i].id); |
| 454 |
} |
| 455 |
} |
| 456 |
return flushed; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
// NOTE: isolate difference between global and local to avoid code repetition |
| 461 |
void |
| 462 |
EventInputSubconnectorGlobal::receive () |
| 463 |
{ |
| 464 |
char data[SPIKE_BUFFER_MAX]; |
| 465 |
MPI_Status status; |
| 466 |
int size; |
| 467 |
//double starttime, endtime; |
| 468 |
//starttime = MPI_Wtime(); |
| 469 |
|
| 470 |
do |
| 471 |
{ |
| 472 |
MPI_Recv (data, |
| 473 |
SPIKE_BUFFER_MAX, |
| 474 |
type_, |
| 475 |
remoteRank_, |
| 476 |
SPIKE_MSG, |
| 477 |
intercomm, |
| 478 |
&status); |
| 479 |
|
| 480 |
MPI_Get_count (&status, type_, &size); |
| 481 |
Event* ev = (Event*) data; |
| 482 |
/* remedius |
| 483 |
* since the message can be of size 0 and contains garbage=FLUSH_MARK, |
| 484 |
* the check for the size of the message was added. |
| 485 |
*/ |
| 486 |
if (ev[0].id == FLUSH_MARK && size > 0) |
| 487 |
{ |
| 488 |
flushed = true; |
| 489 |
MUSIC_LOGR ("received flush message"); |
| 490 |
return; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
int nEvents = size / sizeof (Event); |
| 495 |
|
| 496 |
for (int i = 0; i < nEvents; ++i){ |
| 497 |
(*handleEvent) (ev[i].t, ev[i].id); |
| 498 |
} |
| 499 |
|
| 500 |
} |
| 501 |
while (size == SPIKE_BUFFER_MAX); |
| 502 |
//endtime = MPI_Wtime(); |
| 503 |
//endtime = endtime-starttime; |
| 504 |
//if(tt < endtime) |
| 505 |
//tt = endtime; |
| 506 |
|
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
void |
| 511 |
EventInputSubconnectorLocal::receive () |
| 512 |
{ |
| 513 |
MUSIC_LOGRE ("receive"); |
| 514 |
char data[SPIKE_BUFFER_MAX]; |
| 515 |
MPI_Status status; |
| 516 |
int size; |
| 517 |
do |
| 518 |
{ |
| 519 |
MPI_Recv (data, |
| 520 |
SPIKE_BUFFER_MAX, |
| 521 |
type_, |
| 522 |
remoteRank_, |
| 523 |
SPIKE_MSG, |
| 524 |
intercomm, |
| 525 |
&status); |
| 526 |
MPI_Get_count (&status, type_, &size); |
| 527 |
Event* ev = (Event*) data; |
| 528 |
/* remedius |
| 529 |
* since the message can be of size 0 and contains garbage=FLUSH_MARK, |
| 530 |
* the check for the size of the message was added. |
| 531 |
*/ |
| 532 |
if (ev[0].id == FLUSH_MARK && size > 0) |
| 533 |
{ |
| 534 |
flushed = true; |
| 535 |
return; |
| 536 |
} |
| 537 |
int nEvents = size / sizeof (Event); |
| 538 |
for (int i = 0; i < nEvents; ++i) |
| 539 |
(*handleEvent) (ev[i].t, ev[i].id); |
| 540 |
} |
| 541 |
while (size == SPIKE_BUFFER_MAX); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
void |
| 546 |
EventInputSubconnector::flush (bool& dataStillFlowing) |
| 547 |
{ |
| 548 |
if (!flushed) |
| 549 |
{ |
| 550 |
//MUSIC_LOGRE ("receiving and throwing away data"); |
| 551 |
receive (); |
| 552 |
if (!flushed) |
| 553 |
dataStillFlowing = true; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
/*void |
| 559 |
EventInputSubconnectorGlobal::flush (bool& dataStillFlowing) |
| 560 |
{ |
| 561 |
//handleEvent = &dummyHandler; |
| 562 |
EventInputSubconnector::flush (dataStillFlowing); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
void |
| 567 |
EventInputSubconnectorLocal::flush (bool& dataStillFlowing) |
| 568 |
{ |
| 569 |
//handleEvent = &dummyHandler; |
| 570 |
EventInputSubconnector::flush (dataStillFlowing); |
| 571 |
}*/ |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
/******************************************************************** |
| 576 |
* |
| 577 |
* Message Subconnectors |
| 578 |
* |
| 579 |
********************************************************************/ |
| 580 |
|
| 581 |
MessageOutputSubconnector::MessageOutputSubconnector (//Synchronizer* synch_, |
| 582 |
MPI_Comm intercomm, |
| 583 |
int remoteLeader, |
| 584 |
int remoteRank, |
| 585 |
int receiverPortCode, |
| 586 |
FIBO* buffer) |
| 587 |
: Subconnector (MPI_BYTE, |
| 588 |
intercomm, |
| 589 |
remoteLeader, |
| 590 |
remoteRank, |
| 591 |
remoteRank, |
| 592 |
receiverPortCode), |
| 593 |
buffer_ (buffer) |
| 594 |
{ |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
void |
| 599 |
MessageOutputSubconnector::maybeCommunicate () |
| 600 |
{ |
| 601 |
if (!flushed) |
| 602 |
send (); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
void |
| 607 |
MessageOutputSubconnector::send () |
| 608 |
{ |
| 609 |
void* data; |
| 610 |
int size; |
| 611 |
buffer_->nextBlock (data, size); |
| 612 |
// NOTE: marshalling |
| 613 |
char* buffer = static_cast <char*> (data); |
| 614 |
while (size >= MESSAGE_BUFFER_MAX) |
| 615 |
{ |
| 616 |
MPI_Ssend (buffer, |
| 617 |
MESSAGE_BUFFER_MAX, |
| 618 |
type_, |
| 619 |
remoteRank_, |
| 620 |
MESSAGE_MSG, |
| 621 |
intercomm); |
| 622 |
buffer += MESSAGE_BUFFER_MAX; |
| 623 |
size -= MESSAGE_BUFFER_MAX; |
| 624 |
} |
| 625 |
MPI_Ssend (buffer, size, type_, remoteRank_, MESSAGE_MSG, intercomm); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
void |
| 630 |
MessageOutputSubconnector::flush (bool& dataStillFlowing) |
| 631 |
{ |
| 632 |
if (!flushed) |
| 633 |
{ |
| 634 |
if (!buffer_->isEmpty ()) |
| 635 |
{ |
| 636 |
MUSIC_LOGRE ("sending data remaining in buffers"); |
| 637 |
send (); |
| 638 |
dataStillFlowing = true; |
| 639 |
} |
| 640 |
else |
| 641 |
{ |
| 642 |
char dummy; |
| 643 |
MPI_Ssend (&dummy, 0, type_, remoteRank_, FLUSH_MSG, intercomm); |
| 644 |
} |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
MessageInputSubconnector::MessageInputSubconnector (//Synchronizer* synch_, |
| 650 |
MPI_Comm intercomm, |
| 651 |
int remoteLeader, |
| 652 |
int remoteRank, |
| 653 |
int receiverRank, |
| 654 |
int receiverPortCode, |
| 655 |
MessageHandler* mh) |
| 656 |
: Subconnector (MPI_BYTE, |
| 657 |
intercomm, |
| 658 |
remoteLeader, |
| 659 |
remoteRank, |
| 660 |
receiverRank, |
| 661 |
receiverPortCode), |
| 662 |
handleMessage (mh) |
| 663 |
{ |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
//MessageHandlerDummy |
| 668 |
//MessageInputSubconnector::dummyHandler; |
| 669 |
|
| 670 |
|
| 671 |
void |
| 672 |
MessageInputSubconnector::maybeCommunicate () |
| 673 |
{ |
| 674 |
if (!flushed)// && synch->communicate ()) |
| 675 |
receive (); |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
void |
| 680 |
MessageInputSubconnector::receive () |
| 681 |
{ |
| 682 |
char data[MESSAGE_BUFFER_MAX]; |
| 683 |
MPI_Status status; |
| 684 |
int size; |
| 685 |
do |
| 686 |
{ |
| 687 |
MPI_Recv (data, |
| 688 |
MESSAGE_BUFFER_MAX, |
| 689 |
type_, |
| 690 |
remoteRank_, |
| 691 |
MPI_ANY_TAG, |
| 692 |
intercomm, |
| 693 |
&status); |
| 694 |
if (status.MPI_TAG == FLUSH_MSG) |
| 695 |
{ |
| 696 |
flushed = true; |
| 697 |
MUSIC_LOGRE ("received flush message"); |
| 698 |
return; |
| 699 |
} |
| 700 |
MPI_Get_count (&status, type_, &size); |
| 701 |
int current = 0; |
| 702 |
while (current < size) |
| 703 |
{ |
| 704 |
MessageHeader* header = static_cast<MessageHeader*> |
| 705 |
(static_cast<void*> (&data[current])); |
| 706 |
current += sizeof (MessageHeader); |
| 707 |
(*handleMessage) (header->t (), &data[current], header->size ()); |
| 708 |
current += header->size (); |
| 709 |
} |
| 710 |
} |
| 711 |
while (size == MESSAGE_BUFFER_MAX); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
void |
| 716 |
MessageInputSubconnector::flush (bool& dataStillFlowing) |
| 717 |
{ |
| 718 |
//handleMessage = &dummyHandler; |
| 719 |
if (!flushed) |
| 720 |
{ |
| 721 |
//MUSIC_LOGRE ("receiving and throwing away data"); |
| 722 |
receive (); |
| 723 |
if (!flushed) |
| 724 |
dataStillFlowing = true; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
/******************************************************************** |
| 731 |
* |
| 732 |
* Collective Subconnector |
| 733 |
* |
| 734 |
********************************************************************/ |
| 735 |
|
| 736 |
CollectiveSubconnector::CollectiveSubconnector (MPI_Comm intracomm) |
| 737 |
: intracomm_ (intracomm) |
| 738 |
{ |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
CollectiveSubconnector::~CollectiveSubconnector () |
| 743 |
{ |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
void |
| 748 |
CollectiveSubconnector::allocAllgathervArrays () |
| 749 |
{ |
| 750 |
nProcesses = mpi_get_comm_size (intracomm_); |
| 751 |
ppBytes = new int[nProcesses]; |
| 752 |
displ = new int[nProcesses]; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
void |
| 757 |
CollectiveSubconnector::freeAllgathervArrays () |
| 758 |
{ |
| 759 |
delete ppBytes; |
| 760 |
delete displ; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
void |
| 765 |
CollectiveSubconnector::maybeCommunicate () |
| 766 |
{ |
| 767 |
if (!flushed) |
| 768 |
communicate (); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
int |
| 773 |
CollectiveSubconnector::calcCommDataSize (int local_data_size) |
| 774 |
{ |
| 775 |
int dsize; |
| 776 |
//distributing the size of the buffer |
| 777 |
MPI_Allgather (&local_data_size, 1, MPI_INT, ppBytes, 1, MPI_INT, intracomm_); |
| 778 |
//could it be that dsize is more then unsigned int? |
| 779 |
dsize = 0; |
| 780 |
for(int i=0; i < nProcesses; ++i){ |
| 781 |
displ[i] = dsize; |
| 782 |
dsize += ppBytes[i]; |
| 783 |
//ppBytes[i] /= type_.Get_size(); |
| 784 |
/* if(ppBytes[i] > max_size) |
| 785 |
max_size = ppBytes[i];*/ |
| 786 |
} |
| 787 |
return dsize; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
/* remedius |
| 792 |
* current collective communication is realized through two times calls of |
| 793 |
* mpi allgather function: first time the size of the data is distributed, |
| 794 |
* the second time the data by itself is distributed. |
| 795 |
* Probably that could be not optimal for a huge # of ranks. |
| 796 |
*/ |
| 797 |
std::vector<char> CollectiveSubconnector::getCommData(char *cur_buff, int size) |
| 798 |
{ |
| 799 |
unsigned int dsize; |
| 800 |
char *recv_buff; |
| 801 |
std::vector<char> commData; |
| 802 |
recv_buff = NULL; |
| 803 |
|
| 804 |
dsize = calcCommDataSize (size); |
| 805 |
|
| 806 |
if(dsize > 0){ |
| 807 |
//distributing the data |
| 808 |
recv_buff = new char[dsize]; |
| 809 |
MPI_Allgatherv (cur_buff, size, MPI_BYTE, recv_buff, ppBytes, displ, MPI_BYTE, |
| 810 |
intracomm_); |
| 811 |
std::copy (recv_buff, recv_buff + dsize, std::back_inserter (commData)); |
| 812 |
delete[] recv_buff; |
| 813 |
} |
| 814 |
return commData; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
void |
| 819 |
EventOutputCollectiveSubconnector::setOutputBuffer (void* buffer, |
| 820 |
unsigned int size) |
| 821 |
{ |
| 822 |
router_->setOutputBuffer (buffer, size); |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
unsigned int |
| 827 |
EventOutputCollectiveSubconnector::dataSize () |
| 828 |
{ |
| 829 |
return router_->dataSize (); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
void |
| 834 |
EventOutputCollectiveSubconnector::fillOutputBuffer () |
| 835 |
{ |
| 836 |
router_->fillOutputBuffer (); |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
void |
| 841 |
EventInputCollectiveSubconnector::processData (void* data, unsigned int size) |
| 842 |
{ |
| 843 |
Event* e = static_cast<Event*> (data); |
| 844 |
while (size > 0) |
| 845 |
{ |
| 846 |
router_->processEvent (e->t, e->id); |
| 847 |
++e; |
| 848 |
size -= sizeof (Event); |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
void |
| 854 |
ContCollectiveSubconnector::maybeCommunicate() |
| 855 |
{ |
| 856 |
CollectiveSubconnector::maybeCommunicate(); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
void |
| 861 |
ContCollectiveSubconnector::communicate() |
| 862 |
{ |
| 863 |
int size, bSize, nBuffered; |
| 864 |
void *data; |
| 865 |
char *cur_buff, *dest_buff; |
| 866 |
std::vector<char> commData; |
| 867 |
if(flushed) |
| 868 |
return; |
| 869 |
ContOutputSubconnector::buffer_.nextBlock (data, size); |
| 870 |
commData = getCommData(static_cast <char*> (data), size); |
| 871 |
const int* displ = getDisplArr(); |
| 872 |
//std::copy(commData.begin(),commData.end(),cur_buff); |
| 873 |
cur_buff = (char*)static_cast<void*> (&commData[0]); |
| 874 |
flushed = commData.size() == 0 ? true : false; |
| 875 |
nBuffered= commData.size() / width_; //the received data size is always multiple of port width |
| 876 |
for(int i =0; i < nBuffered ; ++i){ |
| 877 |
bSize = 0; |
| 878 |
dest_buff = static_cast<char*> (ContInputSubconnector::buffer_.insertBlock ()); |
| 879 |
for (std::multimap<int, Interval>::iterator it= |
| 880 |
intervals_.begin() ; it != intervals_.end(); it++ ) //iterates all the intervals |
| 881 |
{ |
| 882 |
memcpy (dest_buff + bSize, |
| 883 |
cur_buff |
| 884 |
+displ[(*it).first]//+displacement of data for a current rank |
| 885 |
+i*blockSizePerRank_[(*it).first] //+displacement of the appropriate buffered chunk of data within current rank data block |
| 886 |
+(*it).second.begin(), //+displacement of the requested data |
| 887 |
(*it).second.end()); // length field is stored overlapping the end field |
| 888 |
bSize += (*it).second.end(); |
| 889 |
} |
| 890 |
ContInputSubconnector::buffer_.trimBlock (bSize); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
void |
| 896 |
ContCollectiveSubconnector::initialCommunication(double initialBufferedTicks) |
| 897 |
{ |
| 898 |
communicate(); |
| 899 |
fillBlockSizes(); |
| 900 |
ContInputSubconnector::buffer_.fill (initialBufferedTicks); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
void ContCollectiveSubconnector::fillBlockSizes() |
| 905 |
{ |
| 906 |
std::map<int, Interval>::iterator it; |
| 907 |
const int* ppBytes = getSizeArr(); |
| 908 |
for ( it=intervals_.begin() ; it != intervals_.end(); it++ ) //iterates all the intervals |
| 909 |
blockSizePerRank_[(*it).first]= ppBytes[(*it).first]; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
void |
| 914 |
ContCollectiveSubconnector::flush(bool& dataStillFlowing) |
| 915 |
{ |
| 916 |
if (!flushed) |
| 917 |
{ |
| 918 |
if (!ContOutputSubconnector::buffer_.isEmpty ()) |
| 919 |
{ |
| 920 |
MUSIC_LOGR ("sending data remaining in buffers"); |
| 921 |
maybeCommunicate (); |
| 922 |
dataStillFlowing = true; |
| 923 |
} |
| 924 |
else |
| 925 |
{ |
| 926 |
//a sign for the end of communication will be an empty buffer |
| 927 |
communicate (); |
| 928 |
if(!flushed) |
| 929 |
dataStillFlowing = true; |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
#endif |