source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_allgatherv.cpp @ 1520

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

save dev. TO DO : test with xios

File size: 3.4 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 && comm->mpi_comm)
23    {
24      return ::MPI_Allgatherv(sendbuf, sendcount, to_mpi_type(sendtype), recvbuf, recvcounts, displs, to_mpi_type(recvtype), to_mpi_comm(comm->mpi_comm));
25    }
26
27    if(!comm->mpi_comm) return 0;
28
29
30    assert(valid_type(sendtype) && valid_type(recvtype));
31
32    MPI_Datatype datatype = sendtype;
33    int count = sendcount;
34
35    ::MPI_Aint datasize, lb;
36
37    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize);
38
39
40    int ep_rank = comm->ep_comm_ptr->size_rank_info[0].first;
41    int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
42    int mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first;
43    int ep_size = comm->ep_comm_ptr->size_rank_info[0].second;
44    int num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
45    int mpi_size = comm->ep_comm_ptr->size_rank_info[2].second;
46
47    assert(sendcount == recvcounts[ep_rank]);
48
49    bool is_master = ep_rank_loc==0;
50
51    void* local_recvbuf;
52    void* tmp_recvbuf;
53
54    int recvbuf_size = 0;
55    for(int i=0; i<ep_size; i++)
56      recvbuf_size = max(recvbuf_size, displs[i]+recvcounts[i]);
57
58
59    vector<int>local_recvcounts(num_ep, 0);
60    vector<int>local_displs(num_ep, 0);
61
62    MPI_Gather_local(&sendcount, 1, MPI_INT, local_recvcounts.data(), 0, comm);
63    for(int i=1; i<num_ep; i++) local_displs[i] = local_displs[i-1] + local_recvcounts[i-1]; 
64
65
66    if(is_master)
67    {
68      local_recvbuf = new void*[datasize * std::accumulate(local_recvcounts.begin(), local_recvcounts.end(), 0)];
69      tmp_recvbuf = new void*[datasize * std::accumulate(recvcounts, recvcounts+ep_size, 0)];
70    }
71
72    MPI_Gatherv_local(sendbuf, count, datatype, local_recvbuf, local_recvcounts.data(), local_displs.data(), 0, comm);
73
74
75    if(is_master)
76    {
77      std::vector<int>mpi_recvcounts(mpi_size, 0);
78      std::vector<int>mpi_displs(mpi_size, 0);
79
80      int local_sendcount = std::accumulate(local_recvcounts.begin(), local_recvcounts.end(), 0);
81      ::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));
82
83      for(int i=1; i<mpi_size; i++)
84        mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1];
85
86
87      ::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));
88
89      // reorder
90      int offset;
91      for(int i=0; i<ep_size; i++)
92      {
93        int extra = 0;
94        for(int j=0, k=0; j<ep_size, k<comm->ep_rank_map->at(i).first; j++)
95          if(comm->ep_rank_map->at(i).second == comm->ep_rank_map->at(j).second)
96          {
97            extra += recvcounts[j];
98            k++;
99          } 
100
101        offset = mpi_displs[comm->ep_rank_map->at(i).second] +  extra;
102
103        memcpy(recvbuf+displs[i]*datasize, tmp_recvbuf+offset*datasize, recvcounts[i]*datasize);
104       
105      }
106
107    }
108
109    MPI_Bcast_local(recvbuf, recvbuf_size, datatype, 0, comm);
110
111    if(is_master)
112    {
113      delete[] local_recvbuf;
114      delete[] tmp_recvbuf;
115    }
116
117
118  }
119
120
121
122}
Note: See TracBrowser for help on using the repository browser.