1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
|
/*
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2015 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "ompi_config.h"
#include "mpi.h"
#include "opal/util/bit_ops.h"
#include "ompi/constants.h"
#include "ompi/communicator/communicator.h"
#include "ompi/mca/coll/base/coll_tags.h"
#include "ompi/mca/coll/base/coll_base_functions.h"
#include "coll_base_topo.h"
/*
* Some static helpers.
*/
static int pown( int fanout, int num )
{
int j, p = 1;
if( num < 0 ) return 0;
if (1==num) return fanout;
if (2==fanout) {
return p<<num;
}
else {
for( j = 0; j < num; j++ ) { p*= fanout; }
}
return p;
}
static int calculate_level( int fanout, int rank )
{
int level, num;
if( rank < 0 ) return -1;
for( level = 0, num = 0; num <= rank; level++ ) {
num += pown(fanout, level);
}
return level-1;
}
static int calculate_num_nodes_up_to_level( int fanout, int level )
{
/* just use geometric progression formula for sum:
a^0+a^1+...a^(n-1) = (a^n-1)/(a-1) */
if (1 == fanout) {
return level;
}
else {
return ((pown(fanout,level) - 1)/(fanout - 1));
}
}
/*
* And now the building functions.
*
* An example for fanout = 2, comm_size = 7
*
* 0 <-- delta = 1 (fanout^0)
* / \
* 1 2 <-- delta = 2 (fanout^1)
* / \ / \
* 3 5 4 6 <-- delta = 4 (fanout^2)
*/
ompi_coll_tree_t*
ompi_coll_base_topo_build_tree( int fanout,
struct ompi_communicator_t* comm,
int root )
{
int rank, size, schild, sparent, shiftedrank, i;
int level; /* location of my rank in the tree structure of size */
int delta; /* number of nodes on my level */
int slimit; /* total number of nodes on levels above me */
ompi_coll_tree_t* tree;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output, "coll:base:topo_build_tree Building fo %d rt %d", fanout, root));
if (fanout<1) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output, "coll:base:topo_build_tree invalid fanout %d", fanout));
return NULL;
}
if (fanout>MAXTREEFANOUT) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo_build_tree invalid fanout %d bigger than max %d", fanout, MAXTREEFANOUT));
return NULL;
}
/*
* Get size and rank of the process in this communicator
*/
size = ompi_comm_size(comm);
rank = ompi_comm_rank(comm);
tree = (ompi_coll_tree_t*)malloc(COLL_TREE_SIZE(MAXTREEFANOUT));
if (!tree) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo_build_tree PANIC::out of memory"));
return NULL;
}
tree->tree_root = MPI_UNDEFINED;
tree->tree_nextsize = MPI_UNDEFINED;
/*
* Set root
*/
tree->tree_root = root;
/*
* Initialize tree
*/
tree->tree_fanout = fanout;
tree->tree_bmtree = 0;
tree->tree_root = root;
tree->tree_prev = -1;
tree->tree_nextsize = 0;
for( i = 0; i < fanout; i++ ) {
tree->tree_next[i] = -1;
}
/* return if we have less than 2 processes */
if( size < 2 ) {
return tree;
}
/*
* Shift all ranks by root, so that the algorithm can be
* designed as if root would be always 0
* shiftedrank should be used in calculating distances
* and position in tree
*/
shiftedrank = rank - root;
if( shiftedrank < 0 ) {
shiftedrank += size;
}
/* calculate my level */
level = calculate_level( fanout, shiftedrank );
delta = pown( fanout, level );
/* find my children */
for( i = 0; i < fanout; i++ ) {
schild = shiftedrank + delta * (i+1);
if( schild < size ) {
tree->tree_next[i] = (schild+root)%size;
tree->tree_nextsize = tree->tree_nextsize + 1;
} else {
break;
}
}
/* find my parent */
slimit = calculate_num_nodes_up_to_level( fanout, level );
sparent = shiftedrank;
if( sparent < fanout ) {
sparent = 0;
} else {
while( sparent >= slimit ) {
sparent -= delta/fanout;
}
}
tree->tree_prev = (sparent+root)%size;
return tree;
}
/*
* Constructs in-order binary tree which can be used for non-commutative reduce
* operations.
* Root of this tree is always rank (size-1) and fanout is 2.
* Here are some of the examples of this tree:
* size == 2 size == 3 size == 4 size == 9
* 1 2 3 8
* / / \ / \ / \
* 0 1 0 2 1 7 3
* / / \ / \
* 0 6 5 2 1
* / /
* 4 0
*/
ompi_coll_tree_t*
ompi_coll_base_topo_build_in_order_bintree( struct ompi_communicator_t* comm )
{
int rank, size, myrank, rightsize, delta, parent, lchild, rchild;
ompi_coll_tree_t* tree;
/*
* Get size and rank of the process in this communicator
*/
size = ompi_comm_size(comm);
rank = ompi_comm_rank(comm);
tree = (ompi_coll_tree_t*)malloc(COLL_TREE_SIZE(MAXTREEFANOUT));
if (!tree) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,
"coll:base:topo_build_tree PANIC::out of memory"));
return NULL;
}
tree->tree_root = MPI_UNDEFINED;
tree->tree_nextsize = MPI_UNDEFINED;
/*
* Initialize tree
*/
tree->tree_fanout = 2;
tree->tree_bmtree = 0;
tree->tree_root = size - 1;
tree->tree_prev = -1;
tree->tree_nextsize = 0;
tree->tree_next[0] = -1;
tree->tree_next[1] = -1;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,
"coll:base:topo_build_in_order_tree Building fo %d rt %d",
tree->tree_fanout, tree->tree_root));
/*
* Build the tree
*/
myrank = rank;
parent = size - 1;
delta = 0;
while ( 1 ) {
/* Compute the size of the right subtree */
rightsize = size >> 1;
/* Determine the left and right child of this parent */
lchild = -1;
rchild = -1;
if (size - 1 > 0) {
lchild = parent - 1;
if (lchild > 0) {
rchild = rightsize - 1;
}
}
/* The following cases are possible: myrank can be
- a parent,
- belong to the left subtree, or
- belong to the right subtee
Each of the cases need to be handled differently.
*/
if (myrank == parent) {
/* I am the parent:
- compute real ranks of my children, and exit the loop. */
if (lchild >= 0) tree->tree_next[0] = lchild + delta;
if (rchild >= 0) tree->tree_next[1] = rchild + delta;
break;
}
if (myrank > rchild) {
/* I belong to the left subtree:
- If I am the left child, compute real rank of my parent
- Iterate down through tree:
compute new size, shift ranks down, and update delta.
*/
if (myrank == lchild) {
tree->tree_prev = parent + delta;
}
size = size - rightsize - 1;
delta = delta + rightsize;
myrank = myrank - rightsize;
parent = size - 1;
} else {
/* I belong to the right subtree:
- If I am the right child, compute real rank of my parent
- Iterate down through tree:
compute new size and parent,
but the delta and rank do not need to change.
*/
if (myrank == rchild) {
tree->tree_prev = parent + delta;
}
size = rightsize;
parent = rchild;
}
}
if (tree->tree_next[0] >= 0) { tree->tree_nextsize = 1; }
if (tree->tree_next[1] >= 0) { tree->tree_nextsize += 1; }
return tree;
}
int ompi_coll_base_topo_destroy_tree( ompi_coll_tree_t** tree )
{
ompi_coll_tree_t *ptr;
if ((!tree)||(!*tree)) {
return OMPI_SUCCESS;
}
ptr = *tree;
free (ptr);
*tree = NULL; /* mark tree as gone */
return OMPI_SUCCESS;
}
/*
*
* Here are some of the examples of this tree:
* size == 2 size = 4 size = 8
* 0 0 0
* / | \ / | \
* 1 2 1 4 2 1
* | | |\
* 3 6 5 3
* |
* 7
*/
ompi_coll_tree_t*
ompi_coll_base_topo_build_bmtree( struct ompi_communicator_t* comm,
int root )
{
int childs = 0, rank, size, mask = 1, index, remote, i;
ompi_coll_tree_t *bmtree;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_bmtree rt %d", root));
/*
* Get size and rank of the process in this communicator
*/
size = ompi_comm_size(comm);
rank = ompi_comm_rank(comm);
index = rank -root;
bmtree = (ompi_coll_tree_t*)malloc(COLL_TREE_SIZE(MAXTREEFANOUT));
if (!bmtree) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_bmtree PANIC out of memory"));
return NULL;
}
bmtree->tree_bmtree = 1;
bmtree->tree_root = MPI_UNDEFINED;
bmtree->tree_nextsize = MPI_UNDEFINED;
for( i = 0;i < MAXTREEFANOUT; i++ ) {
bmtree->tree_next[i] = -1;
}
if( index < 0 ) index += size;
mask = opal_next_poweroftwo(index);
/* Now I can compute my father rank */
if( root == rank ) {
bmtree->tree_prev = root;
} else {
remote = (index ^ (mask >> 1)) + root;
if( remote >= size ) remote -= size;
bmtree->tree_prev = remote;
}
/* And now let's fill my childs */
while( mask < size ) {
remote = (index ^ mask);
if( remote >= size ) break;
remote += root;
if( remote >= size ) remote -= size;
if (childs==MAXTREEFANOUT) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs));
free(bmtree);
return NULL;
}
bmtree->tree_next[childs] = remote;
mask <<= 1;
childs++;
}
bmtree->tree_nextsize = childs;
bmtree->tree_root = root;
return bmtree;
}
/*
* Constructs in-order binomial tree which can be used for gather/scatter
* operations.
*
* Here are some of the examples of this tree:
* size == 2 size = 4 size = 8
* 0 0 0
* / / | / | \
* 1 1 2 1 2 4
* | | | \
* 3 3 5 6
* |
* 7
*/
ompi_coll_tree_t*
ompi_coll_base_topo_build_in_order_bmtree( struct ompi_communicator_t* comm,
int root )
{
int childs = 0, rank, vrank, size, mask = 1, remote, i;
ompi_coll_tree_t *bmtree;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_in_order_bmtree rt %d", root));
/*
* Get size and rank of the process in this communicator
*/
size = ompi_comm_size(comm);
rank = ompi_comm_rank(comm);
vrank = (rank - root + size) % size;
bmtree = (ompi_coll_tree_t*)malloc(COLL_TREE_SIZE(MAXTREEFANOUT));
if (!bmtree) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_bmtree PANIC out of memory"));
return NULL;
}
bmtree->tree_bmtree = 1;
bmtree->tree_root = MPI_UNDEFINED;
bmtree->tree_nextsize = MPI_UNDEFINED;
for(i=0;i<MAXTREEFANOUT;i++) {
bmtree->tree_next[i] = -1;
}
if (root == rank) {
bmtree->tree_prev = root;
}
while (mask < size) {
remote = vrank ^ mask;
if (remote < vrank) {
bmtree->tree_prev = (remote + root) % size;
break;
} else if (remote < size) {
bmtree->tree_next[childs] = (remote + root) % size;
childs++;
if (childs==MAXTREEFANOUT) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,
"coll:base:topo:build_bmtree max fanout incorrect %d needed %d",
MAXTREEFANOUT, childs));
free(bmtree);
return NULL;
}
}
mask <<= 1;
}
bmtree->tree_nextsize = childs;
bmtree->tree_root = root;
return bmtree;
}
/*
* ompi_coll_base_topo_build_kmtree: Build k-nomial tree for Bcast
*
* Example, comm_size=10
* radix=2 radix=3 radix=4
* 0 0 0
* / / \ \ / / | \ \ / / \ \ \
* 8 4 2 1 9 3 6 1 2 4 8 1 2 3
* | |\ | |\ |\ /|\ |
* 9 6 5 3 4 5 7 8 5 6 7 9
* |
* 7
*/
ompi_coll_tree_t*
ompi_coll_base_topo_build_kmtree(struct ompi_communicator_t* comm,
int root, int radix)
{
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,
"coll:base:topo:build_kmtree root %d, radix %d", root, radix));
int comm_size = ompi_comm_size(comm);
int rank = ompi_comm_rank(comm);
/* nchilds <= (radix - 1) * \ceil(\log_{radix}(comm_size)) */
int log_radix = 0;
for (int i = 1; i < comm_size; i *= radix)
log_radix++;
int nchilds_max = (radix - 1) * log_radix;
int vrank = (rank - root + comm_size) % comm_size;
ompi_coll_tree_t *kmtree = malloc(COLL_TREE_SIZE(nchilds_max));
if (NULL == kmtree) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,
"coll:base:topo:build_kmtree PANIC out of memory"));
return NULL;
}
kmtree->tree_bmtree = 0;
kmtree->tree_root = root;
kmtree->tree_prev = MPI_PROC_NULL;
kmtree->tree_nextsize = 0;
/* Setup parent */
int mask = 0x1;
while (mask < comm_size) {
if (vrank % (radix * mask)) {
kmtree->tree_prev = vrank / (radix * mask) * (radix * mask);
kmtree->tree_prev = (kmtree->tree_prev + root) % comm_size;
break;
}
mask *= radix;
}
/* Setup childs */
mask /= radix;
int nchilds = 0;
while (mask > 0) {
for (int r = 1; r < radix; r++) {
int child = vrank + mask * r;
if (child < comm_size) {
child = (child + root) % comm_size;
kmtree->tree_next[nchilds] = child;
nchilds++;
}
}
mask /= radix;
}
kmtree->tree_nextsize = nchilds;
return kmtree;
}
ompi_coll_tree_t*
ompi_coll_base_topo_build_chain( int fanout,
struct ompi_communicator_t* comm,
int root )
{
int i, maxchainlen, mark, head, len, rank, size, srank /* shifted rank */;
ompi_coll_tree_t *chain;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_chain fo %d rt %d", fanout, root));
/*
* Get size and rank of the process in this communicator
*/
size = ompi_comm_size(comm);
rank = ompi_comm_rank(comm);
if( fanout < 1 ) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_chain WARNING invalid fanout of ZERO, forcing to 1 (pipeline)!"));
fanout = 1;
}
if (fanout>MAXTREEFANOUT) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_chain WARNING invalid fanout %d bigger than max %d, forcing to max!", fanout, MAXTREEFANOUT));
fanout = MAXTREEFANOUT;
}
/*
* Allocate space for topology arrays if needed
*/
chain = (ompi_coll_tree_t*)malloc(COLL_TREE_SIZE(MAXTREEFANOUT));
if (!chain) {
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"coll:base:topo:build_chain PANIC out of memory"));
fflush(stdout);
return NULL;
}
chain->tree_root = MPI_UNDEFINED;
chain->tree_nextsize = -1;
for(i=0;i<fanout;i++) chain->tree_next[i] = -1;
/*
* Set root & numchain
*/
chain->tree_root = root;
if( (size - 1) < fanout ) {
chain->tree_nextsize = size-1;
fanout = size-1;
} else {
chain->tree_nextsize = fanout;
}
/*
* Shift ranks
*/
srank = rank - root;
if (srank < 0) srank += size;
/*
* Special case - fanout == 1
*/
if( fanout == 1 ) {
if( srank == 0 ) chain->tree_prev = -1;
else chain->tree_prev = (srank-1+root)%size;
if( (srank + 1) >= size) {
chain->tree_next[0] = -1;
chain->tree_nextsize = 0;
} else {
chain->tree_next[0] = (srank+1+root)%size;
chain->tree_nextsize = 1;
}
return chain;
}
/* Let's handle the case where there is just one node in the communicator */
if( size == 1 ) {
chain->tree_next[0] = -1;
chain->tree_nextsize = 0;
chain->tree_prev = -1;
return chain;
}
/*
* Calculate maximum chain length
*/
maxchainlen = (size-1) / fanout;
if( (size-1) % fanout != 0 ) {
maxchainlen++;
mark = (size-1)%fanout;
} else {
mark = fanout+1;
}
/*
* Find your own place in the list of shifted ranks
*/
if( srank != 0 ) {
int column;
if( srank-1 < (mark * maxchainlen) ) {
column = (srank-1)/maxchainlen;
head = 1+column*maxchainlen;
len = maxchainlen;
} else {
column = mark + (srank-1-mark*maxchainlen)/(maxchainlen-1);
head = mark*maxchainlen+1+(column-mark)*(maxchainlen-1);
len = maxchainlen-1;
}
if( srank == head ) {
chain->tree_prev = 0; /*root*/
} else {
chain->tree_prev = srank-1; /* rank -1 */
}
if( srank == (head + len - 1) ) {
chain->tree_next[0] = -1;
chain->tree_nextsize = 0;
} else {
if( (srank + 1) < size ) {
chain->tree_next[0] = srank+1;
chain->tree_nextsize = 1;
} else {
chain->tree_next[0] = -1;
chain->tree_nextsize = 0;
}
}
chain->tree_prev = (chain->tree_prev+root)%size;
if( chain->tree_next[0] != -1 ) {
chain->tree_next[0] = (chain->tree_next[0]+root)%size;
}
} else {
/*
* Unshift values
*/
chain->tree_prev = -1;
chain->tree_next[0] = (root+1)%size;
for( i = 1; i < fanout; i++ ) {
chain->tree_next[i] = chain->tree_next[i-1] + maxchainlen;
if( i > mark ) {
chain->tree_next[i]--;
}
chain->tree_next[i] %= size;
}
chain->tree_nextsize = fanout;
}
return chain;
}
int ompi_coll_base_topo_dump_tree (ompi_coll_tree_t* tree, int rank)
{
int i;
OPAL_OUTPUT((ompi_coll_base_framework.framework_output, "coll:base:topo:topo_dump_tree %1d tree root %d"
" fanout %d BM %1d nextsize %d prev %d",
rank, tree->tree_root, tree->tree_bmtree, tree->tree_fanout,
tree->tree_nextsize, tree->tree_prev));
if( tree->tree_nextsize ) {
for( i = 0; i < tree->tree_nextsize; i++ )
OPAL_OUTPUT((ompi_coll_base_framework.framework_output,"[%1d] %d", i, tree->tree_next[i]));
}
return (0);
}
|