Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move collective algorithms to separate folders
[simgrid.git] / src / smpi / colls / allgather / allgather-bruck.cpp
1 /* Copyright (c) 2013-2014. 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 #include "../colls_private.h"
8
9 /*****************************************************************************
10
11 Copyright (c) 2006, Ahmad Faraj & Xin Yuan,
12 All rights reserved.
13
14 Redistribution and use in source and binary forms, with or without
15 modification, are permitted provided that the following conditions are met:
16
17   * Redistributions of source code must retain the above copyright notice,
18     this list of conditions and the following disclaimer.
19
20   * Redistributions in binary form must reproduce the above copyright notice,
21     this list of conditions and the following disclaimer in the documentation
22     and/or other materials provided with the distribution.
23
24   * Neither the name of the Florida State University nor the names of its
25     contributors may be used to endorse or promote products derived from this
26     software without specific prior written permission.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
32 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
35 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39   *************************************************************************
40   *     Any results obtained from executing this software require the     *
41   *     acknowledgment and citation of the software and its owners.       *
42   *     The full citation is given below:                                 *
43   *                                                                       *
44   *     A. Faraj and X. Yuan. "Automatic Generation and Tuning of MPI     *
45   *     Collective Communication Routines." The 19th ACM International    *
46   *     Conference on Supercomputing (ICS), Cambridge, Massachusetts,     *
47   *     June 20-22, 2005.                                                 *
48   *************************************************************************
49
50 *****************************************************************************/
51
52
53 /*****************************************************************************
54  * Function: allgather_bruck
55  * return: int
56  * inputs:
57  *   send_buff: send input buffer
58  *   send_count: number of elements to send
59  *   send_type: data type of elements being sent
60  *   recv_buff: receive output buffer
61  *   recv_count: number of elements to received
62  *   recv_type: data type of elements being received
63  *   comm: communication
64  * Descrp: Function realizes the allgather operation using the bruck
65  *         algorithm.
66  * Auther: MPICH
67  * Comment: Original bruck algorithm from MPICH is slightly modified by
68  *          Ahmad Faraj.  
69  ****************************************************************************/
70 int smpi_coll_tuned_allgather_bruck(void *send_buff, int send_count,
71                                     MPI_Datatype send_type, void *recv_buff,
72                                     int recv_count, MPI_Datatype recv_type,
73                                     MPI_Comm comm)
74 {
75   // MPI variables
76   MPI_Status status;
77   MPI_Aint recv_extent;
78
79   // local int variables
80   int src, dst, rank, num_procs, count, remainder;
81   int tag = COLL_TAG_ALLGATHER;
82   int pof2 = 1;
83
84   // local string variables
85   char *tmp_buff;
86   char *send_ptr = (char *) send_buff;
87   char *recv_ptr = (char *) recv_buff;
88
89   // get size of the communicator, followed by rank 
90   num_procs = comm->size();
91   rank = comm->rank();
92
93   // get size of single element's type for recv buffer
94   recv_extent = recv_type->get_extent();
95
96   count = recv_count;
97
98   tmp_buff = (char *) smpi_get_tmp_sendbuffer(num_procs * recv_count * recv_extent);
99
100   // perform a local copy
101   Datatype::copy(send_ptr, send_count, send_type,
102                      tmp_buff, recv_count, recv_type);
103   while (pof2 <= (num_procs / 2)) {
104     src = (rank + pof2) % num_procs;
105     dst = (rank - pof2 + num_procs) % num_procs;
106
107     Request::sendrecv(tmp_buff, count, recv_type, dst, tag,
108                   tmp_buff + count * recv_extent, count, recv_type,
109                   src, tag, comm, &status);
110     count *= 2;
111     pof2 *= 2;
112   }
113
114   remainder = num_procs - pof2;
115   if (remainder) {
116     src = (rank + pof2) % num_procs;
117     dst = (rank - pof2 + num_procs) % num_procs;
118
119     Request::sendrecv(tmp_buff, remainder * recv_count, recv_type, dst, tag,
120                   tmp_buff + count * recv_extent, remainder * recv_count,
121                   recv_type, src, tag, comm, &status);
122   }
123
124   Request::sendrecv(tmp_buff, (num_procs - rank) * recv_count, recv_type, rank,
125                 tag, recv_ptr + rank * recv_count * recv_extent,
126                 (num_procs - rank) * recv_count, recv_type, rank, tag, comm,
127                 &status);
128
129   if (rank)
130     Request::sendrecv(tmp_buff + (num_procs - rank) * recv_count * recv_extent,
131                   rank * recv_count, recv_type, rank, tag, recv_ptr,
132                   rank * recv_count, recv_type, rank, tag, comm, &status);
133   smpi_free_tmp_buffer(tmp_buff);
134   return MPI_SUCCESS;
135 }