Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
681690a63b97740290de8b1d0051b548dbc88dc2
[simgrid.git] / src / smpi / colls / allgather-bruck.c
1 #include "colls_private.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: allgather_bruck
49  * return: int
50  * inputs:
51  *   send_buff: send input buffer
52  *   send_count: number of elements to send
53  *   send_type: data type of elements being sent
54  *   recv_buff: receive output buffer
55  *   recv_count: number of elements to received
56  *   recv_type: data type of elements being received
57  *   comm: communication
58  * Descrp: Function realizes the allgather operation using the bruck
59  *         algorithm.
60  * Auther: MPICH
61  * Comment: Original bruck algorithm from MPICH is slightly modified by
62  *          Ahmad Faraj.  
63  ****************************************************************************/
64 int smpi_coll_tuned_allgather_bruck(void *send_buff, int send_count,
65                                     MPI_Datatype send_type, void *recv_buff,
66                                     int recv_count, MPI_Datatype recv_type,
67                                     MPI_Comm comm)
68 {
69   // MPI variables
70   MPI_Status status;
71   MPI_Aint recv_extent;
72
73   // local int variables
74   int src, dst, rank, num_procs, count, remainder;
75   int tag = 1;
76   int pof2 = 1;
77
78   // local string variables
79   char *tmp_buff;
80   char *send_ptr = (char *) send_buff;
81   char *recv_ptr = (char *) recv_buff;
82
83   // get size of the communicator, followed by rank 
84   num_procs = smpi_comm_size(comm);
85   rank = smpi_comm_rank(comm);
86
87   // get size of single element's type for recv buffer
88   recv_extent = smpi_datatype_get_extent(recv_type);
89
90   count = recv_count;
91
92   tmp_buff = (char *) xbt_malloc(num_procs * recv_count * recv_extent);
93
94   // perform a local copy
95   smpi_datatype_copy(send_ptr, send_count, send_type,
96                      tmp_buff, recv_count, recv_type);
97   while (pof2 <= (num_procs / 2)) {
98     src = (rank + pof2) % num_procs;
99     dst = (rank - pof2 + num_procs) % num_procs;
100
101     smpi_mpi_sendrecv(tmp_buff, count, recv_type, dst, tag,
102                   tmp_buff + count * recv_extent, count, recv_type,
103                   src, tag, comm, &status);
104     count *= 2;
105     pof2 *= 2;
106   }
107
108   remainder = num_procs - pof2;
109   if (remainder) {
110     src = (rank + pof2) % num_procs;
111     dst = (rank - pof2 + num_procs) % num_procs;
112
113     smpi_mpi_sendrecv(tmp_buff, remainder * recv_count, recv_type, dst, tag,
114                   tmp_buff + count * recv_extent, remainder * recv_count,
115                   recv_type, src, tag, comm, &status);
116   }
117
118   smpi_mpi_sendrecv(tmp_buff, (num_procs - rank) * recv_count, recv_type, rank,
119                 tag, recv_ptr + rank * recv_count * recv_extent,
120                 (num_procs - rank) * recv_count, recv_type, rank, tag, comm,
121                 &status);
122
123   if (rank)
124     smpi_mpi_sendrecv(tmp_buff + (num_procs - rank) * recv_count * recv_extent,
125                   rank * recv_count, recv_type, rank, tag, recv_ptr,
126                   rank * recv_count, recv_type, rank, tag, comm, &status);
127   free(tmp_buff);
128   return MPI_SUCCESS;
129 }