source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_allgather.cpp @ 1354

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

omp dev : unify MPI_Comm type

File size: 2.7 KB
Line 
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 <string.h>
12#include "ep_mpi.hpp"
13
14using namespace std;
15
16namespace ep_lib
17{
18
19  int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
20  {
21
22    if(!comm.is_ep && comm.mpi_comm)
23    {
24      return ::MPI_Allgather(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, recvcount, to_mpi_type(recvtype), to_mpi_comm(comm.mpi_comm));
25    }
26
27    assert(sendcount == recvcount);
28
29    assert(valid_type(sendtype) && valid_type(recvtype));
30
31    MPI_Datatype datatype = sendtype;
32    int count = sendcount;
33
34    ::MPI_Aint datasize, lb;
35
36    ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize);
37
38
39    int ep_rank = comm.ep_comm_ptr->size_rank_info[0].first;
40    int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first;
41    int mpi_rank = comm.ep_comm_ptr->size_rank_info[2].first;
42    int ep_size = comm.ep_comm_ptr->size_rank_info[0].second;
43    int num_ep = comm.ep_comm_ptr->size_rank_info[1].second;
44    int mpi_size = comm.ep_comm_ptr->size_rank_info[2].second;
45
46    bool is_master = ep_rank_loc==0;
47
48    void* local_recvbuf;
49    void* tmp_recvbuf;
50
51
52    if(is_master)
53    {
54      local_recvbuf = new void*[datasize * num_ep * count];
55      tmp_recvbuf = new void*[datasize * count * ep_size];
56    }
57
58    MPI_Gather_local(sendbuf, count, datatype, local_recvbuf, 0, comm);
59
60
61    int* mpi_recvcounts;
62    int *mpi_displs;
63   
64    if(is_master)
65    {
66     
67      mpi_recvcounts = new int[mpi_size];
68      mpi_displs = new int[mpi_size];
69
70      int local_sendcount = num_ep * count;
71
72      ::MPI_Allgather(&local_sendcount, 1, to_mpi_type(MPI_INT), mpi_recvcounts, 1, to_mpi_type(MPI_INT), to_mpi_comm(comm.mpi_comm));
73
74      mpi_displs[0] = 0;
75      for(int i=1; i<mpi_size; i++)
76      {
77        mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1];
78      }
79
80   
81      ::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));
82
83
84      // reorder
85      int offset;
86      for(int i=0; i<ep_size; i++)
87      {
88        offset = mpi_displs[comm.rank_map->at(i).second] + comm.rank_map->at(i).first * sendcount; 
89        memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize);
90      }
91
92      delete[] mpi_recvcounts;
93      delete[] mpi_displs;
94    }
95
96    MPI_Bcast_local(recvbuf, count*ep_size, datatype, 0, comm);
97
98    MPI_Barrier(comm);
99
100
101    if(is_master)
102    {
103      delete[] local_recvbuf;
104      delete[] tmp_recvbuf;
105
106    }
107
108  }
109
110
111
112}
Note: See TracBrowser for help on using the repository browser.