source: XIOS/dev/branch_openmp/extern/ep_dev/ep_gather.cpp @ 1381

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

add folder for MPI EP-RMA development. Current: MPI_Win, MPI_win_create, MPI_win_fence, MPI_win_free

File size: 3.4 KB
RevLine 
[1381]1/*!
2   \file ep_gather.cpp
3   \since 2 may 2016
4
5   \brief Definitions of MPI collective function: MPI_Gather, MPI_Allgather
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_Gather_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, 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    #pragma omp critical (_gather)
29    comm.my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf);
30
31    MPI_Barrier_local(comm);
32
33    if(ep_rank_loc == local_root)
34    {
35      for(int i=0; i<num_ep; i++)
36        memcpy(recvbuf + datasize * i * count, comm.my_buffer->void_buffer[i], datasize * count);
37    }
38
39    MPI_Barrier_local(comm);
40  }
41
42  int MPI_Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
43  {
44    if(!comm.is_ep)
45    {
46      return ::MPI_Gather(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, recvcount, to_mpi_type(recvtype),
47                   root, to_mpi_comm(comm.mpi_comm));
48    }
49
50    assert(sendcount == recvcount && sendtype == recvtype);
51
52    int ep_rank = comm.ep_comm_ptr->size_rank_info[0].first;
53    int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first;
54    int mpi_rank = comm.ep_comm_ptr->size_rank_info[2].first;
55    int ep_size = comm.ep_comm_ptr->size_rank_info[0].second;
56    int num_ep = comm.ep_comm_ptr->size_rank_info[1].second;
57    int mpi_size = comm.ep_comm_ptr->size_rank_info[2].second;
58
59    int root_mpi_rank = comm.rank_map->at(root).second;
60    int root_ep_loc = comm.rank_map->at(root).first;
61
62    ::MPI_Aint datasize, lb;
63    ::MPI_Type_get_extent(to_mpi_type(sendtype), &lb, &datasize);
64
65    bool is_master = (ep_rank_loc==0 && mpi_rank != root_mpi_rank ) || ep_rank == root;
66    bool is_root = ep_rank == root;
67
68    void* local_recvbuf;
69
70    if(is_master)
71    {
72      local_recvbuf = new void*[datasize * num_ep * sendcount];
73    }
74
75    void* tmp_recvbuf;
76    if(is_root) tmp_recvbuf = new void*[datasize * recvcount * ep_size];
77
78
79    if(mpi_rank == root_mpi_rank) MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, root_ep_loc, comm);
80    else                          MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, 0, comm);
81
82    std::vector<int> recvcounts(mpi_size, 0);
83    std::vector<int> displs(mpi_size, 0);
84
85
86    if(is_master)
87    {
88      for(int i=0; i<ep_size; i++)
89      {
90        recvcounts[comm.rank_map->at(i).second]+=sendcount;
91      }
92
93      for(int i=1; i<mpi_size; i++)
94        displs[i] = displs[i-1] + recvcounts[i-1];
95
96      ::MPI_Gatherv(local_recvbuf, sendcount*num_ep, to_mpi_type(sendtype), tmp_recvbuf, recvcounts.data(), displs.data(), to_mpi_type(recvtype), root_mpi_rank, to_mpi_comm(comm.mpi_comm));
97    }   
98
99
100    // reorder data
101    if(is_root)
102    {
103      int offset;
104      for(int i=0; i<ep_size; i++)
105      {
106        offset = displs[comm.rank_map->at(i).second] + comm.rank_map->at(i).first * sendcount; 
107        memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize);
108      }
109
110    }
111
112
113    if(is_master)
114    {
115      delete[] local_recvbuf;
116    }
117    if(is_root) delete[] tmp_recvbuf;
118   
119  }
120
121}
Note: See TracBrowser for help on using the repository browser.