1 /* Copyright (c) 2013-2017. 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 "colls_private.h"
23 #include "coll_tuned_topo.h"
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) */
55 return ((pown(fanout,level) - 1)/(fanout - 1));
59 * And now the building functions.
61 * An example for fanout = 2, comm_size = 7
63 * 0 <-- delta = 1 (fanout^0)
65 * 1 2 <-- delta = 2 (fanout^1)
67 * 3 5 4 6 <-- delta = 4 (fanout^2)
71 ompi_coll_tuned_topo_build_tree( int fanout,
77 int level; /* location of my rank in the tree structure of size */
78 int delta; /* number of nodes on my level */
79 int slimit; /* total number of nodes on levels above me */
82 ompi_coll_tree_t* tree;
84 XBT_DEBUG("coll:tuned:topo_build_tree Building fo %d rt %d", fanout, root);
87 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d", fanout);
90 if (fanout>MAXTREEFANOUT) {
91 XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d bigger than max %d", fanout, MAXTREEFANOUT);
96 * Get size and rank of the process in this communicator
101 tree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t));
103 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
107 tree->tree_root = MPI_UNDEFINED;
108 tree->tree_nextsize = MPI_UNDEFINED;
113 tree->tree_root = root;
118 tree->tree_fanout = fanout;
119 tree->tree_bmtree = 0;
120 tree->tree_root = root;
121 tree->tree_prev = -1;
122 tree->tree_nextsize = 0;
123 for( i = 0; i < fanout; i++ ) {
124 tree->tree_next[i] = -1;
127 /* return if we have less than 2 processes */
133 * Shift all ranks by root, so that the algorithm can be
134 * designed as if root would be always 0
135 * shiftedrank should be used in calculating distances
136 * and position in tree
138 shiftedrank = rank - root;
139 if( shiftedrank < 0 ) {
143 /* calculate my level */
144 level = calculate_level( fanout, shiftedrank );
145 delta = pown( fanout, level );
147 /* find my children */
148 for( i = 0; i < fanout; i++ ) {
149 schild = shiftedrank + delta * (i+1);
150 if( schild < size ) {
151 tree->tree_next[i] = (schild+root)%size;
152 tree->tree_nextsize = tree->tree_nextsize + 1;
159 slimit = calculate_num_nodes_up_to_level( fanout, level );
160 sparent = shiftedrank;
161 if( sparent < fanout ) {
164 while( sparent >= slimit ) {
165 sparent -= delta/fanout;
168 tree->tree_prev = (sparent+root)%size;
174 * Constructs in-order binary tree which can be used for non-commutative reduce
176 * Root of this tree is always rank (size-1) and fanout is 2.
177 * Here are some of the examples of this tree:
178 * size == 2 size == 3 size == 4 size == 9
188 ompi_coll_tuned_topo_build_in_order_bintree( MPI_Comm comm )
191 int myrank, rightsize, delta;
192 int parent, lchild, rchild;
193 ompi_coll_tree_t* tree;
196 * Get size and rank of the process in this communicator
201 tree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t));
203 XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
207 tree->tree_root = MPI_UNDEFINED;
208 tree->tree_nextsize = MPI_UNDEFINED;
213 tree->tree_fanout = 2;
214 tree->tree_bmtree = 0;
215 tree->tree_root = size - 1;
216 tree->tree_prev = -1;
217 tree->tree_nextsize = 0;
218 tree->tree_next[0] = -1;
219 tree->tree_next[1] = -1;
221 "coll:tuned:topo_build_in_order_tree Building fo %d rt %d",
222 tree->tree_fanout, tree->tree_root);
232 /* Compute the size of the right subtree */
233 rightsize = size >> 1;
235 /* Determine the left and right child of this parent */
241 rchild = rightsize - 1;
245 /* The following cases are possible: myrank can be
247 - belong to the left subtree, or
248 - belong to the right subtee
249 Each of the cases need to be handled differently.
252 if (myrank == parent) {
254 - compute real ranks of my children, and exit the loop. */
255 if (lchild >= 0) tree->tree_next[0] = lchild + delta;
256 if (rchild >= 0) tree->tree_next[1] = rchild + delta;
259 if (myrank > rchild) {
260 /* I belong to the left subtree:
261 - If I am the left child, compute real rank of my parent
262 - Iterate down through tree:
263 compute new size, shift ranks down, and update delta.
265 if (myrank == lchild) {
266 tree->tree_prev = parent + delta;
268 size = size - rightsize - 1;
269 delta = delta + rightsize;
270 myrank = myrank - rightsize;
274 /* I belong to the right subtree:
275 - If I am the right child, compute real rank of my parent
276 - Iterate down through tree:
277 compute new size and parent,
278 but the delta and rank do not need to change.
280 if (myrank == rchild) {
281 tree->tree_prev = parent + delta;
288 if (tree->tree_next[0] >= 0) { tree->tree_nextsize = 1; }
289 if (tree->tree_next[1] >= 0) { tree->tree_nextsize += 1; }
294 int ompi_coll_tuned_topo_destroy_tree( ompi_coll_tree_t** tree )
296 ompi_coll_tree_t *ptr;
298 if ((tree == nullptr) || (*tree == nullptr)) {
305 *tree = NULL; /* mark tree as gone */
312 * Here are some of the examples of this tree:
313 * size == 2 size = 4 size = 8
323 ompi_coll_tuned_topo_build_bmtree( MPI_Comm comm,
332 ompi_coll_tree_t *bmtree;
335 XBT_DEBUG("coll:tuned:topo:build_bmtree rt %d", root);
338 * Get size and rank of the process in this communicator
345 bmtree = (ompi_coll_tree_t*)malloc(sizeof(ompi_coll_tree_t));
347 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
351 bmtree->tree_bmtree = 1;
353 bmtree->tree_root = MPI_UNDEFINED;
354 bmtree->tree_nextsize = MPI_UNDEFINED;
355 for(i=0;i<MAXTREEFANOUT;i++) {
356 bmtree->tree_next[i] = -1;
359 if( index < 0 ) index += size;
361 while( mask <= index ) mask <<= 1;
363 /* Now I can compute my father rank */
365 bmtree->tree_prev = root;
367 remote = (index ^ (mask >> 1)) + root;
368 if( remote >= size ) remote -= size;
369 bmtree->tree_prev = remote;
371 /* And now let's fill my childs */
372 while( mask < size ) {
373 remote = (index ^ mask);
374 if( remote >= size ) break;
376 if( remote >= size ) remote -= size;
377 if (childs==MAXTREEFANOUT) {
378 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
381 bmtree->tree_next[childs] = remote;
385 bmtree->tree_nextsize = childs;
386 bmtree->tree_root = root;
391 * Constructs in-order binomial tree which can be used for gather/scatter
394 * Here are some of the examples of this tree:
395 * size == 2 size = 4 size = 8
404 ompi_coll_tree_t* ompi_coll_tuned_topo_build_in_order_bmtree(MPI_Comm comm, int root)
411 ompi_coll_tree_t *bmtree;
414 XBT_DEBUG("coll:tuned:topo:build_in_order_bmtree rt %d", root);
417 * Get size and rank of the process in this communicator
422 vrank = (rank - root + size) % size;
424 bmtree = (ompi_coll_tree_t*)xbt_malloc(sizeof(ompi_coll_tree_t));
426 XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
430 bmtree->tree_bmtree = 1;
431 bmtree->tree_root = MPI_UNDEFINED;
432 bmtree->tree_nextsize = MPI_UNDEFINED;
433 for(i=0;i<MAXTREEFANOUT;i++) {
434 bmtree->tree_next[i] = -1;
438 bmtree->tree_prev = root;
441 while (mask < size) {
442 remote = vrank ^ mask;
443 if (remote < vrank) {
444 bmtree->tree_prev = (remote + root) % size;
446 } else if (remote < size) {
447 bmtree->tree_next[childs] = (remote + root) % size;
449 if (childs == MAXTREEFANOUT) {
450 XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
456 bmtree->tree_nextsize = childs;
457 bmtree->tree_root = root;
464 ompi_coll_tuned_topo_build_chain( int fanout,
469 int srank; /* shifted rank */
472 ompi_coll_tree_t *chain;
474 XBT_DEBUG("coll:tuned:topo:build_chain fo %d rt %d", fanout, root);
477 * Get size and rank of the process in this communicator
483 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout of ZERO, forcing to 1 (pipeline)!");
486 if (fanout>MAXTREEFANOUT) {
487 XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout %d bigger than max %d, forcing to max!", fanout, MAXTREEFANOUT);
488 fanout = MAXTREEFANOUT;
492 * Allocate space for topology arrays if needed
494 chain = (ompi_coll_tree_t*)malloc( sizeof(ompi_coll_tree_t) );
496 XBT_DEBUG("coll:tuned:topo:build_chain PANIC out of memory");
500 chain->tree_root = MPI_UNDEFINED;
501 chain->tree_nextsize = -1;
502 for(i=0;i<fanout;i++) chain->tree_next[i] = -1;
505 * Set root & numchain
507 chain->tree_root = root;
508 if( (size - 1) < fanout ) {
509 chain->tree_nextsize = size-1;
512 chain->tree_nextsize = fanout;
519 if (srank < 0) srank += size;
522 * Special case - fanout == 1
525 if( srank == 0 ) chain->tree_prev = -1;
526 else chain->tree_prev = (srank-1+root)%size;
528 if( (srank + 1) >= size) {
529 chain->tree_next[0] = -1;
530 chain->tree_nextsize = 0;
532 chain->tree_next[0] = (srank+1+root)%size;
533 chain->tree_nextsize = 1;
538 /* Let's handle the case where there is just one node in the communicator */
540 chain->tree_next[0] = -1;
541 chain->tree_nextsize = 0;
542 chain->tree_prev = -1;
546 * Calculate maximum chain length
548 maxchainlen = (size-1) / fanout;
549 if( (size-1) % fanout != 0 ) {
551 mark = (size-1)%fanout;
557 * Find your own place in the list of shifted ranks
561 if( srank-1 < (mark * maxchainlen) ) {
562 column = (srank-1)/maxchainlen;
563 head = 1+column*maxchainlen;
566 column = mark + (srank-1-mark*maxchainlen)/(maxchainlen-1);
567 head = mark*maxchainlen+1+(column-mark)*(maxchainlen-1);
571 if( srank == head ) {
572 chain->tree_prev = 0; /*root*/
574 chain->tree_prev = srank-1; /* rank -1 */
576 if( srank == (head + len - 1) ) {
577 chain->tree_next[0] = -1;
578 chain->tree_nextsize = 0;
580 if( (srank + 1) < size ) {
581 chain->tree_next[0] = srank+1;
582 chain->tree_nextsize = 1;
584 chain->tree_next[0] = -1;
585 chain->tree_nextsize = 0;
594 chain->tree_prev = -1;
595 chain->tree_next[0] = (root+1)%size;
596 for( i = 1; i < fanout; i++ ) {
597 chain->tree_next[i] = chain->tree_next[i-1] + maxchainlen;
599 chain->tree_next[i]--;
601 chain->tree_next[i] %= size;
603 chain->tree_nextsize = fanout;
605 chain->tree_prev = (chain->tree_prev+root)%size;
606 if( chain->tree_next[0] != -1 ) {
607 chain->tree_next[0] = (chain->tree_next[0]+root)%size;
614 int ompi_coll_tuned_topo_dump_tree (ompi_coll_tree_t* tree, int rank)
618 XBT_DEBUG("coll:tuned:topo:topo_dump_tree %1d tree root %d"
619 " fanout %d BM %1d nextsize %d prev %d",
620 rank, tree->tree_root, tree->tree_bmtree, tree->tree_fanout,
621 tree->tree_nextsize, tree->tree_prev);
622 if( tree->tree_nextsize ) {
623 for( i = 0; i < tree->tree_nextsize; i++ )
624 XBT_DEBUG("[%1d] %d", i, tree->tree_next[i]);