Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add smpi_status.cpp and hpp.
[simgrid.git] / src / smpi / colls / allgather / allgather-mvapich-smp.cpp
1 /* Copyright (c) 2013-2014. 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  #include "../colls_private.h"
38
39 int Coll_allgather_mvapich2_smp::allgather(void *sendbuf,int sendcnt, MPI_Datatype sendtype,
40                             void *recvbuf, int recvcnt,MPI_Datatype recvtype,
41                             MPI_Comm  comm)
42 {
43     int rank, size;
44     int local_rank, local_size;
45     int leader_comm_size = 0; 
46     int mpi_errno = MPI_SUCCESS;
47     MPI_Aint recvtype_extent = 0;  /* Datatype extent */
48     MPI_Comm shmem_comm, leader_comm;
49
50   if(comm->get_leaders_comm()==MPI_COMM_NULL){
51     comm->init_smp();
52   }
53   
54     if(!comm->is_uniform() || !comm->is_blocked())
55     THROWF(arg_error,0, "allgather MVAPICH2 smp algorithm can't be used with irregular deployment. Please insure that processes deployed on the same node are contiguous and that each node has the same number of processes");
56   
57     if (recvcnt == 0) {
58         return MPI_SUCCESS;
59     }
60
61     rank = comm->rank();
62     size = comm->size();
63
64     /* extract the rank,size information for the intra-node communicator */
65     recvtype_extent=recvtype->get_extent();
66     
67     shmem_comm = comm->get_intra_comm();
68     local_rank = shmem_comm->rank();
69     local_size = shmem_comm->size();
70
71     if (local_rank == 0) {
72         /* Node leader. Extract the rank, size information for the leader communicator */
73         leader_comm = comm->get_leaders_comm();
74         if(leader_comm==MPI_COMM_NULL){
75           leader_comm = MPI_COMM_WORLD;
76         }
77         leader_comm_size = leader_comm->size();
78     }
79
80     /*If there is just one node, after gather itself,
81      * root has all the data and it can do bcast*/
82     if(local_rank == 0) {
83         mpi_errno = Colls::gather(sendbuf, sendcnt,sendtype, 
84                                     (void*)((char*)recvbuf + (rank * recvcnt * recvtype_extent)), 
85                                      recvcnt, recvtype,
86                                      0, shmem_comm);
87     } else {
88         /*Since in allgather all the processes could have 
89          * its own data in place*/
90         if(sendbuf == MPI_IN_PLACE) {
91             mpi_errno = Colls::gather((void*)((char*)recvbuf + (rank * recvcnt * recvtype_extent)), 
92                                          recvcnt , recvtype, 
93                                          recvbuf, recvcnt, recvtype,
94                                          0, shmem_comm);
95         } else {
96             mpi_errno = Colls::gather(sendbuf, sendcnt,sendtype, 
97                                          recvbuf, recvcnt, recvtype,
98                                          0, shmem_comm);
99         }
100     }
101     /* Exchange the data between the node leaders*/
102     if (local_rank == 0 && (leader_comm_size > 1)) {
103         /*When data in each socket is different*/
104         if (comm->is_uniform() != 1) {
105
106             int *displs = NULL;
107             int *recvcnts = NULL;
108             int *node_sizes = NULL;
109             int i = 0;
110
111             node_sizes = comm->get_non_uniform_map();
112
113             displs =  static_cast<int *>(xbt_malloc(sizeof (int) * leader_comm_size));
114             recvcnts =  static_cast<int *>(xbt_malloc(sizeof (int) * leader_comm_size));
115             if (!displs || !recvcnts) {
116                 return MPI_ERR_OTHER;
117             }
118             recvcnts[0] = node_sizes[0] * recvcnt;
119             displs[0] = 0;
120
121             for (i = 1; i < leader_comm_size; i++) {
122                 displs[i] = displs[i - 1] + node_sizes[i - 1] * recvcnt;
123                 recvcnts[i] = node_sizes[i] * recvcnt;
124             }
125
126
127             void* sendbuf=((char*)recvbuf)+recvtype->get_extent()*displs[leader_comm->rank()];
128
129             mpi_errno = Colls::allgatherv(sendbuf,
130                                        (recvcnt*local_size),
131                                        recvtype, 
132                                        recvbuf, recvcnts,
133                                        displs, recvtype,
134                                        leader_comm);
135             xbt_free(displs);
136             xbt_free(recvcnts);
137         } else {
138         void* sendtmpbuf=((char*)recvbuf)+recvtype->get_extent()*(recvcnt*local_size)*leader_comm->rank();
139         
140           
141
142             mpi_errno = Coll_allgather_mpich::allgather(sendtmpbuf, 
143                                                (recvcnt*local_size),
144                                                recvtype,
145                                                recvbuf, (recvcnt*local_size), recvtype,
146                                              leader_comm);
147
148         }
149     }
150
151     /*Bcast the entire data from node leaders to all other cores*/
152     mpi_errno = Colls::bcast (recvbuf, recvcnt * size, recvtype, 0, shmem_comm);
153     return mpi_errno;
154 }