Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Concatenate nested namespaces (sonar).
[simgrid.git] / src / smpi / colls / bcast / bcast-mvapich-smp.cpp
1 /* Copyright (c) 2013-2022. 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 bcast__mpich
55 #define MPIR_Pipelined_Bcast_MV2 bcast__mpich
56 #define MPIR_Bcast_binomial_MV2 bcast__binomial_tree
57 #define MPIR_Bcast_scatter_ring_allgather_shm_MV2 bcast__scatter_LR_allgather
58 #define MPIR_Bcast_scatter_doubling_allgather_MV2 bcast__scatter_rdb_allgather
59 #define MPIR_Bcast_scatter_ring_allgather_MV2 bcast__scatter_LR_allgather
60 #define MPIR_Shmem_Bcast_MV2 bcast__mpich
61 #define MPIR_Bcast_tune_inter_node_helper_MV2 bcast__mvapich2_inter_node
62 #define MPIR_Bcast_inter_node_helper_MV2 bcast__mvapich2_inter_node
63 #define MPIR_Knomial_Bcast_intra_node_MV2 bcast__mvapich2_knomial_intra_node
64 #define MPIR_Bcast_intra_MV2 bcast__mvapich2_intra_node
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::smpi {
77 int bcast__mvapich2_inter_node(void *buffer,
78                                int count,
79                                MPI_Datatype datatype,
80                                int root,
81                                MPI_Comm  comm)
82 {
83     int rank;
84     int mpi_errno = MPI_SUCCESS;
85     MPI_Comm shmem_comm, leader_comm;
86     int local_rank, local_size, global_rank = -1;
87     int leader_root, leader_of_root;
88
89
90     rank = comm->rank();
91     //comm_size = comm->size();
92
93     if (MV2_Bcast_function == nullptr) {
94       MV2_Bcast_function = bcast__mpich;
95     }
96
97     if (MV2_Bcast_intra_node_function == nullptr) {
98       MV2_Bcast_intra_node_function = bcast__mpich;
99     }
100
101     if(comm->get_leaders_comm()==MPI_COMM_NULL){
102       comm->init_smp();
103     }
104
105     shmem_comm = comm->get_intra_comm();
106     local_rank = shmem_comm->rank();
107     local_size = shmem_comm->size();
108
109     leader_comm = comm->get_leaders_comm();
110
111     if ((local_rank == 0) && (local_size > 1)) {
112       global_rank = leader_comm->rank();
113     }
114
115     int* leaders_map = comm->get_leaders_map();
116     leader_of_root = comm->group()->rank(leaders_map[root]);
117     leader_root = leader_comm->group()->rank(leaders_map[root]);
118
119
120     if (local_size > 1) {
121         if ((local_rank == 0) && (root != rank) && (leader_root == global_rank)) {
122             Request::recv(buffer, count, datatype, root,
123                                      COLL_TAG_BCAST, comm, MPI_STATUS_IGNORE);
124         }
125         if ((local_rank != 0) && (root == rank)) {
126             Request::send(buffer, count, datatype,
127                                      leader_of_root, COLL_TAG_BCAST, comm);
128         }
129     }
130 #if defined(_MCST_SUPPORT_)
131     if (comm_ptr->ch.is_mcast_ok) {
132         mpi_errno = MPIR_Mcast_inter_node_MV2(buffer, count, datatype, root, comm_ptr,
133                                               errflag);
134         if (mpi_errno == MPI_SUCCESS) {
135             goto fn_exit;
136         } else {
137             goto fn_fail;
138         }
139     }
140 #endif
141 /*
142     if (local_rank == 0) {
143         leader_comm = comm->get_leaders_comm();
144         root = leader_root;
145     }
146
147     if (MV2_Bcast_function == &MPIR_Pipelined_Bcast_MV2) {
148         mpi_errno = MPIR_Pipelined_Bcast_MV2(buffer, count, datatype,
149                                              root, comm);
150     } else if (MV2_Bcast_function == &MPIR_Bcast_scatter_ring_allgather_shm_MV2) {
151         mpi_errno = MPIR_Bcast_scatter_ring_allgather_shm_MV2(buffer, count,
152                                                               datatype, root,
153                                                               comm);
154     } else */{
155         if (local_rank == 0) {
156       /*      if (MV2_Bcast_function == &MPIR_Knomial_Bcast_inter_node_wrapper_MV2) {
157                 mpi_errno = MPIR_Knomial_Bcast_inter_node_wrapper_MV2(buffer, count,
158                                                               datatype, root,
159                                                               comm);
160             } else {*/
161                 mpi_errno = MV2_Bcast_function(buffer, count, datatype,
162                                                leader_root, leader_comm);
163           //  }
164         }
165     }
166
167     return mpi_errno;
168 }
169
170
171 int bcast__mvapich2_knomial_intra_node(void *buffer,
172                                        int count,
173                                        MPI_Datatype datatype,
174                                        int root, MPI_Comm  comm)
175 {
176     int local_size = 0, rank;
177     int mpi_errno = MPI_SUCCESS;
178     int src, dst, mask, relative_rank;
179     int k;
180     if (MV2_Bcast_function == nullptr) {
181       MV2_Bcast_function = bcast__mpich;
182     }
183
184     if (MV2_Bcast_intra_node_function == nullptr) {
185       MV2_Bcast_intra_node_function = bcast__mpich;
186     }
187
188     if(comm->get_leaders_comm()==MPI_COMM_NULL){
189       comm->init_smp();
190     }
191
192     local_size = comm->size();
193     rank = comm->rank();
194
195     auto* reqarray = new MPI_Request[2 * mv2_intra_node_knomial_factor];
196
197     auto* starray = new MPI_Status[2 * mv2_intra_node_knomial_factor];
198
199     /* intra-node k-nomial bcast  */
200     if (local_size > 1) {
201         relative_rank = (rank >= root) ? rank - root : rank - root + local_size;
202         mask = 0x1;
203
204         while (mask < local_size) {
205             if (relative_rank % (mv2_intra_node_knomial_factor * mask)) {
206                 src = relative_rank / (mv2_intra_node_knomial_factor * mask) *
207                     (mv2_intra_node_knomial_factor * mask) + root;
208                 if (src >= local_size) {
209                     src -= local_size;
210                 }
211
212                 Request::recv(buffer, count, datatype, src,
213                                          COLL_TAG_BCAST, comm,
214                                          MPI_STATUS_IGNORE);
215                 break;
216             }
217             mask *= mv2_intra_node_knomial_factor;
218         }
219         mask /= mv2_intra_node_knomial_factor;
220
221         while (mask > 0) {
222             int reqs = 0;
223             for (k = 1; k < mv2_intra_node_knomial_factor; k++) {
224                 if (relative_rank + mask * k < local_size) {
225                     dst = rank + mask * k;
226                     if (dst >= local_size) {
227                         dst -= local_size;
228                     }
229                     reqarray[reqs++]=Request::isend(buffer, count, datatype, dst,
230                                               COLL_TAG_BCAST, comm);
231                 }
232             }
233             Request::waitall(reqs, reqarray, starray);
234
235             mask /= mv2_intra_node_knomial_factor;
236         }
237     }
238     delete[] reqarray;
239     delete[] starray;
240     return mpi_errno;
241 }
242
243
244 int bcast__mvapich2_intra_node(void *buffer,
245                                int count,
246                                MPI_Datatype datatype,
247                                int root, MPI_Comm  comm)
248 {
249     int mpi_errno = MPI_SUCCESS;
250     int comm_size;
251     bool two_level_bcast = true;
252     size_t nbytes = 0;
253     bool is_homogeneous, is_contig;
254     MPI_Aint type_size;
255     unsigned char* tmp_buf = nullptr;
256     MPI_Comm shmem_comm;
257
258     if (count == 0)
259         return MPI_SUCCESS;
260     if (MV2_Bcast_function == nullptr) {
261       MV2_Bcast_function = bcast__mpich;
262     }
263
264     if (MV2_Bcast_intra_node_function == nullptr) {
265       MV2_Bcast_intra_node_function = bcast__mpich;
266     }
267
268     if(comm->get_leaders_comm()==MPI_COMM_NULL){
269       comm->init_smp();
270     }
271
272     comm_size = comm->size();
273    // rank = comm->rank();
274 /*
275     if (HANDLE_GET_KIND(datatype) == HANDLE_KIND_BUILTIN)*/
276         is_contig = true;
277 /*    else {
278         MPID_Datatype_get_ptr(datatype, dtp);
279         is_contig = dtp->is_contig;
280     }
281 */
282     is_homogeneous = true;
283 #ifdef MPID_HAS_HETERO
284     if (comm_ptr->is_hetero)
285       is_homogeneous = false;
286 #endif
287
288     /* MPI_Type_size() might not give the accurate size of the packed
289      * datatype for heterogeneous systems (because of padding, encoding,
290      * etc). On the other hand, MPI_Pack_size() can become very
291      * expensive, depending on the implementation, especially for
292      * heterogeneous systems. We want to use MPI_Type_size() wherever
293      * possible, and MPI_Pack_size() in other places.
294      */
295     //if (is_homogeneous) {
296         type_size=datatype->size();
297     //}
298 /*    else {*/
299 /*        MPIR_Pack_size_impl(1, datatype, &type_size);*/
300 /*    }*/
301     nbytes = (size_t) (count) * (type_size);
302     if (comm_size <= mv2_bcast_two_level_system_size) {
303         if (nbytes > mv2_bcast_short_msg && nbytes < mv2_bcast_large_msg) {
304           two_level_bcast = true;
305         } else {
306           two_level_bcast = false;
307         }
308     }
309
310     if (two_level_bcast
311 #if defined(_MCST_SUPPORT_)
312             || comm_ptr->ch.is_mcast_ok
313 #endif
314         ) {
315
316       if (not is_contig || not is_homogeneous) {
317         tmp_buf = smpi_get_tmp_sendbuffer(nbytes);
318
319         /* TODO: Pipeline the packing and communication */
320         // position = 0;
321         /*            if (rank == root) {*/
322         /*                mpi_errno =*/
323         /*                    MPIR_Pack_impl(buffer, count, datatype, tmp_buf, nbytes, &position);*/
324         /*                if (mpi_errno)*/
325         /*                    MPIU_ERR_POP(mpi_errno);*/
326         /*            }*/
327         }
328
329         shmem_comm = comm->get_intra_comm();
330         if (not is_contig || not is_homogeneous) {
331           mpi_errno = MPIR_Bcast_inter_node_helper_MV2(tmp_buf, nbytes, MPI_BYTE, root, comm);
332         } else {
333             mpi_errno =
334                 MPIR_Bcast_inter_node_helper_MV2(buffer, count, datatype, root,
335                                                  comm);
336         }
337
338         /* We are now done with the inter-node phase */
339             if (nbytes <= mv2_knomial_intra_node_threshold) {
340               if (not is_contig || not is_homogeneous) {
341                 mpi_errno = MPIR_Shmem_Bcast_MV2(tmp_buf, nbytes, MPI_BYTE, root, shmem_comm);
342                 } else {
343                     mpi_errno = MPIR_Shmem_Bcast_MV2(buffer, count, datatype,
344                                                      root, shmem_comm);
345                 }
346             } else {
347               if (not is_contig || not is_homogeneous) {
348                 mpi_errno = MPIR_Knomial_Bcast_intra_node_MV2(tmp_buf, nbytes, MPI_BYTE, INTRA_NODE_ROOT, shmem_comm);
349                 } else {
350                     mpi_errno =
351                         MPIR_Knomial_Bcast_intra_node_MV2(buffer, count,
352                                                           datatype,
353                                                           INTRA_NODE_ROOT,
354                                                           shmem_comm);
355                 }
356             }
357
358     } else {
359         if (nbytes <= mv2_bcast_short_msg) {
360             mpi_errno = MPIR_Bcast_binomial_MV2(buffer, count, datatype, root,
361                                                 comm);
362         } else {
363             if (mv2_scatter_rd_inter_leader_bcast) {
364                 mpi_errno = MPIR_Bcast_scatter_ring_allgather_MV2(buffer, count,
365                                                                   datatype,
366                                                                   root,
367                                                                   comm);
368             } else {
369                 mpi_errno =
370                     MPIR_Bcast_scatter_doubling_allgather_MV2(buffer, count,
371                                                               datatype, root,
372                                                               comm);
373             }
374         }
375     }
376
377
378     return mpi_errno;
379
380 }
381
382 } // namespace simgrid::smpi