Logo AND Algorithmique Numérique Distribuée

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