Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change smpi::Colls static class into a namespace of functions
[simgrid.git] / src / smpi / colls / gather / gather-mvapich.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 #include <algorithm>
40
41 #define MPIR_Gather_MV2_Direct gather__ompi_basic_linear
42 #define MPIR_Gather_MV2_two_level_Direct gather__ompi_basic_linear
43 #define MPIR_Gather_intra gather__mpich
44 typedef int (*MV2_Gather_function_ptr) (const void *sendbuf,
45     int sendcnt,
46     MPI_Datatype sendtype,
47     void *recvbuf,
48     int recvcnt,
49     MPI_Datatype recvtype,
50     int root, MPI_Comm comm);
51
52 extern MV2_Gather_function_ptr MV2_Gather_inter_leader_function;
53 extern MV2_Gather_function_ptr MV2_Gather_intra_node_function;
54
55 #define TEMP_BUF_HAS_NO_DATA (0)
56 #define TEMP_BUF_HAS_DATA (1)
57
58
59 namespace simgrid{
60 namespace smpi{
61
62 /* sendbuf           - (in) sender's buffer
63  * sendcnt           - (in) sender's element count
64  * sendtype          - (in) sender's data type
65  * recvbuf           - (in) receiver's buffer
66  * recvcnt           - (in) receiver's element count
67  * recvtype          - (in) receiver's data type
68  * root              - (in)root for the gather operation
69  * rank              - (in) global rank(rank in the global comm)
70  * tmp_buf           - (out/in) tmp_buf into which intra node
71  *                     data is gathered
72  * is_data_avail     - (in) based on this, tmp_buf acts
73  *                     as in/out parameter.
74  *                     1 - tmp_buf acts as in parameter
75  *                     0 - tmp_buf acts as out parameter
76  * comm_ptr          - (in) pointer to the communicator
77  *                     (shmem_comm or intra_sock_comm or
78  *                     inter-sock_leader_comm)
79  * intra_node_fn_ptr - (in) Function ptr to choose the
80  *                      intra node gather function
81  * errflag           - (out) to record errors
82  */
83 static int MPIR_pt_pt_intra_gather( const void *sendbuf, int sendcnt, MPI_Datatype sendtype,
84                             void *recvbuf, int recvcnt, MPI_Datatype recvtype,
85                             int root, int rank,
86                             void *tmp_buf, int nbytes,
87                             int is_data_avail,
88                             MPI_Comm comm,
89                             MV2_Gather_function_ptr intra_node_fn_ptr)
90 {
91     int mpi_errno = MPI_SUCCESS;
92     MPI_Aint recvtype_extent = 0;  /* Datatype extent */
93     MPI_Aint true_lb, sendtype_true_extent, recvtype_true_extent;
94
95
96     if (sendtype != MPI_DATATYPE_NULL) {
97         sendtype->extent(&true_lb,
98                                        &sendtype_true_extent);
99     }
100     if (recvtype != MPI_DATATYPE_NULL) {
101         recvtype_extent=recvtype->get_extent();
102         recvtype->extent(&true_lb,
103                                        &recvtype_true_extent);
104     }
105
106     /* Special case, when tmp_buf itself has data */
107     if (rank == root && sendbuf == MPI_IN_PLACE && is_data_avail) {
108
109          mpi_errno = intra_node_fn_ptr(MPI_IN_PLACE,
110                                        sendcnt, sendtype, tmp_buf, nbytes,
111                                        MPI_BYTE, 0, comm);
112
113     } else if (rank == root && sendbuf == MPI_IN_PLACE) {
114          mpi_errno = intra_node_fn_ptr((char*)recvbuf +
115                                        rank * recvcnt * recvtype_extent,
116                                        recvcnt, recvtype, tmp_buf, nbytes,
117                                        MPI_BYTE, 0, comm);
118     } else {
119         mpi_errno = intra_node_fn_ptr(sendbuf, sendcnt, sendtype,
120                                       tmp_buf, nbytes, MPI_BYTE,
121                                       0, comm);
122     }
123
124     return mpi_errno;
125
126 }
127
128
129
130 int gather__mvapich2_two_level(const void *sendbuf,
131                                int sendcnt,
132                                MPI_Datatype sendtype,
133                                void *recvbuf,
134                                int recvcnt,
135                                MPI_Datatype recvtype,
136                                int root,
137                                MPI_Comm comm)
138 {
139   unsigned char* leader_gather_buf = NULL;
140   int comm_size, rank;
141   int local_rank, local_size;
142   int leader_comm_rank = -1, leader_comm_size = 0;
143   int mpi_errno     = MPI_SUCCESS;
144   int recvtype_size = 0, sendtype_size = 0, nbytes = 0;
145   int leader_root, leader_of_root;
146   MPI_Status status;
147   MPI_Aint sendtype_extent = 0, recvtype_extent = 0; /* Datatype extent */
148   MPI_Aint true_lb = 0, sendtype_true_extent = 0, recvtype_true_extent = 0;
149   MPI_Comm shmem_comm, leader_comm;
150   unsigned char* tmp_buf = NULL;
151
152   // if not set (use of the algo directly, without mvapich2 selector)
153   if (MV2_Gather_intra_node_function == NULL)
154     MV2_Gather_intra_node_function = gather__mpich;
155
156   if (comm->get_leaders_comm() == MPI_COMM_NULL) {
157     comm->init_smp();
158     }
159     comm_size = comm->size();
160     rank = comm->rank();
161
162     if (((rank == root) && (recvcnt == 0)) ||
163         ((rank != root) && (sendcnt == 0))) {
164         return MPI_SUCCESS;
165     }
166
167     if (sendtype != MPI_DATATYPE_NULL) {
168         sendtype_extent=sendtype->get_extent();
169         sendtype_size=sendtype->size();
170         sendtype->extent(&true_lb,
171                                        &sendtype_true_extent);
172     }
173     if (recvtype != MPI_DATATYPE_NULL) {
174         recvtype_extent=recvtype->get_extent();
175         recvtype_size=recvtype->size();
176         recvtype->extent(&true_lb,
177                                        &recvtype_true_extent);
178     }
179
180     /* extract the rank,size information for the intra-node
181      * communicator */
182     shmem_comm = comm->get_intra_comm();
183     local_rank = shmem_comm->rank();
184     local_size = shmem_comm->size();
185
186     if (local_rank == 0) {
187         /* Node leader. Extract the rank, size information for the leader
188          * communicator */
189         leader_comm = comm->get_leaders_comm();
190         if(leader_comm==MPI_COMM_NULL){
191           leader_comm = MPI_COMM_WORLD;
192         }
193         leader_comm_size = leader_comm->size();
194         leader_comm_rank = leader_comm->size();
195     }
196
197     if (rank == root) {
198         nbytes = recvcnt * recvtype_size;
199
200     } else {
201         nbytes = sendcnt * sendtype_size;
202     }
203
204 #if defined(_SMP_LIMIC_)
205      if((g_use_limic2_coll) && (shmem_commptr->ch.use_intra_sock_comm == 1)
206          && (use_limic_gather)
207          &&((num_scheme == USE_GATHER_PT_PT_BINOMIAL)
208             || (num_scheme == USE_GATHER_PT_PT_DIRECT)
209             ||(num_scheme == USE_GATHER_PT_LINEAR_BINOMIAL)
210             || (num_scheme == USE_GATHER_PT_LINEAR_DIRECT)
211             || (num_scheme == USE_GATHER_LINEAR_PT_BINOMIAL)
212             || (num_scheme == USE_GATHER_LINEAR_PT_DIRECT)
213             || (num_scheme == USE_GATHER_LINEAR_LINEAR)
214             || (num_scheme == USE_GATHER_SINGLE_LEADER))) {
215
216             mpi_errno = MV2_Gather_intra_node_function(sendbuf, sendcnt, sendtype,
217                                                     recvbuf, recvcnt,recvtype,
218                                                     root, comm);
219      } else
220
221 #endif/*#if defined(_SMP_LIMIC_)*/
222     {
223         if (local_rank == 0) {
224             /* Node leader, allocate tmp_buffer */
225             if (rank == root) {
226               tmp_buf = smpi_get_tmp_recvbuffer(recvcnt * std::max(recvtype_extent, recvtype_true_extent) * local_size);
227             } else {
228               tmp_buf = smpi_get_tmp_sendbuffer(sendcnt * std::max(sendtype_extent, sendtype_true_extent) * local_size);
229             }
230             if (tmp_buf == NULL) {
231                 mpi_errno = MPI_ERR_OTHER;
232                 return mpi_errno;
233             }
234         }
235          /*while testing mpich2 gather test, we see that
236          * which basically splits the comm, and we come to
237          * a point, where use_intra_sock_comm == 0, but if the
238          * intra node function is MPIR_Intra_node_LIMIC_Gather_MV2,
239          * it would use the intra sock comm. In such cases, we
240          * fallback to binomial as a default case.*/
241 #if defined(_SMP_LIMIC_)
242         if(*MV2_Gather_intra_node_function == MPIR_Intra_node_LIMIC_Gather_MV2) {
243
244             mpi_errno  = MPIR_pt_pt_intra_gather(sendbuf,sendcnt, sendtype,
245                                                  recvbuf, recvcnt, recvtype,
246                                                  root, rank,
247                                                  tmp_buf, nbytes,
248                                                  TEMP_BUF_HAS_NO_DATA,
249                                                  shmem_commptr,
250                                                  MPIR_Gather_intra);
251         } else
252 #endif
253         {
254             /*We are gathering the data into tmp_buf and the output
255              * will be of MPI_BYTE datatype. Since the tmp_buf has no
256              * local data, we pass is_data_avail = TEMP_BUF_HAS_NO_DATA*/
257             mpi_errno  = MPIR_pt_pt_intra_gather(sendbuf,sendcnt, sendtype,
258                                                  recvbuf, recvcnt, recvtype,
259                                                  root, rank,
260                                                  tmp_buf, nbytes,
261                                                  TEMP_BUF_HAS_NO_DATA,
262                                                  shmem_comm,
263                                                  MV2_Gather_intra_node_function
264                                                  );
265         }
266     }
267     leader_comm = comm->get_leaders_comm();
268     int* leaders_map = comm->get_leaders_map();
269     leader_of_root = comm->group()->rank(leaders_map[root]);
270     leader_root = leader_comm->group()->rank(leaders_map[root]);
271     /* leader_root is the rank of the leader of the root in leader_comm.
272      * leader_root is to be used as the root of the inter-leader gather ops
273      */
274     if (not comm->is_uniform()) {
275       if (local_rank == 0) {
276         int* displs   = NULL;
277         int* recvcnts = NULL;
278         int* node_sizes;
279         int i = 0;
280         /* Node leaders have all the data. But, different nodes can have
281          * different number of processes. Do a Gather first to get the
282          * buffer lengths at each leader, followed by a Gatherv to move
283          * the actual data */
284
285         if (leader_comm_rank == leader_root && root != leader_of_root) {
286           /* The root of the Gather operation is not a node-level
287            * leader and this process's rank in the leader_comm
288            * is the same as leader_root */
289           if (rank == root) {
290             leader_gather_buf =
291                 smpi_get_tmp_recvbuffer(recvcnt * std::max(recvtype_extent, recvtype_true_extent) * comm_size);
292           } else {
293             leader_gather_buf =
294                 smpi_get_tmp_sendbuffer(sendcnt * std::max(sendtype_extent, sendtype_true_extent) * comm_size);
295           }
296           if (leader_gather_buf == NULL) {
297             mpi_errno = MPI_ERR_OTHER;
298             return mpi_errno;
299           }
300         }
301
302         node_sizes = comm->get_non_uniform_map();
303
304         if (leader_comm_rank == leader_root) {
305           displs   = new int[leader_comm_size];
306           recvcnts = new int[leader_comm_size];
307         }
308
309         if (root == leader_of_root) {
310           /* The root of the gather operation is also the node
311            * leader. Receive into recvbuf and we are done */
312           if (leader_comm_rank == leader_root) {
313             recvcnts[0] = node_sizes[0] * recvcnt;
314             displs[0]   = 0;
315
316             for (i = 1; i < leader_comm_size; i++) {
317               displs[i]   = displs[i - 1] + node_sizes[i - 1] * recvcnt;
318               recvcnts[i] = node_sizes[i] * recvcnt;
319             }
320           }
321           colls::gatherv(tmp_buf, local_size * nbytes, MPI_BYTE, recvbuf, recvcnts, displs, recvtype, leader_root,
322                          leader_comm);
323         } else {
324           /* The root of the gather operation is not the node leader.
325            * Receive into leader_gather_buf and then send
326            * to the root */
327           if (leader_comm_rank == leader_root) {
328             recvcnts[0] = node_sizes[0] * nbytes;
329             displs[0]   = 0;
330
331             for (i = 1; i < leader_comm_size; i++) {
332               displs[i]   = displs[i - 1] + node_sizes[i - 1] * nbytes;
333               recvcnts[i] = node_sizes[i] * nbytes;
334             }
335           }
336           colls::gatherv(tmp_buf, local_size * nbytes, MPI_BYTE, leader_gather_buf, recvcnts, displs, MPI_BYTE,
337                          leader_root, leader_comm);
338         }
339         if (leader_comm_rank == leader_root) {
340           delete[] displs;
341           delete[] recvcnts;
342         }
343       }
344     } else {
345         /* All nodes have the same number of processes.
346          * Just do one Gather to get all
347          * the data at the leader of the root process */
348         if (local_rank == 0) {
349             if (leader_comm_rank == leader_root && root != leader_of_root) {
350                 /* The root of the Gather operation is not a node-level leader
351                  */
352                 leader_gather_buf = smpi_get_tmp_sendbuffer(nbytes * comm_size);
353                 if (leader_gather_buf == NULL) {
354                     mpi_errno = MPI_ERR_OTHER;
355                     return mpi_errno;
356                 }
357             }
358             if (root == leader_of_root) {
359                 mpi_errno = MPIR_Gather_MV2_Direct(tmp_buf,
360                                                    nbytes * local_size,
361                                                    MPI_BYTE, recvbuf,
362                                                    recvcnt * local_size,
363                                                    recvtype, leader_root,
364                                                    leader_comm);
365
366             } else {
367                 mpi_errno = MPIR_Gather_MV2_Direct(tmp_buf, nbytes * local_size,
368                                                    MPI_BYTE, leader_gather_buf,
369                                                    nbytes * local_size,
370                                                    MPI_BYTE, leader_root,
371                                                    leader_comm);
372             }
373         }
374     }
375     if ((local_rank == 0) && (root != rank)
376         && (leader_of_root == rank)) {
377         Request::send(leader_gather_buf,
378                                  nbytes * comm_size, MPI_BYTE,
379                                  root, COLL_TAG_GATHER, comm);
380     }
381
382     if (rank == root && local_rank != 0) {
383         /* The root of the gather operation is not the node leader. Receive
384          y* data from the node leader */
385         Request::recv(recvbuf, recvcnt * comm_size, recvtype,
386                                  leader_of_root, COLL_TAG_GATHER, comm,
387                                  &status);
388     }
389
390     /* check if multiple threads are calling this collective function */
391     if (local_rank == 0 ) {
392         if (tmp_buf != NULL) {
393             smpi_free_tmp_buffer(tmp_buf);
394         }
395         if (leader_gather_buf != NULL) {
396             smpi_free_tmp_buffer(leader_gather_buf);
397         }
398     }
399
400     return (mpi_errno);
401 }
402 }
403 }
404