Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use DBL_MAX for values of type double.
[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 /* Todo: gather_intra_generic, gather_intra_binary, gather_intra_chain,
23  * gather_intra_pipeline, segmentation? */
24 int
25 smpi_coll_tuned_gather_ompi_binomial(void *sbuf, int scount,
26                                       MPI_Datatype sdtype,
27                                       void *rbuf, int rcount,
28                                       MPI_Datatype rdtype,
29                                       int root,
30                                       MPI_Comm comm)
31 {
32     int line = -1;
33     int i;
34     int rank;
35     int vrank;
36     int size;
37     int total_recv = 0;
38     char *ptmp     = NULL;
39     char *tempbuf  = NULL;
40     int err;
41     ompi_coll_tree_t* bmtree;
42     MPI_Status status;
43     MPI_Aint sextent, slb, strue_lb, strue_extent; 
44     MPI_Aint rextent, rlb, rtrue_lb, rtrue_extent;
45
46
47     size = smpi_comm_size(comm);
48     rank = smpi_comm_rank(comm);
49
50     XBT_DEBUG(
51                  "smpi_coll_tuned_gather_ompi_binomial rank %d", rank);
52
53     /* create the binomial tree */
54    // COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
55     bmtree = ompi_coll_tuned_topo_build_in_order_bmtree(comm, root);
56     // data->cached_in_order_bmtree;
57
58     smpi_datatype_extent(sdtype, &slb, &sextent);
59     smpi_datatype_extent(sdtype, &strue_lb, &strue_extent);
60
61     vrank = (rank - root + size) % size;
62
63     if (rank == root) {
64         smpi_datatype_extent(rdtype, &rlb, &rextent);
65         smpi_datatype_extent(rdtype, &rtrue_lb, &rtrue_extent);
66         if (0 == root){
67             /* root on 0, just use the recv buffer */
68             ptmp = (char *) rbuf;
69             if (sbuf != MPI_IN_PLACE) {
70                 err = smpi_datatype_copy(sbuf, scount, sdtype,
71                                       ptmp, rcount, rdtype);
72                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
73             }
74         } else {
75             /* root is not on 0, allocate temp buffer for recv,
76              * rotate data at the end */
77             tempbuf = (char *) malloc(rtrue_extent + (rcount*size - 1) * rextent);
78             if (NULL == tempbuf) {
79                 err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
80             }
81
82             ptmp = tempbuf - rlb;
83             if (sbuf != MPI_IN_PLACE) {
84                 /* copy from sbuf to temp buffer */
85                 err = smpi_datatype_copy(sbuf, scount, sdtype,
86                                       ptmp, rcount, rdtype);
87                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
88             } else {
89                 /* copy from rbuf to temp buffer  */
90                 err = smpi_datatype_copy((char *) rbuf + rank*rextent*rcount, rcount, rdtype, ptmp, rcount, rdtype );
91                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
92             }
93         }
94         total_recv = rcount;
95     } else if (!(vrank % 2)) {
96         /* other non-leaf nodes, allocate temp buffer for data received from
97          * children, the most we need is half of the total data elements due
98          * to the property of binimoal tree */
99         tempbuf = (char *) malloc(strue_extent + (scount*size - 1) * sextent);
100         if (NULL == tempbuf) {
101             err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
102         }
103
104         ptmp = tempbuf - slb;
105         /* local copy to tempbuf */
106         err = smpi_datatype_copy(sbuf, scount, sdtype,
107                                    ptmp, scount, sdtype);
108         if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
109
110         /* use sdtype,scount as rdtype,rdcount since they are ignored on
111          * non-root procs */
112         rdtype = sdtype;
113         rcount = scount;
114         rextent = sextent;
115         total_recv = rcount;
116     } else {
117         /* leaf nodes, no temp buffer needed, use sdtype,scount as
118          * rdtype,rdcount since they are ignored on non-root procs */
119         ptmp = (char *) sbuf;
120         total_recv = scount;
121     }
122
123     if (!(vrank % 2)) {
124         /* all non-leaf nodes recv from children */
125         for (i = 0; i < bmtree->tree_nextsize; i++) {
126             int mycount = 0, vkid;
127             /* figure out how much data I have to send to this child */
128             vkid = (bmtree->tree_next[i] - root + size) % size;
129             mycount = vkid - vrank;
130             if (mycount > (size - vkid))
131                 mycount = size - vkid;
132             mycount *= rcount;
133
134             XBT_DEBUG(
135                          "smpi_coll_tuned_gather_ompi_binomial rank %d recv %d mycount = %d",
136                          rank, bmtree->tree_next[i], mycount);
137
138             smpi_mpi_recv(ptmp + total_recv*rextent, mycount, rdtype,
139                                     bmtree->tree_next[i], COLL_TAG_GATHER,
140                                     comm, &status);
141
142             total_recv += mycount;
143         }
144     }
145
146     if (rank != root) {
147         /* all nodes except root send to parents */
148         XBT_DEBUG(
149                      "smpi_coll_tuned_gather_ompi_binomial rank %d send %d count %d\n",
150                      rank, bmtree->tree_prev, total_recv);
151
152         smpi_mpi_send(ptmp, total_recv, sdtype,
153                                 bmtree->tree_prev,
154                                 COLL_TAG_GATHER,
155                                  comm);
156   }
157     if (rank == root) {
158         if (root != 0) {
159             /* rotate received data on root if root != 0 */
160             err = smpi_datatype_copy(ptmp, rcount*(size - root), rdtype,
161                                                  (char *) rbuf + rextent*root*rcount, rcount*(size - root), rdtype );
162             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
163
164
165             err = smpi_datatype_copy( ptmp + rextent*rcount*(size-root), rcount*root,rdtype, 
166                                                  (char *) rbuf,rcount*root,rdtype);
167             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
168
169             free(tempbuf);
170         }
171     } else if (!(vrank % 2)) {
172         /* other non-leaf nodes */
173         free(tempbuf);
174     }
175     xbt_free(bmtree);
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                                 COLL_TAG_GATHER,
247                                 comm, MPI_STATUS_IGNORE);
248
249         smpi_mpi_send(sbuf, first_segment_count, sdtype, root,
250                                 COLL_TAG_GATHER,
251                                  comm);
252
253         smpi_mpi_send((char*)sbuf + extent * first_segment_count, 
254                                 (scount - first_segment_count), sdtype, 
255                                 root, COLL_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                                      COLL_TAG_GATHER, comm
292                                      );
293             
294             /* send sync message */
295             smpi_mpi_send(rbuf, 0, MPI_BYTE, i,
296                                     COLL_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, COLL_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                                  COLL_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                                     COLL_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 }