Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / colls / reduce / reduce-mvapich-two-level.cpp
1 /* Copyright (c) 2013-2023. 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 #include <algorithm>
40
41 #define MV2_INTRA_SHMEM_REDUCE_MSG 2048
42
43 #define mv2_g_shmem_coll_max_msg_size (1 << 17)
44 #define SHMEM_COLL_BLOCK_SIZE (local_size * mv2_g_shmem_coll_max_msg_size)
45 #define mv2_use_knomial_reduce 1
46
47 #define MPIR_Reduce_inter_knomial_wrapper_MV2 reduce__mvapich2_knomial
48 #define MPIR_Reduce_intra_knomial_wrapper_MV2 reduce__mvapich2_knomial
49 #define MPIR_Reduce_binomial_MV2 reduce__binomial
50 #define MPIR_Reduce_redscat_gather_MV2 reduce__scatter_gather
51 #define MPIR_Reduce_shmem_MV2 reduce__ompi_basic_linear
52
53 extern int (*MV2_Reduce_function)( const void *sendbuf,
54     void *recvbuf,
55     int count,
56     MPI_Datatype datatype,
57     MPI_Op op,
58     int root,
59     MPI_Comm  comm_ptr);
60
61 extern int (*MV2_Reduce_intra_function)( const void *sendbuf,
62     void *recvbuf,
63     int count,
64     MPI_Datatype datatype,
65     MPI_Op op,
66     int root,
67     MPI_Comm  comm_ptr);
68
69
70 /*Fn pointers for collectives */
71 static int (*reduce_fn)(const void *sendbuf,
72                              void *recvbuf,
73                              int count,
74                              MPI_Datatype datatype,
75                              MPI_Op op, int root, MPI_Comm  comm);
76 namespace simgrid::smpi {
77 int reduce__mvapich2_two_level( const void *sendbuf,
78                                 void *recvbuf,
79                                 int count,
80                                 MPI_Datatype datatype,
81                                 MPI_Op op,
82                                 int root,
83                                 MPI_Comm comm)
84 {
85     int mpi_errno = MPI_SUCCESS;
86     int my_rank, total_size, local_rank, local_size;
87     int leader_comm_rank = -1, leader_comm_size = 0;
88     MPI_Comm shmem_comm, leader_comm;
89     int leader_root, leader_of_root;
90     const unsigned char* in_buf = nullptr;
91     unsigned char *out_buf = nullptr, *tmp_buf = nullptr;
92     MPI_Aint true_lb, true_extent, extent;
93     int stride          = 0;
94     int intra_node_root=0;
95
96     //if not set (use of the algo directly, without mvapich2 selector)
97     if (MV2_Reduce_function == nullptr)
98       MV2_Reduce_function = reduce__mpich;
99     if (MV2_Reduce_intra_function == nullptr)
100       MV2_Reduce_intra_function = reduce__mpich;
101
102     if(comm->get_leaders_comm()==MPI_COMM_NULL){
103       comm->init_smp();
104     }
105
106     my_rank = comm->rank();
107     total_size = comm->size();
108     shmem_comm = comm->get_intra_comm();
109     local_rank = shmem_comm->rank();
110     local_size = shmem_comm->size();
111
112     leader_comm = comm->get_leaders_comm();
113     int* leaders_map = comm->get_leaders_map();
114     leader_of_root = comm->group()->rank(leaders_map[root]);
115     leader_root = leader_comm->group()->rank(leaders_map[root]);
116
117     bool is_commutative = (op == MPI_OP_NULL || op->is_commutative());
118
119     datatype->extent(&true_lb,
120                                        &true_extent);
121     extent =datatype->get_extent();
122     stride = count * std::max(extent, true_extent);
123
124     if (local_size == total_size) {
125         /* First handle the case where there is only one node */
126         if (stride <= MV2_INTRA_SHMEM_REDUCE_MSG && is_commutative) {
127             if (local_rank == 0 ) {
128               tmp_buf = smpi_get_tmp_sendbuffer(count * std::max(extent, true_extent));
129               tmp_buf = tmp_buf - true_lb;
130             }
131
132             if (sendbuf != MPI_IN_PLACE) {
133               in_buf = static_cast<const unsigned char*>(sendbuf);
134             } else {
135               in_buf = static_cast<const unsigned char*>(recvbuf);
136             }
137
138             if (local_rank == 0) {
139                  if( my_rank != root) {
140                      out_buf = tmp_buf;
141                  } else {
142                    out_buf = static_cast<unsigned char*>(recvbuf);
143                    if (in_buf == out_buf) {
144                      in_buf  = static_cast<const unsigned char*>(MPI_IN_PLACE);
145                      out_buf = static_cast<unsigned char*>(recvbuf);
146                      }
147                  }
148             } else {
149               in_buf  = static_cast<const unsigned char*>(sendbuf);
150               out_buf = nullptr;
151             }
152
153             if (count * (std::max(extent, true_extent)) < SHMEM_COLL_BLOCK_SIZE) {
154               mpi_errno = MPIR_Reduce_shmem_MV2(in_buf, out_buf, count, datatype, op, 0, shmem_comm);
155             } else {
156               mpi_errno = MPIR_Reduce_intra_knomial_wrapper_MV2(in_buf, out_buf, count, datatype, op, 0, shmem_comm);
157             }
158
159             if (local_rank == 0 && root != my_rank) {
160                 Request::send(out_buf, count, datatype, root,
161                                          COLL_TAG_REDUCE+1, comm);
162             }
163             if ((local_rank != 0) && (root == my_rank)) {
164                 Request::recv(recvbuf, count, datatype,
165                                          leader_of_root, COLL_TAG_REDUCE+1, comm,
166                                          MPI_STATUS_IGNORE);
167             }
168         } else {
169             if(mv2_use_knomial_reduce == 1) {
170                 reduce_fn = &MPIR_Reduce_intra_knomial_wrapper_MV2;
171             } else {
172                 reduce_fn = &MPIR_Reduce_binomial_MV2;
173             }
174             mpi_errno = reduce_fn(sendbuf, recvbuf, count,
175                                   datatype, op,
176                                   root, comm);
177         }
178         /* We are done */
179         if (tmp_buf != nullptr)
180           smpi_free_tmp_buffer(tmp_buf + true_lb);
181         goto fn_exit;
182     }
183
184
185     if (local_rank == 0) {
186         leader_comm = comm->get_leaders_comm();
187         if(leader_comm==MPI_COMM_NULL){
188           leader_comm = MPI_COMM_WORLD;
189         }
190         leader_comm_size = leader_comm->size();
191         leader_comm_rank = leader_comm->rank();
192         tmp_buf          = smpi_get_tmp_sendbuffer(count * std::max(extent, true_extent));
193         tmp_buf          = tmp_buf - true_lb;
194     }
195     if (sendbuf != MPI_IN_PLACE) {
196       in_buf = static_cast<const unsigned char*>(sendbuf);
197     } else {
198       in_buf = static_cast<const unsigned char*>(recvbuf);
199     }
200     if (local_rank == 0) {
201       out_buf = static_cast<unsigned char*>(tmp_buf);
202     } else {
203       out_buf = nullptr;
204     }
205
206
207     if(local_size > 1) {
208         /* Lets do the intra-node reduce operations, if we have more than one
209          * process in the node */
210
211         /*Fix the input and outbuf buffers for the intra-node reduce.
212          *Node leaders will have the reduced data in tmp_buf after
213          *this step*/
214         if (MV2_Reduce_intra_function == & MPIR_Reduce_shmem_MV2)
215         {
216           if (is_commutative && (count * (std::max(extent, true_extent)) < SHMEM_COLL_BLOCK_SIZE)) {
217             mpi_errno = MV2_Reduce_intra_function(in_buf, out_buf, count, datatype, op, intra_node_root, shmem_comm);
218             } else {
219                     mpi_errno = MPIR_Reduce_intra_knomial_wrapper_MV2(in_buf, out_buf, count,
220                                       datatype, op,
221                                       intra_node_root, shmem_comm);
222             }
223         } else {
224
225             mpi_errno = MV2_Reduce_intra_function(in_buf, out_buf, count,
226                                       datatype, op,
227                                       intra_node_root, shmem_comm);
228         }
229     } else {
230       smpi_free_tmp_buffer(tmp_buf + true_lb);
231       tmp_buf = (unsigned char*)in_buf; // xxx
232     }
233
234     /* Now work on the inter-leader phase. Data is in tmp_buf */
235     if (local_rank == 0 && leader_comm_size > 1) {
236         /*The leader of root will have the global reduced data in tmp_buf
237            or recv_buf
238            at the end of the reduce */
239         if (leader_comm_rank == leader_root) {
240             if (my_rank == root) {
241                 /* I am the root of the leader-comm, and the
242                  * root of the reduce op. So, I will write the
243                  * final result directly into my recvbuf */
244                 if(tmp_buf != recvbuf) {
245                   in_buf  = tmp_buf;
246                   out_buf = static_cast<unsigned char*>(recvbuf);
247                 } else {
248
249                   unsigned char* buf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
250                   Datatype::copy(tmp_buf, count, datatype, buf, count, datatype);
251                   // in_buf = MPI_IN_PLACE;
252                   in_buf  = buf;
253                   out_buf = static_cast<unsigned char*>(recvbuf);
254                 }
255             } else {
256               unsigned char* buf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
257               Datatype::copy(tmp_buf, count, datatype, buf, count, datatype);
258               // in_buf = MPI_IN_PLACE;
259               in_buf  = buf;
260               out_buf = tmp_buf;
261             }
262         } else {
263             in_buf = tmp_buf;
264             out_buf = nullptr;
265         }
266
267         /* inter-leader communication  */
268         mpi_errno = MV2_Reduce_function(in_buf, out_buf, count,
269                               datatype, op,
270                               leader_root, leader_comm);
271
272     }
273
274     if (local_size > 1) {
275       /* Send the message to the root if the leader is not the
276        * root of the reduce operation. The reduced data is in tmp_buf */
277       if ((local_rank == 0) && (root != my_rank) && (leader_root == leader_comm_rank)) {
278         Request::send(tmp_buf, count, datatype, root, COLL_TAG_REDUCE + 1, comm);
279       }
280       if ((local_rank != 0) && (root == my_rank)) {
281         Request::recv(recvbuf, count, datatype, leader_of_root, COLL_TAG_REDUCE + 1, comm, MPI_STATUS_IGNORE);
282       }
283       smpi_free_tmp_buffer(tmp_buf + true_lb);
284
285       if (leader_comm_rank == leader_root) {
286         if (my_rank != root || (my_rank == root && tmp_buf == recvbuf)) {
287           smpi_free_tmp_buffer(in_buf);
288         }
289       }
290     }
291
292
293
294   fn_exit:
295     return mpi_errno;
296 }
297 } // namespace simgrid::smpi