1 /* Copyright (c) 2013-2019. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
8 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
9 * University Research and Technology
10 * Corporation. All rights reserved.
11 * Copyright (c) 2004-2005 The University of Tennessee and The University
12 * of Tennessee Research Foundation. All rights
14 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
15 * University of Stuttgart. All rights reserved.
16 * Copyright (c) 2004-2005 The Regents of the University of California.
17 * All rights reserved.
19 * Additional copyrights may follow
22 #include "coll_tuned_topo.hpp"
23 #include "colls_private.hpp"
25 * Some static helpers.
27 static int pown( int fanout, int num )
30 if( num < 0 ) return 0;
31 if (1==num) return fanout;
36 for (int j = 0; j < num; j++) {
43 static int calculate_level( int fanout, int rank )
46 if( rank < 0 ) return -1;
47 for( level = 0, num = 0; num <= rank; level++ ) {
48 num += pown(fanout, level);
53 static int calculate_num_nodes_up_to_level( int fanout, int level )
55 /* just use geometric progression formula for sum:
56 a^0+a^1+...a^(n-1) = (a^n-1)/(a-1) */
58 return ((pown(fanout, level) - 1) / (fanout - 1));
60 return 0; // is this right ?
64 * And now the building functions.
66 * An example for fanout = 2, comm_size = 7
68 * 0 <-- delta = 1 (fanout^0)
70 * 1 2 <-- delta = 2 (fanout^1)
72 * 3 5 4 6 <-- delta = 4 (fanout^2)
76 ompi_coll_tuned_topo_build_tree( int fanout,
82 int level; /* location of my rank in the tree structure of size */
83 int delta; /* number of nodes on my level */
84 int slimit; /* total number of nodes on levels above me */
86 ompi_coll_tree_t* tree;
88 XBT_DEBUG("coll:tuned:topo_build_tree Building fo %d rt %d", fanout, root);
91 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d", fanout);
94 if (fanout>MAXTREEFANOUT) {
95 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d bigger than max %d", fanout, MAXTREEFANOUT);
100 * Get size and rank of the process in this communicator
105 tree = new ompi_coll_tree_t;
107 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
111 tree->tree_root = MPI_UNDEFINED;
112 tree->tree_nextsize = MPI_UNDEFINED;
117 tree->tree_root = root;
122 tree->tree_fanout = fanout;
123 tree->tree_bmtree = 0;
124 tree->tree_root = root;
125 tree->tree_prev = -1;
126 tree->tree_nextsize = 0;
127 for (int i = 0; i < fanout; i++) {
128 tree->tree_next[i] = -1;
131 /* return if we have less than 2 processes */
137 * Shift all ranks by root, so that the algorithm can be
138 * designed as if root would be always 0
139 * shiftedrank should be used in calculating distances
140 * and position in tree
142 shiftedrank = rank - root;
143 if( shiftedrank < 0 ) {
147 /* calculate my level */
148 level = calculate_level( fanout, shiftedrank );
149 delta = pown( fanout, level );
151 /* find my children */
152 for (int i = 0; i < fanout; i++) {
153 int schild = shiftedrank + delta * (i+1);
154 if( schild < size ) {
155 tree->tree_next[i] = (schild+root)%size;
156 tree->tree_nextsize = tree->tree_nextsize + 1;
163 slimit = calculate_num_nodes_up_to_level( fanout, level );
164 sparent = shiftedrank;
165 if( sparent < fanout ) {
168 while( sparent >= slimit ) {
169 sparent -= delta/fanout;
172 tree->tree_prev = (sparent+root)%size;
178 * Constructs in-order binary tree which can be used for non-commutative reduce
180 * Root of this tree is always rank (size-1) and fanout is 2.
181 * Here are some of the examples of this tree:
182 * size == 2 size == 3 size == 4 size == 9
192 ompi_coll_tuned_topo_build_in_order_bintree( MPI_Comm comm )
197 ompi_coll_tree_t* tree;
200 * Get size and rank of the process in this communicator
205 tree = new ompi_coll_tree_t;
207 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
211 tree->tree_root = MPI_UNDEFINED;
212 tree->tree_nextsize = MPI_UNDEFINED;
217 tree->tree_fanout = 2;
218 tree->tree_bmtree = 0;
219 tree->tree_root = size - 1;
220 tree->tree_prev = -1;
221 tree->tree_nextsize = 0;
222 tree->tree_next[0] = -1;
223 tree->tree_next[1] = -1;
225 "coll:tuned:topo_build_in_order_tree Building fo %d rt %d",
226 tree->tree_fanout, tree->tree_root);
236 /* Compute the size of the right subtree */
237 int rightsize = size >> 1;
239 /* Determine the left and right child of this parent */
245 rchild = rightsize - 1;
249 /* The following cases are possible: myrank can be
251 - belong to the left subtree, or
252 - belong to the right subtee
253 Each of the cases need to be handled differently.
256 if (myrank == parent) {
258 - compute real ranks of my children, and exit the loop. */
259 if (lchild >= 0) tree->tree_next[0] = lchild + delta;
260 if (rchild >= 0) tree->tree_next[1] = rchild + delta;
263 if (myrank > rchild) {
264 /* I belong to the left subtree:
265 - If I am the left child, compute real rank of my parent
266 - Iterate down through tree:
267 compute new size, shift ranks down, and update delta.
269 if (myrank == lchild) {
270 tree->tree_prev = parent + delta;
272 size = size - rightsize - 1;
273 delta = delta + rightsize;
274 myrank = myrank - rightsize;
278 /* I belong to the right subtree:
279 - If I am the right child, compute real rank of my parent
280 - Iterate down through tree:
281 compute new size and parent,
282 but the delta and rank do not need to change.
284 if (myrank == rchild) {
285 tree->tree_prev = parent + delta;
292 if (tree->tree_next[0] >= 0) { tree->tree_nextsize = 1; }
293 if (tree->tree_next[1] >= 0) { tree->tree_nextsize += 1; }
298 int ompi_coll_tuned_topo_destroy_tree( ompi_coll_tree_t** tree )
300 ompi_coll_tree_t *ptr;
302 if ((tree == nullptr) || (*tree == nullptr)) {
309 *tree = NULL; /* mark tree as gone */
316 * Here are some of the examples of this tree:
317 * size == 2 size = 4 size = 8
327 ompi_coll_tuned_topo_build_bmtree( MPI_Comm comm,
336 ompi_coll_tree_t *bmtree;
339 XBT_DEBUG("coll:tuned:topo:build_bmtree rt %d", root);
342 * Get size and rank of the process in this communicator
349 bmtree = new ompi_coll_tree_t;
351 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
355 bmtree->tree_bmtree = 1;
357 bmtree->tree_root = MPI_UNDEFINED;
358 bmtree->tree_nextsize = MPI_UNDEFINED;
359 for(i=0;i<MAXTREEFANOUT;i++) {
360 bmtree->tree_next[i] = -1;
363 if( index < 0 ) index += size;
365 while( mask <= index ) mask <<= 1;
367 /* Now I can compute my father rank */
369 bmtree->tree_prev = root;
371 remote = (index ^ (mask >> 1)) + root;
372 if( remote >= size ) remote -= size;
373 bmtree->tree_prev = remote;
375 /* And now let's fill my childs */
376 while( mask < size ) {
377 remote = (index ^ mask);
378 if( remote >= size ) break;
380 if( remote >= size ) remote -= size;
381 if (childs==MAXTREEFANOUT) {
382 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
386 bmtree->tree_next[childs] = remote;
390 bmtree->tree_nextsize = childs;
391 bmtree->tree_root = root;
396 * Constructs in-order binomial tree which can be used for gather/scatter
399 * Here are some of the examples of this tree:
400 * size == 2 size = 4 size = 8
409 ompi_coll_tree_t* ompi_coll_tuned_topo_build_in_order_bmtree(MPI_Comm comm, int root)
415 ompi_coll_tree_t *bmtree;
418 XBT_DEBUG("coll:tuned:topo:build_in_order_bmtree rt %d", root);
421 * Get size and rank of the process in this communicator
426 vrank = (rank - root + size) % size;
428 bmtree = new ompi_coll_tree_t;
430 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
435 bmtree->tree_bmtree = 1;
436 bmtree->tree_root = MPI_UNDEFINED;
437 bmtree->tree_nextsize = MPI_UNDEFINED;
438 for(i=0;i<MAXTREEFANOUT;i++) {
439 bmtree->tree_next[i] = -1;
443 bmtree->tree_prev = root;
446 while (mask < size) {
447 int remote = vrank ^ mask;
448 if (remote < vrank) {
449 bmtree->tree_prev = (remote + root) % size;
451 } else if (remote < size) {
452 bmtree->tree_next[childs] = (remote + root) % size;
454 if (childs == MAXTREEFANOUT) {
455 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
462 bmtree->tree_nextsize = childs;
463 bmtree->tree_root = root;
470 ompi_coll_tuned_topo_build_chain( int fanout,
475 int srank; /* shifted rank */
478 ompi_coll_tree_t *chain;
480 XBT_DEBUG("coll:tuned:topo:build_chain fo %d rt %d", fanout, root);
483 * Get size and rank of the process in this communicator
489 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout of ZERO, forcing to 1 (pipeline)!");
492 if (fanout>MAXTREEFANOUT) {
493 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout %d bigger than max %d, forcing to max!", fanout, MAXTREEFANOUT);
494 fanout = MAXTREEFANOUT;
498 * Allocate space for topology arrays if needed
500 chain = new ompi_coll_tree_t;
502 XBT_DEBUG("coll:tuned:topo:build_chain PANIC out of memory");
506 chain->tree_root = MPI_UNDEFINED;
507 chain->tree_nextsize = -1;
508 for(i=0;i<fanout;i++) chain->tree_next[i] = -1;
511 * Set root & numchain
513 chain->tree_root = root;
514 if( (size - 1) < fanout ) {
515 chain->tree_nextsize = size-1;
518 chain->tree_nextsize = fanout;
525 if (srank < 0) srank += size;
528 * Special case - fanout == 1
531 if( srank == 0 ) chain->tree_prev = -1;
532 else chain->tree_prev = (srank-1+root)%size;
534 if( (srank + 1) >= size) {
535 chain->tree_next[0] = -1;
536 chain->tree_nextsize = 0;
538 chain->tree_next[0] = (srank+1+root)%size;
539 chain->tree_nextsize = 1;
544 /* Let's handle the case where there is just one node in the communicator */
546 chain->tree_next[0] = -1;
547 chain->tree_nextsize = 0;
548 chain->tree_prev = -1;
552 * Calculate maximum chain length
554 maxchainlen = (size-1) / fanout;
555 if( (size-1) % fanout != 0 ) {
557 mark = (size-1)%fanout;
563 * Find your own place in the list of shifted ranks
569 if( srank-1 < (mark * maxchainlen) ) {
570 column = (srank-1)/maxchainlen;
571 head = 1+column*maxchainlen;
574 column = mark + (srank-1-mark*maxchainlen)/(maxchainlen-1);
575 head = mark*maxchainlen+1+(column-mark)*(maxchainlen-1);
579 if( srank == head ) {
580 chain->tree_prev = 0; /*root*/
582 chain->tree_prev = srank-1; /* rank -1 */
584 if( srank == (head + len - 1) ) {
585 chain->tree_next[0] = -1;
586 chain->tree_nextsize = 0;
588 if( (srank + 1) < size ) {
589 chain->tree_next[0] = srank+1;
590 chain->tree_nextsize = 1;
592 chain->tree_next[0] = -1;
593 chain->tree_nextsize = 0;
602 chain->tree_prev = -1;
603 chain->tree_next[0] = (root+1)%size;
604 for (int i = 1; i < fanout; i++) {
605 chain->tree_next[i] = chain->tree_next[i-1] + maxchainlen;
607 chain->tree_next[i]--;
609 chain->tree_next[i] %= size;
611 chain->tree_nextsize = fanout;
613 chain->tree_prev = (chain->tree_prev+root)%size;
614 if( chain->tree_next[0] != -1 ) {
615 chain->tree_next[0] = (chain->tree_next[0]+root)%size;
622 int ompi_coll_tuned_topo_dump_tree (ompi_coll_tree_t* tree, int rank)
624 XBT_DEBUG("coll:tuned:topo:topo_dump_tree %1d tree root %d"
625 " fanout %d BM %1d nextsize %d prev %d",
626 rank, tree->tree_root, tree->tree_bmtree, tree->tree_fanout,
627 tree->tree_nextsize, tree->tree_prev);
628 if( tree->tree_nextsize ) {
629 for (int i = 0; i < tree->tree_nextsize; i++)
630 XBT_DEBUG("[%1d] %d", i, tree->tree_next[i]);