Logo AND Algorithmique Numérique Distribuée

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