Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add check for error.
[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 *) malloc(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 *) malloc(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             err = smpi_datatype_copy(ptmp, scount, sdtype,
131                                   rbuf, rcount, rdtype);
132         }
133         /* send to children on all non-leaf */
134         for (i = 0; i < bmtree->tree_nextsize; i++) {
135             int mycount = 0, vkid;
136             /* figure out how much data I have to send to this child */
137             vkid = (bmtree->tree_next[i] - root + size) % size;
138             mycount = vkid - vrank;
139             if (mycount > (size - vkid))
140                 mycount = size - vkid;
141             mycount *= scount;
142
143             smpi_mpi_send(ptmp + total_send*sextent, mycount, sdtype,
144                                     bmtree->tree_next[i],
145                                     COLL_TAG_SCATTER,
146                                      comm);
147
148             total_send += mycount;
149         }
150
151
152     } else {
153         /* recv from parent on leaf nodes */
154         smpi_mpi_recv(ptmp, rcount, rdtype, bmtree->tree_prev,
155                                 COLL_TAG_SCATTER, comm, &status);
156     }
157
158     if (NULL != tempbuf)
159       free(tempbuf);
160     //!FIXME : store the tree, as done in ompi, instead of calculating it each time ?
161     xbt_free(bmtree);
162
163     return MPI_SUCCESS;
164
165  err_hndl:
166     if (NULL != tempbuf)
167         free(tempbuf);
168
169     XBT_DEBUG(  "%s:%4d\tError occurred %d, rank %2d",
170                  __FILE__, line, err, rank);
171     return err;
172 }
173
174 /*
175  * Linear functions are copied from the BASIC coll module
176  * they do not segment the message and are simple implementations
177  * but for some small number of nodes and/or small data sizes they 
178  * are just as fast as tuned/tree based segmenting operations 
179  * and as such may be selected by the decision functions
180  * These are copied into this module due to the way we select modules
181  * in V1. i.e. in V2 we will handle this differently and so will not
182  * have to duplicate code.
183  * JPG following the examples from other coll_tuned implementations. Dec06.
184  */
185
186 /* copied function (with appropriate renaming) starts here */
187 /*
188  *      scatter_intra
189  *
190  *      Function:       - basic scatter operation
191  *      Accepts:        - same arguments as MPI_Scatter()
192  *      Returns:        - MPI_SUCCESS or error code
193  */
194 int
195 smpi_coll_tuned_scatter_ompi_basic_linear(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 {
203     int i, rank, size, err;
204     char *ptmp;
205     ptrdiff_t lb, incr;
206
207     /* Initialize */
208
209     rank = smpi_comm_rank(comm);
210     size = smpi_comm_size(comm);
211
212     /* If not root, receive data. */
213
214     if (rank != root) {
215         smpi_mpi_recv(rbuf, rcount, rdtype, root,
216                                 COLL_TAG_SCATTER,
217                                 comm, MPI_STATUS_IGNORE);
218         return MPI_SUCCESS;
219     }
220
221     /* I am the root, loop sending data. */
222
223     err = smpi_datatype_extent(sdtype, &lb, &incr);
224     if (MPI_SUCCESS != err) {
225         return MPI_ERR_OTHER;
226     }
227
228     incr *= scount;
229     for (i = 0, ptmp = (char *) sbuf; i < size; ++i, ptmp += incr) {
230
231         /* simple optimization */
232
233         if (i == rank) {
234             if (MPI_IN_PLACE != rbuf) {
235                 err =
236                     smpi_datatype_copy(ptmp, scount, sdtype, rbuf, rcount,
237                                     rdtype);
238             }
239         } else {
240             smpi_mpi_send(ptmp, scount, sdtype, i,
241                                     COLL_TAG_SCATTER,
242                                      comm);
243         }
244         if (MPI_SUCCESS != err) {
245             return err;
246         }
247     }
248
249     /* All done */
250
251     return MPI_SUCCESS;
252 }