Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
22e6e63dd907f7394b6dcef787088f2a45df76ca
[simgrid.git] / src / smpi / colls / gather-ompi.c
1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  *                         University Research and Technology
4  *                         Corporation.  All rights reserved.
5  * Copyright (c) 2004-2009 The University of Tennessee and The University
6  *                         of Tennessee Research Foundation.  All rights
7  *                         reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  *                         University of Stuttgart.  All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  *                         All rights reserved.
12  * $COPYRIGHT$
13  *
14  * Additional copyrights may follow
15  *
16  * $HEADER$
17  */
18
19 #include "colls_private.h"
20 #include "coll_tuned_topo.h"
21
22 #define MCA_COLL_BASE_TAG_GATHER 333
23 /* Todo: gather_intra_generic, gather_intra_binary, gather_intra_chain,
24  * gather_intra_pipeline, segmentation? */
25 int
26 smpi_coll_tuned_gather_ompi_binomial(void *sbuf, int scount,
27                                       MPI_Datatype sdtype,
28                                       void *rbuf, int rcount,
29                                       MPI_Datatype rdtype,
30                                       int root,
31                                       MPI_Comm comm)
32 {
33     int line = -1;
34     int i;
35     int rank;
36     int vrank;
37     int size;
38     int total_recv = 0;
39     char *ptmp     = NULL;
40     char *tempbuf  = NULL;
41     int err;
42     ompi_coll_tree_t* bmtree;
43     MPI_Status status;
44     MPI_Aint sextent, slb, strue_lb, strue_extent; 
45     MPI_Aint rextent, rlb, rtrue_lb, rtrue_extent;
46
47
48     size = smpi_comm_size(comm);
49     rank = smpi_comm_rank(comm);
50
51     XBT_DEBUG(
52                  "smpi_coll_tuned_gather_ompi_binomial rank %d", rank);
53
54     /* create the binomial tree */
55    // COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
56     bmtree = ompi_coll_tuned_topo_build_in_order_bmtree(comm, root);
57     // data->cached_in_order_bmtree;
58
59     smpi_datatype_extent(sdtype, &slb, &sextent);
60     smpi_datatype_extent(sdtype, &strue_lb, &strue_extent);
61
62     vrank = (rank - root + size) % size;
63
64     if (rank == root) {
65         smpi_datatype_extent(rdtype, &rlb, &rextent);
66         smpi_datatype_extent(rdtype, &rtrue_lb, &rtrue_extent);
67         if (0 == root){
68             /* root on 0, just use the recv buffer */
69             ptmp = (char *) rbuf;
70             if (sbuf != MPI_IN_PLACE) {
71                 err = smpi_datatype_copy(sbuf, scount, sdtype,
72                                       ptmp, rcount, rdtype);
73                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
74             }
75         } else {
76             /* root is not on 0, allocate temp buffer for recv,
77              * rotate data at the end */
78             tempbuf = (char *) malloc(rtrue_extent + (rcount*size - 1) * rextent);
79             if (NULL == tempbuf) {
80                 err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
81             }
82
83             ptmp = tempbuf - rlb;
84             if (sbuf != MPI_IN_PLACE) {
85                 /* copy from sbuf to temp buffer */
86                 err = smpi_datatype_copy(sbuf, scount, sdtype,
87                                       ptmp, rcount, rdtype);
88                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
89             } else {
90                 /* copy from rbuf to temp buffer  */
91                 err = smpi_datatype_copy((char *) rbuf + rank*rextent*rcount, rcount, rdtype, ptmp, rcount, rdtype );
92                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
93             }
94         }
95         total_recv = rcount;
96     } else if (!(vrank % 2)) {
97         /* other non-leaf nodes, allocate temp buffer for data received from
98          * children, the most we need is half of the total data elements due
99          * to the property of binimoal tree */
100         tempbuf = (char *) malloc(strue_extent + (scount*size - 1) * sextent);
101         if (NULL == tempbuf) {
102             err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
103         }
104
105         ptmp = tempbuf - slb;
106         /* local copy to tempbuf */
107         err = smpi_datatype_copy(sbuf, scount, sdtype,
108                                    ptmp, scount, sdtype);
109         if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
110
111         /* use sdtype,scount as rdtype,rdcount since they are ignored on
112          * non-root procs */
113         rdtype = sdtype;
114         rcount = scount;
115         rextent = sextent;
116         total_recv = rcount;
117     } else {
118         /* leaf nodes, no temp buffer needed, use sdtype,scount as
119          * rdtype,rdcount since they are ignored on non-root procs */
120         ptmp = (char *) sbuf;
121         total_recv = scount;
122     }
123
124     if (!(vrank % 2)) {
125         /* all non-leaf nodes recv from children */
126         for (i = 0; i < bmtree->tree_nextsize; i++) {
127             int mycount = 0, vkid;
128             /* figure out how much data I have to send to this child */
129             vkid = (bmtree->tree_next[i] - root + size) % size;
130             mycount = vkid - vrank;
131             if (mycount > (size - vkid))
132                 mycount = size - vkid;
133             mycount *= rcount;
134
135             XBT_DEBUG(
136                          "smpi_coll_tuned_gather_ompi_binomial rank %d recv %d mycount = %d",
137                          rank, bmtree->tree_next[i], mycount);
138
139             smpi_mpi_recv(ptmp + total_recv*rextent, rcount*size-total_recv, rdtype,
140                                     bmtree->tree_next[i], MCA_COLL_BASE_TAG_GATHER,
141                                     comm, &status);
142
143             total_recv += mycount;
144         }
145     }
146
147     if (rank != root) {
148         /* all nodes except root send to parents */
149         XBT_DEBUG(
150                      "smpi_coll_tuned_gather_ompi_binomial rank %d send %d count %d\n",
151                      rank, bmtree->tree_prev, total_recv);
152
153         smpi_mpi_send(ptmp, total_recv, sdtype,
154                                 bmtree->tree_prev,
155                                 MCA_COLL_BASE_TAG_GATHER,
156                                  comm);
157   }
158     if (rank == root) {
159         if (root != 0) {
160             /* rotate received data on root if root != 0 */
161             err = smpi_datatype_copy(ptmp, rcount*(size - root), rdtype,
162                                                  (char *) rbuf + rextent*root*rcount, rcount*(size - root), rdtype );
163             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
164
165
166             err = smpi_datatype_copy( ptmp + rextent*rcount*(size-root), rcount*root,rdtype, 
167                                                  (char *) rbuf,rcount*root,rdtype);
168             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
169
170             free(tempbuf);
171         }
172     } else if (!(vrank % 2)) {
173         /* other non-leaf nodes */
174         free(tempbuf);
175     }
176     return MPI_SUCCESS;
177
178  err_hndl:
179     if (NULL != tempbuf)
180         free(tempbuf);
181
182     XBT_DEBUG(  "%s:%4d\tError occurred %d, rank %2d",
183                  __FILE__, line, err, rank);
184     return err;
185 }
186
187 /*
188  *      gather_intra_linear_sync
189  *
190  *      Function:       - synchronized gather operation with
191  *      Accepts:        - same arguments as MPI_Gather(), first segment size
192  *      Returns:        - MPI_SUCCESS or error code
193  */
194 int
195 smpi_coll_tuned_gather_ompi_linear_sync(void *sbuf, int scount,
196                                          MPI_Datatype sdtype,
197                                          void *rbuf, int rcount,
198                                          MPI_Datatype rdtype,
199                                          int root, 
200                                          MPI_Comm comm)
201 {
202     int i;
203     int ret, line;
204     int rank, size;
205     int first_segment_count;
206     size_t typelng;
207     MPI_Aint extent;
208     MPI_Aint lb;
209
210     int first_segment_size=0;
211     size = smpi_comm_size(comm);
212     rank = smpi_comm_rank(comm);
213     
214     size_t dsize, block_size;
215     if (rank == root) {
216         dsize= smpi_datatype_size(rdtype);
217         block_size = dsize * rcount;
218     } else {
219         dsize=smpi_datatype_size(sdtype);
220         block_size = dsize * scount;
221     }
222     
223      if (block_size > 92160){
224      first_segment_size = 32768;
225      }else{
226      first_segment_size = 1024;
227      }
228
229     XBT_DEBUG(
230                  "smpi_coll_tuned_gather_ompi_linear_sync rank %d, segment %d", rank, first_segment_size);
231
232     if (rank != root) {
233         /* Non-root processes:
234            - receive zero byte message from the root,
235            - send the first segment of the data synchronously,
236            - send the second segment of the data.
237         */
238
239         typelng= smpi_datatype_size(sdtype);
240         smpi_datatype_extent(sdtype, &lb, &extent);
241         first_segment_count = scount;
242         COLL_TUNED_COMPUTED_SEGCOUNT( (size_t) first_segment_size, typelng, 
243                                       first_segment_count );
244
245         smpi_mpi_recv(sbuf, 0, MPI_BYTE, root, 
246                                 MCA_COLL_BASE_TAG_GATHER,
247                                 comm, MPI_STATUS_IGNORE);
248
249         smpi_mpi_send(sbuf, first_segment_count, sdtype, root,
250                                 MCA_COLL_BASE_TAG_GATHER,
251                                  comm);
252
253         smpi_mpi_send((char*)sbuf + extent * first_segment_count, 
254                                 (scount - first_segment_count), sdtype, 
255                                 root, MCA_COLL_BASE_TAG_GATHER,
256                                  comm);
257     }
258
259     else {
260         /* Root process, 
261            - For every non-root node:
262            - post irecv for the first segment of the message
263            - send zero byte message to signal node to send the message
264            - post irecv for the second segment of the message
265            - wait for the first segment to complete
266            - Copy local data if necessary
267            - Waitall for all the second segments to complete.
268         */
269         char *ptmp;
270         MPI_Request *reqs = NULL, first_segment_req;
271         reqs = (MPI_Request *) calloc(size, sizeof(MPI_Request ));
272         if (NULL == reqs) { ret = -1; line = __LINE__; goto error_hndl; }
273         
274         typelng=smpi_datatype_size(rdtype);
275         smpi_datatype_extent(rdtype, &lb, &extent);
276         first_segment_count = rcount;
277         COLL_TUNED_COMPUTED_SEGCOUNT( (size_t)first_segment_size, typelng, 
278                                       first_segment_count );
279
280         ptmp = (char *) rbuf;
281         for (i = 0; i < size; ++i) {
282             if (i == rank) {  
283                 /* skip myself */
284                 reqs[i] = MPI_REQUEST_NULL; 
285                 continue; 
286             } 
287
288             /* irecv for the first segment from i */
289             ptmp = (char*)rbuf + i * rcount * extent;
290             first_segment_req = smpi_mpi_irecv(ptmp, first_segment_count, rdtype, i,
291                                      MCA_COLL_BASE_TAG_GATHER, comm
292                                      );
293             
294             /* send sync message */
295             smpi_mpi_send(rbuf, 0, MPI_BYTE, i,
296                                     MCA_COLL_BASE_TAG_GATHER,
297                                      comm);
298
299             /* irecv for the second segment */
300             ptmp = (char*)rbuf + (i * rcount + first_segment_count) * extent;
301             reqs[i]=smpi_mpi_irecv(ptmp, (rcount - first_segment_count), 
302                                      rdtype, i, MCA_COLL_BASE_TAG_GATHER, comm
303                                      );
304
305             /* wait on the first segment to complete */
306             smpi_mpi_wait(&first_segment_req, MPI_STATUS_IGNORE);
307         }
308
309         /* copy local data if necessary */
310         if (MPI_IN_PLACE != sbuf) {
311             ret = smpi_datatype_copy(sbuf, scount, sdtype,
312                                   (char*)rbuf + rank * rcount * extent, 
313                                   rcount, rdtype);
314             if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl; }
315         }
316         
317         /* wait all second segments to complete */
318         ret = smpi_mpi_waitall(size, reqs, MPI_STATUSES_IGNORE);
319
320         free(reqs);
321     }
322
323     /* All done */
324
325     return MPI_SUCCESS;
326  error_hndl:
327     XBT_DEBUG( 
328                    "ERROR_HNDL: node %d file %s line %d error %d\n", 
329                    rank, __FILE__, line, ret );
330     return ret;
331 }
332
333 /*
334  * Linear functions are copied from the BASIC coll module
335  * they do not segment the message and are simple implementations
336  * but for some small number of nodes and/or small data sizes they 
337  * are just as fast as tuned/tree based segmenting operations 
338  * and as such may be selected by the decision functions
339  * These are copied into this module due to the way we select modules
340  * in V1. i.e. in V2 we will handle this differently and so will not
341  * have to duplicate code.
342  * JPG following the examples from other coll_tuned implementations. Dec06.
343  */
344
345 /* copied function (with appropriate renaming) starts here */
346 /*
347  *      gather_intra
348  *
349  *      Function:       - basic gather operation
350  *      Accepts:        - same arguments as MPI_Gather()
351  *      Returns:        - MPI_SUCCESS or error code
352  */
353 int
354 smpi_coll_tuned_gather_ompi_basic_linear(void *sbuf, int scount,
355                                           MPI_Datatype sdtype,
356                                           void *rbuf, int rcount,
357                                           MPI_Datatype rdtype,
358                                           int root,
359                                           MPI_Comm comm)
360 {
361     int i;
362     int err;
363     int rank;
364     int size;
365     char *ptmp;
366     MPI_Aint incr;
367     MPI_Aint extent;
368     MPI_Aint lb;
369
370     size = smpi_comm_size(comm);
371     rank = smpi_comm_rank(comm);
372
373     /* Everyone but root sends data and returns. */
374     XBT_DEBUG(
375                  "ompi_coll_tuned_gather_intra_basic_linear rank %d", rank);
376
377     if (rank != root) {
378         smpi_mpi_send(sbuf, scount, sdtype, root,
379                                  MCA_COLL_BASE_TAG_GATHER,
380                                   comm);
381         return MPI_SUCCESS;
382     }
383
384     /* I am the root, loop receiving the data. */
385
386     smpi_datatype_extent(rdtype, &lb, &extent);
387     incr = extent * rcount;
388     for (i = 0, ptmp = (char *) rbuf; i < size; ++i, ptmp += incr) {
389         if (i == rank) {
390             if (MPI_IN_PLACE != sbuf) {
391                 err = smpi_datatype_copy(sbuf, scount, sdtype,
392                                       ptmp, rcount, rdtype);
393             } else {
394                 err = MPI_SUCCESS;
395             }
396         } else {
397             smpi_mpi_recv(ptmp, rcount, rdtype, i,
398                                     MCA_COLL_BASE_TAG_GATHER,
399                                     comm, MPI_STATUS_IGNORE);
400             err = MPI_SUCCESS;
401         }
402         if (MPI_SUCCESS != err) {
403             return err;
404         }
405     }
406
407     /* All done */
408
409     return MPI_SUCCESS;
410 }