Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8fa5d4070376327be03f228845a3b1f0c4e548cd
[simgrid.git] / src / smpi / colls / allgather-2dmesh.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: is_2dmesh
49
50  * Return: int
51
52  * Inputs:
53      num: the number of processors in a communicator
54      i: x dimension
55      j: y dimension
56
57  * Descp: takes a number and tries to find a factoring of x, y mesh out of it
58
59  * Auther: Ahmad Faraj
60  ****************************************************************************/
61 #ifndef TWOD
62 #define TWOD
63 static int is_2dmesh(int num, int *i, int *j)
64 {
65   int x, max = num / 2;
66   x = sqrt(num);
67
68   while (x <= max) {
69     if ((num % x) == 0) {
70       *i = x;
71       *j = num / x;
72
73       if (*i > *j) {
74         x = *i;
75         *i = *j;
76         *j = x;
77       }
78
79       return 1;
80     }
81     x++;
82   }
83   return 0;
84 }
85 #endif
86 /*****************************************************************************
87  * Function: allgather_2dmesh_shoot
88  * return: int
89  * send_buff: send input buffer
90  * send_count: number of elements to send
91  * send_type: data type of elements being sent
92  * recv_buff: receive output buffer
93  * recv_count: number of elements to received
94  * recv_type: data type of elements being received
95  * comm: communication
96  * Descrp: Function realizes the allgather operation using the 2dmesh
97  * algorithm. Allgather ommunication occurs first in the x dimension then in
98  * the y dimension.  The communication in each dimension follows 
99  * "simple"
100  * Auther: Ahmad Faraj
101 ****************************************************************************/
102 int
103 smpi_coll_tuned_allgather_2dmesh(void *send_buff, int send_count, MPI_Datatype
104                                  send_type, void *recv_buff, int recv_count,
105                                  MPI_Datatype recv_type, MPI_Comm comm)
106 {
107   MPI_Request *req, *req_ptr;
108   MPI_Aint extent;
109
110   int i, src, dst, rank, num_procs;
111   int X, Y, send_offset, recv_offset;
112   int my_row_base, my_col_base, src_row_base, block_size, num_reqs;
113   int success = 0;
114   int failure = 1;
115   int tag = 1;
116
117   MPI_Comm_rank(comm, &rank);
118   MPI_Comm_size(comm, &num_procs);
119
120   MPI_Type_extent(send_type, &extent);
121
122   block_size = extent * send_count;
123
124   is_2dmesh(num_procs, &X, &Y);
125   my_row_base = (rank / Y) * Y;
126   my_col_base = rank % Y;
127
128   num_reqs = X;
129   if (Y > X)
130     num_reqs = Y;
131
132   req = (MPI_Request *) malloc(num_reqs * sizeof(MPI_Request));
133   if (!req) {
134     printf("allgather-2dmesh-shoot.c:85: cannot allocate memory\n");
135     MPI_Finalize();
136     exit(failure);
137   }
138
139   req_ptr = req;
140
141   // do local allgather/local copy 
142   recv_offset = rank * block_size;
143   MPIR_Localcopy(send_buff, send_count, send_type, (char *)recv_buff + recv_offset,
144                  recv_count, recv_type);
145
146   // do row-wise comm
147   for (i = 0; i < Y; i++) {
148     src = i + my_row_base;
149     if (src == rank)
150       continue;
151     recv_offset = src * block_size;
152     MPIC_Irecv((char *)recv_buff + recv_offset, recv_count, recv_type, src, tag,
153                comm, req_ptr++);
154   }
155
156
157   for (i = 0; i < Y; i++) {
158     dst = i + my_row_base;
159     if (dst == rank)
160       continue;
161     MPIC_Send(send_buff, send_count, send_type, dst, tag, comm);
162   }
163
164   MPI_Waitall(Y - 1, req, MPI_STATUSES_IGNORE);
165
166   req_ptr = req;
167
168   // do colwise comm
169   for (i = 0; i < X; i++) {
170     src = (i * Y + my_col_base);
171     if (src == rank)
172       continue;
173     src_row_base = (src / Y) * Y;
174     recv_offset = src_row_base * block_size;
175     MPIC_Irecv((char *)recv_buff + recv_offset, recv_count * Y, recv_type, src, tag,
176                comm, req_ptr++);
177   }
178
179   for (i = 0; i < X; i++) {
180     dst = (i * Y + my_col_base);
181     if (dst == rank)
182       continue;
183     send_offset = my_row_base * block_size;
184     MPIC_Send((char *)recv_buff + send_offset, send_count * Y, send_type, dst, tag,
185               comm);
186   }
187
188   MPI_Waitall(X - 1, req, MPI_STATUSES_IGNORE);
189
190   free(req);
191
192   return success;
193 }