source: XIOS/dev/branch_openmp/extern/ep_dev/ep_allgatherv.cpp @ 1532

Last change on this file since 1532 was 1527, checked in by yushan, 6 years ago

save dev

File size: 3.8 KB
Line 
1/*!
2   \file ep_gather.cpp
3   \since 2 may 2016
4
5   \brief Definitions of MPI collective function: MPI_Gatherv, MPI_Allgatherv
6 */
7
8#include "ep_lib.hpp"
9#include <mpi.h>
10#include "ep_declaration.hpp"
11#include "ep_mpi.hpp"
12
13
14using namespace std;
15
16namespace ep_lib
17{
18
19  int MPI_Allgatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int recvcounts[], const int displs[], MPI_Datatype recvtype, MPI_Comm comm)
20  {
21
22    if(!comm->is_ep) return ::MPI_Allgatherv(sendbuf, sendcount, to_mpi_type(sendtype), recvbuf, recvcounts, displs, to_mpi_type(recvtype), to_mpi_comm(comm->mpi_comm));
23    if(comm->is_intercomm) return MPI_Allgatherv_intercomm(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm);
24
25
26    assert(valid_type(sendtype) && valid_type(recvtype));
27
28    MPI_Datatype datatype = sendtype;
29    int count = sendcount;
30
31    ::MPI_Aint datasize, lb;
32
33    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize);
34
35
36    int ep_rank = comm->ep_comm_ptr->size_rank_info[0].first;
37    int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
38    int mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first;
39    int ep_size = comm->ep_comm_ptr->size_rank_info[0].second;
40    int num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
41    int mpi_size = comm->ep_comm_ptr->size_rank_info[2].second;
42
43    assert(sendcount == recvcounts[ep_rank]);
44
45    bool is_master = ep_rank_loc==0;
46
47    void* local_recvbuf;
48    void* tmp_recvbuf;
49
50    int recvbuf_size = 0;
51    for(int i=0; i<ep_size; i++)
52      recvbuf_size = max(recvbuf_size, displs[i]+recvcounts[i]);
53
54
55    vector<int>local_recvcounts(num_ep, 0);
56    vector<int>local_displs(num_ep, 0);
57
58    MPI_Gather_local(&sendcount, 1, MPI_INT, local_recvcounts.data(), 0, comm);
59    for(int i=1; i<num_ep; i++) local_displs[i] = local_displs[i-1] + local_recvcounts[i-1]; 
60
61
62    if(is_master)
63    {
64      local_recvbuf = new void*[datasize * std::accumulate(local_recvcounts.begin(), local_recvcounts.end(), 0)];
65      tmp_recvbuf = new void*[datasize * std::accumulate(recvcounts, recvcounts+ep_size, 0)];
66    }
67
68    MPI_Gatherv_local(sendbuf, count, datatype, local_recvbuf, local_recvcounts.data(), local_displs.data(), 0, comm);
69
70
71    if(is_master)
72    {
73      std::vector<int>mpi_recvcounts(mpi_size, 0);
74      std::vector<int>mpi_displs(mpi_size, 0);
75
76      int local_sendcount = std::accumulate(local_recvcounts.begin(), local_recvcounts.end(), 0);
77      ::MPI_Allgather(&local_sendcount, 1, to_mpi_type(MPI_INT), mpi_recvcounts.data(), 1, to_mpi_type(MPI_INT), to_mpi_comm(comm->mpi_comm));
78
79      for(int i=1; i<mpi_size; i++)
80        mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1];
81
82
83      ::MPI_Allgatherv(local_recvbuf, local_sendcount, to_mpi_type(datatype), tmp_recvbuf, mpi_recvcounts.data(), mpi_displs.data(), to_mpi_type(datatype), to_mpi_comm(comm->mpi_comm));
84
85      // reorder
86      int offset;
87      for(int i=0; i<ep_size; i++)
88      {
89        int extra = 0;
90        for(int j=0, k=0; j<ep_size, k<comm->ep_rank_map->at(i).first; j++)
91          if(comm->ep_rank_map->at(i).second == comm->ep_rank_map->at(j).second)
92          {
93            extra += recvcounts[j];
94            k++;
95          } 
96
97        offset = mpi_displs[comm->ep_rank_map->at(i).second] +  extra;
98
99        memcpy(recvbuf+displs[i]*datasize, tmp_recvbuf+offset*datasize, recvcounts[i]*datasize);
100       
101      }
102
103    }
104
105    MPI_Bcast_local(recvbuf, recvbuf_size, datatype, 0, comm);
106
107    if(is_master)
108    {
109      delete[] local_recvbuf;
110      delete[] tmp_recvbuf;
111    }
112  }
113
114
115  int MPI_Allgatherv_intercomm(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int recvcounts[], const int displs[], MPI_Datatype recvtype, MPI_Comm comm)
116  {
117    printf("MPI_Allgatherv_intercomm not yet implemented\n");
118    MPI_Abort(comm, 0);
119  }
120
121
122}
Note: See TracBrowser for help on using the repository browser.