Logo AND Algorithmique Numérique Distribuée

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