Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d7f6a3bf53718e1ccc6e8066e46ac7e6c63e4a11
[simgrid.git] / src / smpi / colls / bcast / bcast-mvapich-smp.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  /* -*- Mode: C; c-basic-offset:4 ; -*- */
22 /* Copyright (c) 2001-2014, The Ohio State University. All rights
23  * reserved.
24  *
25  * This file is part of the MVAPICH2 software package developed by the
26  * team members of The Ohio State University's Network-Based Computing
27  * Laboratory (NBCL), headed by Professor Dhabaleswar K. (DK) Panda.
28  *
29  * For detailed copyright and licensing information, please refer to the
30  * copyright file COPYRIGHT in the top level MVAPICH2 directory.
31  */
32 /*
33  *
34  *  (C) 2001 by Argonne National Laboratory.
35  *      See COPYRIGHT in top-level directory.
36  */
37
38 #include "../colls_private.hpp"
39
40 extern int (*MV2_Bcast_function) (void *buffer, int count, MPI_Datatype datatype,
41                            int root, MPI_Comm comm_ptr);
42
43 extern int (*MV2_Bcast_intra_node_function) (void *buffer, int count, MPI_Datatype datatype,
44                                       int root, MPI_Comm comm_ptr);
45
46 extern int zcpy_knomial_factor;
47 extern int mv2_pipelined_zcpy_knomial_factor;
48 extern int bcast_segment_size;
49 extern int mv2_inter_node_knomial_factor;
50 extern int mv2_intra_node_knomial_factor;
51 extern int mv2_bcast_two_level_system_size;
52 #define INTRA_NODE_ROOT 0
53
54 #define MPIR_Pipelined_Bcast_Zcpy_MV2 Coll_bcast_mpich::bcast
55 #define MPIR_Pipelined_Bcast_MV2 Coll_bcast_mpich::bcast
56 #define MPIR_Bcast_binomial_MV2 Coll_bcast_binomial_tree::bcast
57 #define MPIR_Bcast_scatter_ring_allgather_shm_MV2 Coll_bcast_scatter_LR_allgather::bcast
58 #define MPIR_Bcast_scatter_doubling_allgather_MV2 Coll_bcast_scatter_rdb_allgather::bcast
59 #define MPIR_Bcast_scatter_ring_allgather_MV2 Coll_bcast_scatter_LR_allgather::bcast
60 #define MPIR_Shmem_Bcast_MV2 Coll_bcast_mpich::bcast
61 #define MPIR_Bcast_tune_inter_node_helper_MV2 Coll_bcast_mvapich2_inter_node::bcast
62 #define MPIR_Bcast_inter_node_helper_MV2 Coll_bcast_mvapich2_inter_node::bcast
63 #define MPIR_Knomial_Bcast_intra_node_MV2 Coll_bcast_mvapich2_knomial_intra_node::bcast
64 #define MPIR_Bcast_intra_MV2 Coll_bcast_mvapich2_intra_node::bcast
65
66 extern int zcpy_knomial_factor;
67 extern int mv2_pipelined_zcpy_knomial_factor;
68 extern int bcast_segment_size;
69 extern int mv2_inter_node_knomial_factor;
70 extern int mv2_intra_node_knomial_factor;
71 #define mv2_bcast_two_level_system_size  64
72 #define mv2_bcast_short_msg             16384
73 #define mv2_bcast_large_msg            512*1024
74 #define mv2_knomial_intra_node_threshold 131072
75 #define mv2_scatter_rd_inter_leader_bcast 1
76 namespace simgrid{
77 namespace smpi{
78 int Coll_bcast_mvapich2_inter_node::bcast(void *buffer,
79                                                  int count,
80                                                  MPI_Datatype datatype,
81                                                  int root,
82                                                  MPI_Comm  comm)
83 {
84     int rank;
85     int mpi_errno = MPI_SUCCESS;
86     MPI_Comm shmem_comm, leader_comm;
87     int local_rank, local_size, global_rank = -1;
88     int leader_root, leader_of_root;
89
90
91     rank = comm->rank();
92     //comm_size = comm->size();
93
94
95     if (MV2_Bcast_function==NULL){
96       MV2_Bcast_function=Coll_bcast_mpich::bcast;
97     }
98
99     if (MV2_Bcast_intra_node_function==NULL){
100       MV2_Bcast_intra_node_function= Coll_bcast_mpich::bcast;
101     }
102
103     if(comm->get_leaders_comm()==MPI_COMM_NULL){
104       comm->init_smp();
105     }
106
107     shmem_comm = comm->get_intra_comm();
108     local_rank = shmem_comm->rank();
109     local_size = shmem_comm->size();
110
111     leader_comm = comm->get_leaders_comm();
112
113     if ((local_rank == 0) && (local_size > 1)) {
114       global_rank = leader_comm->rank();
115     }
116
117     int* leaders_map = comm->get_leaders_map();
118     leader_of_root = comm->group()->rank(leaders_map[root]);
119     leader_root = leader_comm->group()->rank(leaders_map[root]);
120
121
122     if (local_size > 1) {
123         if ((local_rank == 0) && (root != rank) && (leader_root == global_rank)) {
124             Request::recv(buffer, count, datatype, root,
125                                      COLL_TAG_BCAST, comm, MPI_STATUS_IGNORE);
126         }
127         if ((local_rank != 0) && (root == rank)) {
128             Request::send(buffer, count, datatype,
129                                      leader_of_root, COLL_TAG_BCAST, comm);
130         }
131     }
132 #if defined(_MCST_SUPPORT_)
133     if (comm_ptr->ch.is_mcast_ok) {
134         mpi_errno = MPIR_Mcast_inter_node_MV2(buffer, count, datatype, root, comm_ptr,
135                                               errflag);
136         if (mpi_errno == MPI_SUCCESS) {
137             goto fn_exit;
138         } else {
139             goto fn_fail;
140         }
141     }
142 #endif
143 /*
144     if (local_rank == 0) {
145         leader_comm = comm->get_leaders_comm();
146         root = leader_root;
147     }
148
149     if (MV2_Bcast_function == &MPIR_Pipelined_Bcast_MV2) {
150         mpi_errno = MPIR_Pipelined_Bcast_MV2(buffer, count, datatype,
151                                              root, comm);
152     } else if (MV2_Bcast_function == &MPIR_Bcast_scatter_ring_allgather_shm_MV2) {
153         mpi_errno = MPIR_Bcast_scatter_ring_allgather_shm_MV2(buffer, count,
154                                                               datatype, root,
155                                                               comm);
156     } else */{
157         if (local_rank == 0) {
158       /*      if (MV2_Bcast_function == &MPIR_Knomial_Bcast_inter_node_wrapper_MV2) {
159                 mpi_errno = MPIR_Knomial_Bcast_inter_node_wrapper_MV2(buffer, count,
160                                                               datatype, root,
161                                                               comm);
162             } else {*/
163                 mpi_errno = MV2_Bcast_function(buffer, count, datatype,
164                                                leader_root, leader_comm);
165           //  }
166         }
167     }
168
169     return mpi_errno;
170 }
171
172
173 int Coll_bcast_mvapich2_knomial_intra_node::bcast(void *buffer,
174                                       int count,
175                                       MPI_Datatype datatype,
176                                       int root, MPI_Comm  comm)
177 {
178     int local_size = 0, rank;
179     int mpi_errno = MPI_SUCCESS;
180     int src, dst, mask, relative_rank;
181     int k;
182     if (MV2_Bcast_function==NULL){
183       MV2_Bcast_function=Coll_bcast_mpich::bcast;
184     }
185
186     if (MV2_Bcast_intra_node_function==NULL){
187       MV2_Bcast_intra_node_function= Coll_bcast_mpich::bcast;
188     }
189
190     if(comm->get_leaders_comm()==MPI_COMM_NULL){
191       comm->init_smp();
192     }
193
194     local_size = comm->size();
195     rank = comm->rank();
196
197     MPI_Request* reqarray = new MPI_Request[2 * mv2_intra_node_knomial_factor];
198
199     MPI_Status* starray = new MPI_Status[2 * mv2_intra_node_knomial_factor];
200
201     /* intra-node k-nomial bcast  */
202     if (local_size > 1) {
203         relative_rank = (rank >= root) ? rank - root : rank - root + local_size;
204         mask = 0x1;
205
206         while (mask < local_size) {
207             if (relative_rank % (mv2_intra_node_knomial_factor * mask)) {
208                 src = relative_rank / (mv2_intra_node_knomial_factor * mask) *
209                     (mv2_intra_node_knomial_factor * mask) + root;
210                 if (src >= local_size) {
211                     src -= local_size;
212                 }
213
214                 Request::recv(buffer, count, datatype, src,
215                                          COLL_TAG_BCAST, comm,
216                                          MPI_STATUS_IGNORE);
217                 break;
218             }
219             mask *= mv2_intra_node_knomial_factor;
220         }
221         mask /= mv2_intra_node_knomial_factor;
222
223         while (mask > 0) {
224             int reqs = 0;
225             for (k = 1; k < mv2_intra_node_knomial_factor; k++) {
226                 if (relative_rank + mask * k < local_size) {
227                     dst = rank + mask * k;
228                     if (dst >= local_size) {
229                         dst -= local_size;
230                     }
231                     reqarray[reqs++]=Request::isend(buffer, count, datatype, dst,
232                                               COLL_TAG_BCAST, comm);
233                 }
234             }
235             Request::waitall(reqs, reqarray, starray);
236
237             mask /= mv2_intra_node_knomial_factor;
238         }
239     }
240     delete[] reqarray;
241     delete[] starray;
242     return mpi_errno;
243 }
244
245
246 int Coll_bcast_mvapich2_intra_node::bcast(void *buffer,
247                          int count,
248                          MPI_Datatype datatype,
249                          int root, MPI_Comm  comm)
250 {
251     int mpi_errno = MPI_SUCCESS;
252     int comm_size;
253     int two_level_bcast = 1;
254     size_t nbytes = 0;
255     int is_homogeneous, is_contig;
256     MPI_Aint type_size;
257     void *tmp_buf = NULL;
258     MPI_Comm shmem_comm;
259
260     if (count == 0)
261         return MPI_SUCCESS;
262     if (MV2_Bcast_function==NULL){
263       MV2_Bcast_function=Coll_bcast_mpich::bcast;
264     }
265
266     if (MV2_Bcast_intra_node_function==NULL){
267       MV2_Bcast_intra_node_function= Coll_bcast_mpich::bcast;
268     }
269
270     if(comm->get_leaders_comm()==MPI_COMM_NULL){
271       comm->init_smp();
272     }
273
274     comm_size = comm->size();
275    // rank = comm->rank();
276 /*
277     if (HANDLE_GET_KIND(datatype) == HANDLE_KIND_BUILTIN)*/
278         is_contig = 1;
279 /*    else {
280         MPID_Datatype_get_ptr(datatype, dtp);
281         is_contig = dtp->is_contig;
282     }
283 */
284     is_homogeneous = 1;
285 #ifdef MPID_HAS_HETERO
286     if (comm_ptr->is_hetero)
287         is_homogeneous = 0;
288 #endif
289
290     /* MPI_Type_size() might not give the accurate size of the packed
291      * datatype for heterogeneous systems (because of padding, encoding,
292      * etc). On the other hand, MPI_Pack_size() can become very
293      * expensive, depending on the implementation, especially for
294      * heterogeneous systems. We want to use MPI_Type_size() wherever
295      * possible, and MPI_Pack_size() in other places.
296      */
297     //if (is_homogeneous) {
298         type_size=datatype->size();
299     //}
300 /*    else {*/
301 /*        MPIR_Pack_size_impl(1, datatype, &type_size);*/
302 /*    }*/
303     nbytes = (size_t) (count) * (type_size);
304     if (comm_size <= mv2_bcast_two_level_system_size) {
305         if (nbytes > mv2_bcast_short_msg && nbytes < mv2_bcast_large_msg) {
306             two_level_bcast = 1;
307         } else {
308             two_level_bcast = 0;
309         }
310     }
311
312     if (two_level_bcast == 1
313 #if defined(_MCST_SUPPORT_)
314             || comm_ptr->ch.is_mcast_ok
315 #endif
316         ) {
317
318       if (not is_contig || not is_homogeneous) {
319         tmp_buf = (void*)smpi_get_tmp_sendbuffer(nbytes);
320
321         /* TODO: Pipeline the packing and communication */
322         // position = 0;
323         /*            if (rank == root) {*/
324         /*                mpi_errno =*/
325         /*                    MPIR_Pack_impl(buffer, count, datatype, tmp_buf, nbytes, &position);*/
326         /*                if (mpi_errno)*/
327         /*                    MPIU_ERR_POP(mpi_errno);*/
328         /*            }*/
329         }
330
331         shmem_comm = comm->get_intra_comm();
332         if (not is_contig || not is_homogeneous) {
333           mpi_errno = MPIR_Bcast_inter_node_helper_MV2(tmp_buf, nbytes, MPI_BYTE, root, comm);
334         } else {
335             mpi_errno =
336                 MPIR_Bcast_inter_node_helper_MV2(buffer, count, datatype, root,
337                                                  comm);
338         }
339
340         /* We are now done with the inter-node phase */
341             if (nbytes <= mv2_knomial_intra_node_threshold) {
342               if (not is_contig || not is_homogeneous) {
343                 mpi_errno = MPIR_Shmem_Bcast_MV2(tmp_buf, nbytes, MPI_BYTE, root, shmem_comm);
344                 } else {
345                     mpi_errno = MPIR_Shmem_Bcast_MV2(buffer, count, datatype,
346                                                      root, shmem_comm);
347                 }
348             } else {
349               if (not is_contig || not is_homogeneous) {
350                 mpi_errno = MPIR_Knomial_Bcast_intra_node_MV2(tmp_buf, nbytes, MPI_BYTE, INTRA_NODE_ROOT, shmem_comm);
351                 } else {
352                     mpi_errno =
353                         MPIR_Knomial_Bcast_intra_node_MV2(buffer, count,
354                                                           datatype,
355                                                           INTRA_NODE_ROOT,
356                                                           shmem_comm);
357                 }
358             }
359
360     } else {
361         if (nbytes <= mv2_bcast_short_msg) {
362             mpi_errno = MPIR_Bcast_binomial_MV2(buffer, count, datatype, root,
363                                                 comm);
364         } else {
365             if (mv2_scatter_rd_inter_leader_bcast) {
366                 mpi_errno = MPIR_Bcast_scatter_ring_allgather_MV2(buffer, count,
367                                                                   datatype,
368                                                                   root,
369                                                                   comm);
370             } else {
371                 mpi_errno =
372                     MPIR_Bcast_scatter_doubling_allgather_MV2(buffer, count,
373                                                               datatype, root,
374                                                               comm);
375             }
376         }
377     }
378
379
380     return mpi_errno;
381
382 }
383
384 }
385 }