Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add collectives for allgather, allreduce, bcast and reduce
[simgrid.git] / src / smpi / colls / bcast-binomial-tree.c
1 #include "colls.h"
2
3 /*****************************************************************************
4
5 Copyright (c) 2006, Ahmad Faraj & Xin Yuan,
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11   * Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13
14   * Redistributions in binary form must reproduce the above copyright notice,
15     this list of conditions and the following disclaimer in the documentation
16     and/or other materials provided with the distribution.
17
18   * Neither the name of the Florida State University nor the names of its
19     contributors may be used to endorse or promote products derived from this
20     software without specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33   *************************************************************************
34   *     Any results obtained from executing this software require the     *
35   *     acknowledgment and citation of the software and its owners.       *
36   *     The full citation is given below:                                 *
37   *                                                                       *
38   *     A. Faraj and X. Yuan. "Automatic Generation and Tuning of MPI     *
39   *     Collective Communication Routines." The 19th ACM International    *
40   *     Conference on Supercomputing (ICS), Cambridge, Massachusetts,     *
41   *     June 20-22, 2005.                                                 *
42   *************************************************************************
43
44 *****************************************************************************/
45
46 /*****************************************************************************
47
48  * Function: bcast_binomial_tree
49
50  * Return: int
51
52  * Inputs:
53     buff: send input buffer
54     count: number of elements to send
55     data_type: data type of elements being sent
56     root: source of data
57     comm: communicator
58
59  * Descrp: broadcasts using a bionomial tree.
60
61  * Auther: MPIH / modified by Ahmad Faraj
62
63  ****************************************************************************/
64
65 int
66 smpi_coll_tuned_bcast_binomial_tree(void *buff, int count,
67                                     MPI_Datatype data_type, int root,
68                                     MPI_Comm comm)
69 {
70   int src, dst, rank, num_procs, mask, relative_rank;
71   int tag = 1, success = 0;
72
73   MPI_Comm_rank(comm, &rank);
74   MPI_Comm_size(comm, &num_procs);
75
76   relative_rank = (rank >= root) ? rank - root : rank - root + num_procs;
77
78   mask = 0x1;
79   while (mask < num_procs) {
80     if (relative_rank & mask) {
81       src = rank - mask;
82       if (src < 0)
83         src += num_procs;
84       MPI_Recv(buff, count, data_type, src, tag, comm, MPI_STATUS_IGNORE);
85       break;
86     }
87     mask <<= 1;
88   }
89
90   mask >>= 1;
91   while (mask > 0) {
92     if (relative_rank + mask < num_procs) {
93       dst = rank + mask;
94       if (dst >= num_procs)
95         dst -= num_procs;
96       MPI_Send(buff, count, data_type, dst, tag, comm);
97     }
98     mask >>= 1;
99   }
100
101   return success;
102 }