Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f43cbc5a359b87af4c344645c40a4f1ea8fa816d
[simgrid.git] / src / smpi / colls / coll_tuned_topo.cpp
1 /* Copyright (c) 2013-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 /*
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
13  *                         reserved.
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.
18  *
19  * Additional copyrights may follow
20  */
21
22 #include "coll_tuned_topo.hpp"
23 #include "colls_private.hpp"
24 /*
25  * Some static helpers.
26  */
27 static int pown( int fanout, int num )
28 {
29     int p = 1;
30     if( num < 0 ) return 0;
31     if (1==num) return fanout;
32     if (2==fanout) {
33         return p<<num;
34     }
35     else {
36       for (int j = 0; j < num; j++) {
37         p *= fanout;
38       }
39     }
40     return p;
41 }
42
43 static int calculate_level( int fanout, int rank )
44 {
45     int level, num;
46     if( rank < 0 ) return -1;
47     for( level = 0, num = 0; num <= rank; level++ ) {
48         num += pown(fanout, level);
49     }
50     return level-1;
51 }
52
53 static int calculate_num_nodes_up_to_level( int fanout, int level )
54 {
55     /* just use geometric progression formula for sum:
56        a^0+a^1+...a^(n-1) = (a^n-1)/(a-1) */
57     if (fanout > 1)
58       return ((pown(fanout, level) - 1) / (fanout - 1));
59     else
60       return 0; // is this right ?
61 }
62
63 /*
64  * And now the building functions.
65  *
66  * An example for fanout = 2, comm_size = 7
67  *
68  *              0           <-- delta = 1 (fanout^0)
69  *            /   \
70  *           1     2        <-- delta = 2 (fanout^1)
71  *          / \   / \
72  *         3   5 4   6      <-- delta = 4 (fanout^2)
73  */
74
75 ompi_coll_tree_t*
76 ompi_coll_tuned_topo_build_tree( int fanout,
77                                  MPI_Comm comm,
78                                  int root )
79 {
80     int rank, size;
81     int sparent;
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 */
85     int shiftedrank;
86     ompi_coll_tree_t* tree;
87
88     XBT_DEBUG("coll:tuned:topo_build_tree Building fo %d rt %d", fanout, root);
89
90     if (fanout<1) {
91         XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d", fanout);
92         return NULL;
93     }
94     if (fanout>MAXTREEFANOUT) {
95         XBT_DEBUG("coll:tuned:topo_build_tree invalid fanout %d bigger than max %d", fanout, MAXTREEFANOUT);
96         return NULL;
97     }
98
99     /*
100      * Get size and rank of the process in this communicator
101      */
102     size = comm->size();
103     rank = comm->rank();
104
105     tree = new ompi_coll_tree_t;
106     if (not tree) {
107       XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
108       return NULL;
109     }
110
111     tree->tree_root     = MPI_UNDEFINED;
112     tree->tree_nextsize = MPI_UNDEFINED;
113
114     /*
115      * Set root
116      */
117     tree->tree_root = root;
118
119     /*
120      * Initialize tree
121      */
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;
129     }
130
131     /* return if we have less than 2 processes */
132     if( size < 2 ) {
133         return tree;
134     }
135
136     /*
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
141      */
142     shiftedrank = rank - root;
143     if( shiftedrank < 0 ) {
144         shiftedrank += size;
145     }
146
147     /* calculate my level */
148     level = calculate_level( fanout, shiftedrank );
149     delta = pown( fanout, level );
150
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;
157         } else {
158             break;
159         }
160     }
161
162     /* find my parent */
163     slimit = calculate_num_nodes_up_to_level( fanout, level );
164     sparent = shiftedrank;
165     if( sparent < fanout ) {
166         sparent = 0;
167     } else {
168         while( sparent >= slimit ) {
169             sparent -= delta/fanout;
170         }
171     }
172     tree->tree_prev = (sparent+root)%size;
173
174     return tree;
175 }
176
177 /*
178  * Constructs in-order binary tree which can be used for non-commutative reduce
179  * operations.
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
183  *      1             2             3                        8
184  *     /             / \          /   \                    /   \
185  *    0             1  0         2     1                  7     3
186  *                                    /                 /  \   / \
187  *                                   0                 6    5 2   1
188  *                                                         /     /
189  *                                                        4     0
190  */
191 ompi_coll_tree_t*
192 ompi_coll_tuned_topo_build_in_order_bintree( MPI_Comm comm )
193 {
194     int rank, size;
195     int myrank, delta;
196     int parent;
197     ompi_coll_tree_t* tree;
198
199     /*
200      * Get size and rank of the process in this communicator
201      */
202     size = comm->size();
203     rank = comm->rank();
204
205     tree = new ompi_coll_tree_t;
206     if (not tree) {
207       XBT_DEBUG("coll:tuned:topo_build_tree PANIC::out of memory");
208       return NULL;
209     }
210
211     tree->tree_root     = MPI_UNDEFINED;
212     tree->tree_nextsize = MPI_UNDEFINED;
213
214     /*
215      * Initialize tree
216      */
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;
224     XBT_DEBUG(
225                  "coll:tuned:topo_build_in_order_tree Building fo %d rt %d",
226                  tree->tree_fanout, tree->tree_root);
227
228     /*
229      * Build the tree
230      */
231     myrank = rank;
232     parent = size - 1;
233     delta = 0;
234
235     while ( 1 ) {
236         /* Compute the size of the right subtree */
237         int rightsize = size >> 1;
238
239         /* Determine the left and right child of this parent  */
240         int lchild = -1;
241         int rchild = -1;
242         if (size - 1 > 0) {
243             lchild = parent - 1;
244             if (lchild > 0) {
245                 rchild = rightsize - 1;
246             }
247         }
248
249         /* The following cases are possible: myrank can be
250            - a parent,
251            - belong to the left subtree, or
252            - belong to the right subtee
253            Each of the cases need to be handled differently.
254         */
255
256         if (myrank == parent) {
257             /* I am the 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;
261             break;
262         }
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.
268             */
269             if (myrank == lchild) {
270                 tree->tree_prev = parent + delta;
271             }
272             size = size - rightsize - 1;
273             delta = delta + rightsize;
274             myrank = myrank - rightsize;
275             parent = size - 1;
276
277         } else {
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.
283             */
284             if (myrank == rchild) {
285                 tree->tree_prev = parent + delta;
286             }
287             size = rightsize;
288             parent = rchild;
289         }
290     }
291
292     if (tree->tree_next[0] >= 0) { tree->tree_nextsize = 1; }
293     if (tree->tree_next[1] >= 0) { tree->tree_nextsize += 1; }
294
295     return tree;
296 }
297
298 int ompi_coll_tuned_topo_destroy_tree( ompi_coll_tree_t** tree )
299 {
300     ompi_coll_tree_t *ptr;
301
302     if ((tree == nullptr) || (*tree == nullptr)) {
303       return MPI_SUCCESS;
304     }
305
306     ptr = *tree;
307
308     delete ptr;
309     *tree = NULL;   /* mark tree as gone */
310
311     return MPI_SUCCESS;
312 }
313
314 /*
315  *
316  * Here are some of the examples of this tree:
317  * size == 2                   size = 4                 size = 8
318  *      0                           0                        0
319  *     /                            | \                    / | \
320  *    1                             2  1                  4  2  1
321  *                                     |                     |  |\
322  *                                     3                     6  5 3
323  *                                                                |
324  *                                                                7
325  */
326 ompi_coll_tree_t*
327 ompi_coll_tuned_topo_build_bmtree( MPI_Comm comm,
328                                    int root )
329 {
330     int childs = 0;
331     int rank;
332     int size;
333     int mask = 1;
334     int index;
335     int remote;
336     ompi_coll_tree_t *bmtree;
337     int i;
338
339     XBT_DEBUG("coll:tuned:topo:build_bmtree rt %d", root);
340
341     /*
342      * Get size and rank of the process in this communicator
343      */
344     size = comm->size();
345     rank = comm->rank();
346
347     index = rank -root;
348
349     bmtree = new ompi_coll_tree_t;
350     if (not bmtree) {
351       XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
352       return NULL;
353     }
354
355     bmtree->tree_bmtree   = 1;
356
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;
361     }
362
363     if( index < 0 ) index += size;
364
365     while( mask <= index ) mask <<= 1;
366
367     /* Now I can compute my father rank */
368     if( root == rank ) {
369         bmtree->tree_prev = root;
370     } else {
371         remote = (index ^ (mask >> 1)) + root;
372         if( remote >= size ) remote -= size;
373         bmtree->tree_prev = remote;
374     }
375     /* And now let's fill my childs */
376     while( mask < size ) {
377         remote = (index ^ mask);
378         if( remote >= size ) break;
379         remote += root;
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);
383             return NULL;
384         }
385         bmtree->tree_next[childs] = remote;
386         mask <<= 1;
387         childs++;
388     }
389     bmtree->tree_nextsize = childs;
390     bmtree->tree_root     = root;
391     return bmtree;
392 }
393
394 /*
395  * Constructs in-order binomial tree which can be used for gather/scatter
396  * operations.
397  *
398  * Here are some of the examples of this tree:
399  * size == 2                   size = 4                 size = 8
400  *      0                           0                        0
401  *     /                          / |                      / | \
402  *    1                          1  2                     1  2  4
403  *                                  |                        |  | \
404  *                                  3                        3  5  6
405  *                                                                 |
406  *                                                                 7
407  */
408 ompi_coll_tree_t* ompi_coll_tuned_topo_build_in_order_bmtree(MPI_Comm comm, int root)
409 {
410     int childs = 0;
411     int rank, vrank;
412     int size;
413     int mask = 1;
414     ompi_coll_tree_t *bmtree;
415     int i;
416
417     XBT_DEBUG("coll:tuned:topo:build_in_order_bmtree rt %d", root);
418
419     /*
420      * Get size and rank of the process in this communicator
421      */
422     size = comm->size();
423     rank = comm->rank();
424
425     vrank = (rank - root + size) % size;
426
427     bmtree = new ompi_coll_tree_t;
428     if (not bmtree) {
429       XBT_DEBUG("coll:tuned:topo:build_bmtree PANIC out of memory");
430       return NULL;
431     }
432
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;
438     }
439
440     if (root == rank) {
441       bmtree->tree_prev = root;
442     }
443
444     while (mask < size) {
445       int remote = vrank ^ mask;
446       if (remote < vrank) {
447         bmtree->tree_prev = (remote + root) % size;
448         break;
449       } else if (remote < size) {
450         bmtree->tree_next[childs] = (remote + root) % size;
451         childs++;
452         if (childs == MAXTREEFANOUT) {
453           XBT_DEBUG("coll:tuned:topo:build_bmtree max fanout incorrect %d needed %d", MAXTREEFANOUT, childs);
454           return NULL;
455         }
456       }
457       mask <<= 1;
458     }
459     bmtree->tree_nextsize = childs;
460     bmtree->tree_root     = root;
461
462     return bmtree;
463 }
464
465
466 ompi_coll_tree_t*
467 ompi_coll_tuned_topo_build_chain( int fanout,
468                                   MPI_Comm comm,
469                                   int root )
470 {
471     int rank, size;
472     int srank; /* shifted rank */
473     int i,maxchainlen;
474     int mark;
475     ompi_coll_tree_t *chain;
476
477     XBT_DEBUG("coll:tuned:topo:build_chain fo %d rt %d", fanout, root);
478
479     /*
480      * Get size and rank of the process in this communicator
481      */
482     size = comm->size();
483     rank = comm->rank();
484
485     if( fanout < 1 ) {
486         XBT_DEBUG("coll:tuned:topo:build_chain WARNING invalid fanout of ZERO, forcing to 1 (pipeline)!");
487         fanout = 1;
488     }
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;
492     }
493
494     /*
495      * Allocate space for topology arrays if needed
496      */
497     chain = new ompi_coll_tree_t;
498     if (not chain) {
499       XBT_DEBUG("coll:tuned:topo:build_chain PANIC out of memory");
500       fflush(stdout);
501       return NULL;
502     }
503     chain->tree_root     = MPI_UNDEFINED;
504     chain->tree_nextsize = -1;
505     for(i=0;i<fanout;i++) chain->tree_next[i] = -1;
506
507     /*
508      * Set root & numchain
509      */
510     chain->tree_root = root;
511     if( (size - 1) < fanout ) {
512         chain->tree_nextsize = size-1;
513         fanout = size-1;
514     } else {
515         chain->tree_nextsize = fanout;
516     }
517
518     /*
519      * Shift ranks
520      */
521     srank = rank - root;
522     if (srank < 0) srank += size;
523
524     /*
525      * Special case - fanout == 1
526      */
527     if( fanout == 1 ) {
528         if( srank == 0 ) chain->tree_prev = -1;
529         else chain->tree_prev = (srank-1+root)%size;
530
531         if( (srank + 1) >= size) {
532             chain->tree_next[0] = -1;
533             chain->tree_nextsize = 0;
534         } else {
535             chain->tree_next[0] = (srank+1+root)%size;
536             chain->tree_nextsize = 1;
537         }
538         return chain;
539     }
540
541     /* Let's handle the case where there is just one node in the communicator */
542     if( size == 1 ) {
543         chain->tree_next[0] = -1;
544         chain->tree_nextsize = 0;
545         chain->tree_prev = -1;
546         return chain;
547     }
548     /*
549      * Calculate maximum chain length
550      */
551     maxchainlen = (size-1) / fanout;
552     if( (size-1) % fanout != 0 ) {
553         maxchainlen++;
554         mark = (size-1)%fanout;
555     } else {
556         mark = fanout+1;
557     }
558
559     /*
560      * Find your own place in the list of shifted ranks
561      */
562     if( srank != 0 ) {
563         int column;
564         int head;
565         int len;
566         if( srank-1 < (mark * maxchainlen) ) {
567             column = (srank-1)/maxchainlen;
568             head = 1+column*maxchainlen;
569             len = maxchainlen;
570         } else {
571             column = mark + (srank-1-mark*maxchainlen)/(maxchainlen-1);
572             head = mark*maxchainlen+1+(column-mark)*(maxchainlen-1);
573             len = maxchainlen-1;
574         }
575
576         if( srank == head ) {
577             chain->tree_prev = 0; /*root*/
578         } else {
579             chain->tree_prev = srank-1; /* rank -1 */
580         }
581         if( srank == (head + len - 1) ) {
582             chain->tree_next[0] = -1;
583             chain->tree_nextsize = 0;
584         } else {
585             if( (srank + 1) < size ) {
586                 chain->tree_next[0] = srank+1;
587                 chain->tree_nextsize = 1;
588             } else {
589                 chain->tree_next[0] = -1;
590                 chain->tree_nextsize = 0;
591             }
592         }
593     }
594
595     /*
596      * Unshift values
597      */
598     if( rank == root ) {
599         chain->tree_prev = -1;
600         chain->tree_next[0] = (root+1)%size;
601         for (int i = 1; i < fanout; i++) {
602             chain->tree_next[i] = chain->tree_next[i-1] + maxchainlen;
603             if( i > mark ) {
604                 chain->tree_next[i]--;
605             }
606             chain->tree_next[i] %= size;
607         }
608         chain->tree_nextsize = fanout;
609     } else {
610         chain->tree_prev = (chain->tree_prev+root)%size;
611         if( chain->tree_next[0] != -1 ) {
612             chain->tree_next[0] = (chain->tree_next[0]+root)%size;
613         }
614     }
615
616     return chain;
617 }
618
619 int ompi_coll_tuned_topo_dump_tree (ompi_coll_tree_t* tree, int rank)
620 {
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 (int i = 0; i < tree->tree_nextsize; i++)
627         XBT_DEBUG("[%1d] %d", i, tree->tree_next[i]);
628     }
629     return (0);
630 }