1 /* Copyright (c) 2013-2018. 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( j = 0; j < num; j++ ) { p*= fanout; }
41 static int calculate_level( int fanout, int rank )
44 if( rank < 0 ) return -1;
45 for( level = 0, num = 0; num <= rank; level++ ) {
46 num += pown(fanout, level);
51 static int calculate_num_nodes_up_to_level( int fanout, int level )
53 /* just use geometric progression formula for sum:
54 a^0+a^1+...a^(n-1) = (a^n-1)/(a-1) */
56 return ((pown(fanout, level) - 1) / (fanout - 1));
58 return 0; // is this right ?
62 * And now the building functions.
64 * An example for fanout = 2, comm_size = 7
66 * 0 <-- delta = 1 (fanout^0)
68 * 1 2 <-- delta = 2 (fanout^1)
70 * 3 5 4 6 <-- delta = 4 (fanout^2)
74 ompi_coll_tuned_topo_build_tree( int fanout,
80 int level; /* location of my rank in the tree structure of size */
81 int delta; /* number of nodes on my level */
82 int slimit; /* total number of nodes on levels above me */
85 ompi_coll_tree_t* tree;
87 XBT_DEBUG("coll:tuned:topo_build_tree Building fo %d rt %d", fanout, root);
90 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d", fanout);
93 if (fanout>MAXTREEFANOUT) {
94 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d bigger than max %d", fanout, MAXTREEFANOUT);
99 * Get size and rank of the process in this communicator
104 tree = new ompi_coll_tree_t;
106 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
110 tree->tree_root = MPI_UNDEFINED;
111 tree->tree_nextsize = MPI_UNDEFINED;
116 tree->tree_root = root;
121 tree->tree_fanout = fanout;
122 tree->tree_bmtree = 0;
123 tree->tree_root = root;
124 tree->tree_prev = -1;
125 tree->tree_nextsize = 0;
126 for( i = 0; i < fanout; i++ ) {
127 tree->tree_next[i] = -1;
130 /* return if we have less than 2 processes */
136 * Shift all ranks by root, so that the algorithm can be
137 * designed as if root would be always 0
138 * shiftedrank should be used in calculating distances
139 * and position in tree
141 shiftedrank = rank - root;
142 if( shiftedrank < 0 ) {
146 /* calculate my level */
147 level = calculate_level( fanout, shiftedrank );
148 delta = pown( fanout, level );
150 /* find my children */
151 for( i = 0; i < fanout; i++ ) {
152 schild = shiftedrank + delta * (i+1);
153 if( schild < size ) {
154 tree->tree_next[i] = (schild+root)%size;
155 tree->tree_nextsize = tree->tree_nextsize + 1;
162 slimit = calculate_num_nodes_up_to_level( fanout, level );
163 sparent = shiftedrank;
164 if( sparent < fanout ) {
167 while( sparent >= slimit ) {
168 sparent -= delta/fanout;
171 tree->tree_prev = (sparent+root)%size;
177 * Constructs in-order binary tree which can be used for non-commutative reduce
179 * Root of this tree is always rank (size-1) and fanout is 2.
180 * Here are some of the examples of this tree:
181 * size == 2 size == 3 size == 4 size == 9
191 ompi_coll_tuned_topo_build_in_order_bintree( MPI_Comm comm )
194 int myrank, rightsize, delta;
195 int parent, lchild, rchild;
196 ompi_coll_tree_t* tree;
199 * Get size and rank of the process in this communicator
204 tree = new ompi_coll_tree_t;
206 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
210 tree->tree_root = MPI_UNDEFINED;
211 tree->tree_nextsize = MPI_UNDEFINED;
216 tree->tree_fanout = 2;
217 tree->tree_bmtree = 0;
218 tree->tree_root = size - 1;
219 tree->tree_prev = -1;
220 tree->tree_nextsize = 0;
221 tree->tree_next[0] = -1;
222 tree->tree_next[1] = -1;
224 "coll:tuned:topo_build_in_order_tree Building fo %d rt %d",
225 tree->tree_fanout, tree->tree_root);
235 /* Compute the size of the right subtree */
236 rightsize = size >> 1;
238 /* Determine the left and right child of this parent */
244 rchild = rightsize - 1;
248 /* The following cases are possible: myrank can be
250 - belong to the left subtree, or
251 - belong to the right subtee
252 Each of the cases need to be handled differently.
255 if (myrank == parent) {
257 - compute real ranks of my children, and exit the loop. */
258 if (lchild >= 0) tree->tree_next[0] = lchild + delta;
259 if (rchild >= 0) tree->tree_next[1] = rchild + delta;
262 if (myrank > rchild) {
263 /* I belong to the left subtree:
264 - If I am the left child, compute real rank of my parent
265 - Iterate down through tree:
266 compute new size, shift ranks down, and update delta.
268 if (myrank == lchild) {
269 tree->tree_prev = parent + delta;
271 size = size - rightsize - 1;
272 delta = delta + rightsize;
273 myrank = myrank - rightsize;
277 /* I belong to the right subtree:
278 - If I am the right child, compute real rank of my parent
279 - Iterate down through tree:
280 compute new size and parent,
281 but the delta and rank do not need to change.
283 if (myrank == rchild) {
284 tree->tree_prev = parent + delta;
291 if (tree->tree_next[0] >= 0) { tree->tree_nextsize = 1; }
292 if (tree->tree_next[1] >= 0) { tree->tree_nextsize += 1; }
297 int ompi_coll_tuned_topo_destroy_tree( ompi_coll_tree_t** tree )
299 ompi_coll_tree_t *ptr;
301 if ((tree == nullptr) || (*tree == nullptr)) {
308 *tree = NULL; /* mark tree as gone */
315 * Here are some of the examples of this tree:
316 * size == 2 size = 4 size = 8
326 ompi_coll_tuned_topo_build_bmtree( MPI_Comm comm,
335 ompi_coll_tree_t *bmtree;
338 XBT_DEBUG("coll:tuned:topo:build_bmtree rt %d", root);
341 * Get size and rank of the process in this communicator
348 bmtree = new ompi_coll_tree_t;
350 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
354 bmtree->tree_bmtree = 1;
356 bmtree->tree_root = MPI_UNDEFINED;
357 bmtree->tree_nextsize = MPI_UNDEFINED;
358 for(i=0;i<MAXTREEFANOUT;i++) {
359 bmtree->tree_next[i] = -1;
362 if( index < 0 ) index += size;
364 while( mask <= index ) mask <<= 1;
366 /* Now I can compute my father rank */
368 bmtree->tree_prev = root;
370 remote = (index ^ (mask >> 1)) + root;
371 if( remote >= size ) remote -= size;
372 bmtree->tree_prev = remote;
374 /* And now let's fill my childs */
375 while( mask < size ) {
376 remote = (index ^ mask);
377 if( remote >= size ) break;
379 if( remote >= size ) remote -= size;
380 if (childs==MAXTREEFANOUT) {
381 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
384 bmtree->tree_next[childs] = remote;
388 bmtree->tree_nextsize = childs;
389 bmtree->tree_root = root;
394 * Constructs in-order binomial tree which can be used for gather/scatter
397 * Here are some of the examples of this tree:
398 * size == 2 size = 4 size = 8
407 ompi_coll_tree_t* ompi_coll_tuned_topo_build_in_order_bmtree(MPI_Comm comm, int root)
414 ompi_coll_tree_t *bmtree;
417 XBT_DEBUG("coll:tuned:topo:build_in_order_bmtree rt %d", root);
420 * Get size and rank of the process in this communicator
425 vrank = (rank - root + size) % size;
427 bmtree = new ompi_coll_tree_t;
429 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
433 bmtree->tree_bmtree = 1;
434 bmtree->tree_root = MPI_UNDEFINED;
435 bmtree->tree_nextsize = MPI_UNDEFINED;
436 for(i=0;i<MAXTREEFANOUT;i++) {
437 bmtree->tree_next[i] = -1;
441 bmtree->tree_prev = root;
444 while (mask < size) {
445 remote = vrank ^ mask;
446 if (remote < vrank) {
447 bmtree->tree_prev = (remote + root) % size;
449 } else if (remote < size) {
450 bmtree->tree_next[childs] = (remote + root) % size;
452 if (childs == MAXTREEFANOUT) {
453 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
459 bmtree->tree_nextsize = childs;
460 bmtree->tree_root = root;
467 ompi_coll_tuned_topo_build_chain( int fanout,
472 int srank; /* shifted rank */
475 ompi_coll_tree_t *chain;
477 XBT_DEBUG("coll:tuned:topo:build_chain fo %d rt %d", fanout, root);
480 * Get size and rank of the process in this communicator
486 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout of ZERO, forcing to 1 (pipeline)!");
489 if (fanout>MAXTREEFANOUT) {
490 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout %d bigger than max %d, forcing to max!", fanout, MAXTREEFANOUT);
491 fanout = MAXTREEFANOUT;
495 * Allocate space for topology arrays if needed
497 chain = new ompi_coll_tree_t;
499 XBT_DEBUG("coll:tuned:topo:build_chain PANIC out of memory");
503 chain->tree_root = MPI_UNDEFINED;
504 chain->tree_nextsize = -1;
505 for(i=0;i<fanout;i++) chain->tree_next[i] = -1;
508 * Set root & numchain
510 chain->tree_root = root;
511 if( (size - 1) < fanout ) {
512 chain->tree_nextsize = size-1;
515 chain->tree_nextsize = fanout;
522 if (srank < 0) srank += size;
525 * Special case - fanout == 1
528 if( srank == 0 ) chain->tree_prev = -1;
529 else chain->tree_prev = (srank-1+root)%size;
531 if( (srank + 1) >= size) {
532 chain->tree_next[0] = -1;
533 chain->tree_nextsize = 0;
535 chain->tree_next[0] = (srank+1+root)%size;
536 chain->tree_nextsize = 1;
541 /* Let's handle the case where there is just one node in the communicator */
543 chain->tree_next[0] = -1;
544 chain->tree_nextsize = 0;
545 chain->tree_prev = -1;
549 * Calculate maximum chain length
551 maxchainlen = (size-1) / fanout;
552 if( (size-1) % fanout != 0 ) {
554 mark = (size-1)%fanout;
560 * Find your own place in the list of shifted ranks
564 if( srank-1 < (mark * maxchainlen) ) {
565 column = (srank-1)/maxchainlen;
566 head = 1+column*maxchainlen;
569 column = mark + (srank-1-mark*maxchainlen)/(maxchainlen-1);
570 head = mark*maxchainlen+1+(column-mark)*(maxchainlen-1);
574 if( srank == head ) {
575 chain->tree_prev = 0; /*root*/
577 chain->tree_prev = srank-1; /* rank -1 */
579 if( srank == (head + len - 1) ) {
580 chain->tree_next[0] = -1;
581 chain->tree_nextsize = 0;
583 if( (srank + 1) < size ) {
584 chain->tree_next[0] = srank+1;
585 chain->tree_nextsize = 1;
587 chain->tree_next[0] = -1;
588 chain->tree_nextsize = 0;
597 chain->tree_prev = -1;
598 chain->tree_next[0] = (root+1)%size;
599 for( i = 1; i < fanout; i++ ) {
600 chain->tree_next[i] = chain->tree_next[i-1] + maxchainlen;
602 chain->tree_next[i]--;
604 chain->tree_next[i] %= size;
606 chain->tree_nextsize = fanout;
608 chain->tree_prev = (chain->tree_prev+root)%size;
609 if( chain->tree_next[0] != -1 ) {
610 chain->tree_next[0] = (chain->tree_next[0]+root)%size;
617 int ompi_coll_tuned_topo_dump_tree (ompi_coll_tree_t* tree, int rank)
621 XBT_DEBUG("coll:tuned:topo:topo_dump_tree %1d tree root %d"
622 " fanout %d BM %1d nextsize %d prev %d",
623 rank, tree->tree_root, tree->tree_bmtree, tree->tree_fanout,
624 tree->tree_nextsize, tree->tree_prev);
625 if( tree->tree_nextsize ) {
626 for( i = 0; i < tree->tree_nextsize; i++ )
627 XBT_DEBUG("[%1d] %d", i, tree->tree_next[i]);