Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
96403693a898473504c2f5ef707d7a7d8a2c2618
[simgrid.git] / src / smpi / colls / scatter / scatter-ompi.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 /*
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-2006 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
23 #include "../colls_private.h"
24 #include "../coll_tuned_topo.h"
25
26 namespace simgrid{
27 namespace smpi{
28
29
30 int
31 Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
32                                        MPI_Datatype sdtype,
33                                        void *rbuf, int rcount,
34                                        MPI_Datatype rdtype,
35                                        int root,
36                                        MPI_Comm comm
37                                        )
38 {
39     int line = -1;
40     int i;
41     int rank;
42     int vrank;
43     int size;
44     int total_send = 0;
45     char *ptmp     = NULL;
46     char *tempbuf  = NULL;
47     int err;
48     ompi_coll_tree_t* bmtree;
49     MPI_Status status;
50     MPI_Aint sextent, slb, strue_lb, strue_extent; 
51     MPI_Aint rextent, rlb, rtrue_lb, rtrue_extent;
52
53     size = comm->size();
54     rank = comm->rank();
55
56     XBT_DEBUG(
57                  "Coll_scatter_ompi_binomial::scatter rank %d", rank);
58
59     /* create the binomial tree */
60     
61 //    COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
62     bmtree =  ompi_coll_tuned_topo_build_in_order_bmtree( comm, root);//ompi_ data->cached_in_order_bmtree;
63
64     sdtype->extent(&slb, &sextent);
65     sdtype->extent(&strue_lb, &strue_extent);
66     rdtype->extent(&rlb, &rextent);
67     rdtype->extent(&rtrue_lb, &rtrue_extent);
68
69     vrank = (rank - root + size) % size;
70
71     if (rank == root) {
72         if (0 == root) {
73             /* root on 0, just use the send buffer */
74             ptmp = (char *) sbuf;
75             if (rbuf != MPI_IN_PLACE) {
76                 /* local copy to rbuf */
77                 err = Datatype::copy(sbuf, scount, sdtype,
78                                       rbuf, rcount, rdtype);
79                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
80             }
81         } else {
82             /* root is not on 0, allocate temp buffer for send */
83             tempbuf = (char *) smpi_get_tmp_sendbuffer(strue_extent + (scount*size - 1) * sextent);
84             if (NULL == tempbuf) {
85                 err = MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
86             }
87
88             ptmp = tempbuf - slb;
89
90             /* and rotate data so they will eventually in the right place */
91             err = Datatype::copy((char *) sbuf + sextent*root*scount, scount*(size-root), sdtype,
92             ptmp, scount*(size-root), sdtype);
93             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
94
95
96             err = Datatype::copy((char*)sbuf, scount*root, sdtype,
97                                                  ptmp + sextent*scount*(size - root), scount*root, sdtype);
98             if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
99
100             if (rbuf != MPI_IN_PLACE) {
101                 /* local copy to rbuf */
102                 err = Datatype::copy(ptmp, scount, sdtype,
103                                       rbuf, rcount, rdtype);
104                 if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
105             }
106         }
107         total_send = scount;
108     } else if (!(vrank % 2)) {
109         /* non-root, non-leaf nodes, allocate temp buffer for recv
110          * the most we need is rcount*size/2 */
111         tempbuf = (char *) smpi_get_tmp_recvbuffer(rtrue_extent + (rcount*size - 1) * rextent);
112         if (NULL == tempbuf) {
113             err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
114         }
115
116         ptmp = tempbuf - rlb;
117
118         sdtype = rdtype;
119         scount = rcount;
120         sextent = rextent;
121         total_send = scount;
122     } else {
123         /* leaf nodes, just use rbuf */
124         ptmp = (char *) rbuf;
125     }
126
127     if (!(vrank % 2)) {
128         if (rank != root) {
129             /* recv from parent on non-root */
130             Request::recv(ptmp, rcount*size, rdtype, bmtree->tree_prev,
131                                     COLL_TAG_SCATTER, comm, &status);
132             /* local copy to rbuf */
133             Datatype::copy(ptmp, scount, sdtype, rbuf, rcount, rdtype);
134         }
135         /* send to children on all non-leaf */
136         for (i = 0; i < bmtree->tree_nextsize; i++) {
137             int mycount = 0, vkid;
138             /* figure out how much data I have to send to this child */
139             vkid = (bmtree->tree_next[i] - root + size) % size;
140             mycount = vkid - vrank;
141             if (mycount > (size - vkid))
142                 mycount = size - vkid;
143             mycount *= scount;
144
145             Request::send(ptmp + total_send*sextent, mycount, sdtype,
146                                     bmtree->tree_next[i],
147                                     COLL_TAG_SCATTER,
148                                      comm);
149
150             total_send += mycount;
151         }
152
153
154     } else {
155         /* recv from parent on leaf nodes */
156         Request::recv(ptmp, rcount, rdtype, bmtree->tree_prev,
157                                 COLL_TAG_SCATTER, comm, &status);
158     }
159
160     if (NULL != tempbuf)
161       smpi_free_tmp_buffer(tempbuf);
162     //!FIXME : store the tree, as done in ompi, instead of calculating it each time ?
163     xbt_free(bmtree);
164
165     return MPI_SUCCESS;
166
167  err_hndl:
168     if (NULL != tempbuf)
169         free(tempbuf);
170
171     XBT_DEBUG(  "%s:%4d\tError occurred %d, rank %2d",
172                  __FILE__, line, err, rank);
173     return err;
174 }
175
176 /*
177  * Linear functions are copied from the BASIC coll module
178  * they do not segment the message and are simple implementations
179  * but for some small number of nodes and/or small data sizes they 
180  * are just as fast as tuned/tree based segmenting operations 
181  * and as such may be selected by the decision functions
182  * These are copied into this module due to the way we select modules
183  * in V1. i.e. in V2 we will handle this differently and so will not
184  * have to duplicate code.
185  * JPG following the examples from other coll_tuned implementations. Dec06.
186  */
187
188 /* copied function (with appropriate renaming) starts here */
189 /*
190  *      scatter_intra
191  *
192  *      Function:       - basic scatter operation
193  *      Accepts:        - same arguments as MPI_Scatter()
194  *      Returns:        - MPI_SUCCESS or error code
195  */
196 int
197 Coll_scatter_ompi_basic_linear::scatter(void *sbuf, int scount,
198                                            MPI_Datatype sdtype,
199                                            void *rbuf, int rcount,
200                                            MPI_Datatype rdtype,
201                                            int root,
202                                            MPI_Comm comm
203                                            )
204 {
205     int i, rank, size, err;
206     char *ptmp;
207     ptrdiff_t lb, incr;
208
209     /* Initialize */
210
211     rank = comm->rank();
212     size = comm->size();
213
214     /* If not root, receive data. */
215
216     if (rank != root) {
217         Request::recv(rbuf, rcount, rdtype, root,
218                                 COLL_TAG_SCATTER,
219                                 comm, MPI_STATUS_IGNORE);
220         return MPI_SUCCESS;
221     }
222
223     /* I am the root, loop sending data. */
224
225     err = sdtype->extent(&lb, &incr);
226     if (MPI_SUCCESS != err) {
227         return MPI_ERR_OTHER;
228     }
229
230     incr *= scount;
231     for (i = 0, ptmp = (char *) sbuf; i < size; ++i, ptmp += incr) {
232
233         /* simple optimization */
234
235         if (i == rank) {
236             if (MPI_IN_PLACE != rbuf) {
237                 err =
238                     Datatype::copy(ptmp, scount, sdtype, rbuf, rcount,
239                                     rdtype);
240             }
241         } else {
242             Request::send(ptmp, scount, sdtype, i,
243                                     COLL_TAG_SCATTER,
244                                      comm);
245         }
246         if (MPI_SUCCESS != err) {
247             return err;
248         }
249     }
250
251     /* All done */
252
253     return MPI_SUCCESS;
254 }
255
256 }
257 }