source: XIOS/dev/dev_trunk_omp/extern/src_ep_dev/ep_allgather.cpp @ 1646

Last change on this file since 1646 was 1646, checked in by yushan, 5 years ago

branch merged with trunk @1645. arch file (ep&mpi) added for ADA

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