source: XIOS/dev/branch_openmp/extern/ep_dev/ep_gatherv.cpp @ 1527

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

save dev

File size: 5.5 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
13using namespace std;
14
15namespace ep_lib
16{
17
18  int MPI_Gatherv_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, const int recvcounts[], const int displs[], int local_root, MPI_Comm comm)
19  {
20    assert(valid_type(datatype));
21
22    ::MPI_Aint datasize, lb;
23    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize);
24
25    int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
26    int num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
27
28
29    #pragma omp critical (_gatherv)
30    comm->my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf);
31
32    MPI_Barrier_local(comm);
33
34    if(ep_rank_loc == local_root)
35    {
36      for(int i=0; i<num_ep; i++)
37        memcpy(recvbuf + datasize*displs[i], comm->my_buffer->void_buffer[i], datasize*recvcounts[i]);
38
39    }
40
41    MPI_Barrier_local(comm);
42  }
43
44  int MPI_Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int input_recvcounts[], const int input_displs[],
45                  MPI_Datatype recvtype, int root, MPI_Comm comm)
46  {
47 
48    if(!comm->is_ep) return ::MPI_Gatherv(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, const_cast<int*>(input_recvcounts), const_cast<int*>(input_displs),
49                                          to_mpi_type(recvtype), root, to_mpi_comm(comm->mpi_comm));
50    if(comm->is_intercomm) return MPI_Gatherv_intercomm(sendbuf, sendcount, sendtype, recvbuf, input_recvcounts, input_displs, recvtype, root, comm);
51
52    assert(sendtype == recvtype);
53
54   
55    int ep_rank = comm->ep_comm_ptr->size_rank_info[0].first;
56    int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first;
57    int mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first;
58    int ep_size = comm->ep_comm_ptr->size_rank_info[0].second;
59    int num_ep = comm->ep_comm_ptr->size_rank_info[1].second;
60    int mpi_size = comm->ep_comm_ptr->size_rank_info[2].second;
61
62    int root_mpi_rank = comm->ep_rank_map->at(root).second;
63    int root_ep_loc = comm->ep_rank_map->at(root).first;
64
65    ::MPI_Aint datasize, lb;
66    ::MPI_Type_get_extent(to_mpi_type(sendtype), &lb, &datasize);
67
68    int *recvcounts;
69    int* displs;
70
71    recvcounts = new int[ep_size];
72    displs = new int[ep_size];
73
74
75    bool is_master = (ep_rank_loc==0 && mpi_rank != root_mpi_rank ) || ep_rank == root;
76    bool is_root = ep_rank == root;
77
78    void* local_recvbuf;
79    std::vector<int>local_recvcounts(num_ep, 0);
80    std::vector<int>local_displs(num_ep, 0);
81
82
83    if(is_root)
84    { 
85      copy(input_recvcounts, input_recvcounts+ep_size, recvcounts);
86      copy(input_displs, input_displs+ep_size, displs);
87    }
88
89    MPI_Bcast(recvcounts, ep_size, MPI_INT, root, comm);
90    MPI_Bcast(displs, ep_size, MPI_INT, root, comm);
91
92    if(mpi_rank == root_mpi_rank) MPI_Gather_local(&sendcount, 1, MPI_INT, local_recvcounts.data(), root_ep_loc, comm);
93    else                          MPI_Gather_local(&sendcount, 1, MPI_INT, local_recvcounts.data(), 0, comm);
94
95
96
97    if(is_master)
98    {
99      int local_recvbuf_size = std::accumulate(local_recvcounts.begin(), local_recvcounts.end(), 0);
100     
101      for(int i=1; i<num_ep; i++)
102        local_displs[i] = local_displs[i-1] + local_recvcounts[i-1];
103
104      local_recvbuf = new void*[datasize * local_recvbuf_size];
105    }
106
107    if(mpi_rank == root_mpi_rank) MPI_Gatherv_local(sendbuf, sendcount, sendtype, local_recvbuf, local_recvcounts.data(), local_displs.data(), root_ep_loc, comm);
108    else                          MPI_Gatherv_local(sendbuf, sendcount, sendtype, local_recvbuf, local_recvcounts.data(), local_displs.data(), 0, comm);
109
110
111    void* tmp_recvbuf;
112    int tmp_recvbuf_size = std::accumulate(recvcounts, recvcounts+ep_size, 0);
113
114    if(is_root) tmp_recvbuf = new void*[datasize * tmp_recvbuf_size];
115
116
117    std::vector<int> mpi_recvcounts(mpi_size, 0);
118    std::vector<int> mpi_displs(mpi_size, 0);
119
120
121    if(is_master)
122    {
123      for(int i=0; i<ep_size; i++)
124      {
125        mpi_recvcounts[comm->ep_rank_map->at(i).second]+=recvcounts[i];
126      }
127
128      for(int i=1; i<mpi_size; i++)
129        mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1];
130
131
132      ::MPI_Gatherv(local_recvbuf, sendcount*num_ep, to_mpi_type(sendtype), tmp_recvbuf, mpi_recvcounts.data(), mpi_displs.data(), to_mpi_type(recvtype), root_mpi_rank, to_mpi_comm(comm->mpi_comm));
133    }   
134
135
136    // reorder data
137    if(is_root)
138    {
139      int offset;
140      for(int i=0; i<ep_size; i++)
141      {
142        int extra = 0;
143        for(int j=0, k=0; j<ep_size, k<comm->ep_rank_map->at(i).first; j++)
144          if(comm->ep_rank_map->at(i).second == comm->ep_rank_map->at(j).second)
145          {
146            extra += recvcounts[j];
147            k++;
148          } 
149
150        offset = mpi_displs[comm->ep_rank_map->at(i).second] +  extra;
151
152        memcpy(recvbuf+displs[i]*datasize, tmp_recvbuf+offset*datasize, recvcounts[i]*datasize);
153       
154      }
155
156    }
157
158    delete[] recvcounts;
159    delete[] displs;
160
161    if(is_master)
162    {
163      delete[] local_recvbuf;
164    }
165    if(is_root) delete[] tmp_recvbuf;
166  }
167
168  int MPI_Gatherv_intercomm(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int input_recvcounts[], const int input_displs[],
169                            MPI_Datatype recvtype, int root, MPI_Comm comm)
170  {
171    printf("MPI_Gatherv_intercomm not yet implemented\n");
172    MPI_Abort(comm, 0);
173  }
174
175}
Note: See TracBrowser for help on using the repository browser.