Logo AND Algorithmique Numérique Distribuée

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