Logo AND Algorithmique Numérique Distribuée

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